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