Belle II Software  release-05-02-19
TOPReconstructorModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - 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 <top/modules/TOPReconstruction/TOPReconstructorModule.h>
13 #include <top/reconstruction/TOPreco.h>
14 #include <top/reconstruction/TOPtrack.h>
15 #include <top/reconstruction/TOPconfigure.h>
16 
17 // framework aux
18 #include <framework/gearbox/Const.h>
19 #include <framework/logging/Logger.h>
20 
21 using namespace std;
22 
23 namespace Belle2 {
28  using namespace TOP;
29  //-----------------------------------------------------------------
30  // Register the Module
31  //-----------------------------------------------------------------
32 
33  REG_MODULE(TOPReconstructor)
34 
35 
36  //-----------------------------------------------------------------
37  // Implementation
38  //-----------------------------------------------------------------
39 
41  {
42  // Set description
43  setDescription("Reconstruction for TOP counter. Uses reconstructed tracks "
44  "extrapolated to TOP and TOPDigits to calculate log likelihoods "
45  "for charged stable particles");
46 
47  // Set property flags
48  setPropertyFlags(c_ParallelProcessingCertified);
49 
50  // Add parameters
51  addParam("minBkgPerBar", m_minBkgPerBar,
52  "Minimal number of background photons per bar", 0.0);
53  addParam("scaleN0", m_scaleN0,
54  "Scale factor for N0", 1.0);
55  addParam("sigmaRphi", m_sigmaRphi,
56  "track smearing sigma in Rphi [cm]", 0.0);
57  addParam("sigmaZ", m_sigmaZ,
58  "track smearing sigma in Z [cm]", 0.0);
59  addParam("sigmaTheta", m_sigmaTheta,
60  "track smearing sigma in Theta [radians]", 0.0);
61  addParam("sigmaPhi", m_sigmaPhi,
62  "track smearing sigma in Phi [radians]", 0.0);
63  addParam("minTime", m_minTime,
64  "lower limit for photon time [ns] (default if minTime >= maxTime)", 0.0);
65  addParam("maxTime", m_maxTime,
66  "upper limit for photon time [ns] (default if minTime >= maxTime)", 0.0);
67  addParam("PDGCode", m_PDGCode,
68  "PDG code of hypothesis to construct pulls (0 means: use MC truth)",
69  211);
70  addParam("TOPDigitCollectionName", m_topDigitCollectionName,
71  "Name of the collection of TOPDigits", string(""));
72  addParam("TOPLikelihoodCollectionName", m_topLikelihoodCollectionName,
73  "Name of the produced collection of TOPLikelihoods", string(""));
74  addParam("TOPPullCollectionName", m_topPullCollectionName, "Name of the collection of produced TOPPulls", string(""));
75 
76  }
77 
78 
79  TOPReconstructorModule::~TOPReconstructorModule()
80  {
81  }
82 
83 
84  void TOPReconstructorModule::initialize()
85  {
86  // input
87 
88  m_digits.isRequired(m_topDigitCollectionName);
89  m_tracks.isRequired();
90  m_extHits.isRequired();
91  m_barHits.isOptional();
92  m_recBunch.isOptional();
93 
94  // output
95 
96  m_likelihoods.registerInDataStore(m_topLikelihoodCollectionName);
97  m_likelihoods.registerRelationTo(m_extHits);
98  m_likelihoods.registerRelationTo(m_barHits);
99  m_tracks.registerRelationTo(m_likelihoods);
100 
101  m_topPulls.registerInDataStore(m_topPullCollectionName, DataStore::c_DontWriteOut);
102  m_tracks.registerRelationTo(m_topPulls, DataStore::c_Event, DataStore::c_DontWriteOut);
103 
104  // check for module debug level
105 
106  if (getLogConfig().getLogLevel() == LogConfig::c_Debug) {
107  m_debugLevel = getLogConfig().getDebugLevel();
108  }
109 
110  // Initialize masses
111 
112  for (const auto& part : Const::chargedStableSet) {
113  m_masses[part.getIndex()] = part.getMass();
114  m_pdgCodes[part.getIndex()] = part.getPDGCode();
115  }
116 
117  // set track smearing flag
118 
119  m_smearTrack = m_sigmaRphi > 0 || m_sigmaZ > 0 || m_sigmaTheta > 0 ||
120  m_sigmaPhi > 0;
121 
122  // Configure TOP detector
123 
124  TOPconfigure config;
125  if (m_debugLevel > 0) config.print();
126  }
127 
128  void TOPReconstructorModule::beginRun()
129  {
130  }
131 
132  void TOPReconstructorModule::event()
133  {
134 
135  // clear output objects
136 
137  m_likelihoods.clear();
138  m_topPulls.clear();
139 
140  // check bunch reconstruction status and do the reconstruction:
141  // - if object exists and bunch is found (collision data w/ bunch finder in the path)
142  // - if object doesn't exist (cosmic data and other cases w/o bunch finder in the path)
143 
144  if (m_recBunch.isValid()) {
145  if (!m_recBunch->isReconstructed()) return;
146  }
147 
148  // create reconstruction object
149 
150  TOPreco reco(Const::ChargedStable::c_SetSize, m_masses, m_pdgCodes,
151  m_minBkgPerBar, m_scaleN0);
152 
153  // set time window if given, otherwise use the default one from TOPNominalTDC
154  if (m_maxTime > m_minTime) {
155  reco.setTimeWindow(m_minTime, m_maxTime);
156  }
157 
158  // add photons
159 
160  for (const auto& digit : m_digits) {
161  if (digit.getHitQuality() == TOPDigit::EHitQuality::c_Good) {
162  reco.addData(digit.getModuleID(), digit.getPixelID(), digit.getTime(),
163  digit.getTimeError());
164  }
165  }
166 
167  // reconstruct track-by-track and store the results
168 
169  for (const auto& track : m_tracks) {
170 
171  // construct TOPtrack from mdst track
172  TOPtrack trk(&track);
173  if (!trk.isValid()) continue;
174 
175  // optional track smearing (needed for some MC studies)
176  if (m_smearTrack) {
177  trk.smear(m_sigmaRphi, m_sigmaZ, m_sigmaTheta, m_sigmaPhi);
178  B2INFO("TOPReconstructor: additional smearing of track parameters done");
179  }
180 
181  // reconstruct
182  reco.reconstruct(trk, m_PDGCode);
183  if (m_debugLevel > 1) {
184  trk.dump();
185  reco.dumpTrackHit(c_Local);
186  reco.dumpLogL(Const::ChargedStable::c_SetSize);
187  cout << endl;
188  }
189 
190  // get results
191  double logl[Const::ChargedStable::c_SetSize];
192  double estPhot[Const::ChargedStable::c_SetSize];
193  int nphot = 0;
194  reco.getLogL(Const::ChargedStable::c_SetSize, logl, estPhot, nphot);
195  double estBkg = reco.getExpectedBG();
196 
197  // store results
198  TOPLikelihood* topL = m_likelihoods.appendNew(reco.getFlag(), nphot,
199  logl, estPhot, estBkg);
200  // make relations:
201  track.addRelationTo(topL);
202  topL->addRelationTo(trk.getExtHit());
203  topL->addRelationTo(trk.getBarHit());
204 
205  // store pulls
206  int pixelID; float t, t0, wid, fic, wt;
207  for (int k = 0; k < reco.getPullSize(); k++) {
208  reco.getPull(k, pixelID, t, t0, wid, fic, wt);
209  auto* pull = m_topPulls.appendNew(pixelID, t, t0, wid, fic, wt);
210  track.addRelationTo(pull);
211  }
212 
213  }
214 
215  }
216 
217 
218  void TOPReconstructorModule::endRun()
219  {
220  }
221 
222  void TOPReconstructorModule::terminate()
223  {
224  }
225 
226 
227 
229 } // end Belle2 namespace
230 
Belle2::TOP::TOPreco::getFlag
int getFlag()
Return status.
Definition: TOPreco.cc:278
Belle2::TOP::TOPreco::reconstruct
void reconstruct(const TOPtrack &trk, int pdg=0)
Run reconstruction for a given track.
Definition: TOPreco.cc:268
Belle2::TOP::TOPtrack::getExtHit
const ExtHit * getExtHit() const
Return extrapolated hit (track entrance to the bar) if this track is constructed from mdst track.
Definition: TOPtrack.h:236
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::TOP::TOPtrack
Class to hold reconstructed track, interface to fortran.
Definition: TOPtrack.h:42
Belle2::TOP::TOPtrack::getBarHit
const TOPBarHit * getBarHit() const
Return bar hit of MC particle assigned to this track (if any)
Definition: TOPtrack.h:248
Belle2::RelationsInterface::addRelationTo
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
Definition: RelationsObject.h:144
Belle2::TOP::TOPtrack::isValid
bool isValid() const
Check if track is properly defined.
Definition: TOPtrack.h:87
Belle2::TOP::TOPreco::addData
int addData(int moduleID, int pixelID, double time, double timeError)
Add data.
Definition: TOPreco.cc:214
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::TOP::TOPreco
TOP reconstruction: this class provides interface to fortran code.
Definition: TOPreco.h:36
Belle2::TOP::TOPreco::getLogL
double getLogL(int i=0)
Return log likelihood for i-th mass hypothesis.
Definition: TOPreco.cc:303
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOP::TOPtrack::dump
void dump() const
Print track parameters to std output.
Definition: TOPtrack.cc:229
Belle2::TOP::TOPconfigure
Configure TOP geometry for reconstruction: provides interface to fortran code.
Definition: TOPconfigure.h:36
Belle2::TOP::TOPreco::dumpTrackHit
void dumpTrackHit(int LocGlob)
Print track to std output.
Definition: TOPreco.cc:386
Belle2::TOP::TOPtrack::smear
void smear(double sig_x, double sig_z, double sig_theta, double sig_phi)
Smear track.
Definition: TOPtrack.cc:209
Belle2::TOP::TOPreco::getPull
void getPull(int K, int &pixelID, float &T, float &T0, float &Wid, float &PhiCer, float &Wt)
Get pulls: K-th pull.
Definition: TOPreco.cc:420
Belle2::TOP::TOPreco::getExpectedBG
double getExpectedBG()
Return expected number of background photons.
Definition: TOPreco.cc:291
Belle2::TOPLikelihood
Class to store TOP log likelihoods (output of TOPReconstructor).
Definition: TOPLikelihood.h:37
Belle2::TOP::TOPreco::dumpLogL
void dumpLogL(int NumHyp)
Print log likelihoods to std output.
Definition: TOPreco.cc:356
Belle2::TOP::TOPreco::getPullSize
int getPullSize()
Get pulls: size.
Definition: TOPreco.cc:413
Belle2::TOP::TOPreco::setTimeWindow
void setTimeWindow(double Tmin, double Tmax)
Set time window for photons.
Definition: TOPreco.cc:180
Belle2::TOPReconstructorModule
TOP reconstruction module.
Definition: TOPReconstructorModule.h:46