File: EventHandler.cc

package info (click to toggle)
thepeg 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,312 kB
  • ctags: 11,509
  • sloc: cpp: 57,129; sh: 11,315; java: 3,212; lisp: 1,402; makefile: 830; ansic: 58; perl: 3
file content (522 lines) | stat: -rw-r--r-- 18,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// -*- C++ -*-
//
// EventHandler.cc is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
//
// This is the implementation of the non-inlined, non-templated member
// functions of the EventHandler class.
//

#include "EventHandler.h"
#include "ThePEG/Handlers/XComb.h"
#include "ThePEG/Handlers/Hint.h"
#include "ThePEG/Persistency/PersistentOStream.h"
#include "ThePEG/Persistency/PersistentIStream.h"
#include "ThePEG/Handlers/CascadeHandler.h"
#include "ThePEG/Handlers/MultipleInteractionHandler.h"
#include "ThePEG/Handlers/HadronizationHandler.h"
#include "ThePEG/Handlers/DecayHandler.h"
#include "ThePEG/Handlers/SubProcessHandler.h"
#include "ThePEG/Cuts/Cuts.h"
#include "ThePEG/PDF/PartonExtractor.h"
#include "ThePEG/Interface/Switch.h"
#include "ThePEG/Interface/Parameter.h"
#include "ThePEG/Interface/Reference.h"
#include "ThePEG/Interface/RefVector.h"
#include "ThePEG/Interface/ClassDocumentation.h"
#include "ThePEG/EventRecord/Event.h"
#include "ThePEG/Utilities/Debug.h"
#include "ThePEG/Repository/EventGenerator.h"
#include "ThePEG/Handlers/LuminosityFunction.h"
#include "ThePEG/Utilities/Throw.h"
#include "ThePEG/Utilities/EnumIO.h"
#include "ThePEG/Utilities/Rebinder.h"

using namespace ThePEG;

EventHandler::EventHandler(bool warnincomplete)
: theMaxLoop(100000), theStatLevel(2), theConsistencyLevel(clCollision),
  theConsistencyEpsilon(sqrt(Constants::epsilon)),
  warnIncomplete(warnincomplete) {
  setupGroups();
}

EventHandler::
EventHandler(const EventHandler & x)
  : HandlerBase(x), LastXCombInfo<>(x),
    theMaxLoop(x.theMaxLoop), theStatLevel(x.theStatLevel),
    theConsistencyLevel(x.theConsistencyLevel),
    theConsistencyEpsilon(x.theConsistencyEpsilon),
    theLumiFn(x.theLumiFn), theCuts(x.theCuts),
    thePartonExtractor(x.thePartonExtractor),
    theSubprocessGroup(x.theSubprocessGroup),
    theCascadeGroup(x.theCascadeGroup), theMultiGroup(x.theMultiGroup),
    theHadronizationGroup(x.theHadronizationGroup),
    theDecayGroup(x.theDecayGroup), warnIncomplete(x.warnIncomplete),
    theIncoming(x.theIncoming) {
  setupGroups();
}

EventHandler::~EventHandler() {}

IBPtr EventHandler::clone() const {
  return new_ptr(*this);
}

IBPtr EventHandler::fullclone() const {
  return new_ptr(*this);
}

void EventHandler::lumiFn(LumiFnPtr newLumiFn) {
  if ( newLumiFn->canHandle(incoming()) ) theLumiFn = newLumiFn;
  else throw LumiFuncError()
    << "Cannot use '" << newLumiFn->name()
    << "' as the luminosity function in the event handler '" << name()
    << "' since it cannot handle the requested incoming particles."
    << Exception::warning;
}

void EventHandler::setupGroups() {
  theGroups.clear();
  theGroups.push_back(&theSubprocessGroup);
  theGroups.push_back(&theCascadeGroup);
  theGroups.push_back(&theMultiGroup);
  theGroups.push_back(&theHadronizationGroup);
  theGroups.push_back(&theDecayGroup);
  optGroups.clear();
  optGroups.push_back(&optSubprocessGroup);
  optGroups.push_back(&optCascadeGroup);
  optGroups.push_back(&optMultiGroup);
  optGroups.push_back(&optHadronizationGroup);
  optGroups.push_back(&optDecayGroup);
}

