Belle II Software  release-06-02-00
TOPPDFCheckerModule.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 <top/modules/TOPPDFChecker/TOPPDFCheckerModule.h>
11 #include <top/geometry/TOPGeometryPar.h>
12 #include <top/reconstruction_cpp/TOPTrack.h>
13 #include <top/reconstruction_cpp/PDFConstructor.h>
14 #include <top/reconstruction_cpp/TOPRecoManager.h>
15 
16 // DataStore classes
17 #include <tracking/dataobjects/ExtHit.h>
18 #include <top/dataobjects/TOPBarHit.h>
19 
20 // framework aux
21 #include <framework/gearbox/Unit.h>
22 #include <framework/gearbox/Const.h>
23 #include <framework/logging/Logger.h>
24 
25 // ROOT
26 #include <TRandom.h>
27 #include <iostream>
28 
29 
30 using namespace std;
31 
32 namespace Belle2 {
37  using namespace TOP;
38 
39  //-----------------------------------------------------------------
40  // Register module
41  //-----------------------------------------------------------------
42 
43  REG_MODULE(TOPPDFChecker)
44 
45  //-----------------------------------------------------------------
46  // Implementation
47  //-----------------------------------------------------------------
48 
50 
51  {
52  // set module description
53  setDescription("Module for checking analytic PDF used in likelihood calculation");
54  setPropertyFlags(c_ParallelProcessingCertified);
55 
56  // Add parameters
57  addParam("minTime", m_minTime,
58  "histogram lower bound in time [ns]", 0.0);
59  addParam("maxTime", m_maxTime,
60  "histogram upper bound in time [ns]", 50.0);
61  addParam("numBins", m_numBins,
62  "histogram number of bins in time", 1000);
63 
64  }
65 
66  void TOPPDFCheckerModule::defineHisto()
67  {
68 
69  // time vs. pixel ID
70  m_hits = new TH2F("hits", "photon hits", 512, 0.5, 512.5,
71  m_numBins, m_minTime, m_maxTime);
72  m_hits->SetXTitle("pixel ID");
73  m_hits->SetYTitle("time [ns]");
74 
75  m_pdf = new TH2F("pdf", "PDF", 512, 0.5, 512.5,
76  m_numBins, m_minTime, m_maxTime);
77  m_pdf->SetXTitle("pixel ID");
78  m_pdf->SetYTitle("time [ns]");
79 
80  // time vs pixel column
81  m_hitsCol = new TH2F("hitsCol", "photon hits", 64, 0.5, 64.5,
82  m_numBins, m_minTime, m_maxTime);
83  m_hitsCol->SetXTitle("pixel column");
84  m_hitsCol->SetYTitle("time [ns]");
85 
86  m_pdfCol = new TH2F("pdfCol", "PDF", 64, 0.5, 64.5,
87  m_numBins, m_minTime, m_maxTime);
88  m_pdfCol->SetXTitle("pixel column");
89  m_pdfCol->SetYTitle("time [ns]");
90 
91  }
92 
93  void TOPPDFCheckerModule::initialize()
94  {
95  // Register histograms (calls back defineHisto)
96  REG_HISTOGRAM;
97 
98  // input
99 
100  m_digits.isRequired();
101  m_tracks.isRequired();
102 
103  StoreArray<ExtHit> extHits;
104  extHits.isRequired();
105 
106  StoreArray<MCParticle> mcParticles;
107  mcParticles.isRequired();
108 
109  StoreArray<TOPBarHit> barHits;
110  barHits.isRequired();
111 
112  }
113 
114 
115  void TOPPDFCheckerModule::event()
116  {
117  TOPRecoManager::setTimeWindow(m_minTime, m_maxTime);
118  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
119 
120  // loop over reconstructed tracks, call reconstruction and fill histograms
121 
122  int numTrk = 0;
123  for (const auto& track : m_tracks) {
124  TOPTrack trk(track);
125  if (not trk.isValid()) continue;
126  if (not trk.getMCParticle()) continue;
127  if (not trk.getBarHit()) continue;
128 
129  auto chargedStable = Const::chargedStableSet.find(abs(trk.getPDGCode()));
130  if (chargedStable == Const::invalidParticle) continue;
131 
132  PDFConstructor pdfConstructor(trk, chargedStable, PDFConstructor::c_Fine);
133  if (not pdfConstructor.isValid()) continue;
134  numTrk++;
135 
136  // average values - to print in terminate()
137  const auto& module = geo->getModule(trk.getModuleID());
138  m_avrgMomentum += module.momentumToLocal(trk.getExtHit()->getMomentum());
139  m_avrgPosition += module.pointToLocal(trk.getExtHit()->getPosition());
140  m_numTracks++;
141  m_slotIDs.emplace(trk.getModuleID());
142  m_PDGCodes.emplace(trk.getPDGCode());
143 
144  // histogram photons in a slot crossed by the track
145  for (const auto& digit : m_digits) {
146  if (digit.getModuleID() == trk.getModuleID() and digit.getTime() < m_maxTime) {
147  if (not isFromThisParticle(digit, trk.getMCParticle())) continue;
148  m_hits->Fill(digit.getPixelID(), digit.getTime());
149  m_hitsCol->Fill(digit.getPixelCol(), digit.getTime());
150  }
151  }
152 
153  // histogram PDF using MC approach
154  for (const auto& signalPDF : pdfConstructor.getSignalPDF()) {
155  int pixelID = signalPDF.getPixelID();
156  for (const auto& peak : signalPDF.getPDFPeaks()) {
157  double numPhot = pdfConstructor.getExpectedSignalPhotons() * peak.nph;
158  double sigma = sqrt(peak.wid);
159  for (int i = 0; i < gRandom->Poisson(numPhot); i++) {
160  double time = gRandom->Gaus(peak.t0, sigma);
161  m_pdf->Fill(pixelID, time);
162  int pixelCol = (pixelID - 1) % 64 + 1;
163  m_pdfCol->Fill(pixelCol, time);
164  }
165  }
166  }
167 
168  }
169 
170  if (numTrk == 0) {
171  B2WARNING("No track hitting the bars");
172  } else if (numTrk > 1) {
173  B2WARNING("More than one track hits the bars");
174  }
175 
176  }
177 
178 
179  void TOPPDFCheckerModule::terminate()
180  {
181  m_avrgPosition *= 1.0 / m_numTracks;
182  m_avrgMomentum *= 1.0 / m_numTracks;
183 
184  cout << "Average particle parameters at entrance to bar (in local frame):" << endl;
185  cout << " slot ID: ";
186  for (auto slot : m_slotIDs) cout << slot << " ";
187  cout << endl;
188  cout << " PDG code: ";
189  for (auto pdg : m_PDGCodes) cout << pdg << " ";
190  cout << endl;
191  cout << " position: x = " << m_avrgPosition.X()
192  << ", y = " << m_avrgPosition.Y()
193  << ", z = " << m_avrgPosition.Z() << endl;
194  cout << " momentum: p = " << m_avrgMomentum.Mag()
195  << ", theta = " << m_avrgMomentum.Theta() / Unit::deg
196  << ", phi = " << m_avrgMomentum.Phi() / Unit::deg << endl;
197  cout << "Number of particles: " << m_numTracks << endl;
198  cout << "Photons per particle: simulation = " << m_hits->GetSum() / m_numTracks
199  << ", PDF = " << m_pdf->GetSum() / m_numTracks << endl;
200 
201  }
202 
203 
204  bool TOPPDFCheckerModule::isFromThisParticle(const TOPDigit& digit,
205  const MCParticle* particle)
206  {
207  const auto particles = digit.getRelationsWith<MCParticle>();
208  for (unsigned i = 0; i < particles.size(); ++i) {
209  if (particles[i] == particle and particles.weight(i) > 0) return true;
210  }
211  return false;
212  }
213 
214 
216 } // end Belle2 namespace
217 
TVector3 getMomentum() const
Get momentum at this extrapolation hit.
Definition: ExtHit.h:147
TVector3 getPosition() const
Get position of this extrapolation hit.
Definition: ExtHit.h:143
HistoModule.h is supposed to be used instead of Module.h for the modules with histogram definitions t...
Definition: HistoModule.h:29
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
RelationVector< T > getRelationsWith(const std::string &name="", const std::string &namedRelation="") const
Get the relations between this object and another store array.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Class to store TOP digitized hits (output of TOPDigitizer or raw data unpacker) relations to TOPSimHi...
Definition: TOPDigit.h:24
Module for checking analytic PDF used in likelihood calculation.
PDF construction and log likelihood determination for a given track and particle hypothesis.
bool isValid() const
Checks the object status.
double getExpectedSignalPhotons() const
Returns the expected number of signal photons within the default time window.
const std::vector< SignalPDF > & getSignalPDF() const
Returns signal PDF.
Reconstructed track at TOP.
Definition: TOPTrack.h:40
int getPDGCode() const
Returns PDG code of associated MCParticle (returns 0 if none)
Definition: TOPTrack.h:230
bool isValid() const
Checks if track is successfully constructed.
Definition: TOPTrack.h:139
const ExtHit * getExtHit() const
Returns extrapolated hit (track entrance to the bar)
Definition: TOPTrack.h:218
const TOPBarHit * getBarHit() const
Returns bar hit of MC particle assigned to this track (if any)
Definition: TOPTrack.h:240
int getModuleID() const
Returns slot ID.
Definition: TOPTrack.h:145
const MCParticle * getMCParticle() const
Returns MC particle assigned to this track (if any)
Definition: TOPTrack.h:224
#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.