9#include <reconstruction/modules/VXDDedxPID/VXDDedxPIDModule.h>
10#include <reconstruction/modules/VXDDedxPID/HelixHelper.h>
12#include <framework/gearbox/Const.h>
14#include <vxd/geometry/GeoCache.h>
16#include <genfit/MaterialEffects.h>
18#include <Math/VectorUtil.h>
36 setDescription(
"Extract dE/dx and corresponding log-likelihood from fitted tracks and hits in the SVD and PXD.");
40 "Include PDF value for each hit in likelihood. If false, the truncated mean of dedx values for the detectors will be used.",
false);
43 addParam(
"usePXD",
m_usePXD,
"Use PXDClusters for dE/dx calculation",
false);
46 "Use a faster helix parametrisation, with corrections as soon as the approximation is more than ... cm off.",
double(4.0));
60 int nBinsXPXD, nBinsYPXD;
61 double xMinPXD, xMaxPXD, yMinPXD, yMaxPXD;
62 nBinsXPXD = nBinsYPXD = -1;
63 xMinPXD = xMaxPXD = yMinPXD = yMaxPXD = 0.0;
65 int nBinsXSVD, nBinsYSVD;
66 double xMinSVD, xMaxSVD, yMinSVD, yMaxSVD;
67 nBinsXSVD = nBinsYSVD = -1;
68 xMinSVD = xMaxSVD = yMinSVD = yMaxSVD = 0.0;
70 for (
unsigned int iPart = 0; iPart < 6; iPart++) {
75 if (pxd_pdf->GetEntries() == 0 || svd_pdf->GetEntries() == 0) {
78 B2FATAL(
"Couldn't find PDF for PDG " << pdgCode);
82 const double epsFactor = 1e-5;
83 if (nBinsXPXD == -1 and nBinsYPXD == -1) {
84 nBinsXPXD = pxd_pdf->GetNbinsX();
85 nBinsYPXD = pxd_pdf->GetNbinsY();
86 xMinPXD = pxd_pdf->GetXaxis()->GetXmin();
87 xMaxPXD = pxd_pdf->GetXaxis()->GetXmax();
88 yMinPXD = pxd_pdf->GetYaxis()->GetXmin();
89 yMaxPXD = pxd_pdf->GetYaxis()->GetXmax();
90 }
else if (nBinsXPXD != pxd_pdf->GetNbinsX()
91 or nBinsYPXD != pxd_pdf->GetNbinsY()
92 or fabs(xMinPXD - pxd_pdf->GetXaxis()->GetXmin()) > epsFactor * xMaxPXD
93 or fabs(xMaxPXD - pxd_pdf->GetXaxis()->GetXmax()) > epsFactor * xMaxPXD
94 or fabs(yMinPXD - pxd_pdf->GetYaxis()->GetXmin()) > epsFactor * yMaxPXD
95 or fabs(yMaxPXD - pxd_pdf->GetYaxis()->GetXmax()) > epsFactor * yMaxPXD) {
96 B2FATAL(
"PDF for PDG " << pdgCode <<
", PXD has binning/dimensions differing from previous PDF.");
100 if (nBinsXSVD == -1 and nBinsYSVD == -1) {
101 nBinsXSVD = svd_pdf->GetNbinsX();
102 nBinsYSVD = svd_pdf->GetNbinsY();
103 xMinSVD = svd_pdf->GetXaxis()->GetXmin();
104 xMaxSVD = svd_pdf->GetXaxis()->GetXmax();
105 yMinSVD = svd_pdf->GetYaxis()->GetXmin();
106 yMaxSVD = svd_pdf->GetYaxis()->GetXmax();
107 }
else if (nBinsXSVD != svd_pdf->GetNbinsX()
108 or nBinsYSVD != svd_pdf->GetNbinsY()
109 or fabs(xMinSVD - svd_pdf->GetXaxis()->GetXmin()) > epsFactor * xMaxSVD
110 or fabs(xMaxSVD - svd_pdf->GetXaxis()->GetXmax()) > epsFactor * xMaxSVD
111 or fabs(yMinSVD - svd_pdf->GetYaxis()->GetXmin()) > epsFactor * yMaxSVD
112 or fabs(yMaxSVD - svd_pdf->GetYaxis()->GetXmax()) > epsFactor * yMaxSVD) {
113 B2FATAL(
"PDF for PDG " << pdgCode <<
", PXD has binning/dimensions differing from previous PDF.");
154 if (!genfit::MaterialEffects::getInstance()->isInitialized()) {
155 B2FATAL(
"Need to have SetupGenfitExtrapolationModule in path before this one.");
178 for (
const auto& track :
m_tracks) {
181 std::shared_ptr<VXDDedxTrack> dedxTrack = std::make_shared<VXDDedxTrack>();
189 B2WARNING(
"No related fit for track ...");
193 if (numMCParticles != 0) {
205 dedxTrack->m_motherPDG = mother ? mother->
getPDG() : 0;
207 const ROOT::Math::XYZVector trueMomentum = mcpart->
getMomentum();
208 dedxTrack->m_pTrue = trueMomentum.R();
213 const ROOT::Math::XYZVector& trackPos = fitResult->
getPosition();
214 const ROOT::Math::XYZVector& trackMom = fitResult->
getMomentum();
215 dedxTrack->m_p = trackMom.R();
216 dedxTrack->m_cosTheta = cos(trackMom.Theta());
222 B2WARNING(
"No related track for this fit...");
229 B2ERROR(
"GFTrack is pruned, please run VXDDedxPID only on unpruned tracks! Skipping this track.");
234 const HelixHelper helixAtOrigin(trackPos, trackMom, dedxTrack->m_charge);
237 const std::vector<PXDCluster*>& pxdClusters = recoTrack->
getPXDHitList();
238 saveSiHits(dedxTrack.get(), helixAtOrigin, pxdClusters);
242 const std::vector<SVDCluster*>& svdClusters = recoTrack->
getSVDHitList();
243 saveSiHits(dedxTrack.get(), helixAtOrigin, svdClusters);
246 if (dedxTrack->dedx.empty()) {
247 B2DEBUG(50,
"Found track with no hits, ignoring.");
253 for (
int detector = 0; detector <= c_SVD; detector++) {
257 if (detector == 0)
savePXDLogLikelihood(dedxTrack->m_vxdLogl, dedxTrack->m_p, dedxTrack->m_dedxAvgTruncated[detector]);
258 else if (detector == 1)
saveSVDLogLikelihood(dedxTrack->m_vxdLogl, dedxTrack->m_p, dedxTrack->m_dedxAvgTruncated[detector]);
263 const int numDedx = dedxTrack->dedx.size();
264 dedxTrack->m_nHits = numDedx;
266 dedxTrack->m_nHitsUsed = numDedx - 2;
270 track.addRelationTo(newVXDDedxTrack);
274 track.addRelationTo(likelihoodObj);
282 B2DEBUG(50,
"VXDDedxPIDModule exiting after processing " <<
m_trackID <<
283 " tracks in " <<
m_eventID + 1 <<
" events.");
287 const std::vector<double>& dedx)
const
290 std::vector<double> sortedDedx = dedx;
291 std::sort(sortedDedx.begin(), sortedDedx.end());
293 double truncatedMeanTmp = 0.0;
294 double meanTmp = 0.0;
295 double sumOfSquares = 0.0;
296 const int numDedx = sortedDedx.size();
299 for (
int i = 0; i < numDedx; i++) {
300 meanTmp += sortedDedx[i];
306 for (
int i = 0; i < numDedx - 2; i++) {
307 truncatedMeanTmp += sortedDedx[i];
308 sumOfSquares += sortedDedx[i] * sortedDedx[i];
310 if (numDedx - 2 != 0) {
311 truncatedMeanTmp /= numDedx - 2;
315 *truncatedMean = truncatedMeanTmp;
317 if (numDedx - 2 > 1) {
318 *truncatedMeanErr =
sqrt(sumOfSquares /
double(numDedx - 2) - truncatedMeanTmp * truncatedMeanTmp) / double(
321 *truncatedMeanErr = 0;
330 const ROOT::Math::XYZVector localPos(hit->getU(), hit->getV(), 0.0);
331 const ROOT::Math::XYZVector& globalPos = sensor.pointToGlobal(localPos);
334 const ROOT::Math::XYZVector& sensorNormal = sensor.vectorToGlobal(ROOT::Math::XYZVector(0.0, 0.0, 1.0));
335 const double angle = ROOT::Math::VectorUtil::Angle(sensorNormal, localMomentum);
338 return TMath::Min(sensor.getWidth(), sensor.getThickness() / fabs(cos(angle)));
347 ROOT::Math::XYZVector a, b;
348 if (hit->isUCluster()) {
349 const float u = hit->getPosition();
350 a = sensor.pointToGlobal(ROOT::Math::XYZVector(sensor.getBackwardWidth() / sensor.getWidth(0) * u, -0.5 * sensor.getLength(), 0.0));
351 b = sensor.pointToGlobal(ROOT::Math::XYZVector(sensor.getForwardWidth() / sensor.getWidth(0) * u, +0.5 * sensor.getLength(), 0.0));
353 const float v = hit->getPosition();
354 a = sensor.pointToGlobal(ROOT::Math::XYZVector(-0.5 * sensor.getWidth(v), v, 0.0));
355 b = sensor.pointToGlobal(ROOT::Math::XYZVector(+0.5 * sensor.getWidth(v), v, 0.0));
357 const double pathLength = helix->
pathLengthToLine(ROOT::Math::XYZVector(a), ROOT::Math::XYZVector(b));
358 const ROOT::Math::XYZVector& localMomentum = helix->
momentum(pathLength);
360 const ROOT::Math::XYZVector& sensorNormal = sensor.vectorToGlobal(ROOT::Math::XYZVector(0.0, 0.0, 1.0));
361 const double angle = ROOT::Math::VectorUtil::Angle(sensorNormal, localMomentum);
363 return TMath::Min(sensor.getWidth(), sensor.getThickness() / fabs(cos(angle)));
368 const std::vector<HitClass*>& hits)
const
370 const int numHits = hits.size();
379 assert(currentDetector <= 1);
381 std::vector<double> siliconDedx;
382 siliconDedx.reserve(numHits);
385 for (
int i = 0; i < numHits; i++) {
386 const HitClass* hit = hits[i];
388 B2ERROR(
"Added hit is a null pointer!");
391 const VxdID& currentSensor = hit->getSensorID();
393 assert(layer >= -6 && layer < 0);
398 const float charge = hit->getCharge();
399 const float dedx = charge / totalDistance;
401 B2WARNING(
"dE/dx is " << dedx <<
" in layer " << layer);
402 }
else if (i == 0 or prevSensor != currentSensor) {
403 prevSensor = currentSensor;
405 siliconDedx.push_back(dedx);
406 track->m_dedxAvg[currentDetector] += dedx;
407 track->addDedx(layer, totalDistance, dedx);
414 track->addHit(currentSensor, layer, charge, totalDistance, dedx);
419 &(track->m_dedxAvgTruncated[currentDetector]),
420 &(track->m_dedxAvgTruncatedErr[currentDetector]),
428 const Int_t binX = pdf->GetXaxis()->FindFixBin(p);
429 const Int_t binY = pdf->GetYaxis()->FindFixBin(dedx);
433 if (pdf->GetEntries() == 0)
435 double probability = 0.0;
438 if (binX < 1 or binX > pdf->GetNbinsX()
439 or binY < 1 or binY > pdf->GetNbinsY()) {
440 probability = pdf->GetBinContent(binX, binY);
445 probability =
const_cast<TH2F*
>(pdf)->Interpolate(p, dedx);
448 if (probability != probability)
449 B2ERROR(
"probability NAN for a track with p=" << p <<
" and dedx=" << dedx);
452 if (probability == 0.0)
455 logl[iPart] += log(probability);
463 const Int_t binX = pdf->GetXaxis()->FindFixBin(p);
464 const Int_t binY = pdf->GetYaxis()->FindFixBin(dedx);
468 if (pdf->GetEntries() == 0)
470 double probability = 0.0;
473 if (binX < 1 or binX > pdf->GetNbinsX()
474 or binY < 1 or binY > pdf->GetNbinsY()) {
475 probability = pdf->GetBinContent(binX, binY);
480 probability =
const_cast<TH2F*
>(pdf)->Interpolate(p, dedx);
483 if (probability != probability)
484 B2ERROR(
"probability NAN for a track with p=" << p <<
" and dedx=" << dedx);
487 if (probability == 0.0)
490 logl[iPart] += log(probability);
static const unsigned int c_SetSize
Number of elements (for use in array bounds etc.)
const ParticleType & at(unsigned int index) const
Return particle at given index, or end() if out of range.
int getPDGCode() const
PDG code.
static const ParticleSet chargedStableSet
set of charged stable particles
static const ChargedStable pion
charged pion particle
Helper class representing a helical track.
double pathLengthToPoint(const ROOT::Math::XYZVector &p) const
returns the path length (along the helix) to the helix point closest to p.
ROOT::Math::XYZVector momentum(double s=0) const
momentum of the particle, at the helix point corresponding to a flown path length s (from poca).
double pathLengthToLine(const ROOT::Math::XYZVector &a, const ROOT::Math::XYZVector &b) const
returns the path length (along the helix) to the helix point closest to the line going through points...
A Class to store the Monte Carlo particle information.
@ c_PrimaryParticle
bit 0: Particle is primary particle.
bool hasStatus(unsigned short int bitmask) const
Return if specific status bit is set.
int m_pdg
PDG-Code of the particle.
int getPDG() const
Return PDG code of particle.
ROOT::Math::XYZVector getMomentum() const
Return momentum.
void setDescription(const std::string &description)
Sets the description of the module.
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
The PXD Cluster class This class stores all information about reconstructed PXD clusters The position...
This is the Reconstruction Event-Data Model Track.
std::vector< Belle2::RecoTrack::UsedPXDHit * > getPXDHitList() const
Return an unsorted list of pxd hits.
std::vector< Belle2::RecoTrack::UsedSVDHit * > getSVDHitList() const
Return an unsorted list of svd hits.
const genfit::FitStatus * getTrackFitStatus(const genfit::AbsTrackRep *representation=nullptr) const
Return the track fit status for the given representation or for the cardinal one. You are not allowed...
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
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.
int getEntries() const
Get the number of objects in the array.
Values of the result of a track fit with a given particle hypothesis.
short getChargeSign() const
Return track charge (1 or -1).
ROOT::Math::XYZVector getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
ROOT::Math::XYZVector getPosition() const
Getter for vector of position at closest approach of track in r/phi projection.
Container for likelihoods obtained by the VXD dE/dx PID (VXDDedxPIDModule).
void saveSVDLogLikelihood(double(&logl)[Const::ChargedStable::c_SetSize], double p, float dedx) const
for all particles in the SVD, save log-likelihood values into 'logl'.
DBObjPtr< SVDdEdxPDFs > m_SVDDedxPDFs
SVD DB object for dedx:momentum PDFs.
int m_trackID
counter for tracks in this event
virtual void initialize() override
Initialize the module.
StoreArray< SVDCluster > m_svdClusters
Optional array of SVDClusters.
bool m_usePXD
use PXD hits for likelihood
virtual void event() override
This method is called for each event.
StoreArray< MCParticle > m_mcparticles
Optional array of MCParticles.
StoreArray< VXDDedxLikelihood > m_dedxLikelihoods
Output array of VXDDedxLikelihoods.
void checkPDFs()
Check the pdfs for consistency every time they change in the database.
virtual void terminate() override
End of the event processing.
void saveSiHits(VXDDedxTrack *track, const HelixHelper &helix, const std::vector< HitClass * > &hits) const
save energy loss and hit information from SVD/PXDHits to track
DBObjPtr< PXDdEdxPDFs > m_PXDDedxPDFs
PXD DB object for dedx:momentum PDFs.
void calculateMeans(double *mean, double *truncatedMean, double *truncatedMeanErr, const std::vector< double > &dedx) const
Save arithmetic and truncated mean for the 'dedx' values.
StoreArray< VXDDedxTrack > m_dedxTracks
Output array of VXDDedxTracks.
virtual ~VXDDedxPIDModule()
Destructor.
int m_eventID
counter for events
bool m_useSVD
use SVD hits for likelihood
bool detectorEnabled(Dedx::Detector d) const
should info from this detector be included in likelihood?
bool m_ignoreMissingParticles
Ignore particles for which no PDFs are found.
StoreArray< Track > m_tracks
Required array of Tracks.
bool m_onlyPrimaryParticles
Only save data for primary particles (as determined by MC truth)
StoreArray< PXDCluster > m_pxdClusters
Optional array of PXDClusters.
static double getTraversedLength(const PXDCluster *hit, const HelixHelper *helix)
returns traversed length through active medium of given PXDCluster.
StoreArray< RecoTrack > m_recoTracks
Required array of input RecoTracks.
bool m_useIndividualHits
Include PDF value for each hit in likelihood.
VXDDedxPIDModule()
Default constructor.
double m_trackDistanceThreshhold
Use a faster helix parametrisation, with corrections as soon as the approximation is more than ....
void savePXDLogLikelihood(double(&logl)[Const::ChargedStable::c_SetSize], double p, float dedx) const
for all particles in the PXD, save log-likelihood values into 'logl'.
Debug output for VXDDedxPID module.
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
const SensorInfoBase & getSensorInfo(Belle2::VxdID id) const
Return a referecne to the SensorInfo of a given SensorID.
static GeoCache & getInstance()
Return a reference to the singleton instance.
Base class to provide Sensor Information for PXD and SVD.
SensorType getType() const
Return the Type of the Sensor.
Class to uniquely identify a any structure of the PXD and SVD.
baseType getLayerNumber() const
Get the layer id.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
double sqrt(double a)
sqrt for double
MCParticle * getMother() const
Returns a pointer to the mother particle.
Abstract base class for different kinds of events.