Belle II Software  release-06-02-00
TOPPDFDebuggerModule.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/TOPPDFDebugger/TOPPDFDebuggerModule.h>
11 #include <top/geometry/TOPGeometryPar.h>
12 #include <top/reconstruction_cpp/TOPRecoManager.h>
13 #include <top/reconstruction_cpp/TOPTrack.h>
14 #include <top/reconstruction_cpp/PDFConstructor.h>
15 
16 // Hit classes
17 #include <tracking/dataobjects/ExtHit.h>
18 #include <mdst/dataobjects/MCParticle.h>
19 #include <top/dataobjects/TOPBarHit.h>
20 
21 // framework aux
22 #include <framework/logging/Logger.h>
23 
24 #include <cmath>
25 #include <algorithm>
26 
27 using namespace std;
28 
29 namespace Belle2 {
34  using namespace TOP;
35  //-----------------------------------------------------------------
36  // Register the Module
37  //-----------------------------------------------------------------
38 
39  REG_MODULE(TOPPDFDebugger)
40 
41 
42  //-----------------------------------------------------------------
43  // Implementation
44  //-----------------------------------------------------------------
45 
47  {
48  // Set description
49  setDescription("This module makes an analytic PDF available in "
50  "a store array TOPPDFCollections, related from Tracks");
51 
52  // Set property flags
53  setPropertyFlags(c_ParallelProcessingCertified);
54 
55  // Add parameters
56  addParam("minTime", m_minTime,
57  "lower limit for photon time [ns] (default from DB used, if minTime >= maxTime)", 0.0);
58  addParam("maxTime", m_maxTime,
59  "upper limit for photon time [ns] (default from DB used, if minTime >= maxTime)", 0.0);
60  addParam("pdfOption", m_pdfOption,
61  "PDF option, one of 'rough', 'fine', 'optimal'", std::string("fine"));
62  addParam("pdgCodes", m_pdgCodes,
63  "PDG codes of charged stable particles for which to construct PDF. "
64  "Empty list means all charged stable particles.",
65  m_pdgCodes);
66  }
67 
68 
69  void TOPPDFDebuggerModule::initialize()
70  {
71  // input
72  m_digits.isRequired();
73  m_tracks.isRequired();
74 
75  StoreArray<ExtHit> extHits;
76  extHits.isRequired();
77 
78  StoreArray<MCParticle> mcParticles;
79  mcParticles.isOptional();
80 
81  StoreArray<TOPBarHit> barHits;
82  barHits.isOptional();
83 
84  // output
85  m_pdfCollection.registerInDataStore();
86  m_tracks.registerRelationTo(m_pdfCollection);
87  m_associatedPDFs.registerInDataStore();
88  m_digits.registerRelationTo(m_associatedPDFs);
89  m_pixelData.registerInDataStore();
90  m_tracks.registerRelationTo(m_pixelData);
91 
92  // particle hypotheses
93  if (m_pdgCodes.empty()) {
94  for (const auto& part : Const::chargedStableSet) {
95  m_chargedStables.push_back(part);
96  }
97  } else {
98  for (auto pdg : m_pdgCodes) {
99  auto part = Const::ChargedStable(abs(pdg)); //throws runtime error for invalid pdg
100  m_chargedStables.push_back(part);
101  }
102  }
103 
104  // Parse PDF option
105  if (m_pdfOption == "rough") {
106  m_PDFOption = PDFConstructor::c_Rough;
107  } else if (m_pdfOption == "fine") {
108  m_PDFOption = PDFConstructor::c_Fine;
109  } else if (m_pdfOption == "optimal") {
110  m_PDFOption = PDFConstructor::c_Optimal;
111  } else {
112  B2ERROR("TOPPDFDebuggerModule: unknown PDF option '" << m_pdfOption << "'");
113  }
114 
115  }
116 
117 
118  void TOPPDFDebuggerModule::event()
119  {
120  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
121  TOPRecoManager::setTimeWindow(m_minTime, m_maxTime);
122 
123  // reconstruct track-by-track and store the results
124 
125  for (const auto& track : m_tracks) {
126  TOPTrack trk(track);
127  if (not trk.isValid()) continue;
128 
129  // add this vector of vector of triplets to the TOPPDFCollection
130  TOPPDFCollection* topPDFColl = m_pdfCollection.appendNew();
131  const auto& module = geo->getModule(trk.getModuleID());
132  topPDFColl->setLocalPositionMomentum(module.pointToLocal(trk.getExtHit()->getPosition()),
133  module.momentumToLocal(trk.getExtHit()->getMomentum()),
134  trk.getModuleID());
135 
136  TOPPixelLikelihood* topPLkhs = m_pixelData.appendNew();
137  topPLkhs->setModuleID(trk.getModuleID());
138 
139  for (const auto& chargedStable : m_chargedStables) {
140  const PDFConstructor pdfConstructor(trk, chargedStable, m_PDFOption, PDFConstructor::c_Full);
141  if (not pdfConstructor.isValid()) {
142  B2ERROR("PDFConstructor found not valid - bug in reconstruction code?"
143  << LogVar("PDG", chargedStable.getPDGCode()));
144  continue;
145  }
146 
147  // associate PDF peaks with photons using S-plot technique
148  associatePDFPeaks(pdfConstructor);
149 
150  // collection of gaussian_t's for each pixel
151  TOPPDFCollection::modulePDF_t channelPDFCollection;
152 
153  // store PDF peaks in the collection
154  for (const auto& signalPDF : pdfConstructor.getSignalPDF()) {
155  int pixelID = signalPDF.getPixelID();
156  for (const auto& peak : signalPDF.getPDFPeaks()) {
157  float position = peak.t0;
158  float width = sqrt(peak.wid);
159  float numPhotons = pdfConstructor.getExpectedSignalPhotons() * peak.nph;
160  auto tp = TOPPDFCollection::Gaussian(position, width, numPhotons);
161  channelPDFCollection.at(pixelID - 1).push_back(tp);
162  } // end loop over peaks in the pdf for this pixel
163  } // end loop over pixels
164 
165  topPDFColl->addHypothesisPDF(channelPDFCollection, chargedStable.getPDGCode());
166 
167  // Initialize logL and sfot pixel arrays
168  TOPPixelLikelihood::PixelArray_t pixLogLs, pixSigPhots;
169  pixLogLs.fill(0);
170  pixSigPhots.fill(0);
171 
172  const auto& pixelLogLs = pdfConstructor.getPixelLogLs(0);
173  for (size_t i = 0; i < std::min(pixelLogLs.size(), pixLogLs.size()); i++) {
174  pixLogLs[i] = pixelLogLs[i].logL;
175  pixSigPhots[i] = pixelLogLs[i].expPhotons;
176  }
177 
178  topPLkhs->addHypothesisLikelihoods(pixLogLs, chargedStable.getPDGCode());
179  topPLkhs->addHypothesisSignalPhotons(pixSigPhots, chargedStable.getPDGCode());
180  }
181  track.addRelationTo(topPDFColl);
182  track.addRelationTo(topPLkhs);
183 
184  } // end loop over tracks
185 
186  }
187 
188 
189  void TOPPDFDebuggerModule::associatePDFPeaks(const PDFConstructor& pdfConstructor)
190  {
191 
192  const auto& signalPDFs = pdfConstructor.getSignalPDF();
193  auto signalPhotons = pdfConstructor.getExpectedSignalPhotons();
194  auto bkgPhotons = pdfConstructor.getExpectedBkgPhotons();
195  auto deltaPhotons = pdfConstructor.getExpectedDeltaPhotons();
196  int PDGCode = pdfConstructor.getHypothesis().getPDGCode();
197 
198  for (const auto& digit : m_digits) {
199  if (digit.getModuleID() != pdfConstructor.getModuleID()) continue;
200  if (digit.getHitQuality() != TOPDigit::c_Good) continue;
201  int pixelID = digit.getPixelID();
202  unsigned index = pixelID - 1;
203  if (index >= signalPDFs.size()) continue;
204  const auto& signalPDF = signalPDFs[index];
205  const auto* tts = signalPDF.getTTS();
206  const auto& pdfPeaks = signalPDF.getPDFPeaks();
207  const auto& extraInfos = signalPDF.getPDFExtraInfo();
208  if (extraInfos.size() != pdfPeaks.size()) {
209  B2ERROR("associatePDFPeaks: sizes of PDFPeaks and ExtraInfo's differ");
210  continue;
211  }
212 
213  auto* associatedPDF = m_associatedPDFs.appendNew(PDGCode);
214  digit.addRelationTo(associatedPDF);
215 
216  double bkgLevel = pdfConstructor.getBackgroundPDF()->getPDFValue(pixelID) * bkgPhotons;
217  associatedPDF->setBackgroundWeight(bkgLevel);
218  double deltaLevel = pdfConstructor.getDeltaRayPDF().getPDFValue(pixelID, digit.getTime()) * deltaPhotons;
219  associatedPDF->setDeltaRayWeight(deltaLevel);
220 
221  float time = digit.getTime();
222  float timeErr = digit.getTimeError();
223 
224  for (size_t k = 0; k < pdfPeaks.size(); k++) {
225  const auto& pdfPeak = pdfPeaks[k];
226  const auto& pdfExtra = extraInfos[k];
228  peak.position = pdfPeak.t0;
229  peak.width = sqrt(pdfPeak.wid);
230  peak.numPhotons = pdfPeak.nph * signalPhotons;
231  float wt = 0;
232  for (const auto& gaus : tts->getTTS()) {
233  float sig2 = peak.width * peak.width + gaus.sigma * gaus.sigma + timeErr * timeErr;
234  float x = pow(time - peak.position - gaus.position, 2) / sig2;
235  if (x > 20) continue;
236  wt += peak.numPhotons * gaus.fraction / sqrt(2 * M_PI * sig2) * exp(-x / 2);
237  }
238  if (wt > 0) {
239  peak.fic = pdfPeak.fic;
240  peak.e = pdfExtra.e;
241  peak.sige = pdfExtra.sige;
242  peak.nx = abs(pdfExtra.Nxm) + abs(pdfExtra.Nxb) + abs(pdfExtra.Nxe);
243  peak.ny = abs(pdfExtra.Nym) + abs(pdfExtra.Nyb) + abs(pdfExtra.Nye);
244  peak.nxm = abs(pdfExtra.Nxm);
245  peak.nym = abs(pdfExtra.Nym);
246  peak.nxe = abs(pdfExtra.Nxe);
247  peak.nye = abs(pdfExtra.Nye);
248  peak.xd = pdfExtra.xD;
249  peak.yd = pdfExtra.yD;
250  peak.type = pdfExtra.type;
251  peak.kxe = pdfExtra.kxE;
252  peak.kye = pdfExtra.kyE;
253  peak.kze = pdfExtra.kzE;
254  peak.kxd = pdfExtra.kxD;
255  peak.kyd = pdfExtra.kyD;
256  peak.kzd = pdfExtra.kzD;
257  associatedPDF->appendPeak(peak, wt);
258  }
259  }
260  }
261 
262  }
263 
265 } // end Belle2 namespace
266 
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:470
int getPDGCode() const
PDG code.
Definition: Const.h:354
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
Base class for Modules.
Definition: Module.h:72
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Class to store analytical PDF relation from Tracks filled top/modules/TOPPDFDebugger/src/TOPPDFDebugg...
std::array< channelPDF_t, 512 > modulePDF_t
the PDF of the module is a list of 512 channel PDFs
void setLocalPositionMomentum(const TVector3 &pos, const TVector3 &mom, int moduleID)
sets the position and momentum of the exthit in local coordinates
bool addHypothesisPDF(const modulePDF_t &pdf, const int hypothesis)
adds the pdf for the given hypothesis (PDG code)
TOP reconstruction module.
Class to store pixel-by-pixel likelihoods for a track relation from Tracks filled in top/modules/TOPP...
std::array< float, 512 > PixelArray_t
Array of length 512 to hold per-pixel information.
void setModuleID(int moduleID)
Sets module ID of the associated exthit.
bool addHypothesisSignalPhotons(const PixelArray_t &sfots, const int hypothesis)
Adds the signal photon array for the given hypothesis (PDG code)
bool addHypothesisLikelihoods(const PixelArray_t &plkhs, const int hypothesis)
Adds the likelihood array for the given hypothesis (PDG code)
double getPDFValue(int pixelID) const
Returns PDF value for given pixel.
double getPDFValue(int pixelID, double time) const
Returns PDF value at given time and pixel.
Definition: DeltaRayPDF.cc:112
PDF construction and log likelihood determination for a given track and particle hypothesis.
const Const::ChargedStable & getHypothesis() const
Returns particle hypothesis.
double getExpectedDeltaPhotons() const
Returns the expected number of delta-ray photons within the default time window.
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< LogL > & getPixelLogLs(double t0, double sigt=0) const
Returns extended log likelihoods in pixels for PDF shifted in time.
int getModuleID() const
Returns slot ID.
const std::vector< SignalPDF > & getSignalPDF() const
Returns signal PDF.
const BackgroundPDF * getBackgroundPDF() const
Returns background PDF.
double getExpectedBkgPhotons() const
Returns the expected number of background photons within the default time window.
const DeltaRayPDF & getDeltaRayPDF() const
Returns delta-ray PDF.
Reconstructed track at TOP.
Definition: TOPTrack.h:40
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
int getModuleID() const
Returns slot ID.
Definition: TOPTrack.h:145
Class to store variables with their name which were sent to the logging service.
#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.
parameters of a PDF peak
int nx
total number of reflections in x
float kxd
reconstructed photon direction in x at detection
float fic
Cerenkov azimuthal angle phi.
float kze
reconstructed photon direction in z at emission
float e
mean photon energy [eV]
float xd
unfolded x coordinate of a pixel
int nym
number of reflections in y before mirror
int nxe
number of reflections in x in prism
int nye
number of reflections in y in prism
int nxm
number of reflections in x before mirror
float kzd
reconstructed photon direction in z at detection
float sige
photon energy sigma squared [eV^2]
int ny
total number of reflections in y
float numPhotons
number of photons
float kyd
reconstructed photon direction in y at detection
int type
0 unknown, 1 direct photon, 2 reflected photon
float yd
unfolded y coordinate of a pixel
float kxe
reconstructed photon direction in x at emission
float kye
reconstructed photon direction in y at emission
parameters to describe a Gaussian