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