FairRoot/PandaRoot
PndEvtGenGenerator.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndEvtGenGenerator source file -----
3 // -------------------------------------------------------------------------
4 
5 
6 #include <iostream>
7 #include "TClonesArray.h"
8 #include "TFile.h"
9 #include "TLorentzVector.h"
10 #include "TTree.h"
11 #include "TVector3.h"
12 #include "TParticle.h"
13 #include "PndEvtGenGenerator.h"
14 #include "FairPrimaryGenerator.h"
15 
16 #include "TF1.h"
17 #include "TRandom.h"
18 
19 #define MAX 200
20 
21 
22 using std::cout;
23 using std::endl;
24 using std::max;
25 
26 
27 // ----- Default constructor ------------------------------------------
29  FairGenerator(), iEvent(0), fFileName(""), fInputRootFile(), fInputTree(),
30  fInputAsciiFile(), fRPx(), fRPy(), fRPz(), fRVx(), fRVy(), fRVz(),
31  fRPdg(), fRDF(), fRDL(), fRNTrk(0), fFileType(kFALSE), fGasmode(0),
32  fRsigma(0.), fDensityFunction()
33 
34 {
35 }
36 // ------------------------------------------------------------------------
37 
38 // ----- Standard constructor -----------------------------------------
39 PndEvtGenGenerator::PndEvtGenGenerator(const Char_t* fileName) :
40  FairGenerator(), iEvent(0), fFileName(fileName), fInputRootFile(), fInputTree(),
41  fInputAsciiFile(), fRPx(), fRPy(), fRPz(), fRVx(), fRVy(), fRVz(),
42  fRPdg(), fRDF(), fRDL(), fRNTrk(0), fFileType(kFALSE), fGasmode(0),
43  fRsigma(0.), fDensityFunction()
44 {
45  Init();
46 }
47 // ------------------------------------------------------------------------
48 
49 PndEvtGenGenerator::PndEvtGenGenerator(const Char_t* fileName, Double_t Rsigma, TF1 * DensityFunction) :
50  FairGenerator(), iEvent(0), fFileName(fileName), fInputRootFile(), fInputTree(),
51  fInputAsciiFile(), fRPx(), fRPy(), fRPz(), fRVx(), fRVy(), fRVz(),
52  fRPdg(), fRDF(), fRDL(), fRNTrk(0), fFileType(kFALSE), fGasmode(1),
53  fRsigma(Rsigma), fDensityFunction(DensityFunction)
54 {
55  Init();
56 }
57 
58 
59 // ----- Destructor ---------------------------------------------------
61  CloseInput();
62  if (fFileType==1)
63  {
64  delete[] fRPx;
65  delete[] fRPy;
66  delete[] fRPz;
67  delete[] fRVx;
68  delete[] fRVy;
69  delete[] fRVz;
70  delete[] fRPdg;
71  }
72 }
73 // ------------------------------------------------------------------------
74 
76 {
77  iEvent = 0;
78 
79  cout << "-I PndEvtGenGenerator: Opening input file " << fFileName.Data() << endl;
80 
81  // open a ROOT input file
82 
83  if (fFileName.EndsWith(".root"))
84  {
85  fFileType = 1; // ROOT file
86 
87  // create the arrays for the branches
88 
89  fRPx = new Double_t[MAX];
90  fRPy = new Double_t[MAX];
91  fRPz = new Double_t[MAX];
92  fRVx = new Double_t[MAX];
93  fRVy = new Double_t[MAX];
94  fRVz = new Double_t[MAX];
95  fRPdg = new Int_t[MAX];
96  fRDF = new Int_t[MAX];
97  fRDL = new Int_t[MAX];
98 
99  fInputRootFile = new TFile(fFileName,"READ");
100  if (fInputRootFile->IsZombie())
101  Fatal("PndEvtGenGenerator","Cannot open ROOT input file.");
102 
103  fInputTree = (TTree*) fInputRootFile->Get("ntp");
104  if (!SetBranchAddresses())
105  Fatal("PndEvtGenGenerator","Incompatible ROOT input file!");
106 
107  }
108  // or an ASCII input file
109  else
110  {
111  fFileType = 0; // ASCII file
112 
113  if ((fInputAsciiFile = fopen(fFileName.Data(),"r"))==NULL)
114  Fatal("PndEvtGenGenerator","Cannot open ASCII input file.");
115  }
116  return kTRUE;
117 }
118 
119 // ------------------------------------------------------------------------
120 
122 {
123  if (0==fInputTree) return false;
124 
125  fInputTree->SetBranchStatus("*",0);
126 
127  fInputTree->SetBranchStatus("nTrk",1);
128 
129  fInputTree->SetBranchStatus("px",1);
130  fInputTree->SetBranchStatus("py",1);
131  fInputTree->SetBranchStatus("pz",1);
132  fInputTree->SetBranchStatus("x",1);
133  fInputTree->SetBranchStatus("y",1);
134  fInputTree->SetBranchStatus("z",1);
135  fInputTree->SetBranchStatus("Id",1);
136  fInputTree->SetBranchStatus("DF",1);
137  fInputTree->SetBranchStatus("DL",1);
138 
139 
140  fInputTree->SetBranchAddress("nTrk",&fRNTrk);
141  fInputTree->SetBranchAddress("px",fRPx);
142  fInputTree->SetBranchAddress("py",fRPy);
143  fInputTree->SetBranchAddress("pz",fRPz);
144  fInputTree->SetBranchAddress("x",fRVx);
145  fInputTree->SetBranchAddress("y",fRVx);
146  fInputTree->SetBranchAddress("z",fRVz);
147  fInputTree->SetBranchAddress("Id",fRPdg);
148  fInputTree->SetBranchAddress("DF",fRDF);
149  fInputTree->SetBranchAddress("DL",fRDL);
150 
151  return true;
152 }
153 
154 // ----- Public method ReadEvent --------------------------------------
156 {
157  if (fFileType==0) // fFileType is boolean
158  {
159  if ( !fInputAsciiFile ) {
160  cout << "-E PndEvtGenGenerator: Input ASCII file not open!" << endl;
161  return kFALSE;
162  }
163  return ReadAsciiEvent(primGen);
164  }
165  else
166  {
167  if ( ! fInputRootFile ) {
168  cout << "-E PndEvtGenGenerator: Input ROOT file not open!" << endl;
169  return kFALSE;
170  }
171  return ReadRootEvent(primGen);
172  }
173 
174  return kFALSE; // It cannot happen
175 
176 }
177 
178 
179 // ------------------------------------------------------------------------
180 
182 {
183  // Check for number of events in input file
184  if ( iEvent > fInputTree->GetEntries() ) {
185  cout << "-E PndEvtGenGenerator: No more events in input file!" << endl;
186  CloseInput();
187  return kFALSE;
188  }
189 
190  // preserve orginal TDirectory
191  TFile *g=gFile;
192  fInputRootFile->cd();
193  fInputTree->GetEntry(iEvent++);
194  g->cd();
195 
196  for (Int_t i=0; i<fRNTrk;i++)
197  {
198  //cout << fRPdg[i]<<" "<<fRDF[i]<<" "<<fRDL[i]<<" "<<fRPx[i]<<" "<< fRPy[i]
199  //<<" "<<fRPz[i] <<" "<<fRVx[i] <<" "<<fRVy[i]<<" "<< fRVz[i]<<endl;
200 
201  // add all final state particles
202  if ( -1==fRDF[i] && -1==fRDL[i] )
203  {
204  Double_t fVx = fRVx[i]/10.; // mm -> cm conversion
205  Double_t fVy = fRVy[i]/10.;
206  Double_t fVz = fRVz[i]/10.;
207  /* Check if fGasmode is set */
208  if (fGasmode == 1)
209  {
210  // Random 2D point in a circle of radius r (simple beamprofile)
211  Double_t fX, fY, fZ, radius;
212  radius = gRandom->Gaus(0,fRsigma);
213  gRandom->Circle(fX, fY, radius);
214  fVx = fVx + fX;
215  fVy = fVy + fY;
216 
217  // calculate fZ according to some (probability) density function of the
218  // gas
219  fZ=fDensityFunction->GetRandom();
220  fVz = fVz + fZ;
221  }
222 
223  primGen->AddTrack(fRPdg[i],fRPx[i],fRPy[i],fRPz[i],fVx,fVy,fVz);
224  //cout <<"P = "<<fRPx[i]<<" "<<fRPy[i]<<" "<<fRPz[i]<<endl;
225  }
226  }
227 
228  return kTRUE;
229 
230 }
231 
232 
233 // ------------------------------------------------------------------------
234 
236 {
237  // Define event variable to be read from file
238  Int_t ntracks = 0, eventID = 0, ncols = 0;
239 
240  // Define track variables to be read from file
241  Int_t nLine = 0, pdgID = 0, nDecay = 0, nM1 = -1, nM2 = -1, nDF = -1, nDL = -1;
242  Double_t fPx = 0., fPy = 0., fPz = 0., fE = 0.;
243  Double_t fVx = 0., fVy = 0., fVz = 0., fT = 0.;
244 
245  // Read event header line from input file
246 
247  Int_t max_nr = 0;
248 
249  Text_t buffer[400];
250  ncols = fscanf(fInputAsciiFile,"%d\t%d", &eventID, &ntracks);
251 
252  if (ncols && ntracks>0)
253  {
254 
255  // cout << "Event number: " << eventID << "\tNtracks: " << ntracks << endl;
256  for (Int_t ii=0; ii<15; ii++) {
257  ncols = fscanf(fInputAsciiFile,"%s",buffer);
258  // cout << buffer << "\t" ;
259  }
260  //cout << endl;
261 
262  for (Int_t ll=0; ll<ntracks; ll++)
263  {
264  ncols = fscanf(fInputAsciiFile,"%d %d %d %d %d %d %d %lf %lf %lf %lf %lf %lf %lf %lf",
265  &nLine, &pdgID, &nDecay, &nM1, &nM2, &nDF, &nDL, &fPx, &fPy, &fPz, &fE, &fT, &fVx, &fVy, &fVz);
266  max_nr = max(max_nr, nDF);
267  max_nr = max(max_nr, nDL);
268  if ((nDF==-1) && (nDL==-1))
269  {
270  // Conversion mm -> cm for vertex position
271  fVx = fVx / 10.;
272  fVy = fVy / 10.;
273  fVz = fVz / 10.;
274  /* Check if fGasmode is set */
275  if (fGasmode == 1)
276  {
277  // Random 2D point in a circle of radius r (simple beamprofile)
278  Double_t fX, fY, fZ, radius;
279  radius = gRandom->Gaus(0,fRsigma);
280  gRandom->Circle(fX, fY, radius);
281  fVx = fVx + fX;
282  fVy = fVy + fY;
283 
284  // calculate fZ according to some (probability) density function of the
285  // gas
286  fZ=fDensityFunction->GetRandom();
287  fVz = fVz + fZ;
288 
289  }
290  //printf("Insert coordinates: %f, %f, %f ...\n", fVx, fVy, fVz);
291  //cout <<"P = "<<fPx<<" "<<fPy<<" "<<fPz<<endl;
292  primGen->AddTrack(pdgID, fPx, fPy, fPz, fVx, fVy, fVz);
293 
294  }
295  }
296  }
297  else
298  {
299  cout << "-I FairEvtGenGenerator: End of input file reached " << endl;
300  CloseInput();
301  return kFALSE;
302  }
303 
304 
305  // If end of input file is reached : close it and abort run
306  if ( feof(fInputAsciiFile) )
307  {
308  cout << "-I FairEvtGenGenerator: End of input file reached " << endl;
309  CloseInput();
310  return kFALSE;
311  }
312 
313  /*
314  cout << "-I FairEvtGenGenerator: Event " << eventID << ", vertex = ("
315  << vx << "," << vy << "," << vz << ") cm, multiplicity "
316  << ntracks << endl;
317  */
318 
319  return kTRUE;
320 }
321 
322 
323 
324 // ----- Private method CloseInput ------------------------------------
326 {
327  if (fFileType==0) // fFileType is boolean
328  {
329  if (fInputAsciiFile)
330  {
331  cout << "-I PndEvtGenGenerator: Closing ASCII input file " << fFileName.Data() << endl;
332 
333  fclose(fInputAsciiFile);
334  delete fInputAsciiFile;
335  fInputAsciiFile = NULL;
336  }
337  }
338  else
339  {
340  if (fInputRootFile)
341  {
342  cout << "-I PndEvtGenGenerator: Closing ROOT input file " << fFileName.Data() << endl;
343  fInputRootFile->Close();
344  delete fInputRootFile;
345  fInputRootFile = NULL;
346  }
347  }
348 
349 }
350 // ------------------------------------------------------------------------
351 
352 
353 
Int_t i
Definition: run_full.C:25
TClonesArray * fT
Definition: drawGLTracks.C:13
Bool_t ReadRootEvent(FairPrimaryGenerator *primGen)
Double_t fX
Definition: PndCaloDraw.cxx:34
int fGasmode
0: ASCII, 1:ROOT
Int_t fRNTrk
Number of daughters.
TFile * g
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:26
Double_t fZ
Definition: PndCaloDraw.cxx:34
FairPrimaryGenerator * primGen
Definition: sim_emc_apd.C:81
TFile * fInputRootFile
Input file name.
Double_t * fRVx
Momentum of particle.
FILE * fInputAsciiFile
Pointer to input tree.
TF1 * fDensityFunction
Double_t
double fRsigma
Gas mode (vertex smearing)
TString fFileName
Event number.
#define MAX
TF1 * fDensityFunction
sigma for vertex smearing
TTree * fInputTree
Pointer to input file.
Double_t fY
Definition: PndCaloDraw.cxx:34
Bool_t fFileType
number of particles in event
Int_t * fRDF
PDG code of particle.
Bool_t ReadAsciiEvent(FairPrimaryGenerator *primGen)
ClassImp(PndAnaContFact)
Int_t * fRPdg
Start Vertex of particle.
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen)