Class Reference for E1039 Core & Analysis Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PHPythia8.C
Go to the documentation of this file.
1 #include "PHPythia8.h"
2 
3 #include "PHPy8GenTrigger.h"
4 
8 
10 #include <phool/PHCompositeNode.h>
11 #include <phool/PHIODataNode.h>
12 #include <phool/PHRandomSeed.h>
13 #include <phool/getClass.h>
14 
15 #include <HepMC/GenEvent.h>
16 #include <Pythia8/Pythia.h>
17 #include <Pythia8Plugins/HepMC2.h>
18 
19 #include <boost/format.hpp>
20 
21 #include <gsl/gsl_randist.h>
22 
23 #include <cassert>
24 #include <cstdlib>
25 
26 using namespace std;
27 
29 
30 //static const Float_t CM2MM = 10.; // cm to mm comversion \todo why not use HepMC functions?
31 
32 PHPythia8::PHPythia8(const std::string &name)
33  : SubsysReco(name)
34  , _eventcount(0)
35  , _registeredTriggers()
36  , _triggersOR(true)
37  , _triggersAND(false)
38  , _pythia(NULL)
39  , _configFile("phpythia8.cfg")
40  , _commands()
41  , _pythiaToHepMC(NULL)
42  , _save_integrated_luminosity(true)
43  , _integral_node(nullptr)
44 {
45  char *charPath = getenv("PYTHIA8");
46  if (!charPath)
47  {
48  cout << "PHPythia8::Could not find $PYTHIA8 path!" << endl;
49  return;
50  }
51 
52  std::string thePath(charPath);
53  thePath += "/xmldoc/";
54  _pythia = new Pythia8::Pythia(thePath.c_str());
55 
56  _pythiaToHepMC = new HepMC::Pythia8ToHepMC();
57  _pythiaToHepMC->set_store_proc(true);
58  _pythiaToHepMC->set_store_pdf(true);
59  _pythiaToHepMC->set_store_xsec(true);
60 
61  hepmc_helper.set_embedding_id(1); // default embedding ID to 1
62 }
63 
65 {
66  delete _pythia;
67 }
68 
70 {
71  if (!_configFile.empty()) read_config();
72  for (unsigned int j = 0; j < _commands.size(); j++)
73  {
74  _pythia->readString(_commands[j]);
75  }
76 
77  create_node_tree(topNode);
78 
79  // event numbering will start from 1
80  _eventcount = 0;
81 
82  // PYTHIA8 has very specific requires for its random number range
83  // I map the designated unique seed from recoconst into something
84  // acceptable for PYTHIA8
85 
86  unsigned int seed = PHRandomSeed();
87 
88  if (seed > 900000000)
89  {
90  seed = seed % 900000000;
91  }
92 
93  if ((seed > 0) && (seed <= 900000000))
94  {
95  _pythia->readString("Random:setSeed = on");
96  _pythia->readString(str(boost::format("Random:seed = %1%") % seed));
97  }
98  else
99  {
100  cout << PHWHERE << " ERROR: seed " << seed << " is not valid" << endl;
101  exit(1);
102  }
103 
104  _pythia->init();
105 
107 }
108 
110 {
111  if (verbosity >= VERBOSITY_MORE) cout << "PHPythia8::End - I'm here!" << endl;
112 
113  if (verbosity >= VERBOSITY_SOME)
114  {
115  //-* dump out closing info (cross-sections, etc)
116  _pythia->stat();
117 
118  //match pythia printout
119  cout << " | "
120  << " | " << endl;
121  cout << " PHPythia8::End - " << _eventcount
122  << " events passed trigger" << endl;
123  cout << " Fraction passed: " << _eventcount
124  << "/" << _pythia->info.nAccepted()
125  << " = " << _eventcount / float(_pythia->info.nAccepted()) << endl;
126  cout << " *------- End PYTHIA Trigger Statistics ------------------------"
127  << "-------------------------------------------------* " << endl;
128 
129  if (_integral_node)
130  {
131  cout << "Integral information on stored on node RUN/PHGenIntegral:" << endl;
132  _integral_node->identify();
133  cout << " *------- End PYTHIA Integral Node Print ------------------------"
134  << "-------------------------------------------------* " << endl;
135  }
136  }
137 
139 }
140 
141 //__________________________________________________________
142 int PHPythia8::read_config(const char *cfg_file)
143 {
144  if (cfg_file) _configFile = cfg_file;
145 
146  if (verbosity >= VERBOSITY_SOME)
147  cout << "PHPythia8::read_config - Reading " << _configFile << endl;
148 
149  ifstream infile(_configFile.c_str());
150  if (infile.fail())
151  {
152  cout << "PHPythia8::read_config - Failed to open file " << _configFile << endl;
153  exit(2);
154  }
155 
156  _pythia->readFile(_configFile.c_str());
157 
159 }
160 
161 //-* print pythia config info
163 {
164  _pythia->info.list();
165 }
166 
168 {
169  if (verbosity >= VERBOSITY_MORE) cout << "PHPythia8::process_event - event: " << _eventcount << endl;
170 
171  bool passedGen = false;
172  bool passedTrigger = false;
173  int genCounter = 0;
174 
175  while (!passedTrigger)
176  {
177  ++genCounter;
178 
179  // generate another pythia event
180  while (!passedGen)
181  {
182  passedGen = _pythia->next();
183  }
184 
185  // test trigger logic
186 
187  bool andScoreKeeper = true;
189  {
190  cout << "PHPythia8::process_event - triggersize: " << _registeredTriggers.size() << endl;
191  }
192 
193  for (unsigned int tr = 0; tr < _registeredTriggers.size(); tr++)
194  {
195  bool trigResult = _registeredTriggers[tr]->Apply(_pythia);
196 
198  {
199  cout << "PHPythia8::process_event trigger: "
200  << _registeredTriggers[tr]->GetName() << " " << trigResult << endl;
201  }
202 
203  if (_triggersOR && trigResult)
204  {
205  passedTrigger = true;
206  break;
207  }
208  else if (_triggersAND)
209  {
210  andScoreKeeper &= trigResult;
211  }
212 
213  if (verbosity >= VERBOSITY_EVEN_MORE && !passedTrigger)
214  {
215  cout << "PHPythia8::process_event - failed trigger: "
216  << _registeredTriggers[tr]->GetName() << endl;
217  }
218  }
219 
220  if ((andScoreKeeper && _triggersAND) || (_registeredTriggers.size() == 0))
221  {
222  passedTrigger = true;
223  genCounter = 0;
224  }
225 
226  passedGen = false;
227  }
228 
229  // fill HepMC object with event & pass to
230 
231  HepMC::GenEvent *genevent = new HepMC::GenEvent(HepMC::Units::GEV, HepMC::Units::MM);
232  _pythiaToHepMC->fill_next_event(*_pythia, genevent, _eventcount);
233 
234  /* pass HepMC to PHNode*/
235  PHHepMCGenEvent *success = hepmc_helper.insert_event(genevent);
236  if (!success)
237  {
238  cout << "PHPythia8::process_event - Failed to add event to HepMC record!" << endl;
240  }
241 
242  // print outs
243 
244  if (verbosity >= VERBOSITY_MORE) cout << "PHPythia8::process_event - FINISHED WHOLE EVENT" << endl;
245  if (_eventcount < 2 && verbosity >= VERBOSITY_SOME) _pythia->event.list();
246  if (_eventcount >= 2 && verbosity >= VERBOSITY_A_LOT) _pythia->event.list();
247 
248  ++_eventcount;
249 
250  // save statistics
251  if (_integral_node)
252  {
253  _integral_node->set_N_Generator_Accepted_Event(_pythia->info.nAccepted());
254  _integral_node->set_N_Processed_Event(_eventcount);
255  _integral_node->set_Sum_Of_Weight(_pythia->info.weightSum());
256  _integral_node->set_Integrated_Lumi(_pythia->info.nAccepted() / (_pythia->info.sigmaGen() * 1e9));
257  }
258 
260 }
261 
262 int PHPythia8::create_node_tree(PHCompositeNode *topNode)
263 {
264  // HepMC IO
265  hepmc_helper.create_node_tree(topNode);
266 
267  PHNodeIterator iter(topNode);
268  PHCompositeNode *sumNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
269  if (!sumNode)
270  {
271  cout << PHWHERE << "RUN Node missing doing nothing" << endl;
273  }
274 
275  _integral_node = findNode::getClass<PHGenIntegral>(sumNode, "PHGenIntegral");
276  if (!_integral_node)
277  {
278  _integral_node = new PHGenIntegralv1("PHPythia8 with embedding ID of " + std::to_string(hepmc_helper.get_embedding_id()));
279  PHIODataNode<PHObject> *newmapnode = new PHIODataNode<PHObject>(_integral_node, "PHGenIntegral", "PHObject");
280  sumNode->addNode(newmapnode);
281  }
282  else
283  {
284  cout << "PHPythia8::create_node_tree - Fatal Error - "
285  << "RUN/PHGenIntegral node already exist. "
286  << "It is messy to overwrite integrated luminosities. Please turn off this function in the macro with " << endl;
287  cout << " PHPythia8::save_integrated_luminosity(false);" << endl;
288  cout << "The current RUN/PHGenIntegral node is ";
289  _integral_node->identify(cout);
290 
291  exit(EXIT_FAILURE);
292  }
293  assert(_integral_node);
294 
296 }
297 
299 {
301 }
302 
304 {
305  if (verbosity >= VERBOSITY_MORE) cout << "PHPythia8::registerTrigger - trigger " << theTrigger->GetName() << " registered" << endl;
306  _registeredTriggers.push_back(theTrigger);
307 }
int verbosity
The verbosity level. 0 means not verbose at all.
Definition: Fun4AllBase.h:75
#define PHWHERE
Definition: phool.h:23
int get_embedding_id() const
PHHepMCGenEvent * insert_event(HepMC::GenEvent *evt)
send HepMC::GenEvent to DST tree. This function takes ownership of evt
int End(PHCompositeNode *topNode)
Called at the end of all processing.
Definition: PHPythia8.C:109
virtual void set_N_Processed_Event(ULong64_t nProcessedEvent)
Number of processed events in the Fun4All cycles.
Definition: PHGenIntegral.h:55
virtual void set_N_Generator_Accepted_Event(ULong64_t nGeneratorAcceptedEvent)
Number of accepted events in the event generator. This can be higher than fNProcessedEvent depending ...
Definition: PHGenIntegral.h:44
#define NULL
Definition: Pdb.h:9
PHGenIntegralv1.
virtual void identify(std::ostream &os=std::cout) const
Definition: PHObject.cc:21
Output more messages.
Definition: Fun4AllBase.h:42
void print_config() const
Definition: PHPythia8.C:162
int Init(PHCompositeNode *topNode)
Definition: PHPythia8.C:69
Output a lot of messages.
Definition: Fun4AllBase.h:48
Output even more messages.
Definition: Fun4AllBase.h:45
Output some useful messages during manual command line running.
Definition: Fun4AllBase.h:39
virtual void set_Sum_Of_Weight(Double_t sumOfWeight)
Definition: PHGenIntegral.h:70
virtual std::string GetName()
int process_event(PHCompositeNode *topNode)
Definition: PHPythia8.C:167
void register_trigger(PHPy8GenTrigger *theTrigger)
set event selection criteria
Definition: PHPythia8.C:303
int ResetEvent(PHCompositeNode *topNode)
Clean up after each event.
Definition: PHPythia8.C:298
virtual void set_Integrated_Lumi(Double_t integratedLumi)
Integrated luminosity in pb^-1.
Definition: PHGenIntegral.h:33
PHIODataNode< PHObject > PHObjectNode_t
virtual ~PHPythia8()
Definition: PHPythia8.C:64
PHPythia8(const std::string &name="PHPythia8")
Definition: PHPythia8.C:32
int create_node_tree(PHCompositeNode *topNode)
init interface nodes
void set_embedding_id(int id)