6 #include "ValidationControl.h"
9 #include "SimpleEventTool.h"
13 #include "PhotosValidationTool.h"
17 #include "TauolaValidationTool.h"
21 #include "McTesterValidationTool.h"
25 #include "PythiaValidationTool.h"
31 ValidationControl::ValidationControl():
33 m_momentum_check_events(0),
34 m_momentum_check_threshold(10e-6),
38 m_timer(
"processing time"),
39 m_has_input_source(0) {
42 ValidationControl::~ValidationControl() {
43 for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
47 void ValidationControl::read_file(
const std::string &filename) {
50 std::ifstream in(filename.c_str());
53 printf(
"ValidationControl: error reading config file: %s\n",filename.c_str());
57 else printf(
"ValidationControl: parsing config file: %s\n",filename.c_str());
64 PARSING_STATUS status = PARSING_OK;
69 if( strlen(buf) < 3 || buf[0] ==
' ' || buf[0] ==
'#' ) {
75 if( strncmp(buf,
"EVENTS",6)==0 ) {
79 else if( strncmp(buf,
"INPUT",5)==0 ) {
82 if( m_has_input_source ) status = ADDITIONAL_INPUT;
86 if( strncmp(buf,
"tool",4)==0 ) {
87 #ifdef SIMPLEEVENTTOOL
90 status = UNAVAILABLE_TOOL;
93 else if( strncmp(buf,
"pythia8",7)==0) {
98 status = UNAVAILABLE_TOOL;
101 else status = UNRECOGNIZED_INPUT;
104 m_has_input_source =
true;
105 m_toolchain.insert(m_toolchain.begin(),input);
110 else if( strncmp(buf,
"TOOL",3)==0 ) {
113 if ( strncmp(buf,
"tauola",6)==0 ) {
117 status = UNAVAILABLE_TOOL;
120 else if( strncmp(buf,
"photos",6)==0 ) {
124 status = UNAVAILABLE_TOOL;
127 else if( strncmp(buf,
"mctester",8)==0 ) {
131 status = UNAVAILABLE_TOOL;
134 else status = UNRECOGNIZED_TOOL;
137 else if( strncmp(buf,
"SET",3)==0 ) {
140 if ( strncmp(buf,
"print_events",12)==0 ) {
144 if( strncmp(buf,
"ALL",3)==0 ) events = -1;
145 else events = atoi(buf);
147 print_events(events);
149 else if( strncmp(buf,
"check_momentum",14)==0 ) {
153 if( strncmp(buf,
"ALL",3)==0 ) events = -1;
154 else events = atoi(buf);
156 check_momentum_for_events(events);
158 else status = UNRECOGNIZED_OPTION;
160 else status = UNRECOGNIZED_COMMAND;
163 if(status != PARSING_OK) printf(
"ValidationControl: config file line %i: ",line);
166 case UNRECOGNIZED_COMMAND:
167 printf(
"skipping unrecognised command: '%s'\n",buf);
169 case UNRECOGNIZED_OPTION:
170 printf(
"skipping unrecognised option: '%s'\n",buf);
172 case UNRECOGNIZED_INPUT:
173 printf(
"skipping unrecognised input source: '%s'\n",buf);
175 case UNRECOGNIZED_TOOL:
176 printf(
"skipping unrecognised tool: '%s'\n",buf);
178 case UNAVAILABLE_TOOL:
179 printf(
"skipping unavailable tool: '%s'\n",buf);
181 case ADDITIONAL_INPUT:
182 printf(
"skipping additional input source: '%s'\n",buf);
184 case CANNOT_OPEN_FILE:
185 printf(
"skipping input file: '%s'\n",buf);
196 if(m_has_input_source) m_status = 0;
197 else printf(
"ValidationControl: no valid input source\n");
200 bool ValidationControl::new_event() {
201 if( m_status )
return false;
202 if( m_events && ( m_event_counter >= m_events ) )
return false;
204 if(m_event_counter) {
205 if( m_momentum_check_events>0 ) --m_momentum_check_events;
206 if( m_print_events>0 ) --m_print_events;
208 else m_timer.start();
213 if( m_event_counter == 1 ) {
214 printf(
"ValidationControl: event 1 of %-7i\n",m_events);
215 m_events_print_step = m_events/10;
217 else if( m_event_counter%m_events_print_step == 0 ) {
218 int elapsed = m_timer.elapsed_time();
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);
226 if( m_event_counter == 1 ) {
227 printf(
"ValidationControl: event 1\n");
228 m_events_print_step = 1000;
230 else if( m_event_counter%m_events_print_step == 0 ) {
231 int elapsed = m_timer.elapsed_time();
233 int total = m_timer.total_time();
234 printf(
"ValidationControl: event %7i (%6ims current, %7ims total)\n",m_event_counter,elapsed,total);
242 void ValidationControl::initialize() {
243 printf(
"ValidationControl: initializing\n");
245 for (std::vector<ValidationTool *>::iterator tool=m_toolchain.begin(); tool!=m_toolchain.end(); ++tool) (*tool)->initialize();
248 void ValidationControl::process(
GenEvent &hepmc) {
253 for (std::vector<ValidationTool *>::iterator tool=m_toolchain.begin(); tool!=m_toolchain.end(); ++tool) {
255 Timer *timer = (*tool)->timer();
257 if(timer) timer->
start();
258 m_status = (*tool)->process(hepmc);
259 if(timer) timer->
stop();
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");
269 HEPMC2CODE( hepmc.print(); )
273 if((*tool)->tool_modifies_event() && m_momentum_check_events ) {
278 for ( GenEvent::particle_const_iterator p = hepmc.particles_begin();
279 p != hepmc.particles_end(); ++p ) {
280 if( (*p)->status() != 1 )
continue;
286 sum.
setE ( sum.
e() + m.
e() );
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);
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();
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());
307 if( delta < m_momentum_check_threshold ) printf(
"\n");
308 else printf(
" - WARNING! Difference = %+15.8e\n",delta);
310 input_momentum = sum;
315 void ValidationControl::finalize() {
316 printf(
"ValidationControl: finalizing\n");
319 for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
322 printf(
"ValidationControl: printing timers\n");
325 for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin(); t!=m_toolchain.end(); ++t)
326 if((*t)->timer()) (*t)->timer()->print();
329 printf(
"ValidationControl: finished processing:\n");
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());
bool is_zero() const
Check if the length of this vertex is zero.
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
void setPy(double pyy)
Set y-component of momentum.
double e() const
Energy component of momentum.
Stores event-related information.
double px() const
x-component of momentum
void setPz(double pzz)
Set z-component of momentum.
void setPx(double pxx)
Set x-component of momentum.
double length() const
Magnitude of spatial (x, y, z) 3-vector.
double pz() const
z-component of momentum
double py() const
y-component of momentum
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
void setE(double ee)
Set energy component of momentum.