forked from alisw/AliRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AliGenExtFile.cxx
474 lines (436 loc) · 15.4 KB
/
AliGenExtFile.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
* *
* Author: The ALICE Off-line Project. *
* Contributors are mentioned in the code where appropriate. *
* *
* Permission to use, copy, modify and distribute this software and its *
* documentation strictly for non-commercial purposes is hereby granted *
* without fee, provided that the above copyright notice appears in all *
* copies and that both the copyright notice and this permission notice *
* appear in the supporting documentation. The authors make no claims *
* about the suitability of this software for any purpose. It is *
* provided "as is" without express or implied warranty. *
**************************************************************************/
/* $Id$ */
// Event generator that using an instance of type AliGenReader
// reads particles from a file and applies cuts.
// Example: In your Config.C you can include the following lines
// AliGenExtFile *gener = new AliGenExtFile(-1);
// gener->SetMomentumRange(0,999);
// gener->SetPhiRange(-180.,180.);
// gener->SetThetaRange(0,180);
// gener->SetYRange(-999,999);
// AliGenReaderTreeK * reader = new AliGenReaderTreeK();
// reader->SetFileName("myFileWithTreeK.root");
// gener->SetReader(reader);
// gener->Init();
#include <Riostream.h>
#include "AliLog.h"
#include "AliGenExtFile.h"
#include "AliRunLoader.h"
#include "AliHeader.h"
#include "AliStack.h"
#include "AliGenEventHeader.h"
#include "AliGenReader.h"
#include <TParticle.h>
#include <TFile.h>
#include <TTree.h>
using std::cout;
using std::endl;
using std::map;
ClassImp(AliGenExtFile)
AliGenExtFile::AliGenExtFile()
:AliGenMC(),
fFileName(0),
fReader(0),
fStartEvent(0),
///
fLimitDiscardedEvents(999999),
fSetMultTrig(0),
fMultCut(0),
fSetPtTrig(0),
fPtCut(0),
fSetUserTrig(0)
///
{
// Constructor
//
// Read all particles
fNpart = -1;
}
AliGenExtFile::AliGenExtFile(Int_t npart)
:AliGenMC(npart),
fFileName(0),
fReader(0),
fStartEvent(0),
///
fLimitDiscardedEvents(999999),
fSetMultTrig(0),
fMultCut(0),
fSetPtTrig(0),
fPtCut(0),
fSetUserTrig(0)
///
{
// Constructor
fName = "ExtFile";
fTitle = "Primaries from ext. File";
}
//____________________________________________________________
AliGenExtFile::~AliGenExtFile()
{
// Destructor
delete fReader;
}
//___________________________________________________________
void AliGenExtFile::Init()
{
// Initialize
if (fReader) fReader->Init();
}
//___________________________________________________________
void AliGenExtFile::Generate()
{
// Generate particles
Double_t polar[] = {0,0,0};
//
Double_t origin[] = {0,0,0};
Double_t time = 0.;
Double_t p[] = {0,0,0,0};
Float_t random[6];
Int_t nt = 0;
//
//
// Fast forward up to start Event
for (Int_t ie=0; ie<fStartEvent; ++ie ) {
Int_t nTracks = fReader->NextEvent();
if (nTracks == 0) {
// printf("\n No more events !!! !\n");
Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
return;
}
for (Int_t i = 0; i < nTracks; ++i) {
if (NULL == fReader->NextParticle())
AliFatal("Error while skipping tracks");
}
cout << "Skipping event " << ie << endl;
}
fStartEvent = 0; // do not skip events the second time
///
Int_t consecutiveDiscardedEvents = 0;
///
while(1) {
if (fVertexSmear == kPerEvent) Vertex();
Int_t nTracks = fReader->NextEvent();
if (nTracks == 0) {
// printf("\n No more events !!! !\n");
Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
return;
}
//
// Particle selection loop
//
// The selection criterium for the external file generator is as follows:
//
// 1) All tracks are subject to the cuts defined by AliGenerator, i.e.
// fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
// fYMin, fYMax.
// If the particle does not satisfy these cuts, it is not put on the
// stack.
// 2) If fCutOnChild and some specific child is selected (e.g. if
// fForceDecay==kSemiElectronic) the event is rejected if NOT EVEN ONE
// child falls into the child-cuts.
TParticle* iparticle = 0x0;
if(fCutOnChild) {
// Count the selected children
Int_t nSelected = 0;
while ((iparticle=fReader->NextParticle()) ) {
Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
kf = TMath::Abs(kf);
if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
nSelected++;
}
}
if (!nSelected) continue; // No particle selected: Go to next event
fReader->RewindEvent();
}
//
// Stack selection loop
//
class SelectorLogic { // need to do recursive back tracking, requires a "nested" function
private:
Int_t idCount;
map<Int_t,Int_t> firstMotherMap;
map<Int_t,Int_t> secondMotherMap;
map<Int_t,Bool_t> selectedIdMap;
map<Int_t,Int_t> newIdMap;
void selectMothersToo(Int_t particleId) {
Int_t mum1 = firstMotherMap[particleId];
if (mum1 > -1 && !selectedIdMap[mum1]) {
selectedIdMap[mum1] = true;
selectMothersToo(mum1);
}
Int_t mum2 = secondMotherMap[particleId];
if (mum2 > -1 && !selectedIdMap[mum2]) {
selectedIdMap[mum2] = true;
selectMothersToo(mum2);
}
}
public:
SelectorLogic():idCount(0), firstMotherMap(), secondMotherMap(), selectedIdMap(), newIdMap() {}
void init() {
idCount = 0;
}
void setData(Int_t id, Int_t mum1, Int_t mum2, Bool_t selected) {
idCount++; // we know that this function is called in succession of ids, so counting is fine to determine max id
firstMotherMap[id] = mum1;
secondMotherMap[id] = mum2;
selectedIdMap[id] = selected;
}
void reselectCuttedMothersAndRemapIDs() {
for (Int_t id = 0; id < idCount; ++id) {
if (selectedIdMap[id]) {
selectMothersToo(id);
}
}
Int_t newId0 = 0;
for (Int_t id = 0; id < idCount; id++) {
if (selectedIdMap[id]) {
newIdMap[id] = newId0; ++newId0;
} else {
newIdMap[id] = -1;
}
}
}
Bool_t isSelected(Int_t id) {
return selectedIdMap[id];
}
Int_t newId(Int_t id) {
if (id == -1) return -1;
return newIdMap[id];
}
};
SelectorLogic selector;
selector.init();
for (Int_t i = 0; i < nTracks; i++) {
TParticle* jparticle = fReader->NextParticle();
if(jparticle){
selector.setData(i,
jparticle->GetFirstMother(),
jparticle->GetSecondMother(),
KinematicSelection(jparticle,0));
}
else{
AliWarning(Form("Particle %d = NULL",i));
}
}
selector.reselectCuttedMothersAndRemapIDs();
fReader->RewindEvent();
//
// Stack filling loop
//
fNprimaries = 0;
AliDebugStream(3) << "Initial mother particle assignment" << std::endl;
for (Int_t i = 0; i < nTracks; i++) {
TParticle* jparticle = fReader->NextParticle();
Bool_t selected = selector.isSelected(i);
if (!selected) {
continue;
}
Int_t parent = selector.newId(jparticle->GetFirstMother());
AliDebugStream(3) << "particle " << i << " -> " << selector.newId(i) << ", with mother " << jparticle->GetFirstMother() << " -> " << parent << std::endl;
p[0] = jparticle->Px();
p[1] = jparticle->Py();
p[2] = jparticle->Pz();
p[3] = jparticle->Energy();
Int_t idpart = jparticle->GetPdgCode();
if(fVertexSmear==kPerTrack)
{
Rndm(random,6);
for (Int_t j = 0; j < 3; j++) {
origin[j]=fOrigin[j]+
fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
}
Rndm(random,2);
time = fTimeOrigin + fOsigma[2]/TMath::Ccgs()*
TMath::Cos(2*random[0]*TMath::Pi())*
TMath::Sqrt(-2*TMath::Log(random[1]));
} else {
origin[0] = fVertex[0] + jparticle->Vx();
origin[1] = fVertex[1] + jparticle->Vy();
origin[2] = fVertex[2] + jparticle->Vz();
time = fTime + jparticle->T();
}
Int_t doTracking = fTrackIt && selected && (jparticle->TestBit(kTransportBit));
PushTrack(doTracking, parent, idpart,
p[0], p[1], p[2], p[3], origin[0], origin[1], origin[2], time,
polar[0], polar[1], polar[2],
kPPrimary, nt, 1., jparticle->GetStatusCode());
KeepTrack(nt);
fNprimaries++;
SetHighWaterMark(nt);
} // track loop
// User Trigger
if(fSetUserTrig){
AliStack *stack = AliRunLoader::Instance()->Stack();
if(!fUserTrigger(stack)){
stack->Clean();
consecutiveDiscardedEvents++;
if(consecutiveDiscardedEvents>fLimitDiscardedEvents){AliFatal(Form("More than %i events discarded consequently",fLimitDiscardedEvents));}
continue;
}
consecutiveDiscardedEvents=0;
}
AliStack *stack = AliRunLoader::Instance()->Stack();
// Base multiplicity trigger
if(fSetMultTrig){
AliInfo(Form("mult cut : %i",fMultCut));
if(!MultiplicityTrigger(stack)){
stack->Clean();
consecutiveDiscardedEvents++;
if(consecutiveDiscardedEvents>fLimitDiscardedEvents){AliFatal(Form("More than %i events discarded consequently",fLimitDiscardedEvents));}
continue;
}
consecutiveDiscardedEvents=0;
}
// Base pT trigger
if(fSetPtTrig){
AliInfo(Form("pT cut : %f GeV/c",fPtCut));
AliStack *stack = AliRunLoader::Instance()->Stack();
if(!PtTrigger(stack)){
stack->Clean();
consecutiveDiscardedEvents++;
if(consecutiveDiscardedEvents>fLimitDiscardedEvents){AliFatal(Form("More than %i events discarded consequently",fLimitDiscardedEvents));}
continue;
}
consecutiveDiscardedEvents=0;
}
///
fReader->RewindEvent();
AliDebugStream(3) << "Final mother particle assignment" << std::endl;
for (int iold = 0; iold < nTracks; iold++) {
TParticle* orig_particle = fReader->NextParticle();
if (!orig_particle) continue;
Bool_t selected = selector.isSelected(iold);
AliDebugStream(3) << "Particle " << iold << " with PDG = " << orig_particle->GetPdgCode() << std::endl;
AliDebugStream(3) << orig_particle->Px() << " " << orig_particle->Py() << " " << orig_particle->Pz() << " " << orig_particle->Energy() << std::endl;
if (!selected) {
AliDebugStream(3) << "Particle rejected." << std::endl;
continue;
}
Int_t inew = selector.newId(iold);
TParticle* particle = stack->Particle(inew);
if (!particle) {
AliErrorStream() << "Could not find particle " << inew << std::endl;
continue;
}
else {
AliDebugStream(3) << "New index is " << inew << std::endl;
}
Int_t iparent1 = selector.newId(orig_particle->GetFirstMother());
Int_t iparent2 = selector.newId(orig_particle->GetSecondMother());
TParticle* parent1 = nullptr;
TParticle* parent2 = nullptr;
if (iparent1 >= 0) parent1 = stack->Particle(iparent1);
if (iparent2 >= 0) parent2 = stack->Particle(iparent2);
if (parent1 && !parent2) {
// Only the first mother exists, nothing to be done
AliDebugStream(3) << "Only first mother exists. Confirm parent 1 with index " << iparent1
<< " and PDG = " << parent1->GetPdgCode() << std::endl;
}
else if (parent2 && !parent1) {
// Only the second mother exists
AliDebugStream(3) << "Only second mother exists. Using mother 2 with index " << iparent2
<< " and PDG = " << parent2->GetPdgCode() << std::endl;
particle->SetFirstMother(iparent2);
}
else if (parent1 && parent2) {
// Both mothers exist. Select the mother with the larger PDG code
AliDebugStream(3) << "Both mothers exist"
<< ". Mother 1 with index " << iparent1
<< " and PDG = " << parent1->GetPdgCode()
<< ". Mother 2 with index " << iparent2
<< " and PDG = " << parent2->GetPdgCode()
<< std::endl;
Bool_t confirmParent1 = kTRUE;
// The criterion is the following:
// if one of the mother is a beam particle, prefer the other mother;
// if one of the mother is a charm or beauty quark prefer it
// if one of the mother is a MC special code (81-100), prefer it
UInt_t abdPdg1 = TMath::Abs(parent1->GetPdgCode());
UInt_t abdPdg2 = TMath::Abs(parent2->GetPdgCode());
if (iparent1 <= 1 && iparent2 > 1) {
AliDebugStream(3) << "Parent 1 is a beam particle." << std::endl;
confirmParent1 = kFALSE;
}
else if (abdPdg2 == 5 && abdPdg1 != 5) {
AliDebugStream(3) << "Parent 2 is a beauty quark." << std::endl;
confirmParent1 = kFALSE;
}
else if (abdPdg2 == 4 && abdPdg1 != 4 && abdPdg1 != 5) {
AliDebugStream(3) << "Parent 2 is a charm quark." << std::endl;
confirmParent1 = kFALSE;
}
else if ((abdPdg2 >= 81 && abdPdg2 <= 100) && (abdPdg1 < 81 || abdPdg1 > 100)) {
AliDebugStream(3) << "Parent 2 is a cluster." << std::endl;
confirmParent1 = kFALSE;
}
if (!confirmParent1) {
particle->SetFirstMother(iparent2);
AliDebugStream(3) << "Using mother 2." << std::endl;
}
else {
AliDebugStream(3) << "Confirming mother 1." << std::endl;
}
}
else {
AliDebugStream(3) << "No mother found." << std::endl;
}
}
// Generated event header
AliGenEventHeader * header = fReader->GetGenEventHeader();
if (!header) header = new AliGenEventHeader();
header->SetName(GetName());
header->SetNProduced(fNprimaries);
header->SetPrimaryVertex(fVertex);
header->SetInteractionTime(fTime);
AddHeader(header);
break;
} // event loop
CdEventFile();
}
//___________________________________________________________
void AliGenExtFile::CdEventFile()
{
// CD back to the event file
AliRunLoader::Instance()->CdGAFile();
}
//__________________________________________________________
Bool_t AliGenExtFile::MultiplicityTrigger(AliStack *stack){
Int_t nTracks = stack->GetNtrack();
AliInfo(Form("n Tracks = %i",nTracks));
if(nTracks>fMultCut){return kTRUE;}
else{
AliInfo(Form("n Tracks < %i --> EVENT DISCARDED",fMultCut));
return kFALSE;
}
}
//__________________________________________________________
Bool_t AliGenExtFile::PtTrigger(AliStack *stack){
Int_t nTracks = stack->GetNtrack();
Int_t partCounter = 0; // counter of particles with pT > pT threshold
for(int i = 0;i < nTracks;i++){
TParticle *track = (TParticle*) stack->Particle(i);
if(track->Pt() > fPtCut){
AliInfo(Form("pT = %f",track->Pt()));
partCounter++;
}
}
if(partCounter>0){return kTRUE;}
else{
AliInfo(Form("No particle with pT > %f GeV/c --> EVENT DISCARDED",fPtCut));
return kFALSE;
}
}