Belle II Software  release-05-02-19
EventT0GeneratorModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <generators/modules/EventT0GeneratorModule.h>
13 
14 // framework - DataStore
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 
18 // framework aux
19 #include <framework/gearbox/Unit.h>
20 #include <framework/logging/Logger.h>
21 
22 // root
23 #include <TRandom.h>
24 
25 using namespace std;
26 
27 namespace Belle2 {
33  //-----------------------------------------------------------------
34  // Register module
35  //-----------------------------------------------------------------
36 
37  REG_MODULE(EventT0Generator)
38 
39  //-----------------------------------------------------------------
40  // Implementation
41  //-----------------------------------------------------------------
42 
44 
45  {
46  // set module description
47  setDescription("Module generates discrete event t0 in ~4ns steps (bunch spacing) "
48  "according to double gaussian distribution and adds it to the "
49  "production and decay times of MCParticles. This means that after "
50  "this module the time origin (t = 0) is set to what L1 trigger "
51  "would give as the collision time.");
52 
53  setPropertyFlags(c_ParallelProcessingCertified);
54 
55  // Add parameters
56  addParam("coreGaussWidth", m_coreGaussWidth, "sigma of core gaussian [ns]", 10.0);
57  addParam("tailGaussWidth", m_tailGaussWidth, "sigma of tail gaussian [ns]", 20.0);
58  addParam("tailGaussFraction", m_tailGaussFraction,
59  "fraction (by area) of tail gaussian", 0.0);
60  addParam("fixedT0", m_fixedT0,
61  "If set, a fixed event t0 is used instead of simulating the bunch timing.", m_fixedT0);
62  addParam("maximumT0", m_maximumT0,
63  "If set, randomize between -maximum and maximum.",
64  m_maximumT0);
65 
66  }
67 
68 
69  void EventT0GeneratorModule::initialize()
70  {
71  m_mcParticles.isRequired();
72  m_initialParticles.registerInDataStore();
73 
74  // bunch time separation: every second bunch is filled
75  m_bunchTimeSep = 2 * 1.96516 * Unit::ns; //TODO: get it from DB (which object?)
76 
77  if (not std::isnan(m_maximumT0) and not std::isnan(m_fixedT0)) {
78  B2ERROR("You can not set both the maximum T0 and the fixed T0 option.");
79  }
80  }
81 
82 
83  void EventT0GeneratorModule::event()
84  {
85  double collisionTime = 0.0f;
86 
87  if (not std::isnan(m_maximumT0)) {
88  collisionTime = -m_maximumT0 + (2 * m_maximumT0) * gRandom->Rndm();
89  } else if (not std::isnan(m_fixedT0)) {
90  collisionTime = m_fixedT0;
91  } else {
92  // generate collision time
93  double sigma = m_coreGaussWidth;
94  if (gRandom->Rndm() < m_tailGaussFraction) sigma = m_tailGaussWidth;
95 
96  collisionTime = gRandom->Gaus(0., sigma);
97  }
98  const int relBunchNo = round(collisionTime / m_bunchTimeSep);
99  collisionTime = relBunchNo * m_bunchTimeSep;
100 
101  // correct MC particles times according to generated collision time
102  for (auto& particle : m_mcParticles) {
103  particle.setProductionTime(particle.getProductionTime() + collisionTime);
104  particle.setDecayTime(particle.getDecayTime() + collisionTime);
105  }
106 
107  // store collision time to MC initial particles (e.g. beam particles)
108  if (!m_initialParticles.isValid()) m_initialParticles.create();
109  m_initialParticles->setTime(collisionTime);
110 
111  // t = 0 is from now on the time L1 thinks the collision happened,
112  // but in fact it happened at t = collisionTime
113 
114  }
115 
116 
118 } // end Belle2 namespace
119 
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::EventT0GeneratorModule
Module generates discrete event t0 in ~4ns steps (bunch spacing) according to (double) gaussian distr...
Definition: EventT0GeneratorModule.h:45