SetSeed implementation
[u/mrichter/AliRoot.git] / EVGEN / AliGenExtFile.cxx
CommitLineData
4c039060 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
25454878 16/* $Id$ */
675e9664 17
a48e3376 18// Event generator that using an instance of type AliGenReader
25454878 19// reads particles from a file and applies cuts.
20// Example: In your Config.C you can include the following lines
4f23c932 21// AliGenExtFile *gener = new AliGenExtFile(-1);
25454878 22// gener->SetMomentumRange(0,999);
23// gener->SetPhiRange(-180.,180.);
24// gener->SetThetaRange(0,180);
25// gener->SetYRange(-999,999);
26// AliGenReaderTreeK * reader = new AliGenReaderTreeK();
27// reader->SetFileName("myFileWithTreeK.root");
28// gener->SetReader(reader);
29// gener->Init();
30
675e9664 31
159ec319 32#include <Riostream.h>
084c1b4a 33
3ff7721b 34#include "AliLog.h"
693caace 35#include "AliGenExtFile.h"
4f23c932 36#include "AliRunLoader.h"
9ad9d6f5 37#include "AliHeader.h"
aa4e331d 38#include "AliStack.h"
9ad9d6f5 39#include "AliGenEventHeader.h"
4a33c50d 40#include "AliGenReader.h"
5c3fd7ea 41
a48e3376 42#include <TParticle.h>
693caace 43#include <TFile.h>
a48e3376 44#include <TTree.h>
45
5c3fd7ea 46
83cb58d0 47using std::cout;
48using std::endl;
6256e5e7 49using std::map;
50
198bb1c7 51ClassImp(AliGenExtFile)
380956c5 52
53AliGenExtFile::AliGenExtFile()
1c56e311 54 :AliGenMC(),
55 fFileName(0),
9d188d33 56 fReader(0),
57 fStartEvent(0)
693caace 58{
f87cfe57 59// Constructor
693caace 60//
61// Read all particles
e1b35da7 62 fNpart = -1;
693caace 63}
64
65AliGenExtFile::AliGenExtFile(Int_t npart)
1c56e311 66 :AliGenMC(npart),
67 fFileName(0),
9d188d33 68 fReader(0),
69 fStartEvent(0)
693caace 70{
f87cfe57 71// Constructor
a48e3376 72 fName = "ExtFile";
73 fTitle = "Primaries from ext. File";
693caace 74}
75
76//____________________________________________________________
77AliGenExtFile::~AliGenExtFile()
78{
f87cfe57 79// Destructor
a48e3376 80 delete fReader;
693caace 81}
82
a48e3376 83//___________________________________________________________
84void AliGenExtFile::Init()
693caace 85{
a48e3376 86// Initialize
87 if (fReader) fReader->Init();
693caace 88}
89
9d188d33 90//___________________________________________________________
693caace 91void AliGenExtFile::Generate()
92{
f87cfe57 93// Generate particles
693caace 94
eed0bb9a 95 Double_t polar[3] = {0,0,0};
693caace 96 //
eed0bb9a 97 Double_t origin[3] = {0,0,0};
21391258 98 Double_t time = 0.;
1628e931 99 Double_t p[4];
693caace 100 Float_t random[6];
df2fcbcb 101 Int_t i=0, j, nt;
693caace 102 //
4f23c932 103 //
104 if (fVertexSmear == kPerEvent) Vertex();
693caace 105
9d188d33 106 // Fast forward up to start Event
107 for (Int_t ie=0; ie<fStartEvent; ++ie ) {
108 Int_t nTracks = fReader->NextEvent();
109 if (nTracks == 0) {
110 // printf("\n No more events !!! !\n");
111 Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
112 return;
113 }
df2fcbcb 114 for (i = 0; i < nTracks; ++i) {
3ff7721b 115 if (NULL == fReader->NextParticle())
116 AliFatal("Error while skipping tracks");
117 }
9d188d33 118 cout << "Skipping event " << ie << endl;
119 }
3ff7721b 120 fStartEvent = 0; // do not skip events the second time
9d188d33 121
380956c5 122 while(1) {
123 Int_t nTracks = fReader->NextEvent();
124 if (nTracks == 0) {
125 // printf("\n No more events !!! !\n");
126 Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
693caace 127 return;
380956c5 128 }
129
130 //
131 // Particle selection loop
132 //
4f23c932 133 // The selection criterium for the external file generator is as follows:
380956c5 134 //
4f23c932 135 // 1) All tracks are subject to the cuts defined by AliGenerator, i.e.
380956c5 136 // fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
137 // fYMin, fYMax.
138 // If the particle does not satisfy these cuts, it is not put on the
139 // stack.
140 // 2) If fCutOnChild and some specific child is selected (e.g. if
141 // fForceDecay==kSemiElectronic) the event is rejected if NOT EVEN ONE
142 // child falls into the child-cuts.
88cb7938 143 TParticle* iparticle = 0x0;
144
380956c5 145 if(fCutOnChild) {
146 // Count the selected children
147 Int_t nSelected = 0;
4f23c932 148 while ((iparticle=fReader->NextParticle()) ) {
149 Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
150 kf = TMath::Abs(kf);
151 if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
152 nSelected++;
153 }
380956c5 154 }
155 if (!nSelected) continue; // No particle selected: Go to next event
156 fReader->RewindEvent();
157 }
a48e3376 158
380956c5 159 //
0d1e7588 160 // Stack selection loop
161 //
162 class SelectorLogic { // need to do recursive back tracking, requires a "nested" function
163 private:
5263658e 164 Int_t idCount;
0d1e7588 165 map<Int_t,Int_t> firstMotherMap;
166 map<Int_t,Int_t> secondMotherMap;
167 map<Int_t,Bool_t> selectedIdMap;
5263658e 168 map<Int_t,Int_t> newIdMap;
0d1e7588 169 void selectMothersToo(Int_t particleId) {
89c6e78b 170 Int_t mum1 = firstMotherMap[particleId];
0d1e7588 171 if (mum1 > -1 && !selectedIdMap[mum1]) {
172 selectedIdMap[mum1] = true;
173 selectMothersToo(mum1);
174 }
175 Int_t mum2 = secondMotherMap[particleId];
176 if (mum2 > -1 && !selectedIdMap[mum2]) {
177 selectedIdMap[mum2] = true;
178 selectMothersToo(mum2);
179 }
180 }
181 public:
89c6e78b 182 SelectorLogic():idCount(0), firstMotherMap(), secondMotherMap(), selectedIdMap(), newIdMap() {}
183 void init() {
5263658e 184 idCount = 0;
185 }
0d1e7588 186 void setData(Int_t id, Int_t mum1, Int_t mum2, Bool_t selected) {
5263658e 187 idCount++; // we know that this function is called in succession of ids, so counting is fine to determine max id
0d1e7588 188 firstMotherMap[id] = mum1;
189 secondMotherMap[id] = mum2;
190 selectedIdMap[id] = selected;
5263658e 191 }
192 void reselectCuttedMothersAndRemapIDs() {
193 for (Int_t id = 0; id < idCount; ++id) {
194 if (selectedIdMap[id]) {
195 selectMothersToo(id);
196 }
197 }
89c6e78b 198 Int_t newId0 = 0;
5263658e 199 for (Int_t id = 0; id < idCount; id++) {
200 if (selectedIdMap[id]) {
89c6e78b 201 newIdMap[id] = newId0; ++newId0;
396b3bc9 202 } else {
203 newIdMap[id] = -1;
5263658e 204 }
0d1e7588 205 }
206 }
207 Bool_t isSelected(Int_t id) {
208 return selectedIdMap[id];
209 }
5263658e 210 Int_t newId(Int_t id) {
211 if (id == -1) return -1;
212 return newIdMap[id];
213 }
0d1e7588 214 };
215 SelectorLogic selector;
5263658e 216 selector.init();
df2fcbcb 217 for (i = 0; i < nTracks; i++) {
0d1e7588 218 TParticle* jparticle = fReader->NextParticle();
219 selector.setData(i,
220 jparticle->GetFirstMother(),
221 jparticle->GetSecondMother(),
222 KinematicSelection(jparticle,0));
223 }
5263658e 224 selector.reselectCuttedMothersAndRemapIDs();
0d1e7588 225 fReader->RewindEvent();
226
227 //
380956c5 228 // Stack filling loop
229 //
7cf36cae 230 fNprimaries = 0;
380956c5 231 for (i = 0; i < nTracks; i++) {
bce6240d 232 TParticle* jparticle = fReader->NextParticle();
0d1e7588 233 Bool_t selected = selector.isSelected(i);
bce6240d 234 if (!selected) {
bce6240d 235 continue;
236 }
5263658e 237 Int_t parent = selector.newId(jparticle->GetFirstMother());
238// printf("particle %d -> %d, with mother %d -> %d\n", i, selector.newId(i), jparticle->GetFirstMother(), parent);
bce6240d 239
ea79897e 240 p[0] = jparticle->Px();
241 p[1] = jparticle->Py();
242 p[2] = jparticle->Pz();
1628e931 243 p[3] = jparticle->Energy();
244
b7a824ae 245 Int_t idpart = jparticle->GetPdgCode();
4f23c932 246 if(fVertexSmear==kPerTrack)
247 {
248 Rndm(random,6);
249 for (j = 0; j < 3; j++) {
250 origin[j]=fOrigin[j]+
251 fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
252 TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
253 }
21391258 254 Rndm(random,2);
255 time = fTimeOrigin + fOsigma[2]/TMath::Ccgs()*
256 TMath::Cos(2*random[0]*TMath::Pi())*
257 TMath::Sqrt(-2*TMath::Log(random[1]));
380956c5 258 } else {
ea79897e 259 origin[0] = fVertex[0] + jparticle->Vx();
260 origin[1] = fVertex[1] + jparticle->Vy();
261 origin[2] = fVertex[2] + jparticle->Vz();
21391258 262 time = fTime + jparticle->T();
380956c5 263 }
8f126362 264 Int_t doTracking = fTrackIt && selected && (jparticle->TestBit(kTransportBit));
4f23c932 265
eed0bb9a 266 PushTrack(doTracking, parent, idpart,
21391258 267 p[0], p[1], p[2], p[3], origin[0], origin[1], origin[2], time,
eed0bb9a 268 polar[0], polar[1], polar[2],
269 kPPrimary, nt, 1., jparticle->GetStatusCode());
270
4f23c932 271 KeepTrack(nt);
7cf36cae 272 fNprimaries++;
380956c5 273 } // track loop
0b05fbf9 274
275 // Generated event header
7a005d69 276 AliGenEventHeader * header = fReader->GetGenEventHeader();
277 if (!header) header = new AliGenEventHeader();
7cf36cae 278 header->SetNProduced(fNprimaries);
0b05fbf9 279 header->SetPrimaryVertex(fVertex);
21391258 280 header->SetInteractionTime(fTime);
7cf36cae 281 AddHeader(header);
380956c5 282 break;
4f23c932 283
380956c5 284 } // event loop
4f23c932 285
380956c5 286 SetHighWaterMark(nt);
2c354237 287 CdEventFile();
288}
380956c5 289
9d188d33 290//___________________________________________________________
2c354237 291void AliGenExtFile::CdEventFile()
292{
293// CD back to the event file
33c3c91a 294 AliRunLoader::Instance()->CdGAFile();
693caace 295}
296
297
693caace 298
299
300