void EventHandler::initGroups() {
  for ( unsigned int i = 0; i < groups().size(); ++i )
    groups()[i]->init(*optGroups[i]);
}

tCascHdlPtr EventHandler::CKKWHandler() const {
  return dynamic_ptr_cast<tCascHdlPtr>(theCascadeGroup.defaultHandler());
}

EventPtr EventHandler::generateEvent() {
  throw EventHandlerIncompleteError()
    << "The function generateEvent() was called for the EventHandler '" << name()
    << "' which is not capable of generating complete events."
    << Exception::runerror;
}

tEventPtr EventHandler::generateEvent(tStepPtr s) {
  EventPtr e = new_ptr(Event(PPair(), tcEventBasePtr(), generator()->runName(),
			     generator()->currentEventNumber()));
  e->newCollision()->addStep(s);
  return generateEvent(tEventPtr(e));
}

tEventPtr EventHandler::generateEvent(tEventPtr e) {
  clearEvent();
  e->setInfo(this, generator()->runName(),
	     generator()->currentEventNumber(), 1.0);
  currentEvent(e);
  if ( e->collisions().empty() || !e->collisions().back()->finalStep()) {
    throw EventHandlerIncompleteError()
      << "The function generateEvent() was called with an empty Event "
      << "for the EventHandler '" << name()
      << "' which is not capable of generating complete events."
      << Exception::runerror;
  }
  currentCollision(e->collisions().back());
  currentStep(currentCollision()->finalStep());
  EventHandler::initGroups();
  continueCollision();
  return currentEvent();
}

tCollPtr EventHandler::continueCollision() {
  generator()->currentEventHandler(this);
  while (1) {
    if ( consistencyLevel() == clStep || consistencyLevel() == clPrintStep )
      checkConsistency();
    bool done = true;
    for ( GroupVector::iterator git = groups().begin();
	  git != groups().end(); ++git ) {
      HandlerGroupBase & group = **git;
      HandlerGroupBase::StepWithHint sh;
      sh = group.next();
      if ( !group.empty() ) {
	performStep(sh.first, sh.second);
	done = false;
	break;
      }
    }
    if ( done ) break;
  }
  if ( consistencyLevel() == clCollision ||
       consistencyLevel() == clPrintCollision ) checkConsistency();
  return currentCollision();
}

void EventHandler::clearEvent() {
  for ( GroupVector::const_iterator git = groups().begin();
	git != groups().end(); ++git ) (**git).clear();
}

bool EventHandler::empty() const {
  for ( GroupVector::const_iterator git = groups().begin();
	git != groups().end(); ++git )
    if ( !(**git).empty() ) return false;
  return true;
}

void EventHandler::select(tXCombPtr newXComb) {
  theLastXComb = newXComb;
  lumiFn().select(newXComb);
  if ( CKKWHandler() )
    CKKWHandler()->setXComb(newXComb);
}

void EventHandler::
performStep(tStepHdlPtr handler, tHintPtr hint) {
  if ( !lastXCombPtr() ) theLastXComb = new_ptr(XComb());

  if ( hint->stop() ) throw Stop();
  tStepPtr oldStep = currentStep();
  currentStepHandler(handler);
  handler->eventHandler(this);
  try {
    generator()->currentStepHandler(handler);
    handler->handle(*this, hint->tagged(*oldStep), *hint);
    generator()->currentStepHandler(tStepHdlPtr());
  }
  catch (...) {
    generator()->currentStepHandler(tStepHdlPtr());
    if ( oldStep != currentStep() ) popStep();
    throw;
  }

  while ( currentStep()->nullStep() ) popStep();

  if ( ThePEG_DEBUG_ITEM(2) && oldStep != currentStep() )
    generator()->logfile() << *currentStep();
}

void EventHandler::
addStep(Group::Level level, Group::Handler group, tStepHdlPtr s, tHintPtr h) {
  if ( !h ) h = Hint::Default();
  switch ( level ) {
  case Group::main:
    if ( s && !groups()[group]->setHandler(s, *optGroups[group]) )
      throw EventHandlerStepError()
	<< "Cannot add the StepHandler '" << s->name()
	<< "' to the list of step handlers in the EventHandler '" << name()
	<< "' because it is not of class '" << groups()[group]->handlerClass()
	<< "'." << Exception::abortnow;
    groups()[group]->addHint(h, *optGroups[group]);
    break;
  case Group::before:
    groups()[group]->addPreHandler(s, h, *optGroups[group]);
    break;
  case Group::after:
    groups()[group]->addPostHandler(s, h, *optGroups[group]);
    break;
  }
  for ( long gr = group + 1; gr <= Group::decay; ++gr )
    groups()[gr]->addHint(Hint::Default(), *optGroups[group]);
}

