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