FairRoot/PandaRoot
ChiGenContext.cxx
Go to the documentation of this file.
1 /*
2  * @author Alexey Luchinsky
3  * @author Stanislav Poslavsky (stvlpos (at) mail.ru)
4  */
5 
6 #include "ChiGenContext.h"
7 
8 #include <algorithm>
9 #include <ctime>
10 #include <fstream>
11 #include <stdexcept>
12 #include <boost/date_time/posix_time/posix_time.hpp>
13 
14 #include <boost/iostreams/tee.hpp>
15 #include <boost/iostreams/stream.hpp>
16 #include "TRandom.h"
17 
18 namespace chigen {
19 
20 
21 #define __millisecondsTime__ boost::posix_time::microsec_clock::local_time().time_of_day().total_milliseconds()
22 
23  double elapsedTimeSeconds() {
24  static const double start = (double) __millisecondsTime__;
25  return ((__millisecondsTime__ - start) / 1e3);
26  }
27 
28  namespace {
29  long __seed__ = 0;
30 
31  std::string toString(int a) {
32  std::stringstream ss;
33  ss << a;
34  return ss.str();
35  }
36  };
37 
38  namespace {
39  typedef boost::iostreams::tee_device<std::ostream, std::ofstream> TeeDevice;
40  typedef boost::iostreams::stream<TeeDevice> TeeStream;
41  }
42 
43  namespace ostreams {
44  bool suppress_all_cout = false;
45  bool write_log_file = false;
46  bool suppress_pandaroot = false;
48  bool verbose_mode = true;
49 
50 
55 
57  remove(CHIGEN_LOG_FILE);
58  std::ofstream* __temp_logFile__ = new std::ofstream();
59  if (write_log_file)
60  __temp_logFile__->open(CHIGEN_LOG_FILE);
61  ostreams::log_file = new std::ostream(__temp_logFile__->rdbuf());
62  ostreams::terminal = new std::ostream(std::cout.rdbuf());
63  TeeDevice __temp_tee_device(*ostreams::terminal, *__temp_logFile__);
64  ostreams::tee_stream = new TeeStream(__temp_tee_device);
65 
67  //write all to log.file
68  std::cout.rdbuf(__temp_logFile__->rdbuf());
69  ostreams::chigen_cout = &std::cout;
70  } else if (suppress_all_cout && !write_log_file) {
71  //no any cout at all
72  std::cout.rdbuf(0);
73  ostreams::chigen_cout = &std::cout;
74  } else if (write_log_file) {
75  //writing log file
76  if (suppress_pandaroot) {
77  //suppress panda root in log file
78  std::cout.rdbuf(0);
80  } else if (suppress_pandaroot_cout) {
81  //suppress pandaroot in terminal but not in log
82  std::cout.rdbuf(__temp_logFile__->rdbuf());
84  } else {
85  //both pandaroot & chigen goes to log and terminal
86  std::cout.rdbuf(ostreams::tee_stream->rdbuf());
88  }
89  } else {
90  //no log file will be created
92  //suppress pandaroot in terminal
93  std::cout.rdbuf(0);
94  }
96  }
97  }
98  }
99 
100  namespace random {
102  Pythia8::Rndm* pythia_random_engine = 0;
103 
106  random::pythia_random_engine = new Pythia8::Rndm(__seed__);
108  }
109  };
110 
111  namespace evtgen {
112  namespace {
113 
114  std::string addVMCDir(const char* _str) {
115  static std::string workingDir = getenv("VMCWORKDIR");
116  return (workingDir + _str);
117  };
118  }
119  std::string EvtGenPDL = addVMCDir("/pgenerators/EvtGen/EvtGen/Private/evt.pdl");
120  std::string EvtGenDecFile = addVMCDir("/pgenerators/EvtGen/EvtGen/Private/DECAY.DEC");
121  std::string EvtGenChiDecFile = addVMCDir("/pgenerators/chigen/PolarizedDecays.dec");
122 
123  bool evt_gen_is_loaded = false;
124  EvtGen* evt_gen = 0;
125 
126  namespace {
127  bool evt_gen_ids_are_loaded = false;
128  }
129 
130  std::string chi_c1_str = std::string(CHI1_STRING),
131  chi_c2_str = std::string(CHI2_STRING),
132  x3872_str = std::string(X3872_STRING);
133 
135  if (!evt_gen_is_loaded)
136  throw std::runtime_error("EvtGen is not loaded yet.");
137 
138  if (evt_gen_ids_are_loaded)
139  return;
140 
141  chi_c1_evt_id = EvtPDL::getId(chi_c1_str);
142  chi_c2_evt_id = EvtPDL::getId(chi_c2_str);
143  x3872_evt_id = EvtPDL::getId(x3872_str);
144  evt_gen_ids_are_loaded = true;
145  }
146 
148 
150  //initializing EvtGen
151  EvtExternalGenList genList;
152  EvtAbsRadCorr* radCorrEngine = genList.getPhotosModel();
153  std::list<EvtDecayBase*> extraModels = genList.getListOfModels();
154 
155  evtgen::evt_gen = new EvtGen(
156  EvtGenDecFile.c_str(),
157  EvtGenPDL.c_str(),
159  radCorrEngine, &extraModels);
160 
161  evt_gen->readUDecay(EvtGenChiDecFile.c_str());
162 
165  }
166 
167  void read_dec_file(char* dec_file_name) {
169  evt_gen->readUDecay(dec_file_name);
170  }
171  };
172 
173  namespace pythia {
174  Pythia8::Pythia* pythia = 0;
175  Pythia8::PDF* pdf = 0;
176 
178  pdf = new Pythia8::CTEQ6pdf(2212, 1, getenv("PYTHIA8DATA"));
179 
180  pythia = new Pythia8::Pythia(getenv("PYTHIA8DATA"));
182 
183  pythia->readString("ProcessLevel:all = off");
184  //not allow charmonia to decay with Pythia
185  pythia->readString("20443:mayDecay = off");
186  pythia->readString("445:mayDecay = off");
187  pythia->readString("443:mayDecay = off");
188 
189  //x3872 is not implemented in PYTHIA
190  pythia->particleData.addParticle(
191  X3872_PDG_ID, // id
192  X3872_STRING, // name
193  3, // spinType
194  0, // chargeType
195  0, // colType
196  X3872_MASS, // m0
197  0.0001, // mWidth
198  X3872_MASS-0.0001, // mMin
199  X3872_MASS+0.0001 // mMax
200  );
201  pythia->readString("9920443:mayDecay = off");
202 
203  pythia->init();
204  }
205  };
206 
207  namespace {
208  bool chigen_is_initialized = false;
209  }
210 
212  if (!chigen_is_initialized)
213  initialize();
214  }
215 
216  void initialize() {
217  initialize((long) gRandom->GetSeed());
218  }
219 
220  void initialize(long seed) {
221  chigen_is_initialized = true;
222 
223  __seed__ = seed;
224 
225  //initializing streams
227 
228  __chigen_cout__ << " Initializing with seed " << __seed__ << "." << std::endl;
229 
230  //initializing random
231  srand(seed); //<-to ensure that std::random will use same seed
233 
234  __chigen_cout__ << " Initializing EvtGen ...";
235  //initializing EvtGen
237  __chigen_direct_cout__ << "done." << std::endl;
238 
239  __chigen_cout__ << " Initializing Pythia ...";
240  //initializing Pythia
242  __chigen_direct_cout__ << "done." << std::endl;
243 
244  };
245 
246  long get_seed() {
247  return __seed__;
248  };
249 
250  EvtId pdgId2EvtId(int pdgId) {
252  switch (pdgId) {
253  case CHI1_PDG_ID:
254  return evtgen::chi_c1_evt_id;
255  case CHI2_PDG_ID:
256  return evtgen::chi_c2_evt_id;
257  case X3872_PDG_ID:
258  return evtgen::x3872_evt_id;
259  default:
260  throw std::runtime_error(std::string("Unknown particle id: ") + toString(pdgId));
261  }
262  }
263 
264  bool isPWaveCharmonia(int pdgCode) {
265  return pdgCode == CHI1_PDG_ID || pdgCode == CHI2_PDG_ID || pdgCode == X3872_PDG_ID;
266  };
267 
268  bool isCharmonia(int pdgCode) {
269  return pdgCode == CHI1_PDG_ID || pdgCode == CHI2_PDG_ID
270  || pdgCode == X3872_PDG_ID || pdgCode == PSI_PDG_ID;
271  };
272 
273 }
274 
#define CHIGEN_LOG_FILE
Definition: ChiGenContext.h:15
#define __millisecondsTime__
bool isPWaveCharmonia(int pdgCode)
void ensure_chigen_is_initialized()
bool isCharmonia(int pdgCode)
#define CHI1_PDG_ID
Definition: ChiGenContext.h:43
void ensure_evt_gen_is_inialized()
std::string x3872_str
std::ostream * terminal
#define __chigen_direct_cout__
std::ostream * chigen_cout
#define X3872_PDG_ID
Definition: ChiGenContext.h:46
void initialize_random()
std::string EvtGenPDL
Pythia8::Rndm * pythia_random_engine
EvtId pdgId2EvtId(int pdgId)
Int_t a
Definition: anaLmdDigi.C:126
void read_dec_file(char *dec_file_name)
void initialize()
basic_ostream< char, char_traits< char > > ostream
unsigned int seed
#define CHI2_PDG_ID
Definition: ChiGenContext.h:49
void initialize_evtgen()
void initialize_pythia()
std::ostream * tee_stream
#define X3872_MASS
Definition: ChiGenContext.h:35
#define CHI2_STRING
Definition: ChiGenContext.h:50
std::string chi_c2_str
#define CHI1_STRING
Definition: ChiGenContext.h:44
#define X3872_STRING
Definition: ChiGenContext.h:47
double elapsedTimeSeconds()
std::string chi_c1_str
long get_seed()
#define PSI_PDG_ID
Definition: ChiGenContext.h:41
Pythia8::PDF * pdf
Pythia8::Pythia * pythia
ChiGenRandomEngine * random_engine
#define __chigen_cout__
std::string EvtGenChiDecFile
std::ostream * log_file
std::string EvtGenDecFile