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