Belle II Software  release-08-01-10
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 header.
10 #include <top/modules/TOPPDFDebugger/TOPPDFDebuggerModule.h>
11 
12 // TOP headers.
13 #include <top/geometry/TOPGeometryPar.h>
14 #include <top/reconstruction_cpp/TOPRecoManager.h>
15 #include <top/reconstruction_cpp/TOPTrack.h>
16 #include <top/reconstruction_cpp/PDFConstructor.h>
17 
18 // Hit classes
19 #include <tracking/dataobjects/ExtHit.h>
20 #include <mdst/dataobjects/MCParticle.h>
21 #include <top/dataobjects/TOPBarHit.h>
22 
23 // framework aux
24 #include <framework/logging/Logger.h>
25 
26 #include <cmath>
27 #include <algorithm>
28 
29 using namespace std;
30 using namespace ROOT::Math;
31 
32 namespace Belle2 {
37  using namespace TOP;
38  //-----------------------------------------------------------------
40  //-----------------------------------------------------------------
41 
42  REG_MODULE(TOPPDFDebugger);
43 
44 
45  //-----------------------------------------------------------------
46  // Implementation
47  //-----------------------------------------------------------------
48 
49  TOPPDFDebuggerModule::TOPPDFDebuggerModule() : Module()
50  {
51  // Set description
52  setDescription("This module makes an analytic PDF available in "
53  "a store array TOPPDFCollections, related from Tracks");
54 
55  // Set property flags
57 
58  // Add parameters
59  addParam("minTime", m_minTime,
60  "lower limit for photon time [ns] (default from DB used, if minTime >= maxTime)", 0.0);
61  addParam("maxTime", m_maxTime,
62  "upper limit for photon time [ns] (default from DB used, if minTime >= maxTime)", 0.0);
63  addParam("pdfOption", m_pdfOption,
64  "PDF option, one of 'rough', 'fine', 'optimal'", std::string("fine"));
65  addParam("pdgCodes", m_pdgCodes,
66  "PDG codes of charged stable particles for which to construct PDF. "
67  "Empty list means all charged stable particles.",
68  m_pdgCodes);
69  }
70 
71 
73  {
74  // input
75  m_digits.isRequired();
76  m_tracks.isRequired();
77 
78  StoreArray<ExtHit> extHits;
79  extHits.isRequired();
80 
81  StoreArray<MCParticle> mcParticles;
82  mcParticles.isOptional();
83 
84  StoreArray<TOPBarHit> barHits;
85  barHits.isOptional();
86 
87  // output
88  m_pdfCollection.registerInDataStore();
89  m_tracks.registerRelationTo(m_pdfCollection);
90  m_associatedPDFs.registerInDataStore();
91  m_digits.registerRelationTo(m_associatedPDFs);
92  m_pixelData.registerInDataStore();
93  m_tracks.registerRelationTo(m_pixelData);
94 
95  // particle hypotheses
96  if (m_pdgCodes.empty()) {
97  for (const auto& part : Const::chargedStableSet) {
98  m_chargedStables.push_back(part);
99  }
100  } else {
101  for (auto pdg : m_pdgCodes) {
102  auto part = Const::ChargedStable(abs(pdg)); //throws runtime error for invalid pdg
103  m_chargedStables.push_back(part);
104  }
105  }
106 
107  // Parse PDF option
108  if (m_pdfOption == "rough") {
110  } else if (m_pdfOption == "fine") {
112  } else if (m_pdfOption == "optimal") {
114  } else {
115  B2ERROR("TOPPDFDebuggerModule: unknown PDF option '" << m_pdfOption << "'");
116  }
117 
118  }
119 
120 
122  {
123  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
125 
126  // reconstruct track-by-track and store the results
127 
128  for (const auto& track : m_tracks) {
129  TOPTrack trk(track);
130  if (not trk.isValid()) continue;
131 
132  // add this vector of vector of triplets to the TOPPDFCollection
133  TOPPDFCollection* topPDFColl = m_pdfCollection.appendNew();
134  const auto& module = geo->getModule(trk.getModuleID());
135  topPDFColl->setLocalPositionMomentum(module.pointToLocal(static_cast<XYZPoint>(trk.getExtHit()->getPosition())),
136  module.momentumToLocal(trk.getExtHit()->getMomentum()),
137  trk.getModuleID());
138 
139  TOPPixelLikelihood* topPLkhs = m_pixelData.appendNew();
140  topPLkhs->setModuleID(trk.getModuleID());
141 
142  for (const auto& chargedStable : m_chargedStables) {
143  const PDFConstructor pdfConstructor(trk, chargedStable, m_PDFOption, PDFConstructor::c_Full);
144  if (not pdfConstructor.isValid()) {
145  B2ERROR("PDFConstructor found not valid - bug in reconstruction code?"
146  << LogVar("PDG", chargedStable.getPDGCode()));
147  continue;
148  }
149 
150  // associate PDF peaks with photons using S-plot technique
151  associatePDFPeaks(pdfConstructor);
152 
153  // collection of gaussian_t's for each pixel
154  TOPPDFCollection::modulePDF_t channelPDFCollection;
155 
156  // store PDF peaks in the collection
157  for (const auto& signalPDF : pdfConstructor.getSignalPDF()) {
158  int pixelID = signalPDF.getPixelID();
159  for (const auto& peak : signalPDF.getPDFPeaks()) {
160  float position = peak.t0;
161  float width = sqrt(peak.wid);
162  float numPhotons = pdfConstructor.getExpectedSignalPhotons() * peak.nph;
163  auto tp = TOPPDFCollection::Gaussian(position, width, numPhotons);
164  channelPDFCollection.at(pixelID - 1).push_back(tp);
165  } // end loop over peaks in the pdf for this pixel
166  } // end loop over pixels
167 
168  topPDFColl->addHypothesisPDF(channelPDFCollection, chargedStable.getPDGCode());
169 
170  // Initialize logL and sfot pixel arrays
171  TOPPixelLikelihood::PixelArray_t pixLogLs, pixSigPhots;
172  pixLogLs.fill(0);
173  pixSigPhots.fill(0);
174 
175  const auto& pixelLogLs = pdfConstructor.getPixelLogLs(0);
176  for (size_t i = 0; i < std::min(pixelLogLs.size(), pixLogLs.size()); i++) {
177  pixLogLs[i] = pixelLogLs[i].logL;
178  pixSigPhots[i] = pixelLogLs[i].expPhotons;
179  }
180 
181  topPLkhs->addHypothesisLikelihoods(pixLogLs, chargedStable.getPDGCode());
182  topPLkhs->addHypothesisSignalPhotons(pixSigPhots, chargedStable.getPDGCode());
183  }
184  track.addRelationTo(topPDFColl);
185  track.addRelationTo(topPLkhs);
186 
187  } // end loop over tracks
188 
189  }
190 
191 
193  {
194 
195  const auto& signalPDFs = pdfConstructor.getSignalPDF();
196  auto signalPhotons = pdfConstructor.getExpectedSignalPhotons();
197  auto bkgPhotons = pdfConstructor.getExpectedBkgPhotons();
198  auto deltaPhotons = pdfConstructor.getExpectedDeltaPhotons();
199  int PDGCode = pdfConstructor.getHypothesis().getPDGCode();
200 
201  for (const auto& digit : m_digits) {
202  if (digit.getModuleID() != pdfConstructor.getModuleID()) continue;
203  if (digit.getHitQuality() != TOPDigit::c_Good) continue;
204  int pixelID = digit.getPixelID();
205  unsigned index = pixelID - 1;
206  if (index >= signalPDFs.size()) continue;
207  const auto& signalPDF = signalPDFs[index];
208  const auto* tts = signalPDF.getTTS();
209  const auto& pdfPeaks = signalPDF.getPDFPeaks();
210  const auto& extraInfos = signalPDF.getPDFExtraInfo();
211  if (extraInfos.size() != pdfPeaks.size()) {
212  B2ERROR("associatePDFPeaks: sizes of PDFPeaks and ExtraInfo's differ");
213  continue;
214  }
215 
216  auto* associatedPDF = m_associatedPDFs.appendNew(PDGCode);
217  digit.addRelationTo(associatedPDF);
218 
219  double bkgLevel = pdfConstructor.getBackgroundPDF()->getPDFValue(pixelID) * bkgPhotons;
220  associatedPDF->setBackgroundWeight(bkgLevel);
221  double deltaLevel = pdfConstructor.getDeltaRayPDF().getPDFValue(pixelID, digit.getTime()) * deltaPhotons;
222  associatedPDF->setDeltaRayWeight(deltaLevel);
223 
224  float time = digit.getTime();
225  float timeErr = digit.getTimeError();
226 
227  for (size_t k = 0; k < pdfPeaks.size(); k++) {
228  const auto& pdfPeak = pdfPeaks[k];
229  const auto& pdfExtra = extraInfos[k];
231  peak.position = pdfPeak.t0;
232  peak.width = sqrt(pdfPeak.wid);
233  peak.numPhotons = pdfPeak.nph * signalPhotons;
234  float wt = 0;
235  for (const auto& gaus : tts->getTTS()) {
236  float sig2 = peak.width * peak.width + gaus.sigma * gaus.sigma + timeErr * timeErr;
237  float x = pow(time - peak.position - gaus.position, 2) / sig2;
238  if (x > 20) continue;
239  wt += peak.numPhotons * gaus.fraction / sqrt(2 * M_PI * sig2) * exp(-x / 2);
240  }
241  if (wt > 0) {
242  peak.fic = pdfPeak.fic;
243  peak.e = pdfExtra.e;
244  peak.sige = pdfExtra.sige;
245  peak.nx = abs(pdfExtra.Nxm) + abs(pdfExtra.Nxb) + abs(pdfExtra.Nxe);
246  peak.ny = abs(pdfExtra.Nym) + abs(pdfExtra.Nyb) + abs(pdfExtra.Nye);
247  peak.nxm = abs(pdfExtra.Nxm);
248  peak.nym = abs(pdfExtra.Nym);
249  peak.nxe = abs(pdfExtra.Nxe);
250  peak.nye = abs(pdfExtra.Nye);
251  peak.xd = pdfExtra.xD;
252  peak.yd = pdfExtra.yD;
253  peak.type = pdfExtra.type;
254  peak.kxe = pdfExtra.kxE;
255  peak.kye = pdfExtra.kyE;
256  peak.kze = pdfExtra.kzE;
257  peak.kxd = pdfExtra.kxD;
258  peak.kyd = pdfExtra.kyD;
259  peak.kzd = pdfExtra.kzD;
260  associatedPDF->appendPeak(peak, wt);
261  }
262  }
263  }
264 
265  }
266 
268 } // end Belle2 namespace
269 
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:580
int getPDGCode() const
PDG code.
Definition: Const.h:464
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:609
ROOT::Math::XYZVector getMomentum() const
Get momentum at this extrapolation hit.
Definition: ExtHit.h:159
ROOT::Math::XYZVector getPosition() const
Get position of this extrapolation hit.
Definition: ExtHit.h:145
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
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 ROOT::Math::XYZPoint &pos, const ROOT::Math::XYZVector &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)
double m_maxTime
optional time limit for photons
double m_minTime
optional time limit for photons
StoreArray< TOPPDFCollection > m_pdfCollection
collection of analytic PDF's
TOP::PDFConstructor::EPDFOption m_PDFOption
PDF option.
StoreArray< TOPPixelLikelihood > m_pixelData
collection of per-pixel data
StoreArray< Track > m_tracks
collection of tracks
StoreArray< TOPAssociatedPDF > m_associatedPDFs
collection of associated PDF's
std::vector< int > m_pdgCodes
particle codes
StoreArray< TOPDigit > m_digits
collection of digits
std::vector< Const::ChargedStable > m_chargedStables
particle hypotheses
std::string m_pdfOption
PDF option name.
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:113
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.
@ c_Full
also extra information and derivatives
@ c_Optimal
y dependent only where necessary
@ c_Fine
y dependent everywhere
@ c_Rough
no dependence on y
double getExpectedBkgPhotons() const
Returns the expected number of background photons within the default time window.
const DeltaRayPDF & getDeltaRayPDF() const
Returns delta-ray PDF.
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using basf2 units.
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
static void setTimeWindow(double minTime, double maxTime)
Sets time window.
Reconstructed track at TOP.
Definition: TOPTrack.h:39
bool isValid() const
Checks if track is successfully constructed.
Definition: TOPTrack.h:137
const ExtHit * getExtHit() const
Returns extrapolated hit (track entrance to the bar)
Definition: TOPTrack.h:216
int getModuleID() const
Returns slot ID.
Definition: TOPTrack.h:143
Class to store variables with their name which were sent to the logging service.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
void associatePDFPeaks(const TOP::PDFConstructor &pdfConstructor)
Associate PDF peaks with photons using S-plot technique.
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.