void EventHandler::throwCurrent() {
  clean();
}

void EventHandler::clean() {
  currentEvent(EventPtr());
  currentCollision(CollPtr());
  currentStep(StepPtr());
}

void EventHandler::statistics(ostream &) const {}

void EventHandler::initialize() {}

EventPtr EventHandler::continueEvent() {
  try {
    continueCollision();
  }
  catch (Veto) {
  }
  catch (Stop) {
  }
  catch (Exception &) {
    throw;
  }
  return currentEvent(); 
}

CrossSection EventHandler::histogramScale() const {
  generator()->logWarning(
    EventHandlerHistError()
    << "The event handler '" << name() << "' was not able give a cross "
    "section for scaling histograms. The resulting histograms will not "
    "yeald correct cross section." << Exception::warning);
  return 1.0*picobarn;
}

CrossSection EventHandler::integratedXSec() const {
  generator()->logWarning(
    EventHandlerHistError()
    << "The event handler '" << name() << "' was not able give the cross "
    "section for the generated processes." << Exception::warning);
  return 1.0*picobarn;
}

CrossSection EventHandler::integratedXSecErr() const {
  generator()->logWarning(
    EventHandlerHistError()
    << "The event handler '" << name() << "' was not able give the cross "
    "section error for the generated processes." << Exception::warning);
  return ZERO;
}

void EventHandler::checkConsistency() const {
  if ( !currentCollision() ) return;
  const Collision & c = *currentCollision();
  if ( !c.incoming().first && !c.incoming().second ) return;
  if ( !c.finalStep() ) return;
  LorentzMomentum pi;
  if ( c.incoming().first ) pi = c.incoming().first->momentum();
  if ( c.incoming().second ) pi += c.incoming().second->momentum();
  int ci = 0;
  if ( c.incoming().first ) ci = c.incoming().first->data().iCharge();
  if ( c.incoming().second ) ci += c.incoming().second->data().iCharge();

  const ParticleSet & fs = c.finalStep()->particles();
  LorentzMomentum pf;
  int cf = 0;
  for ( ParticleSet::const_iterator it = fs.begin(); it != fs.end(); ++it ) {
    pf += (**it).momentum();
    cf += (**it).data().iCharge();
  }

  if ( cf != ci ) Throw<ConsistencyException>()
    << "Event handler '" << name() << "' found charge non-conservation by "
    << cf - ci << "/3 units after generating step number " << c.steps().size()
    << ". Dump of event follows: \n\n" <<  *currentEvent() << "\n"
    << Exception::warning;

  Energy eps = consistencyEpsilon()*pi.m()*sqrt(double(fs.size()));
  pf -= pi;
  if ( abs(pf.x()) > eps || abs(pf.y()) > eps || abs(pf.z()) > eps ||
       abs(pf.e()) > eps )
    Throw<ConsistencyException>()
      << "Event handler '" << name() << "' found energy-momentum non-"
      << "conservation by (" << pf.x()/GeV << "," << pf.y()/GeV << ","
      << pf.z()/GeV << ";" << pf.e()/GeV
      << ") GeV after generating step number " << c.steps().size()
      << ". Dump of event follows: \n\n" <<  *currentEvent() << "\n"
      << Exception::warning;
}


void EventHandler::rebind(const TranslationMap & trans) {
  theIncoming.first = trans.translate(theIncoming.first);
  theIncoming.second = trans.translate(theIncoming.second);
  HandlerBase::rebind(trans);
}

