Belle II Software  release-05-02-19
TOPPDFDebuggerModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric, Jan Strube, Sam Cunliffe *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Module manager
12 
13 
14 // Own include
15 #include <top/modules/TOPPDFDebugger/TOPPDFDebuggerModule.h>
16 #include <top/geometry/TOPGeometryPar.h>
17 #include <top/reconstruction/TOPreco.h>
18 #include <top/reconstruction/TOPtrack.h>
19 #include <top/reconstruction/TOPconfigure.h>
20 
21 // Hit classes
22 #include <tracking/dataobjects/ExtHit.h>
23 #include <mdst/dataobjects/MCParticle.h>
24 #include <top/dataobjects/TOPBarHit.h>
25 
26 // framework - DataStore
27 #include <framework/datastore/StoreArray.h>
28 
29 // framework aux
30 #include <framework/gearbox/Const.h>
31 #include <framework/logging/Logger.h>
32 
33 #include <cmath>
34 
35 using namespace std;
36 
37 namespace Belle2 {
42  using namespace TOP;
43  //-----------------------------------------------------------------
44  // Register the Module
45  //-----------------------------------------------------------------
46 
47  REG_MODULE(TOPPDFDebugger)
48 
49 
50  //-----------------------------------------------------------------
51  // Implementation
52  //-----------------------------------------------------------------
53 
55  {
56  // Set description
57  setDescription("This module makes an analytic PDF available in "
58  "a store array TOPPDFCollections, related from Tracks");
59 
60  // Set property flags
61  setPropertyFlags(c_ParallelProcessingCertified);
62 
63  // Add parameters
64  addParam("minBkgPerBar", m_minBkgPerBar,
65  "Minimal number of background photons per bar", 0.0);
66  addParam("scaleN0", m_scaleN0,
67  "Scale factor for N0", 1.0);
68  addParam("minTime", m_minTime,
69  "lower limit for photon time [ns] (default if minTime >= maxTime)", 0.0);
70  addParam("maxTime", m_maxTime,
71  "upper limit for photon time [ns] (default if minTime >= maxTime)", 0.0);
72  /* not implemented
73  addParam("writeNPdfs", m_writeNPdfs,
74  "Write out the PDF for the first N events. -1 is for all.", 0);
75  addParam("writeNPulls", m_writeNPulls,
76  "Write out pulls for the first N events. -1 is for all.", 0);
77  */
78  addParam("pdfOption", m_pdfOption,
79  "PDF option, one of 'rough', 'fine', 'optimal'", std::string("fine"));
80  addParam("pdgCodes", m_pdgCodes,
81  "PDG codes of charged stable particles for which to construct PDF. "
82  "Empty list means all charged stable particles.",
83  m_pdgCodes);
84  }
85 
86 
87  TOPPDFDebuggerModule::~TOPPDFDebuggerModule()
88  {
89  }
90 
91 
92  void TOPPDFDebuggerModule::initialize()
93  {
94  // input
95  m_digits.isRequired();
96  m_tracks.isRequired();
97 
98  StoreArray<ExtHit> extHits;
99  extHits.isRequired();
100 
101  StoreArray<MCParticle> mcParticles;
102  mcParticles.isOptional();
103 
104  StoreArray<TOPBarHit> barHits;
105  barHits.isOptional();
106 
107  // output
108  m_pdfCollection.registerInDataStore();
109  m_tracks.registerRelationTo(m_pdfCollection);
110  m_associatedPDFs.registerInDataStore();
111  m_digits.registerRelationTo(m_associatedPDFs);
112 
113  // check for module debug level
114  if (getLogConfig().getLogLevel() == LogConfig::c_Debug) {
115  m_debugLevel = getLogConfig().getDebugLevel();
116  }
117 
118  // particle hypotheses
119  if (m_pdgCodes.empty()) {
120  for (const auto& part : Const::chargedStableSet) {
121  m_pdgCodes.push_back(abs(part.getPDGCode()));
122  m_masses.push_back(part.getMass());
123  }
124  } else {
125  for (auto& pdg : m_pdgCodes) pdg = abs(pdg);
126  for (auto pdg : m_pdgCodes) {
127  auto part = Const::ChargedStable(pdg); //throws runtime error for invalid pdg
128  m_masses.push_back(part.getMass());
129  }
130  }
131 
132  // Configure TOP detector
133  TOPconfigure config;
134  if (m_debugLevel > 0) config.print();
135 
136  // Parse PDF option
137  if (m_pdfOption == "rough") {
138  m_PDFOption = TOPreco::c_Rough;
139  } else if (m_pdfOption == "fine") {
140  m_PDFOption = TOPreco::c_Fine;
141  } else if (m_pdfOption == "optimal") {
142  m_PDFOption = TOPreco::c_Optimal;
143  } else {
144  B2ERROR("TOPPDFDebuggerModule: unknown PDF option '" << m_pdfOption << "'");
145  }
146 
147  }
148 
149  void TOPPDFDebuggerModule::beginRun()
150  {
151  }
152 
153  void TOPPDFDebuggerModule::event()
154  {
155  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
156 
157  // create reconstruction object
158  TOPreco reco(m_masses.size(), m_masses.data(), m_pdgCodes.data(),
159  m_minBkgPerBar, m_scaleN0);
160  reco.setPDFoption(m_PDFOption);
161  reco.setStoreOption(TOPreco::c_Full);
162 
163  // set time limit for photons lower than that given by TDC range (optional)
164  if (m_maxTime > m_minTime) {
165  reco.setTimeWindow(m_minTime, m_maxTime);
166  }
167 
168  // add photons
169  for (const auto& digit : m_digits) {
170  if (digit.getHitQuality() == TOPDigit::EHitQuality::c_Good) {
171  reco.addData(digit.getModuleID(), digit.getPixelID(),
172  digit.getTime(), digit.getTimeError());
173  }
174  }
175 
176  // reconstruct track-by-track and store the results
177  for (const auto& track : m_tracks) {
178  // construct TOPtrack from mdst track
179  TOPtrack trk(&track);
180  if (!trk.isValid()) continue;
181 
182  // add this vector of vector of triplets to the TOPPDFCollection
183  TOPPDFCollection* topPDFColl = m_pdfCollection.appendNew();
184  const auto& module = geo->getModule(trk.getModuleID());
185  topPDFColl->setLocalPositionMomentum(
186  module.pointToLocal(trk.getPosition()),
187  module.momentumToLocal(trk.getMomentum()),
188  trk.getModuleID()
189  );
190  for (unsigned i = 0; i < m_pdgCodes.size(); i++) {
191  double mass = m_masses[i];
192  int iPDGCode = m_pdgCodes[i];
193  reco.setMass(mass, iPDGCode);
194  reco.reconstruct(trk); // will run reconstruction only for this mass hypothesis
195  if (reco.getFlag() != 1) break; // track is not in the acceptance of TOP
196 
197  // associate PDF peaks with photons using S-plot technique
198  associatePDFPeaks(reco, trk.getModuleID(), iPDGCode);
199 
200  // collection of gaussian_t's for each pixel
201  TOPPDFCollection::modulePDF_t channelPDFCollection;
202 
203  // the mean, width and normalisation for each peak in the pdf
204  float position = 0, width = 0, numPhotons = 0;
205  for (int pixelID = 1; pixelID <= static_cast<int>(channelPDFCollection.size()); pixelID++) {
206  for (int k = 0; k < reco.getNumofPDFPeaks(pixelID); k++) {
207 
208  // get this peak
209  reco.getPDFPeak(pixelID, k, position, width, numPhotons);
210 
211  auto tp = TOPPDFCollection::Gaussian(position, width, numPhotons);
212  channelPDFCollection.at(pixelID - 1).push_back(tp);
213 
214  } // end loop over peaks in the pdf for this pixel
215  } // end loop over pixels
216 
217  topPDFColl->addHypothesisPDF(channelPDFCollection, iPDGCode);
218  }
219  if (reco.getFlag() == 1) track.addRelationTo(topPDFColl);
220  } // end loop over tracks
221  ++m_iEvent;
222  }
223 
224 
225  void TOPPDFDebuggerModule::associatePDFPeaks(const TOPreco& reco, int moduleID, int pdg)
226  {
227 
228  for (const auto& digit : m_digits) {
229  if (digit.getModuleID() != moduleID) continue;
230  if (digit.getHitQuality() != TOPDigit::c_Good) continue;
231 
232  auto* associatedPDF = m_associatedPDFs.appendNew(pdg);
233  digit.addRelationTo(associatedPDF);
234 
235  int pixelID = digit.getPixelID();
236  associatedPDF->setBackgroundWeight(reco.getBkgLevel(pixelID));
237 
238  const auto& tts = TOPGeometryPar::Instance()->getTTS(moduleID, digit.getPMTNumber());
239  float time = digit.getTime();
240  float timeErr = digit.getTimeError();
241  for (int k = 0; k < reco.getNumofPDFPeaks(pixelID); k++) {
243  reco.getPDFPeak(pixelID, k, peak.position, peak.width, peak.numPhotons);
244  float wt = 0;
245  for (const auto& gaus : tts.getTTS()) {
246  float sig2 = peak.width * peak.width + gaus.sigma * gaus.sigma + timeErr * timeErr;
247  float x = pow(time - peak.position - gaus.position, 2) / sig2;
248  if (x > 20) continue;
249  wt += peak.numPhotons * gaus.fraction / sqrt(2 * M_PI * sig2) * exp(-x / 2);
250  }
251  if (wt > 0) {
252  peak.fic = reco.getPDFPeakFic(pixelID, k);
253  peak.e = reco.getPDFPeakE(pixelID, k);
254  peak.sige = reco.getPDFPeakSigE(pixelID, k);
255  peak.nx = reco.getPDFPeakNx(pixelID, k);
256  peak.ny = reco.getPDFPeakNy(pixelID, k);
257  peak.nxm = reco.getPDFPeakNxm(pixelID, k);
258  peak.nym = reco.getPDFPeakNym(pixelID, k);
259  peak.nxe = reco.getPDFPeakNxe(pixelID, k);
260  peak.nye = reco.getPDFPeakNye(pixelID, k);
261  peak.xd = reco.getPDFPeakXD(pixelID, k);
262  peak.yd = reco.getPDFPeakYD(pixelID, k);
263  peak.type = reco.getPDFPeakType(pixelID, k);
264  peak.kxe = reco.getPDFPeakKxe(pixelID, k);
265  peak.kye = reco.getPDFPeakKye(pixelID, k);
266  peak.kze = reco.getPDFPeakKze(pixelID, k);
267  peak.kxd = reco.getPDFPeakKxd(pixelID, k);
268  peak.kyd = reco.getPDFPeakKyd(pixelID, k);
269  peak.kzd = reco.getPDFPeakKzd(pixelID, k);
270  associatedPDF->appendPeak(peak, wt);
271  }
272  }
273  }
274 
275  }
276 
277 
278  void TOPPDFDebuggerModule::endRun()
279  {
280  }
281 
282  void TOPPDFDebuggerModule::terminate()
283  {
284  }
285 
286 
287 
289 } // end Belle2 namespace
290 
Belle2::TOPAssociatedPDF::PDFPeak::kyd
float kyd
reconstructed photon direction in y at detection
Definition: TOPAssociatedPDF.h:60
Belle2::TOPPDFCollection::addHypothesisPDF
bool addHypothesisPDF(const modulePDF_t &pdf, const int hypothesis)
adds the pdf for the given hypothesis (PDG code)
Definition: TOPPDFCollection.h:71
Belle2::TOP::TOPreco::getPDFPeakNy
int getPDFPeakNy(int pixelID, int k) const
Returns total number of reflections in y of PDF peak.
Definition: TOPreco.cc:505
Belle2::TOP::TOPreco::getFlag
int getFlag()
Return status.
Definition: TOPreco.cc:278
Belle2::StoreArray::registerRelationTo
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:150
Belle2::TOPPDFCollection
Class to store analytical PDF relation from Tracks filled top/modules/TOPPDFDebugger/src/TOPPDFDebugg...
Definition: TOPPDFCollection.h:42
Belle2::TOP::TOPreco::reconstruct
void reconstruct(const TOPtrack &trk, int pdg=0)
Run reconstruction for a given track.
Definition: TOPreco.cc:268
Belle2::TOP::TOPreco::getPDFPeakNxe
int getPDFPeakNxe(int pixelID, int k) const
Returns number of reflections in x in prism.
Definition: TOPreco.cc:526
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::TOP::TOPreco::getPDFPeakNxm
int getPDFPeakNxm(int pixelID, int k) const
Returns number of reflections in x before mirror.
Definition: TOPreco.cc:512
Belle2::TOP::TOPtrack
Class to hold reconstructed track, interface to fortran.
Definition: TOPtrack.h:42
Belle2::TOPAssociatedPDF::PDFPeak::width
float width
width (sigma)
Definition: TOPAssociatedPDF.h:42
Belle2::TOP::TOPreco::getPDFPeakNx
int getPDFPeakNx(int pixelID, int k) const
Returns total number of reflections in x of PDF peak.
Definition: TOPreco.cc:498
Belle2::TOPPDFCollection::modulePDF_t
std::array< channelPDF_t, 512 > modulePDF_t
the PDF of the module is a list of 512 channel PDFs
Definition: TOPPDFCollection.h:61
Belle2::TOPAssociatedPDF::PDFPeak::kye
float kye
reconstructed photon direction in y at emission
Definition: TOPAssociatedPDF.h:57
Belle2::TOPAssociatedPDF::PDFPeak
parameters of a PDF peak
Definition: TOPAssociatedPDF.h:40
Belle2::TOP::TOPtrack::isValid
bool isValid() const
Check if track is properly defined.
Definition: TOPtrack.h:87
Belle2::TOP::TOPreco::getPDFPeakKxe
float getPDFPeakKxe(int pixelID, int k) const
Returns photon reconstructed direction in x at emission.
Definition: TOPreco.cc:554
Belle2::TOPPDFCollection::Gaussian
parameters to describe a Gaussian
Definition: TOPPDFCollection.h:49
Belle2::TOPAssociatedPDF::PDFPeak::xd
float xd
unfolded x coordinate of a pixel
Definition: TOPAssociatedPDF.h:53
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::TOPreco::addData
int addData(int moduleID, int pixelID, double time, double timeError)
Add data.
Definition: TOPreco.cc:214
Belle2::TOP::TOPreco::getPDFPeakKyd
float getPDFPeakKyd(int pixelID, int k) const
Returns photon reconstructed direction in y at detection.
Definition: TOPreco.cc:582
Belle2::TOP::TOPtrack::getModuleID
int getModuleID() const
Return module ID.
Definition: TOPtrack.h:217
Belle2::TOPAssociatedPDF::PDFPeak::nxm
int nxm
number of reflections in x before mirror
Definition: TOPAssociatedPDF.h:49
Belle2::TOP::TOPreco::getPDFPeakYD
float getPDFPeakYD(int pixelID, int k) const
Returns unfolded y position of pixel.
Definition: TOPreco.cc:547
Belle2::TOPAssociatedPDF::PDFPeak::nye
int nye
number of reflections in y in prism
Definition: TOPAssociatedPDF.h:52
Belle2::TOPAssociatedPDF::PDFPeak::nxe
int nxe
number of reflections in x in prism
Definition: TOPAssociatedPDF.h:51
Belle2::TOPPDFDebuggerModule
TOP reconstruction module.
Definition: TOPPDFDebuggerModule.h:41
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::TOPAssociatedPDF::PDFPeak::nx
int nx
total number of reflections in x
Definition: TOPAssociatedPDF.h:47
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::TOP::TOPreco::getPDFPeakXD
float getPDFPeakXD(int pixelID, int k) const
Returns unfolded x position of pixel.
Definition: TOPreco.cc:540
Belle2::TOP::TOPreco::getPDFPeakNye
int getPDFPeakNye(int pixelID, int k) const
Returns number of reflections in y in prism.
Definition: TOPreco.cc:533
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOPAssociatedPDF::PDFPeak::fic
float fic
Cerenkov azimuthal angle phi.
Definition: TOPAssociatedPDF.h:44
Belle2::TOPAssociatedPDF::PDFPeak::position
float position
position in time
Definition: TOPAssociatedPDF.h:41
Belle2::TOP::TOPreco::getPDFPeakType
int getPDFPeakType(int pixelID, int k) const
Returns type of the k-th PDF peak for given pixel.
Definition: TOPreco.cc:470
Belle2::TOPAssociatedPDF::PDFPeak::ny
int ny
total number of reflections in y
Definition: TOPAssociatedPDF.h:48
Belle2::TOPPDFCollection::setLocalPositionMomentum
void setLocalPositionMomentum(const TVector3 &pos, const TVector3 &mom, int moduleID)
sets the position and momentum of the exthit in local coordinates
Definition: TOPPDFCollection.h:91
Belle2::TOP::TOPtrack::getPosition
const TVector3 & getPosition() const
Return spatial position.
Definition: TOPtrack.h:93
Belle2::TOPAssociatedPDF::PDFPeak::type
int type
0 unknown, 1 direct photon, 2 reflected photon
Definition: TOPAssociatedPDF.h:55
Belle2::TOP::TOPconfigure
Configure TOP geometry for reconstruction: provides interface to fortran code.
Definition: TOPconfigure.h:36
Belle2::TOP::TOPreco::getPDFPeakKze
float getPDFPeakKze(int pixelID, int k) const
Returns photon reconstructed direction in z at emission.
Definition: TOPreco.cc:568
Belle2::TOP::TOPreco::getPDFPeakNym
int getPDFPeakNym(int pixelID, int k) const
Returns number of reflections in y before mirror.
Definition: TOPreco.cc:519
Belle2::TOP::TOPtrack::getMomentum
const TVector3 & getMomentum() const
Return momentum vector.
Definition: TOPtrack.h:99
Belle2::TOPAssociatedPDF::PDFPeak::e
float e
mean photon energy [eV]
Definition: TOPAssociatedPDF.h:45
Belle2::TOP::TOPreco::getPDFPeakSigE
float getPDFPeakSigE(int pixelID, int k) const
Returns photon energy spread of PDF peak.
Definition: TOPreco.cc:491
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::TOPAssociatedPDF::PDFPeak::numPhotons
float numPhotons
number of photons
Definition: TOPAssociatedPDF.h:43
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465
Belle2::TOPAssociatedPDF::PDFPeak::kzd
float kzd
reconstructed photon direction in z at detection
Definition: TOPAssociatedPDF.h:61
Belle2::TOP::TOPreco::getPDFPeakKzd
float getPDFPeakKzd(int pixelID, int k) const
Returns photon reconstructed direction in z at detection.
Definition: TOPreco.cc:589
Belle2::TOPAssociatedPDF::PDFPeak::kze
float kze
reconstructed photon direction in z at emission
Definition: TOPAssociatedPDF.h:58
Belle2::TOPAssociatedPDF::PDFPeak::yd
float yd
unfolded y coordinate of a pixel
Definition: TOPAssociatedPDF.h:54
Belle2::TOPAssociatedPDF::PDFPeak::sige
float sige
photon energy sigma squared [eV^2]
Definition: TOPAssociatedPDF.h:46
Belle2::TOP::TOPreco::getPDFPeakKxd
float getPDFPeakKxd(int pixelID, int k) const
Returns photon reconstructed direction in x at detection.
Definition: TOPreco.cc:575
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::TOP::TOPreco::getPDFPeakFic
float getPDFPeakFic(int pixelID, int k) const
Returns Cerenkov azimuthal angle of PDF peak.
Definition: TOPreco.cc:477
Belle2::TOPAssociatedPDF::PDFPeak::nym
int nym
number of reflections in y before mirror
Definition: TOPAssociatedPDF.h:50
Belle2::TOPAssociatedPDF::PDFPeak::kxd
float kxd
reconstructed photon direction in x at detection
Definition: TOPAssociatedPDF.h:59
Belle2::TOP::TOPreco::getBkgLevel
float getBkgLevel(int pixelID) const
Returns estimated background level for given pixel.
Definition: TOPreco.cc:464
Belle2::TOP::TOPreco::setStoreOption
void setStoreOption(StoreOption opt)
Sets option for storing PDF parameters in Fortran common TOP_PIK.
Definition: TOPreco.cc:202
Belle2::TOP::TOPreco::getPDFPeakE
float getPDFPeakE(int pixelID, int k) const
Returns photon energy of PDF peak.
Definition: TOPreco.cc:484
Belle2::TOP::TOPreco::getPDFPeakKye
float getPDFPeakKye(int pixelID, int k) const
Returns photon reconstructed direction in y at emission.
Definition: TOPreco.cc:561
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::TOPAssociatedPDF::PDFPeak::kxe
float kxe
reconstructed photon direction in x at emission
Definition: TOPAssociatedPDF.h:56