]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVGEN/AliGenExtFile.cxx
Do not copy, but create the generated event header
[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 "AliGenEventHeader.h"
38
39 #include <TParticle.h>
40 #include <TFile.h>
41 #include <TTree.h>
42
43
44 ClassImp(AliGenExtFile)
45
46 AliGenExtFile::AliGenExtFile()
47   :AliGenMC()
48 {
49 //  Constructor
50 //
51 //  Read all particles
52     fNpart  =- 1;
53     fReader =  0;
54 }
55
56 AliGenExtFile::AliGenExtFile(Int_t npart)
57     :AliGenMC(npart)
58 {
59 //  Constructor
60     fName   = "ExtFile";
61     fTitle  = "Primaries from ext. File";
62     fReader = 0;
63 }
64
65 AliGenExtFile::AliGenExtFile(const AliGenExtFile & ExtFile):
66     AliGenMC(ExtFile)
67 {
68 // Copy constructor
69     ExtFile.Copy(*this);
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   Float_t polar[3]  = {0,0,0};
91   //
92   Float_t origin[3] = {0,0,0};
93   Float_t p[3];
94   Float_t random[6];
95   Int_t i = 0, j, nt;
96   //
97   //
98   if (fVertexSmear == kPerEvent) Vertex();
99
100   while(1) {
101     Int_t nTracks = fReader->NextEvent();       
102     if (nTracks == 0) {
103       // printf("\n No more events !!! !\n");
104       Warning("AliGenExtFile::Generate","\nNo more events in external file!!!\nLast event may be empty or incomplete.\n");
105       return;
106     }
107
108     //
109     // Particle selection loop
110     //
111     // The selection criterium for the external file generator is as follows:
112     //
113     // 1) All tracks are subject to the cuts defined by AliGenerator, i.e.
114     //    fThetaMin, fThetaMax, fPhiMin, fPhiMax, fPMin, fPMax, fPtMin, fPtMax,
115     //    fYMin, fYMax.
116     //    If the particle does not satisfy these cuts, it is not put on the
117     //    stack.
118     // 2) If fCutOnChild and some specific child is selected (e.g. if
119     //    fForceDecay==kSemiElectronic) the event is rejected if NOT EVEN ONE
120     //    child falls into the child-cuts.
121     TParticle* iparticle = 0x0;
122     
123     if(fCutOnChild) {
124       // Count the selected children
125       Int_t nSelected = 0;
126       while ((iparticle=fReader->NextParticle()) ) {
127           Int_t kf = CheckPDGCode(iparticle->GetPdgCode());
128           kf = TMath::Abs(kf);
129           if (ChildSelected(kf) && KinematicSelection(iparticle, 1)) {
130               nSelected++;
131           }
132       }
133       if (!nSelected) continue;    // No particle selected:  Go to next event
134       fReader->RewindEvent();
135     }
136
137     //
138     // Stack filling loop
139     //
140     for (i = 0; i < nTracks; i++) {
141         TParticle* iparticle = fReader->NextParticle();
142         Bool_t selected = KinematicSelection(iparticle,0); 
143         if (!selected) continue;
144         p[0] = iparticle->Px();
145         p[1] = iparticle->Py();
146         p[2] = iparticle->Pz();
147         Int_t idpart = iparticle->GetPdgCode();
148         if(fVertexSmear==kPerTrack) 
149         {
150             Rndm(random,6);
151             for (j = 0; j < 3; j++) {
152                 origin[j]=fOrigin[j]+
153                     fOsigma[j]*TMath::Cos(2*random[2*j]*TMath::Pi())*
154                     TMath::Sqrt(-2*TMath::Log(random[2*j+1]));
155             }
156         } else {
157             origin[0] = fVertex[0] + iparticle->Vx();
158             origin[1] = fVertex[1] + iparticle->Vy();
159             origin[2] = fVertex[2] + iparticle->Vz();
160         }
161         
162         Int_t decayed    = iparticle->GetFirstDaughter();
163         Int_t doTracking = fTrackIt && (decayed < 0) && (TMath::Abs(idpart) > 10) && selected;
164         Int_t parent     = iparticle->GetFirstMother();
165         
166         PushTrack(doTracking,parent,idpart,p,origin,polar,0,kPPrimary,nt);
167         KeepTrack(nt);
168     } // track loop
169
170     // Generated event header
171     
172     AliGenEventHeader * header = new AliGenEventHeader();
173     header->SetNProduced(nt+1);
174     header->SetPrimaryVertex(fVertex);
175     AliRunLoader::GetRunLoader()->GetHeader()->SetGenEventHeader(header);
176
177     break;
178     
179   } // event loop
180   
181   SetHighWaterMark(nt);
182   CdEventFile();
183 }
184
185 void AliGenExtFile::CdEventFile()
186 {
187 // CD back to the event file
188     (AliRunLoader::GetRunLoader())->CdGAFile();
189 }
190
191
192 AliGenExtFile& AliGenExtFile::operator=(const  AliGenExtFile& rhs)
193 {
194 // Assignment operator
195     rhs.Copy(*this);
196     return *this;
197 }
198  
199
200 void AliGenExtFile::Copy(TObject&) const
201 {
202     //
203     // Copy 
204     //
205     Fatal("Copy","Not implemented!\n");
206 }
207
208
209
210
211
212
213