1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 // Event generator that using an instance of type AliGenReader
19 // reads particles from a file and applies cuts.
20 // Example: In your Config.C you can include the following lines
21 // AliGenExtFile *gener = new AliGenExtFile(-1);
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);
32 #include <Riostream.h>
35 #include "AliGenExtFile.h"
36 #include "AliRunLoader.h"
37 #include "AliHeader.h"
39 #include "AliGenEventHeader.h"
40 #include "AliGenReader.h"
42 #include <TParticle.h>
51 ClassImp(AliGenExtFile)
53 AliGenExtFile::AliGenExtFile()
65 AliGenExtFile::AliGenExtFile(Int_t npart)
73 fTitle = "Primaries from ext. File";
76 //____________________________________________________________
77 AliGenExtFile::~AliGenExtFile()
83 //___________________________________________________________
84 void AliGenExtFile::Init()
87 if (fReader) fReader->Init();
90 //___________________________________________________________
91 void AliGenExtFile::Generate()
95 Double_t polar[3] = {0,0,0};
97 Double_t origin[3] = {0,0,0};
104 if (fVertexSmear == kPerEvent) Vertex();
106 // Fast forward up to start Event
107 for (Int_t ie=0; ie<fStartEvent; ++ie ) {
108 Int_t nTracks = fReader->NextEvent();
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");
114 for (i = 0; i < nTracks; ++i) {
115 if (NULL == fReader->NextParticle())
116 AliFatal("Error while skipping tracks");
118 cout << "Skipping event " << ie << endl;
120 fStartEvent = 0; // do not skip events the second time
123 Int_t nTracks = fReader->NextEvent();
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");
131 // Particle selection loop
133 // The selection criterium for the external file generator is as follows:
135 // 1) All tracks are subject to the cuts defined by AliGenerator, i.e.
136 // fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
138 // If the particle does not satisfy these cuts, it is not put on the
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.
143 TParticle* iparticle = 0x0;
146 // Count the selected children
148 while ((iparticle=fReader->NextParticle()) ) {
149 Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
151 if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
155 if (!nSelected) continue; // No particle selected: Go to next event
156 fReader->RewindEvent();
160 // Stack selection loop
162 class SelectorLogic { // need to do recursive back tracking, requires a "nested" function
165 map<Int_t,Int_t> firstMotherMap;
166 map<Int_t,Int_t> secondMotherMap;
167 map<Int_t,Bool_t> selectedIdMap;
168 map<Int_t,Int_t> newIdMap;
169 void selectMothersToo(Int_t particleId) {
170 Int_t mum1 = firstMotherMap[particleId];
171 if (mum1 > -1 && !selectedIdMap[mum1]) {
172 selectedIdMap[mum1] = true;
173 selectMothersToo(mum1);
175 Int_t mum2 = secondMotherMap[particleId];
176 if (mum2 > -1 && !selectedIdMap[mum2]) {
177 selectedIdMap[mum2] = true;
178 selectMothersToo(mum2);
182 SelectorLogic():idCount(0), firstMotherMap(), secondMotherMap(), selectedIdMap(), newIdMap() {}
186 void setData(Int_t id, Int_t mum1, Int_t mum2, Bool_t selected) {
187 idCount++; // we know that this function is called in succession of ids, so counting is fine to determine max id
188 firstMotherMap[id] = mum1;
189 secondMotherMap[id] = mum2;
190 selectedIdMap[id] = selected;
192 void reselectCuttedMothersAndRemapIDs() {
193 for (Int_t id = 0; id < idCount; ++id) {
194 if (selectedIdMap[id]) {
195 selectMothersToo(id);
199 for (Int_t id = 0; id < idCount; id++) {
200 if (selectedIdMap[id]) {
201 newIdMap[id] = newId0; ++newId0;
207 Bool_t isSelected(Int_t id) {
208 return selectedIdMap[id];
210 Int_t newId(Int_t id) {
211 if (id == -1) return -1;
215 SelectorLogic selector;
217 for (i = 0; i < nTracks; i++) {
218 TParticle* jparticle = fReader->NextParticle();
220 jparticle->GetFirstMother(),
221 jparticle->GetSecondMother(),
222 KinematicSelection(jparticle,0));
224 selector.reselectCuttedMothersAndRemapIDs();
225 fReader->RewindEvent();
228 // Stack filling loop
231 for (i = 0; i < nTracks; i++) {
232 TParticle* jparticle = fReader->NextParticle();
233 Bool_t selected = selector.isSelected(i);
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);
240 p[0] = jparticle->Px();
241 p[1] = jparticle->Py();
242 p[2] = jparticle->Pz();
243 p[3] = jparticle->Energy();
245 Int_t idpart = jparticle->GetPdgCode();
246 if(fVertexSmear==kPerTrack)
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]));
255 time = fTimeOrigin + fOsigma[2]/TMath::Ccgs()*
256 TMath::Cos(2*random[0]*TMath::Pi())*
257 TMath::Sqrt(-2*TMath::Log(random[1]));
259 origin[0] = fVertex[0] + jparticle->Vx();
260 origin[1] = fVertex[1] + jparticle->Vy();
261 origin[2] = fVertex[2] + jparticle->Vz();
262 time = fTime + jparticle->T();
264 Int_t doTracking = fTrackIt && selected && (jparticle->TestBit(kTransportBit));
266 PushTrack(doTracking, parent, idpart,
267 p[0], p[1], p[2], p[3], origin[0], origin[1], origin[2], time,
268 polar[0], polar[1], polar[2],
269 kPPrimary, nt, 1., jparticle->GetStatusCode());
275 // Generated event header
276 AliGenEventHeader * header = fReader->GetGenEventHeader();
277 if (!header) header = new AliGenEventHeader();
278 header->SetNProduced(fNprimaries);
279 header->SetPrimaryVertex(fVertex);
280 header->SetInteractionTime(fTime);
286 SetHighWaterMark(nt);
290 //___________________________________________________________
291 void AliGenExtFile::CdEventFile()
293 // CD back to the event file
294 AliRunLoader::Instance()->CdGAFile();