IVector EventHandler::getReferences() {
  IVector ret = HandlerBase::getReferences();
  ret.push_back(const_ptr_cast<PDPtr>(theIncoming.first));
  ret.push_back(const_ptr_cast<PDPtr>(theIncoming.second));
  return ret;
}
void EventHandler::persistentOutput(PersistentOStream & os) const {
  os << theLastXComb << theMaxLoop << theStatLevel << oenum(theConsistencyLevel)
     << theConsistencyEpsilon << theLumiFn << theCuts << thePartonExtractor
     << theSubprocessGroup << theCascadeGroup << theMultiGroup
     << theHadronizationGroup << theDecayGroup << theCurrentEvent
     << theCurrentCollision << theCurrentStep << theCurrentStepHandler
     << warnIncomplete << theIncoming;
}

void EventHandler::persistentInput(PersistentIStream & is, int) {
  is >> theLastXComb >> theMaxLoop >> theStatLevel >> ienum(theConsistencyLevel)
     >> theConsistencyEpsilon >> theLumiFn >> theCuts >> thePartonExtractor
     >> theSubprocessGroup >> theCascadeGroup >> theMultiGroup
     >> theHadronizationGroup >> theDecayGroup >> theCurrentEvent
     >> theCurrentCollision >> theCurrentStep >> theCurrentStepHandler
     >> warnIncomplete >> theIncoming;
}

ThePEG_IMPLEMENT_CLASS_DESCRIPTION(EventHandler);

