CbmRoot
CbmShieldGenerator.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmShieldGenerator source file -----
3 // ----- Created 15/09/06 by V. Friese -----
4 // -------------------------------------------------------------------------
5 #include "CbmShieldGenerator.h"
6 
7 #include "FairMCEventHeader.h"
8 #include "FairPrimaryGenerator.h"
9 
10 #include "FairIon.h"
11 #include "FairRunSim.h"
12 
13 #include "TDatabasePDG.h"
14 #include "TParticlePDG.h"
15 
16 #include <iostream>
17 
18 using std::cout;
19 using std::endl;
20 using std::ifstream;
21 using std::map;
22 
23 // ----- Default constructor ------------------------------------------
25  : FairGenerator()
26  , fInputFile(nullptr)
27  , fFileName(nullptr)
28  , fPDG(nullptr)
29  , fpartType(-1)
30  , fIonMap() {}
31 // ------------------------------------------------------------------------
32 
33 
34 // ----- Standard constructor -----------------------------------------
36  : FairGenerator()
37  , fInputFile(nullptr)
38  , fFileName(fileName)
39  , fPDG(TDatabasePDG::Instance())
40  , fpartType(-1)
41  , fIonMap() {
42  cout << "-I- CbmShieldGenerator: Opening input file " << fileName << endl;
43  fInputFile = new ifstream(fFileName);
44  if (!fInputFile->is_open())
45  Fatal("CbmShieldGenerator", "Cannot open input file.");
46  cout << "-I- CbmShieldGenerator: Looking for ions..." << endl;
47  Int_t nIons = RegisterIons();
48  cout << "-I- CbmShieldGenerator: " << nIons << " ions registered." << endl;
49  CloseInput();
50  cout << "-I- CbmShieldGenerator: Reopening input file " << fileName << endl;
51  fInputFile = new ifstream(fFileName);
52 }
53 // ------------------------------------------------------------------------
54 
55 
56 // ----- Destructor ---------------------------------------------------
58 // ------------------------------------------------------------------------
59 
60 
61 // ----- Public method ReadEvent --------------------------------------
62 Bool_t CbmShieldGenerator::ReadEvent(FairPrimaryGenerator* primGen) {
63 
64  // Check for input file
65  if (!fInputFile->is_open()) {
66  cout << "-E- CbmShieldGenerator: Input file not open!" << endl;
67  return kFALSE;
68  }
69 
70 
71  // Define event variable to be read from file
72  Int_t eventId = 0;
73  Int_t nTracks = 0;
74  Double_t pBeam = 0.;
75  Double_t bx = 0.;
76  Double_t by = 0.;
77  Double_t b = 0.; //SELIM
78 
79 
80  // Define track variables to be read from file
81  // Int_t iPid = 0;
82  Int_t iMass = 0;
83  Int_t iCharge = 0;
84  Double_t px = 0.;
85  Double_t py = 0.;
86  Double_t pz = 0.;
87  Int_t pdgType = 0;
88  Double_t etot = 0.; //SELIM
89 
90  // Read event header line from input file
91  //*fInputFile >> eventId >> nTracks >> pBeam >> bx >> by;
92  *fInputFile >> eventId >> b >> bx >> by
93  >> nTracks; //SELIM: update of SHIELD file structure
94 
95  // If end of input file is reached : close it and abort run
96  if (fInputFile->eof()) {
97  cout << "-I- CbmShieldGenerator: End of input file reached " << endl;
98  CloseInput();
99  return kFALSE;
100  }
101 
102 
103  cout << "-I- CbmShieldGenerator: Event " << eventId << ", pBeam = " << pBeam
104  << "GeV, b = " << bx << " " << by << " fm, multiplicity " << nTracks
105  << endl;
106  TVector2 bb;
107  bb.Set(bx, by);
108 
109 
110  // Set event id and impact parameter in MCEvent if not yet done
111  FairMCEventHeader* event = primGen->GetEvent();
112  if (event && (!event->IsSet())) {
113  event->SetEventID(eventId);
114  event->SetB(bb.Mod());
115  event->SetRotZ(bb.Phi());
116  event->MarkSet(kTRUE);
117  }
118 
119 
120  // Loop over tracks in the current event
121  for (Int_t itrack = 0; itrack < nTracks; itrack++) {
122 
123  // Read PID and momentum from file
124  //*fInputFile >> iPid >> iMass >> iCharge >> px >> py >> pz;
125  *fInputFile >> pdgType >> iMass >> iCharge >> px >> py >> pz
126  >> etot; //SELIM: update of SHIELD file structure
127 
128  // Case ion
129  /* //SELIM
130  if ( iPid == 1000 ) {
131  char ionName[20];
132  sprintf(ionName, "Ion_%d_%d", iMass, iCharge);
133  TParticlePDG* part = fPDG->GetParticle(ionName);
134  if ( ! part ) {
135  cout << "-W- CbmShieldGenerator::ReadEvent: Cannot find "
136  << ionName << " in database!" << endl;
137  continue;
138  }
139  pdgType = part->PdgCode();
140  }
141  else pdgType = fPDG->ConvertGeant3ToPdg(iPid); // "normal" particle
142  */
143 
144  // Give track to PrimaryGenerator
145 
146  if (fpartType == 1 && pdgType == 2112)
147  primGen->AddTrack(pdgType, px, py, pz, 0., 0., 0.);
148  if (fpartType == 2 && pdgType == 2212)
149  primGen->AddTrack(pdgType, px, py, pz, 0., 0., 0.);
150  if (fpartType == 3 && pdgType >= 1000000000)
151  primGen->AddTrack(pdgType, px, py, pz, 0., 0., 0.);
152 
153  if (fpartType == -1) primGen->AddTrack(pdgType, px, py, pz, 0., 0., 0.);
154  }
155 
156 
157  return kTRUE;
158 }
159 // ------------------------------------------------------------------------
160 
161 
162 // ----- Private method CloseInput ------------------------------------
164  if (fInputFile) {
165  if (fInputFile->is_open()) {
166  cout << "-I- CbmShieldGenerator: Closing input file " << fFileName
167  << endl;
168  fInputFile->close();
169  }
170  delete fInputFile;
171  fInputFile = nullptr;
172  }
173 }
174 // ------------------------------------------------------------------------
175 
176 
177 // ----- Private method RegisterIons ----------------------------------
179 
180  cout << " CbmShieldGenerator::RegisterIons() start " << endl;
181  Int_t nIons = 0;
182  // Int_t eventId, nTracks, iPid, iMass, iCharge;
183  // Double_t pBeam, bx, by, px, py, pz;
184  Int_t eventId, nTracks, iMass, iCharge;
185  Double_t bx, by, px, py, pz;
186  Double_t b; //SELIM
187  Double_t etot = 0.; //SELIM
188  Int_t pdgType = 0; //SELIM
189  fIonMap.clear();
190 
191  while (!fInputFile->eof()) {
192 
193  //*fInputFile >> eventId >> nTracks >> pBeam >> bx >>by;
194  *fInputFile >> eventId >> b >> bx >> by
195  >> nTracks; //SELIM: update of SHIELD file structure
196  if (fInputFile->eof()) continue;
197  for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
198  //*fInputFile >> iPid >> iMass >> iCharge >> px >> py >> pz;
199  *fInputFile >> pdgType >> iMass >> iCharge >> px >> py >> pz
200  >> etot; //SELIM: update of SHIELD file structure
201  //if ( iPid == 1000 ) { // ion
202  if (pdgType > 1000000000) //SELIM
203  { // ion
204  char buffer[20];
205  sprintf(buffer, "Ion_%d_%d", iMass, iCharge);
206  TString ionName(buffer);
207  if (fIonMap.find(ionName) == fIonMap.end()) { // new ion
208  FairIon* ion = new FairIon(ionName, iCharge, iMass, iCharge);
209  fIonMap[ionName] = ion;
210  nIons++;
211  } // new ion
212  } // ion
213  } // track loop
214 
215  } // event loop
216 
217  FairRunSim* run = FairRunSim::Instance();
218  map<TString, FairIon*>::iterator mapIt;
219  for (mapIt = fIonMap.begin(); mapIt != fIonMap.end(); mapIt++) {
220  FairIon* ion = (*mapIt).second;
221  run->AddNewIon(ion);
222  }
223 
224  return nIons;
225 }
226 // ------------------------------------------------------------------------
227 
228 
CbmShieldGenerator::CloseInput
void CloseInput()
Definition: CbmShieldGenerator.cxx:163
CbmShieldGenerator::fFileName
const Char_t * fFileName
Input file stream.
Definition: CbmShieldGenerator.h:66
CbmShieldGenerator
Definition: CbmShieldGenerator.h:38
CbmShieldGenerator::CbmShieldGenerator
CbmShieldGenerator()
Definition: CbmShieldGenerator.cxx:24
CbmShieldGenerator::ReadEvent
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen)
Definition: CbmShieldGenerator.cxx:62
buffer
@ buffer
Definition: CbmMvdSensorPlugin.h:22
CbmShieldGenerator::~CbmShieldGenerator
virtual ~CbmShieldGenerator()
Definition: CbmShieldGenerator.cxx:57
CbmShieldGenerator.h
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmShieldGenerator::RegisterIons
Int_t RegisterIons()
Definition: CbmShieldGenerator.cxx:178
CbmShieldGenerator::fpartType
int fpartType
PDG database.
Definition: CbmShieldGenerator.h:69
CbmShieldGenerator::fIonMap
std::map< TString, FairIon * > fIonMap
Definition: CbmShieldGenerator.h:82
CbmShieldGenerator::fInputFile
std::ifstream * fInputFile
Definition: CbmShieldGenerator.h:65