FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairEvtGenGenerator.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 // -------------------------------------------------------------------------
9 // ----- FairEvtGenGenerator source file -----
10 // ----- Created 09/10/06 by S. Spataro -----
11 // -------------------------------------------------------------------------
12 #include "FairEvtGenGenerator.h"
13 
14 #include "FairLogger.h"
15 #include "FairPrimaryGenerator.h" // for FairPrimaryGenerator
16 
17 #include <TF1.h> // for TF1
18 #include <TRandom.h> // for TRandom, gRandom
19 #include <algorithm> // max
20 
22  : FairGenerator()
23  , fFileName("")
24  , fInputFile(nullptr)
25  , fGasmode(0)
26  , fRsigma(0.)
27  , fDensityFunction(nullptr)
28 {}
29 
31  : FairGenerator("EvtGen", fileName)
32  , fFileName(fileName)
33  , fInputFile(nullptr)
34  , fGasmode(0)
35  , fRsigma(0.)
36  , fDensityFunction(0)
37 {
38  LOG(info) << "FairEvtGenGenerator: Opening input file " << fileName;
39  if ((fInputFile = fopen(fFileName, "r")) == nullptr)
40  // fInputFile = new ifstream(fFileName);
41  // if ( ! fInputFile->is_open() )
42  {
43  Fatal("FairEvtGenGenerator", "Cannot open input file.");
44  }
45 
46  // fPDG=TDatabasePDG::Instance();
47 }
48 
49 FairEvtGenGenerator::FairEvtGenGenerator(const char* fileName, Double_t Rsigma, TF1* DensityFunction)
50  : FairGenerator("EvtGen", fileName)
51  , fFileName(fileName)
52  , fInputFile(nullptr)
53  , fGasmode(1)
54  , fRsigma(Rsigma)
55  , fDensityFunction(DensityFunction)
56 {
57  LOG(info) << "FairEvtGenGenerator: Opening input file " << fileName;
58  if ((fInputFile = fopen(fFileName, "r")) == nullptr) {
59  LOG(fatal) << "Cannot open input file.";
60  }
61 }
62 
64 {
65  delete fDensityFunction;
66  if (fInputFile) {
67  fclose(fInputFile);
68  }
69 }
70 
72 {
73  // Check for input file
74  if (!fInputFile) {
75  // if ( ! fInputFile->is_open() ) {
76  LOG(info) << "FairEvtGenGenerator: Input file not open!";
77  return kFALSE;
78  }
79 
80  // Define event variable to be read from file
81  Int_t ntracks = 0, eventID = 0, ncols = 0;
82 
83  // Define track variables to be read from file
84  Int_t nLine = 0, pdgID = 0, nDecay = 0, nM1 = -1, nM2 = -1, nDF = -1, nDL = -1;
85  Float_t fPx = 0., fPy = 0., fPz = 0., fE = 0.;
86  Float_t fVx = 0., fVy = 0., fVz = 0., fT = 0.;
87 
88  // Read event header line from input file
89 
90  Int_t max_nr = 0;
91 
92  Text_t buffer[200];
93  ncols = fscanf(fInputFile, "%d\t%d", &eventID, &ntracks);
94 
95  if (ncols && ntracks > 0) {
96 
97  // LOG(info) << "Event number: " << eventID << "\tNtracks: " << ntracks;
98  // std::stringstream ss;
99  for (Int_t ii = 0; ii < 15; ii++) {
100  ncols += fscanf(fInputFile, "%s", buffer);
101  // ss << buffer << "\t" ;
102  }
103  // LOG(info) << ss.str();
104 
105  for (Int_t ll = 0; ll < ntracks; ll++) {
106  ncols += fscanf(fInputFile,
107  "%d %d %d %d %d %d %d %g %f %f %f %f %f %f %f",
108  &nLine,
109  &pdgID,
110  &nDecay,
111  &nM1,
112  &nM2,
113  &nDF,
114  &nDL,
115  &fPx,
116  &fPy,
117  &fPz,
118  &fE,
119  &fT,
120  &fVx,
121  &fVy,
122  &fVz);
123  // LOG(info) << nLine << "\t" << pdgID << "\t" << nDecay << "\t" << nM1 << "\t" << nM2 << "\t" << nDF <<
124  // "\t" << nDL <<
125  // "\t" << fPx << "\t" << fPy << "\t" << fPz << "\t" << fE << "\t" << fT << "\t" << fVx << "\t" << fVy <<
126  // "\t" << fVz;
127  max_nr = std::max(max_nr, nDF);
128  max_nr = std::max(max_nr, nDL);
129  if ((nDF == -1) && (nDL == -1)) {
130 
131  /* Check if fGasmode is set */
132  if (fGasmode == 1) {
133 
134  // Random 2D point in a circle of radius r (simple beamprofile)
135  Double_t fX, fY, fZ, radius;
136  radius = gRandom->Gaus(0, fRsigma);
137  gRandom->Circle(fX, fY, radius);
138  fVx = fVx + fX;
139  fVy = fVy + fY;
140 
141  // calculate fZ according to some (probability) density function of the
142  // gas
143  fZ = fDensityFunction->GetRandom();
144  fVz = fVz + fZ;
145  }
146  // printf("Insert coordinates: %f, %f, %f ...\n", fVx, fVy, fVz);
147  primGen->AddTrack(pdgID, fPx, fPy, fPz, fVx, fVy, fVz);
148  }
149  }
150  } else {
151  LOG(info) << "FairEvtGenGenerator: End of input file reached ";
152  CloseInput();
153  return kFALSE;
154  }
155 
156  // If end of input file is reached : close it and abort run
157  if (feof(fInputFile)) {
158  LOG(info) << "FairEvtGenGenerator: End of input file reached ";
159  CloseInput();
160  return kFALSE;
161  }
162 
163  /*
164  LOG(info) << "FairEvtGenGenerator: Event " << eventID << ", vertex = ("
165  << vx << "," << vy << "," << vz << ") cm, multiplicity "
166  << ntracks;
167  */
168 
169  return kTRUE;
170 }
171 
172 void FairEvtGenGenerator::CloseInput()
173 {
174  if (fInputFile) {
175  // if ( fInputFile->is_open() ) {
176  {
177  LOG(info) << "FairEvtGenGenerator: Closing input file " << fFileName;
178  // fInputFile->close();
179 
180  fclose(fInputFile);
181  }
182  // delete fInputFile;
183  fInputFile = nullptr;
184  }
185 }
186 
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen)
ClassImp(FairEventBuilder)
Double_t fZ
Double_t fY
virtual void AddTrack(Int_t pdgid, Double_t px, Double_t py, Double_t pz, Double_t vx, Double_t vy, Double_t vz, Int_t parent=-1, Bool_t wanttracking=true, Double_t e=-9e9, Double_t tof=0., Double_t weight=0., TMCProcess proc=kPPrimary)
Double_t fX