HepMC3 event record library
ValidationControl.cc
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 #include "ValidationControl.h"
7 
8 #ifdef SIMPLEEVENTTOOL
9 #include "SimpleEventTool.h"
10 #endif
11 
12 #ifdef PHOTOSPP
13 #include "PhotosValidationTool.h"
14 #endif
15 
16 #ifdef TAUOLAPP
17 #include "TauolaValidationTool.h"
18 #endif
19 
20 #ifdef MCTESTER
21 #include "McTesterValidationTool.h"
22 #endif
23 
24 #ifdef PYTHIA8
25 #include "PythiaValidationTool.h"
26 #endif
27 
28 #include <fstream>
29 #include <cstdio>
30 
32  m_events(0),
33  m_momentum_check_events(0),
34  m_momentum_check_threshold(10e-6),
35  m_print_events(0),
36  m_event_counter(0),
37  m_status(-1),
38  m_timer("processing time"),
39  m_has_input_source(0) {
40 }
41 
43  for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
44  delete *t;
45 }
46 
47 void ValidationControl::read_file(const std::string &filename) {
48 
49  // Open config file
50  std::ifstream in(filename.c_str());
51 
52  if(!in.is_open()) {
53  printf("ValidationControl: error reading config file: %s\n",filename.c_str());
54  m_status = -1;
55  return;
56  }
57  else printf("ValidationControl: parsing config file: %s\n",filename.c_str());
58 
59  // Parse config file
60  char buf[256];
61  int line = 0;
62 
63  while(!in.eof()) {
64  PARSING_STATUS status = PARSING_OK;
65  ++line;
66 
67  in >> buf;
68 
69  if( strlen(buf) < 3 || buf[0] == ' ' || buf[0] == '#' ) {
70  in.getline(buf,255);
71  continue;
72  }
73 
74  // Parse event number
75  if( strncmp(buf,"EVENTS",6)==0 ) {
76  in>>m_events;
77  }
78  // Parse input source
79  else if( strncmp(buf,"INPUT",5)==0 ) {
80  in >> buf;
81 
82  if( m_has_input_source ) status = ADDITIONAL_INPUT;
83  else {
84  ValidationTool *input = NULL;
85  // Use tool as input source - currently only one supported tool
86  if( strncmp(buf,"tool",4)==0 ) {
87 #ifdef SIMPLEEVENTTOOL
88  input = new SimpleEventTool();
89 #else
90  status = UNAVAILABLE_TOOL;
91 #endif
92  }
93  else if( strncmp(buf,"pythia8",7)==0) {
94 #ifdef PYTHIA8
95  in >> buf;
96  input = new PythiaValidationTool(buf);
97 #else
98  status = UNAVAILABLE_TOOL;
99 #endif
100  }
101  else status = UNRECOGNIZED_INPUT;
102 
103  if(!status) {
104  m_has_input_source = true;
105  m_toolchain.insert(m_toolchain.begin(),input);
106  }
107  }
108  }
109  // Parse tools used
110  else if( strncmp(buf,"TOOL",3)==0 ) {
111  in >> buf;
112 
113  if ( strncmp(buf,"tauola",6)==0 ) {
114 #ifdef TAUOLAPP
115  m_toolchain.push_back( new TauolaValidationTool() );
116 #else
117  status = UNAVAILABLE_TOOL;
118 #endif
119  }
120  else if( strncmp(buf,"photos",6)==0 ) {
121 #ifdef PHOTOSPP
122  m_toolchain.push_back( new PhotosValidationTool() );
123 #else
124  status = UNAVAILABLE_TOOL;
125 #endif
126  }
127  else if( strncmp(buf,"mctester",8)==0 ) {
128 #ifdef MCTESTER
129  m_toolchain.push_back( new McTesterValidationTool() );
130 #else
131  status = UNAVAILABLE_TOOL;
132 #endif
133  }
134  else status = UNRECOGNIZED_TOOL;
135  }
136  // Parse option
137  else if( strncmp(buf,"SET",3)==0 ) {
138  in >> buf;
139 
140  if ( strncmp(buf,"print_events",12)==0 ) {
141  in >> buf;
142 
143  int events = 0;
144  if( strncmp(buf,"ALL",3)==0 ) events = -1;
145  else events = atoi(buf);
146 
147  print_events(events);
148  }
149  else if( strncmp(buf,"check_momentum",14)==0 ) {
150  in >> buf;
151 
152  int events = 0;
153  if( strncmp(buf,"ALL",3)==0 ) events = -1;
154  else events = atoi(buf);
155 
157  }
158  else status = UNRECOGNIZED_OPTION;
159  }
160  else status = UNRECOGNIZED_COMMAND;
161 
162  // Error checking
163  if(status != PARSING_OK) printf("ValidationControl: config file line %i: ",line);
164 
165  switch(status) {
166  case UNRECOGNIZED_COMMAND:
167  printf("skipping unrecognised command: '%s'\n",buf);
168  break;
169  case UNRECOGNIZED_OPTION:
170  printf("skipping unrecognised option: '%s'\n",buf);
171  break;
172  case UNRECOGNIZED_INPUT:
173  printf("skipping unrecognised input source: '%s'\n",buf);
174  break;
175  case UNRECOGNIZED_TOOL:
176  printf("skipping unrecognised tool: '%s'\n",buf);
177  break;
178  case UNAVAILABLE_TOOL:
179  printf("skipping unavailable tool: '%s'\n",buf);
180  break;
181  case ADDITIONAL_INPUT:
182  printf("skipping additional input source: '%s'\n",buf);
183  break;
184  case CANNOT_OPEN_FILE:
185  printf("skipping input file: '%s'\n",buf);
186  break;
187  default:
188  break;
189  }
190 
191  // Ignore rest of the line
192  in.getline(buf,255);
193  }
194 
195  // Having input source is enough to start validation
197  else printf("ValidationControl: no valid input source\n");
198 }
199 
201  if( m_status ) return false;
202  if( m_events && ( m_event_counter >= m_events ) ) return false;
203 
204  if(m_event_counter) {
206  if( m_print_events>0 ) --m_print_events;
207  }
208  else m_timer.start();
209 
210  ++m_event_counter;
211 
212  if( m_events ) {
213  if( m_event_counter == 1 ) {
214  printf("ValidationControl: event 1 of %-7i\n",m_events);
216  }
217  else if( m_event_counter%m_events_print_step == 0 ) {
218  int elapsed = m_timer.elapsed_time();
219  m_timer.stop();
220  int total = m_timer.total_time();
221  printf("ValidationControl: event %7i (%6.2f%%, %7ims current, %7ims total)\n",m_event_counter,m_event_counter*100./m_events,elapsed,total);
222  m_timer.start();
223  }
224  }
225  else {
226  if( m_event_counter == 1 ) {
227  printf("ValidationControl: event 1\n");
228  m_events_print_step = 1000;
229  }
230  else if( m_event_counter%m_events_print_step == 0 ) {
231  int elapsed = m_timer.elapsed_time();
232  m_timer.stop();
233  int total = m_timer.total_time();
234  printf("ValidationControl: event %7i (%6ims current, %7ims total)\n",m_event_counter,elapsed,total);
235  m_timer.start();
236  }
237  }
238 
239  return true;
240 }
241 
243  printf("ValidationControl: initializing\n");
244 
245  for (std::vector<ValidationTool *>::iterator tool=m_toolchain.begin(); tool!=m_toolchain.end(); ++tool) (*tool)->initialize();
246 }
247 
249 
250  m_status = 0;
251 
252  FourVector input_momentum;
253  for (std::vector<ValidationTool *>::iterator tool=m_toolchain.begin(); tool!=m_toolchain.end(); ++tool) {
254 
255  Timer *timer = (*tool)->timer();
256 
257  if(timer) timer->start();
258  m_status = (*tool)->process(hepmc);
259  if(timer) timer->stop();
260 
261  // status != 0 means an error - stop processing current event
262  if(m_status) return;
263 
264  if((*tool)->tool_modifies_event() && m_print_events) {
265  printf("--------------------------------------------------------------\n");
266  printf(" Print event: %s\n",(*tool)->name().c_str());
267  printf("--------------------------------------------------------------\n");
268 
269  HEPMC2CODE( hepmc.print(); )
270  HEPMC3CODE( Print::listing(hepmc,8); )
271  }
272 
273  if((*tool)->tool_modifies_event() && m_momentum_check_events ) {
274  FourVector sum;
275  double delta = 0.0;
276 
277  HEPMC2CODE(
278  for ( GenEvent::particle_const_iterator p = hepmc.particles_begin();
279  p != hepmc.particles_end(); ++p ) {
280  if( (*p)->status() != 1 ) continue;
281  //(*p)->print();
282  FourVector m = (*p)->momentum();
283  sum.setPx( sum.px() + m.px() );
284  sum.setPy( sum.py() + m.py() );
285  sum.setPz( sum.pz() + m.pz() );
286  sum.setE ( sum.e() + m.e() );
287  }
288 
289  double momentum = input_momentum.px() + input_momentum.py() + input_momentum.pz() + input_momentum.e();
290  if( fabs(momentum) > 10e-12 ) {
291  double px = input_momentum.px() - sum.px();
292  double py = input_momentum.py() - sum.py();
293  double pz = input_momentum.pz() - sum.pz();
294  double e = input_momentum.e() - sum.e();
295  delta = sqrt(px*px + py*py + pz*pz + e*e);
296  }
297  )
298 
299  HEPMC3CODE(
300  //vector<GenParticlePtr> results = applyFilter(Selector::STATUS==1,hepmc.particles());
301  for (auto p: hepmc.particles()) if( p->status() != 1 ) continue; else sum += p->momentum();
302  if(!input_momentum.is_zero()) delta = (input_momentum - sum).length();
303  )
304 
305  printf("Momentum sum: %+15.8e %+15.8e %+15.8e %+15.8e (evt: %7i, %s)",sum.px(),sum.py(),sum.pz(),sum.e(),m_event_counter,(*tool)->name().c_str());
306 
307  if( delta < m_momentum_check_threshold ) printf("\n");
308  else printf(" - WARNING! Difference = %+15.8e\n",delta);
309 
310  input_momentum = sum;
311  }
312  }
313 }
314 
316  printf("ValidationControl: finalizing\n");
317 
318  // Finalize
319  for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
320  (*t)->finalize();
321 
322  printf("ValidationControl: printing timers\n");
323 
324  // Print timers
325  for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
326  if((*t)->timer()) (*t)->timer()->print();
327 
328 
329  printf("ValidationControl: finished processing:\n");
330 
331  // List tools
332  for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
333  printf(" tool: %s\n",(*t)->long_name().c_str());
334 
335 }
HepMC3::FourVector
Generic 4-vector.
Definition: FourVector.h:35
ValidationControl::m_events
int m_events
events
Definition: ValidationControl.h:69
ValidationControl::m_events_print_step
int m_events_print_step
events print step
Definition: ValidationControl.h:70
HepMC3::FourVector::pz
double pz() const
z-component of momentum
Definition: FourVector.h:105
ValidationTool
Virtual Interface to validation tools.
Definition: ValidationTool.h:26
TauolaValidationTool
Interface for validatio to Tauola.
Definition: TauolaValidationTool.h:25
PythiaValidationTool
Interface for validatio to Pythia.
Definition: PythiaValidationTool.h:29
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:42
ValidationControl::~ValidationControl
~ValidationControl()
Destructor.
Definition: ValidationControl.cc:42
ValidationControl::m_momentum_check_events
int m_momentum_check_events
mom check events
Definition: ValidationControl.h:71
ValidationControl::check_momentum_for_events
void check_momentum_for_events(int events)
N events to check momentum.
Definition: ValidationControl.h:61
McTesterValidationTool
Interface to MCTester.
Definition: McTesterValidationTool.h:23
ValidationControl::m_toolchain
std::vector< ValidationTool * > m_toolchain
Toolchain.
Definition: ValidationControl.h:67
HepMC3::FourVector::length
double length() const
Magnitude of spatial (x, y, z) 3-vector.
Definition: FourVector.h:127
ValidationControl::read_file
void read_file(const std::string &filename)
Read file.
Definition: ValidationControl.cc:47
ValidationControl::m_event_counter
int m_event_counter
counter of events
Definition: ValidationControl.h:74
Timer
Used to benchmark MC generators.
Definition: Timer.h:38
HepMC3::FourVector::e
double e() const
Energy component of momentum.
Definition: FourVector.h:112
ValidationControl::PARSING_STATUS
PARSING_STATUS
parsing stutus
Definition: ValidationControl.h:81
Timer::elapsed_time
int elapsed_time()
Definition: Timer.h:49
Timer::stop
void stop()
Definition: Timer.h:60
ValidationControl::m_timer
Timer m_timer
Times.
Definition: ValidationControl.h:76
ValidationControl::m_print_events
int m_print_events
print events
Definition: ValidationControl.h:73
ValidationControl::initialize
void initialize()
Init function.
Definition: ValidationControl.cc:242
ValidationControl::print_events
void print_events(int events)
N events to print.
Definition: ValidationControl.h:59
ValidationControl::m_status
int m_status
status
Definition: ValidationControl.h:75
ValidationControl::m_momentum_check_threshold
double m_momentum_check_threshold
mom check threshold
Definition: ValidationControl.h:72
PhotosValidationTool
Interface for validatio to Photos.
Definition: PhotosValidationTool.h:25
ValidationControl::m_has_input_source
bool m_has_input_source
Input source flag.
Definition: ValidationControl.h:78
SimpleEventTool
Simple validation.
Definition: SimpleEventTool.h:20
HepMC3::FourVector::is_zero
bool is_zero() const
Check if the length of this vertex is zero.
Definition: FourVector.h:174
HepMC3::FourVector::py
double py() const
y-component of momentum
Definition: FourVector.h:98
Timer::total_time
int total_time()
Definition: Timer.h:55
ValidationControl::finalize
void finalize()
Finalize.
Definition: ValidationControl.cc:315
HepMC3::FourVector::px
double px() const
x-component of momentum
Definition: FourVector.h:91
ValidationControl::new_event
bool new_event()
New event.
Definition: ValidationControl.cc:200
ValidationControl::ValidationControl
ValidationControl()
Constructor.
Definition: ValidationControl.cc:31
ValidationControl::process
void process(GenEvent &hepmc)
Process event.
Definition: ValidationControl.cc:248
HepMC3::GenEvent::particles
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Definition: GenEvent.cc:40
Timer::start
void start()
Definition: Timer.h:44