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: printf(
"skipping unrecognised command: '%s'\n",buf);
break;
167 case UNRECOGNIZED_OPTION: printf(
"skipping unrecognised option: '%s'\n",buf);
break;
168 case UNRECOGNIZED_INPUT: printf(
"skipping unrecognised input source: '%s'\n",buf);
break;
169 case UNRECOGNIZED_TOOL: printf(
"skipping unrecognised tool: '%s'\n",buf);
break;
170 case UNAVAILABLE_TOOL: printf(
"skipping unavailable tool: '%s'\n",buf);
break;
171 case ADDITIONAL_INPUT: printf(
"skipping additional input source: '%s'\n",buf);
break;
172 case CANNOT_OPEN_FILE: printf(
"skipping input file: '%s'\n",buf);
break;
181 if(m_has_input_source) m_status = 0;
182 else printf(
"ValidationControl: no valid input source\n");
185 bool ValidationControl::new_event() {
186 if( m_status )
return false;
187 if( m_events && ( m_event_counter >= m_events ) )
return false;
189 if(m_event_counter) {
190 if( m_momentum_check_events>0 ) --m_momentum_check_events;
191 if( m_print_events>0 ) --m_print_events;
193 else m_timer.start();
198 if( m_event_counter == 1 ) {
199 printf(
"ValidationControl: event 1 of %-7i\n",m_events);
200 m_events_print_step = m_events/10;
202 else if( m_event_counter%m_events_print_step == 0 ) {
203 int elapsed = m_timer.elapsed_time();
205 int total = m_timer.total_time();
206 printf(
"ValidationControl: event %7i (%6.2f%%, %7ims current, %7ims total)\n",m_event_counter,m_event_counter*100./m_events,elapsed,total);
211 if( m_event_counter == 1 ) {
212 printf(
"ValidationControl: event 1\n");
213 m_events_print_step = 1000;
215 else if( m_event_counter%m_events_print_step == 0 ) {
216 int elapsed = m_timer.elapsed_time();
218 int total = m_timer.total_time();
219 printf(
"ValidationControl: event %7i (%6ims current, %7ims total)\n",m_event_counter,elapsed,total);
227 void ValidationControl::initialize() {
228 printf(
"ValidationControl: initializing\n");
230 for (std::vector<ValidationTool *>::iterator tool=m_toolchain.begin();tool!=m_toolchain.end();++tool) (*tool)->initialize();
233 void ValidationControl::process(
GenEvent &hepmc) {
238 for (std::vector<ValidationTool *>::iterator tool=m_toolchain.begin();tool!=m_toolchain.end();++tool) {
240 Timer *timer = (*tool)->timer();
242 if(timer) timer->
start();
243 m_status = (*tool)->process(hepmc);
244 if(timer) timer->
stop();
249 if((*tool)->tool_modifies_event() && m_print_events) {
250 printf(
"--------------------------------------------------------------\n");
251 printf(
" Print event: %s\n",(*tool)->name().c_str());
252 printf(
"--------------------------------------------------------------\n");
254 HEPMC2CODE( hepmc.print(); )
258 if((*tool)->tool_modifies_event() && m_momentum_check_events ) {
263 for ( GenEvent::particle_const_iterator p = hepmc.particles_begin();
264 p != hepmc.particles_end(); ++p ) {
265 if( (*p)->status() != 1 )
continue;
271 sum.
setE ( sum.
e() + m.
e() );
274 double momentum = input_momentum.
px() + input_momentum.
py() + input_momentum.
pz() + input_momentum.
e();
275 if( fabs(momentum) > 10e-12 ) {
276 double px = input_momentum.
px() - sum.
px();
277 double py = input_momentum.
py() - sum.
py();
278 double pz = input_momentum.
pz() - sum.
pz();
279 double e = input_momentum.
e() - sum.
e();
280 delta = sqrt(px*px + py*py + pz*pz + e*e);
286 for (
auto p: hepmc.
particles())
if( p->status() != 1 )
continue;
else sum += p->momentum();
287 if(!input_momentum.
is_zero()) delta = (input_momentum - sum).
length();
290 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());
292 if( delta < m_momentum_check_threshold ) printf(
"\n");
293 else printf(
" - WARNING! Difference = %+15.8e\n",delta);
295 input_momentum = sum;
300 void ValidationControl::finalize() {
301 printf(
"ValidationControl: finalizing\n");
304 for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin();t!=m_toolchain.end();++t)
307 printf(
"ValidationControl: printing timers\n");
310 for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin();t!=m_toolchain.end();++t)
311 if((*t)->timer()) (*t)->timer()->print();
314 printf(
"ValidationControl: finished processing:\n");
317 for (std::vector<ValidationTool *>::iterator t=m_toolchain.begin();t!=m_toolchain.end();++t)
318 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.