Belle II Software  release-06-00-14
EventT0GeneratorModule.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 // Own include
10 #include <generators/modules/EventT0GeneratorModule.h>
11 
12 // framework aux
13 #include <framework/gearbox/Unit.h>
14 #include <framework/logging/Logger.h>
15 
16 // root
17 #include <TRandom.h>
18 
19 using namespace std;
20 
21 namespace Belle2 {
27  //-----------------------------------------------------------------
28  // Register module
29  //-----------------------------------------------------------------
30 
31  REG_MODULE(EventT0Generator)
32 
33  //-----------------------------------------------------------------
34  // Implementation
35  //-----------------------------------------------------------------
36 
38 
39  {
40  // set module description
41  setDescription("Module generates discrete event t0 in ~4ns steps (bunch spacing) "
42  "according to double gaussian distribution and adds it to the "
43  "production and decay times of MCParticles. This means that after "
44  "this module the time origin (t = 0) is set to what L1 trigger "
45  "would give as the collision time. In case of cosmics, the L1 trigger"
46  "jitter is generated according to a continuos double gaussian distribution");
47 
48  setPropertyFlags(c_ParallelProcessingCertified);
49 
50  // Add parameters
51  addParam("coreGaussWidth", m_coreGaussWidth, "sigma of core gaussian [ns]", double(5.6));
52  addParam("tailGaussWidth", m_tailGaussWidth, "sigma of tail gaussian [ns]", double(14.5));
53  addParam("tailGaussFraction", m_tailGaussFraction,
54  "fraction (by area) of tail gaussian", double(0.08));
55  addParam("fixedT0", m_fixedT0,
56  "If set, a fixed event t0 is used instead of simulating the bunch timing.", m_fixedT0);
57  addParam("maximumT0", m_maximumT0,
58  "If set, randomize between -maximum and maximum.",
59  m_maximumT0);
60  addParam("isCosmics", m_isCosmics,
61  "if True simulate L1 jitter for cosmics", bool(false));
62  addParam("coreGaussWidthCosmics", m_coreGaussWidthCosmics, "sigma of core gaussian for cosmics [ns]", double(13));
63  addParam("tailGaussMeanCosmics", m_tailGaussMeanCosmics, "mean of tail gaussian for cosmics [ns]", double(28));
64  addParam("tailGaussWidthCosmics", m_tailGaussWidthCosmics, "sigma of tail gaussian for cosmics [ns]", double(15));
65  addParam("tailGaussFractionCosmics", m_tailGaussFractionCosmics,
66  "fraction (by area) of tail gaussian for cosmics", double(0.09));
67 
68  }
69 
70 
71  void EventT0GeneratorModule::initialize()
72  {
73  m_mcParticles.isRequired();
74  m_initialParticles.registerInDataStore();
75  m_simClockState.registerInDataStore();
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 
86 
87  double eventTime = 0;
88  int bucket = 0;
89  int beamRevo9Cycle = 0;
90  int relBucketNo = 0;
91  //if m_isCosmics we need to randomize revo9count,
92  //at least for the SVD trigger bin
93  //if !m_isCosmics the revo9Count value is overwritten
94  unsigned revo9range = (m_bunchStructure->getRFBucketsPerRevolution() / 4) * 9;
95  int revo9count = gRandom->Integer(revo9range);
96 
97  if (!m_isCosmics) {
98  // generate bucket number w.r.t revo9 marker
99 
100  bucket = m_bunchStructure->generateBucketNumber(); // [RF clock]
101  beamRevo9Cycle = gRandom->Integer(9); // number of beam revolutions w.r.t revo9 marker
102  int bucketRevo9 = bucket + beamRevo9Cycle * m_bunchStructure->getRFBucketsPerRevolution(); // [RF clock]
103 
104  // generate L1 time jitter
105 
106  double timeJitter = 0;
107  if (not std::isnan(m_maximumT0)) {
108  timeJitter = -m_maximumT0 + (2 * m_maximumT0) * gRandom->Rndm();
109  } else if (not std::isnan(m_fixedT0)) {
110  timeJitter = m_fixedT0;
111  } else {
112  double sigma = m_coreGaussWidth;
113  if (gRandom->Rndm() < m_tailGaussFraction) sigma = m_tailGaussWidth;
114  timeJitter = gRandom->Gaus(0., sigma);
115  }
116 
117  // calculate revo9count (system clock ticks since revo9 marker as determined by L1 trigger)
118 
119  double bucketTimeSep = 1 / m_clockSettings->getAcceleratorRF();
120  relBucketNo = round(timeJitter / bucketTimeSep); // RF clock
121  revo9count = (bucketRevo9 + relBucketNo) / 4; // system clock
122 
123  // calculate collision time w.r.t L1 trigger
124 
125  eventTime = (bucketRevo9 - revo9count * 4) * bucketTimeSep;
126 
127  } else {
128 
129  //compute jitter for cosmics, .i.e. no filling pattern
130  double sigma = m_coreGaussWidthCosmics;
131  if (gRandom->Rndm() < m_tailGaussFractionCosmics) sigma = m_tailGaussWidthCosmics;
132  eventTime = gRandom->Gaus(0., sigma);
133  }
134 
135  // correct MC particle times according to generated collision time
136 
137  for (auto& particle : m_mcParticles) {
138  particle.setProductionTime(particle.getProductionTime() + eventTime);
139  particle.setDecayTime(particle.getDecayTime() + eventTime);
140  }
141 
142  // store collision time to MC initial particles (e.g. beam particles)
143 
144  if (not m_initialParticles.isValid()) m_initialParticles.create();
145  m_initialParticles->setTime(eventTime);
146 
147  // store revo9count (modulo range) in order to be distibuted to sub-detectors
148  revo9count %= revo9range;
149  if (revo9count < 0) revo9count += revo9range;
150 
151  m_simClockState.create();
152  m_simClockState->setRevo9Count(revo9count);
153  m_simClockState->setBucketNumber(bucket);
154  m_simClockState->setBeamCycleNumber(beamRevo9Cycle);
155  m_simClockState->setRelativeBucketNo(relBucketNo);
156  }
157 
158 
160 } // end Belle2 namespace
161 
Module generates discrete event t0 in ~4ns steps (bunch spacing) according to (double) gaussian distr...
Base class for Modules.
Definition: Module.h:72
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.