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