Belle II Software  release-05-02-19
TOPPDFCheckerModule.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 <top/modules/TOPPDFChecker/TOPPDFCheckerModule.h>
13 #include <top/geometry/TOPGeometryPar.h>
14 #include <top/reconstruction/TOPreco.h>
15 #include <top/reconstruction/TOPtrack.h>
16 #include <top/reconstruction/TOPconfigure.h>
17 
18 // framework - DataStore
19 #include <framework/datastore/StoreArray.h>
20 
21 // DataStore classes
22 #include <mdst/dataobjects/Track.h>
23 #include <tracking/dataobjects/ExtHit.h>
24 #include <top/dataobjects/TOPDigit.h>
25 #include <mdst/dataobjects/MCParticle.h>
26 #include <top/dataobjects/TOPBarHit.h>
27 
28 // framework aux
29 #include <framework/gearbox/Unit.h>
30 #include <framework/gearbox/Const.h>
31 #include <framework/logging/Logger.h>
32 
33 // ROOT
34 #include <TRandom.h>
35 #include <iostream>
36 
37 
38 using namespace std;
39 
40 namespace Belle2 {
45  using namespace TOP;
46 
47  //-----------------------------------------------------------------
48  // Register module
49  //-----------------------------------------------------------------
50 
51  REG_MODULE(TOPPDFChecker)
52 
53  //-----------------------------------------------------------------
54  // Implementation
55  //-----------------------------------------------------------------
56 
58 
59  {
60  // set module description (e.g. insert text)
61  setDescription("Module for checking analytic PDF used in likelihood calculation");
62  setPropertyFlags(c_ParallelProcessingCertified);
63 
64  // Add parameters
65  addParam("minTime", m_minTime,
66  "histogram lower bound in time [ns]", 0.0);
67  addParam("maxTime", m_maxTime,
68  "histogram upper bound in time [ns]", 50.0);
69  addParam("numBins", m_numBins,
70  "histogram number of bins in time", 1000);
71 
72  }
73 
74  TOPPDFCheckerModule::~TOPPDFCheckerModule()
75  {
76  }
77 
78  void TOPPDFCheckerModule::defineHisto()
79  {
80 
81  // time vs. pixel ID
82  m_hits = new TH2F("hits", "photon hits", 512, 0.5, 512.5,
83  m_numBins, m_minTime, m_maxTime);
84  m_hits->SetXTitle("pixel ID");
85  m_hits->SetYTitle("time [ns]");
86 
87  m_pdf = new TH2F("pdf", "PDF", 512, 0.5, 512.5,
88  m_numBins, m_minTime, m_maxTime);
89  m_pdf->SetXTitle("pixel ID");
90  m_pdf->SetYTitle("time [ns]");
91 
92  // time vs pixel column
93  m_hitsCol = new TH2F("hitsCol", "photon hits", 64, 0.5, 64.5,
94  m_numBins, m_minTime, m_maxTime);
95  m_hitsCol->SetXTitle("pixel column");
96  m_hitsCol->SetYTitle("time [ns]");
97 
98  m_pdfCol = new TH2F("pdfCol", "PDF", 64, 0.5, 64.5,
99  m_numBins, m_minTime, m_maxTime);
100  m_pdfCol->SetXTitle("pixel column");
101  m_pdfCol->SetYTitle("time [ns]");
102 
103  }
104 
105  void TOPPDFCheckerModule::initialize()
106  {
107  // Register histograms (calls back defineHisto)
108  REG_HISTOGRAM;
109 
110  // input
111 
112  StoreArray<TOPDigit> topDigits;
113  topDigits.isRequired();
114 
115  StoreArray<Track> tracks;
116  tracks.isRequired();
117 
118  StoreArray<ExtHit> extHits;
119  extHits.isRequired();
120 
121  StoreArray<MCParticle> mcParticles;
122  mcParticles.isRequired();
123 
124  StoreArray<TOPBarHit> barHits;
125  barHits.isRequired();
126 
127  // Configure TOP detector
128 
129  TOPconfigure config;
130 
131  }
132 
133  void TOPPDFCheckerModule::beginRun()
134  {
135  }
136 
137  void TOPPDFCheckerModule::event()
138  {
139 
140  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
141 
142  // create reconstruction object and set various options
143 
144  int Nhyp = 1;
145  double mass = Const::pion.getMass(); // pion used just to initialize
146  int pdg = Const::pion.getPDGCode();
147  TOPreco reco(Nhyp, &mass, &pdg);
148  reco.setPDFoption(TOPreco::c_Fine);
149  reco.setTimeWindow(m_minTime, m_maxTime);
150 
151  // loop over reconstructed tracks, call reconstruction and fill histograms
152 
153  StoreArray<Track> tracks;
154  int numTrk = 0;
155  for (const auto& track : tracks) {
156  TOPtrack trk(&track);
157  if (!trk.isValid()) continue;
158  if (!trk.getMCParticle()) continue;
159  if (!trk.getBarHit()) continue;
160  mass = trk.getMCParticle()->getMass();
161  pdg = trk.getMCParticle()->getPDG();
162  reco.setMass(mass, pdg);
163  reco.reconstruct(trk);
164  if (reco.getFlag() != 1) continue; // track is not in the acceptance of TOP
165  numTrk++;
166 
167  // average values - to print in terminate()
168  const auto& module = geo->getModule(trk.getModuleID());
169  m_avrgMomentum += module.momentumToLocal(trk.getMomentum());
170  m_avrgPosition += module.pointToLocal(trk.getPosition());
171  m_numTracks++;
172  m_slotIDs.emplace(trk.getModuleID());
173  m_PDGCodes.emplace(trk.getPDGcode());
174 
175  // histogram photons in a slot crossed by the track
176  StoreArray<TOPDigit> digits;
177  for (const auto& digit : digits) {
178  if (digit.getModuleID() == trk.getModuleID() and digit.getTime() < m_maxTime) {
179  if (!isFromThisParticle(digit, trk.getMCParticle())) continue;
180  m_hits->Fill(digit.getPixelID(), digit.getTime());
181  m_hitsCol->Fill(digit.getPixelCol(), digit.getTime());
182  }
183  }
184 
185  // histogram PDF using MC approach
186  for (int pixelID = 1; pixelID <= 512; pixelID++) {
187  for (int peak = 0; peak < reco.getNumofPDFPeaks(pixelID); peak++) {
188  float t0 = 0;
189  float sigma = 0;
190  float numPhot = 0;
191  reco.getPDFPeak(pixelID, peak, t0, sigma, numPhot);
192  for (int i = 0; i < gRandom->Poisson(numPhot); i++) {
193  double time = gRandom->Gaus(t0, sigma);
194  m_pdf->Fill(pixelID, time);
195  int pixelCol = (pixelID - 1) % 64 + 1;
196  m_pdfCol->Fill(pixelCol, time);
197  }
198  }
199  }
200 
201  }
202 
203  if (numTrk == 0) {
204  B2WARNING("No track hitting the bars");
205  } else if (numTrk > 1) {
206  B2WARNING("More than one track hits the bars");
207  }
208 
209  }
210 
211 
212  void TOPPDFCheckerModule::endRun()
213  {
214  }
215 
216  void TOPPDFCheckerModule::terminate()
217  {
218 
219  m_avrgPosition *= 1.0 / m_numTracks;
220  m_avrgMomentum *= 1.0 / m_numTracks;
221 
222  cout << "Average particle parameters at entrance to bar (in local frame):" << endl;
223  cout << " slot ID: ";
224  for (auto slot : m_slotIDs) cout << slot << " ";
225  cout << endl;
226  cout << " PDG code: ";
227  for (auto pdg : m_PDGCodes) cout << pdg << " ";
228  cout << endl;
229  cout << " position: x = " << m_avrgPosition.X()
230  << ", y = " << m_avrgPosition.Y()
231  << ", z = " << m_avrgPosition.Z() << endl;
232  cout << " momentum: p = " << m_avrgMomentum.Mag()
233  << ", theta = " << m_avrgMomentum.Theta() / Unit::deg
234  << ", phi = " << m_avrgMomentum.Phi() / Unit::deg << endl;
235  cout << "Number of particles: " << m_numTracks << endl;
236  cout << "Photons per particle: simulation = " << m_hits->GetSum() / m_numTracks
237  << ", PDF = " << m_pdf->GetSum() / m_numTracks << endl;
238 
239  }
240 
241 
242  bool TOPPDFCheckerModule::isFromThisParticle(const TOPDigit& digit,
243  const MCParticle* particle)
244  {
245  const auto particles = digit.getRelationsWith<MCParticle>();
246  for (unsigned i = 0; i < particles.size(); ++i) {
247  if (particles[i] == particle and particles.weight(i) > 0) return true;
248  }
249  return false;
250  }
251 
252 
254 } // end Belle2 namespace
255 
Belle2::TOP::TOPtrack::getPDGcode
int getPDGcode() const
Return PDG code.
Definition: TOPtrack.h:199
Belle2::TOP::TOPreco::getFlag
int getFlag()
Return status.
Definition: TOPreco.cc:278
Belle2::RelationsInterface::getRelationsWith
RelationVector< T > getRelationsWith(const std::string &name="", const std::string &namedRelation="") const
Get the relations between this object and another store array.
Definition: RelationsObject.h:232
Belle2::TOP::TOPreco::reconstruct
void reconstruct(const TOPtrack &trk, int pdg=0)
Run reconstruction for a given track.
Definition: TOPreco.cc:268
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::TOP::TOPtrack::isValid
bool isValid() const
Check if track is properly defined.
Definition: TOPtrack.h:87
Belle2::TOP::TOPreco::getNumofPDFPeaks
int getNumofPDFPeaks(int pixelID) const
Returns number of peaks for given pixel describing signal PDF.
Definition: TOPreco.cc:450
Belle2::TOP::TOPtrack::getModuleID
int getModuleID() const
Return module ID.
Definition: TOPtrack.h:217
Belle2::TOPPDFCheckerModule
Module for checking analytic PDF used in likelihood calculation.
Definition: TOPPDFCheckerModule.h:38
Belle2::TOPDigit
Class to store TOP digitized hits (output of TOPDigitizer or raw data unpacker) relations to TOPSimHi...
Definition: TOPDigit.h:34
Belle2::TOP::TOPtrack::getMCParticle
const MCParticle * getMCParticle() const
Return MC particle assigned to this track (if any)
Definition: TOPtrack.h:242
Belle2::TOP::TOPreco
TOP reconstruction: this class provides interface to fortran code.
Definition: TOPreco.h:36
Belle2::TOP::TOPreco::setMass
void setMass(double mass, int pdg)
Set mass of the particle hypothesis (overrides settings in the constructor)
Definition: TOPreco.cc:172
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOP::TOPtrack::getPosition
const TVector3 & getPosition() const
Return spatial position.
Definition: TOPtrack.h:93
Belle2::MCParticle::getPDG
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:123
Belle2::TOP::TOPconfigure
Configure TOP geometry for reconstruction: provides interface to fortran code.
Definition: TOPconfigure.h:36
Belle2::MCParticle::getMass
float getMass() const
Return the particle mass in GeV.
Definition: MCParticle.h:146
Belle2::TOP::TOPtrack::getMomentum
const TVector3 & getMomentum() const
Return momentum vector.
Definition: TOPtrack.h:99
Belle2::TOP::TOPreco::getPDFPeak
void getPDFPeak(int pixelID, int k, float &position, float &width, float &numPhotons) const
Returns k-th PDF peak for given pixel describing signal PDF.
Definition: TOPreco.cc:456
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::TOP::TOPreco::setPDFoption
void setPDFoption(PDFoption opt, int NP=0, int NC=0)
Sets PDF option.
Definition: TOPreco.cc:196
Belle2::TOP::TOPreco::setTimeWindow
void setTimeWindow(double Tmin, double Tmax)
Set time window for photons.
Definition: TOPreco.cc:180
Belle2::HistoModule
HistoModule.h is supposed to be used instead of Module.h for the modules with histogram definitions t...
Definition: HistoModule.h:29