]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVGEN/AliGenExtFile.cxx
b1d0a38f88f5f9c174378a956ac9b469cc86d75d
[u/mrichter/AliRoot.git] / EVGEN / AliGenExtFile.cxx
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
16 /* $Id$ */
17
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);
29 //  gener->Init();
30
31
32 #include <Riostream.h>
33
34 #include "AliGenExtFile.h"
35 #include "AliRunLoader.h"
36 #include "AliHeader.h"
37 #include "AliStack.h"
38 #include "AliGenEventHeader.h"
39 #include "AliGenReader.h"
40
41 #include <TParticle.h>
42 #include <TFile.h>
43 #include <TTree.h>
44
45
46 ClassImp(AliGenExtFile)
47
48 AliGenExtFile::AliGenExtFile()
49     :AliGenMC(),
50      fFileName(0),
51      fReader(0),
52      fStartEvent(0)
53 {
54 //  Constructor
55 //
56 //  Read all particles
57     fNpart  = -1;
58 }
59
60 AliGenExtFile::AliGenExtFile(Int_t npart)
61     :AliGenMC(npart),
62      fFileName(0),
63      fReader(0),
64      fStartEvent(0)
65 {
66 //  Constructor
67     fName   = "ExtFile";
68     fTitle  = "Primaries from ext. File";
69 }
70
71 //____________________________________________________________
72 AliGenExtFile::~AliGenExtFile()
73 {
74 // Destructor
75     delete fReader;
76 }
77
78 //___________________________________________________________
79 void AliGenExtFile::Init()
80 {
81 // Initialize
82     if (fReader) fReader->Init();
83 }
84
85 //___________________________________________________________
86 void AliGenExtFile::Generate()
87 {
88 // Generate particles
89
90   Double_t polar[3]  = {0,0,0};
91   //
92   Double_t origin[3] = {0,0,0};
93   Double_t time = 0.;
94   Double_t p[4];
95   Float_t random[6];
96   Int_t i = 0, j, nt;
97   //
98   //
99   if (fVertexSmear == kPerEvent) Vertex();
100
101   // Fast forward up to start Event
102   for (Int_t ie=0; ie<fStartEvent; ++ie ) {
103     Int_t nTracks = fReader->NextEvent();       
104     if (nTracks == 0) {
105       // printf("\n No more events !!! !\n");
106       Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
107       return;
108     }
109     TParticle* iparticle = 0x0;
110     while ((iparticle=fReader->NextParticle()) ) ;
111     cout << "Skipping event " << ie << endl;
112   }  
113
114   while(1) {
115     Int_t nTracks = fReader->NextEvent();       
116     if (nTracks == 0) {
117       // printf("\n No more events !!! !\n");
118       Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
119       return;
120     }
121
122     //
123     // Particle selection loop
124     //
125     // The selection criterium for the external file generator is as follows:
126     //
127     // 1) All tracks are subject to the cuts defined by AliGenerator, i.e.
128     //    fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
129     //    fYMin, fYMax.
130     //    If the particle does not satisfy these cuts, it is not put on the
131     //    stack.
132     // 2) If fCutOnChild and some specific child is selected (e.g. if
133     //    fForceDecay==kSemiElectronic) the event is rejected if NOT EVEN ONE
134     //    child falls into the child-cuts.
135     TParticle* iparticle = 0x0;
136     
137     if(fCutOnChild) {
138       // Count the selected children
139       Int_t nSelected = 0;
140       while ((iparticle=fReader->NextParticle()) ) {
141           Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
142           kf = TMath::Abs(kf);
143           if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
144               nSelected++;
145           }
146       }
147       if (!nSelected) continue;    // No particle selected:  Go to next event
148       fReader->RewindEvent();
149     }
150
151     //
152     // Stack filling loop
153     //
154     fNprimaries = 0;
155     for (i = 0; i < nTracks; i++) {
156         TParticle* jparticle = fReader->NextParticle();
157         Bool_t selected = KinematicSelection(jparticle,0); 
158         if (!selected) continue;
159         p[0] = jparticle->Px();
160         p[1] = jparticle->Py();
161         p[2] = jparticle->Pz();
162         p[3] = jparticle->Energy();
163         
164         Int_t idpart = jparticle->GetPdgCode();
165         if(fVertexSmear==kPerTrack) 
166         {
167             Rndm(random,6);
168             for (j = 0; j < 3; j++) {
169                 origin[j]=fOrigin[j]+
170                     fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
171                     TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
172             }
173             Rndm(random,2);
174             time = fTimeOrigin + fOsigma[2]/TMath::Ccgs()*
175               TMath::Cos(2*random[0]*TMath::Pi())*
176               TMath::Sqrt(-2*TMath::Log(random[1]));
177         } else {
178             origin[0] = fVertex[0] + jparticle->Vx();
179             origin[1] = fVertex[1] + jparticle->Vy();
180             origin[2] = fVertex[2] + jparticle->Vz();
181             time = fTime + jparticle->T();
182         }
183         Int_t doTracking = fTrackIt && selected && (jparticle->TestBit(kTransportBit));
184         Int_t parent     = jparticle->GetFirstMother();
185         
186         PushTrack(doTracking, parent, idpart,
187                   p[0], p[1], p[2], p[3], origin[0], origin[1], origin[2], time,
188                   polar[0], polar[1], polar[2],
189                   kPPrimary, nt, 1., jparticle->GetStatusCode());
190
191         KeepTrack(nt);
192         fNprimaries++;
193     } // track loop
194
195     // Generated event header
196     
197     AliGenEventHeader * header = new AliGenEventHeader();
198     header->SetNProduced(fNprimaries);
199     header->SetPrimaryVertex(fVertex);
200     header->SetInteractionTime(fTime);
201     AddHeader(header);
202     break;
203     
204   } // event loop
205   
206   SetHighWaterMark(nt);
207   CdEventFile();
208 }
209
210 //___________________________________________________________
211 void AliGenExtFile::CdEventFile()
212 {
213 // CD back to the event file
214   AliRunLoader::Instance()->CdGAFile();
215 }
216
217
218
219
220