void EventHandler::Init() {

  static ClassDocumentation<EventHandler> documentation
    ("This is the base class controlling the generation of the event after "
     "the hard sub-process generation. To its help it has a number of step "
     "handler objects, most noticeable the "
     "<interface>CascadeHandler</interface>, the "
     "<interface>HadronizationHandler</interface> and the "
     "<interface>DecayHandler</interface>. Derived classes may "
     "also make use of the <interface>LuminosityFunction</interface> "
     "to specify the incoming beams, the "
     "<interface>PartonExtractor</interface> to administer the "
     "extraction of partons from incoming hadrons, and the "
     "<interface>Cuts</interface> to specify cuts on the primary sub-process.");

  static Parameter<EventHandler,long> interfaceMaxLoop
    ("MaxLoop",
     "The maximum number of attempts per event when selecting a sub-process.",
     &EventHandler::theMaxLoop, 100000, 100, 100000000, true, false, true);

  static Switch<EventHandler,int> interfaceStatLevel
    ("StatLevel",
     "Controlls the amount of statistics written out after each run to "
     "the EventGenerators .out file.",
     &EventHandler::theStatLevel, 2, true, false);
  static SwitchOption interfaceStatLevelNone
    (interfaceStatLevel, "None", "No statistics is written out.", 0);
  static SwitchOption interfaceStatLevelTot
    (interfaceStatLevel, "TotalXSec",
     "Only the total cross-section is written out.", 1);
  static SwitchOption interfaceStatLevelME
    (interfaceStatLevel, "MatrixElements",
     "The cross-section for each subprocess group is written out.", 2);
  static SwitchOption interfaceStatLevelBins
    (interfaceStatLevel, "Bins",
     "The cross-sections for individual subprocesses are written out.", 3);
  static SwitchOption interfaceStatLevelFull
    (interfaceStatLevel, "Full",
     "A detailed account of the cross-sections of the generated sub-processes "
     "is written out.", 4);

  static Reference<EventHandler,LuminosityFunction> interfaceLumifn
    ("LuminosityFunction",
     "An object derived from the ThePEG::LuminosityFunction class, "
     "which describes the beams in the collider under study.",
     &EventHandler::theLumiFn, false, false, true, true,
     &EventHandler::lumiFn);

  static Reference<EventHandler,Cuts> interfaceCuts
    ("Cuts",
     "Kinematical cuts to be used by subclasses which do not provide "
     "their own. Note that this may be overridden by subclasses.",
     &EventHandler::theCuts, true, false, true, true, false);

  static Reference<EventHandler,PartonExtractor> interfacePartonExtractor
    ("PartonExtractor",
     "A PartonExtractor object to be used by subclasses which do not "
     "provide their own. Note that this may be overridden by subclasses.",
     &EventHandler::thePartonExtractor, true, false, true, true, false);

  ThePEG_DECLARE_PREPOST_OBJECTS(EventHandler, SubProcessHandler,
				  Post, after);
  ThePEG_DECLARE_GROUPINTERFACE_OBJECTS(EventHandler,
					 CascadeHandler);
  ThePEG_DECLARE_GROUPINTERFACE_OBJECTS(EventHandler,
					 MultipleInteractionHandler);
  ThePEG_DECLARE_GROUPINTERFACE_OBJECTS(EventHandler,
					 HadronizationHandler);
  ThePEG_DECLARE_GROUPINTERFACE_OBJECTS(EventHandler, DecayHandler);


  static Switch<EventHandler,bool> interfaceWarnIncomplete
    ("WarnIncomplete",
     "Emit a warning during the initialization that this EventHandler is "
     "incomplete. Should only be manipulated if the object is in fact an "
     "incomlete EventHandler and you do not want a warning.",
     &EventHandler::warnIncomplete, true, true, false);
  static SwitchOption interfaceWarnIncompleteYes
    (interfaceWarnIncomplete,
     "Yes",
     "This is an incomplete EventHandler and warning should be emitted.",
     true);
  static SwitchOption interfaceWarnIncompleteNo
    (interfaceWarnIncomplete,
     "No",
     "This EventHandler is either complete or no warning should be emitted "
     "in either case.",
     false);
  interfaceWarnIncomplete.setHasDefault(false);

  static Switch<EventHandler,ConsistencyLevel> interfaceConsistencyLevel
    ("ConsistencyLevel",
     "Determines how often the event handler should check for charge and "
     "energy-momentum conservation.",
     &EventHandler::theConsistencyLevel, clCollision, true, false);
  static SwitchOption interfaceConsistencyLevelNever
    (interfaceConsistencyLevel,
     "Never",
     "No consistency checks are made.",
     clNoCheck);
  static SwitchOption interfaceConsistencyLevelEveryCollision
    (interfaceConsistencyLevel,
     "EveryCollision",
     "Every collision is checked for consistency.",
     clCollision);
  static SwitchOption interfaceConsistencyLevelEveryStep
    (interfaceConsistencyLevel,
     "EveryStep",
     "Every step is checked for consistency.",
     clStep);
  static SwitchOption interfaceConsistencyLevelPrintEveryCollision
    (interfaceConsistencyLevel,
     "PrintEveryCollision",
     "Every collision is checked for consistency. If an inconsistency is "
     "found, the event is printed to the log file.",
     clPrintCollision);
  static SwitchOption interfaceConsistencyLevelPrintEveryStep
    (interfaceConsistencyLevel,
     "PrintEveryStep",
     "Every step is checked for consistency. If an inconsistency is "
     "found, the event is printed to the log file.",
     clPrintStep);
  interfaceConsistencyLevel.setHasDefault(false);

  static Parameter<EventHandler,double> interfaceConsistencyEpsilon
    ("ConsistencyEpsilon",
     "The maximum fraction of the total invariant mass of a collision that "
     "any of the components of the summed momentum is allowed to change during "
     "the generation. Used if <interface>ConsistencyLevel</interface> is "
     "switched on.",
     &EventHandler::theConsistencyEpsilon, sqrt(Constants::epsilon), 0.0, 1.0,
     true, false, Interface::limited);

  interfaceLumifn.rank(10);
  interfaceCascadeHandler.rank(9);
  interfaceHadronizationHandler.rank(8);
  interfaceDecayHandler.rank(7);
  interfaceCuts.rank(6);
  interfacePartonExtractor.rank(5);

}

ThePEG_IMPLEMENT_PREPOST_GROUP(EventHandler,SubProcessHandler,
				theSubprocessGroup,Post) \

ThePEG_IMPLEMENT_GROUPINTERFACE(EventHandler,CascadeHandler,
				 theCascadeGroup,CascHdlPtr) \

ThePEG_IMPLEMENT_GROUPINTERFACE(EventHandler,
				 MultipleInteractionHandler,
				 theMultiGroup,MIHdlPtr)  \

ThePEG_IMPLEMENT_GROUPINTERFACE(EventHandler,HadronizationHandler,
				 theHadronizationGroup,HadrHdlPtr) \

ThePEG_IMPLEMENT_GROUPINTERFACE(EventHandler,DecayHandler,
				 theDecayGroup,DecayHdlPtr) \


EventHandler::EventLoopException::
EventLoopException(const EventHandler & eh) {
  theMessage << "The maximum number of attempts per event (" << eh.maxLoop()
	     << ") in event handler '" << eh.name() << "' was exceeded.";
  severity(maybeabort);
}