10#include <klm/modules/KLMTracking/KLMTrackingModule.h>
13#include <klm/bklm/geometry/GeometryPar.h>
14#include <klm/modules/KLMTracking/KLMTrackFinder.h>
15#include <klm/eklm/geometry/TransformDataGlobalAligned.h>
18#include <framework/dataobjects/EventMetaData.h>
19#include <framework/datastore/StoreObjPtr.h>
20#include <framework/datastore/StoreArray.h>
21#include <framework/logging/Logger.h>
22#include <framework/utilities/MathHelpers.h>
23#include <tracking/dataobjects/RecoHitInformation.h>
30using namespace Belle2::KLM;
45 for (
int i = 0; i < 8; ++i) {
53 setDescription(
"Perform standard-alone straight line tracking for KLM. ");
56 "[degree], match KLMTrack to RecoTrack; angle between them is required to be smaller than (default 10)",
double(10.0));
58 "[cm], During efficiency calculation, distance between track and 2dhit must be smaller than (default 10)",
double(10.0));
60 "[sigma], During efficiency calculation, uncertainty of 2dhit must be smaller than (default 5); ",
double(5));
62 ", During track finding, a good track after initial seed hits must be larger than is (default 2); ",
unsigned(2));
64 ", During track finding, a good track after initial seed hits must be smaller than is (default 60); ",
unsigned(60));
66 ", Only look at tracks with more than n number of layers; ",
int(4));
67 addParam(
"StudyEffiMode",
m_studyEffi,
"[bool], run in efficieny study mode (default is false)",
false);
68 addParam(
"outputName",
m_outPath,
"[string], output file name containing efficiencies plots ",
69 std::string(
"standaloneKLMEffi.root"));
88 B2INFO(
"KLMTrackingModule::initialize this module is running in efficiency study mode!");
92 std::string labelFB[2] = {
"BB",
"BF"};
99 m_totalYX =
new TH2F(
"totalYX",
" denominator Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
100 m_passYX =
new TH2F(
"passYX",
" numerator Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
101 m_totalYZ =
new TH2F(
"totalYZ",
" denominator Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
102 m_passYZ =
new TH2F(
"passYZ",
" numerator Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
103 m_effiYX =
new TH2F(
"effiYX",
" effi. Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
104 m_effiYZ =
new TH2F(
"effiYZ",
" effi. Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
105 m_effiYX->GetXaxis()->SetTitle(
"x (cm)");
106 m_effiYX->GetYaxis()->SetTitle(
"y (cm)");
107 m_effiYZ->GetXaxis()->SetTitle(
"z (cm)");
108 m_effiYZ->GetYaxis()->SetTitle(
"y (cm)");
109 for (
int iF = 0; iF < 2; iF++) {
110 for (
int iS = 0; iS < 8; iS++) {
111 hname.Form(
"effi_%s%i", labelFB[iF].c_str(), iS);
112 m_effiVsLayer[iF][iS] =
new TEfficiency(hname, hname, Nbin, 0, 16);
113 hname.Form(
"total_%s%i", labelFB[iF].c_str(), iS);
114 m_total[iF][iS] =
new TH1F(hname, hname, Nbin, 0, 16);
115 hname.Form(
"pass_%s%i", labelFB[iF].c_str(), iS);
116 m_pass[iF][iS] =
new TH1F(hname, hname, Nbin, 0, 16);
149 m_runNumber.push_back((
int)eventMetaData->getRun());
157 bool thereIsATrack =
false;
163 thereIsATrack =
true;
165 for (
int iSection = 0; iSection < 2; iSection++) {
166 for (
int iSector = 0; iSector < 8; iSector++) {
167 for (
int iLayer = 0; iLayer < 15; iLayer++) {
170 thereIsATrack =
true;
192 if (
hits2D.getEntries() < 1)
195 for (
int j = 0; j <
hits2D.getEntries(); j++) {
196 if (
hits2D[j]->getSubdetector() != iSubdetector)
198 hits2D[j]->isOnStaTrack(
false);
202 for (
int hi = 0; hi <
hits2D.getEntries() - 1; ++hi) {
203 if (
hits2D[hi]->getSubdetector() != iSubdetector)
210 if (
hits2D[hi]->isOnStaTrack())
212 if (
hits2D[hi]->isOutOfTime())
214 for (
int hj = hi + 1; hj <
hits2D.getEntries(); ++hj) {
216 if (
hits2D[hj]->isOnStaTrack())
218 if (
hits2D[hj]->isOutOfTime())
221 if (
hits2D[hi]->getSubdetector() !=
hits2D[hj]->getSubdetector())
224 std::abs(
hits2D[hi]->getLayer() -
hits2D[hj]->getLayer()) < 3)
227 std::list<KLMHit2d*> sectorHitList;
230 std::list<KLMHit2d*> seed;
231 seed.push_back(
hits2D[hi]);
232 seed.push_back(
hits2D[hj]);
234 for (
int ho = 0; ho <
hits2D.getEntries(); ++ho) {
237 if (ho == hi || ho == hj)
239 if (mode == 1 && (
hits2D[ho]->getSubdetector() != iSubdetector))
245 if (
hits2D[ho]->isOnStaTrack())
250 if (
hits2D[ho]->isOutOfTime())
252 sectorHitList.push_back(
hits2D[ho]);
262 std::list<KLMHit2d*> m_hits;
264 if (m_finder->
filter(seed, sectorHitList, m_hits, iSubdetector)) {
272 std::list<KLMHit2d*>::iterator j;
274 int nBKLM = 0;
int nEKLM = 0;
275 for (j = m_hits.begin(); j != m_hits.end(); ++j) {
276 (*j)->isOnStaTrack(
true);
283 B2DEBUG(31,
"KLMTracking::runTracking totalHit " << m_hits.size() <<
", nBKLM " << nBKLM <<
", nEKLM " << nEKLM);
289 B2DEBUG(30,
"KLMTracking::runTracking started RecoTrack matching");
292 B2DEBUG(30,
"KLMTracking::runTracking was able to find ClosestRecoTrack");
294 for (j = m_hits.begin(); j != m_hits.end(); ++j) {
297 closestTrack->
addBKLMHit((*j), sortingParameter, RecoHitInformation::OriginTrackFinder::c_LocalTrackFinder);
300 closestTrack->
addEKLMHit(&(alignmentHit), sortingParameter,
301 RecoHitInformation::OriginTrackFinder::c_LocalTrackFinder);
325 for (
long unsigned int i = 0; i <
m_runNumber.size(); i++) {
327 B2INFO(
"KLMTrackingModule::terminate run " <<
m_runNumber.at(i) <<
" --> " << ratio * 100 <<
"% of events has 1+ KLMTracks");
331 for (
int iF = 0; iF < 2; iF++) {
332 for (
int iS = 0; iS < 8; iS++) {
339 for (
int i = 0; i <
m_totalYX->GetNbinsX(); i++) {
340 for (
int j = 0; j <
m_totalYX->GetNbinsY(); j++) {
341 float num =
m_passYX->GetBinContent(i + 1, j + 1);
342 float denom =
m_totalYX->GetBinContent(i + 1, j + 1);
344 m_effiYX->SetBinContent(i + 1, j + 1, num / denom);
345 m_effiYX->SetBinError(i + 1, j + 1,
sqrt(num * (denom - num) / (denom * denom * denom)));
347 m_effiYX->SetBinContent(i + 1, j + 1, 0);
348 m_effiYX->SetBinError(i + 1, j + 1, 0);
351 num =
m_passYZ->GetBinContent(i + 1, j + 1);
352 denom =
m_totalYZ->GetBinContent(i + 1, j + 1);
354 m_effiYZ->SetBinContent(i + 1, j + 1, num / denom);
355 m_effiYZ->SetBinError(i + 1, j + 1,
sqrt(num * (denom - num) / (denom * denom * denom)));
357 m_effiYZ->SetBinContent(i + 1, j + 1, 0);
358 m_effiYZ->SetBinError(i + 1, j + 1, 0);
395 if (klmHits.
size() < 1) {
396 B2INFO(
"KLMTrackingModule::findClosestRecoTrack, something is wrong! there is a KLMTrack but no klmHits");
400 B2DEBUG(20,
"KLMTrackingModule::findClosestRecoTrack, there is no recoTrack");
403 double oldDistanceSq = INFINITY;
404 double oldAngle = INFINITY;
405 closestTrack =
nullptr;
410 TVector3 firstKLMHitPosition(klmHits[0]->
getPosition().X(),
428 if (track.wasFitSuccessful()) {
430 genfit::MeasuredStateOnPlane state = track.getMeasuredStateOnPlaneFromLastHit();
431 B2DEBUG(30,
"KLMTracking::findClosestRecoTrack, finished MSOP from last hit");
433 state.getPosMomCov(pos, mom, cov);
434 if (mom.Y() * pos.Y() < 0) {
435 state = track.getMeasuredStateOnPlaneFromFirstHit();
437 const TVector3& distanceVec = firstKLMHitPosition - pos;
438 state.extrapolateToPoint(firstKLMHitPosition);
439 double newDistanceSq = distanceVec.Mag2();
440 double angle = klmTrkVec.Angle(mom);
443 if (newDistanceSq < oldDistanceSq) {
444 oldDistanceSq = newDistanceSq;
445 closestTrack = &track;
455 B2DEBUG(30,
"KLMTracking::findClosestRecoTrack, step one done");
456 }
catch (genfit::Exception& e) {
468 B2DEBUG(28,
"KLMTrackingModule::findClosestRecoTrack RecoTrack found! ");
479 std::set<int> m_pointUsed;
480 std::set<int> layerList;
484 B2DEBUG(10,
"KLMTrackingModule:generateEffi: " << iSection <<
" " << iSector <<
" " << iLayer);
496 for (
const KLMHit2d& hit2D : relatedHit2D) {
497 if (hit2D.getSubdetector() != iSubdetector)
499 if (hit2D.getLayer() > iLayer + 1)
500 {cnt1++; layerList.insert(hit2D.getLayer());}
501 if (hit2D.getLayer() < iLayer + 1)
502 {cnt2++; layerList.insert(hit2D.getLayer());}
503 if (hit2D.getLayer() == iLayer + 1) {
504 B2DEBUG(10,
"generateEffi: Hit info. Secti/sector/Lay = " << hit2D.getSection()
505 <<
"/" << hit2D.getSector() - 1 <<
"/" << hit2D.getLayer() - 1);
506 B2DEBUG(11,
"generateEffi: Hit info. x/y/z = " << hit2D.getPositionX()
507 <<
"/" << hit2D.getPositionY() <<
"/" << hit2D.getPositionZ());
514 if (iLayer != 0 && cnt2 < 1)
516 if (iLayer != 14 && cnt1 < 1)
526 int minPhiStrip =
module->getPhiStripMin();
527 int maxPhiStrip =
module->getPhiStripMax();
528 int minZStrip =
module->getZStripMin();
529 int maxZStrip =
module->getZStripMax();
531 CLHEP::Hep3Vector local =
module->getLocalPosition(minPhiStrip, minZStrip);
532 CLHEP::Hep3Vector local2 =
module->getLocalPosition(maxPhiStrip, maxZStrip);
533 float minLocalY, maxLocalY;
534 float minLocalZ, maxLocalZ;
535 if (local[1] > local2[1]) {
536 maxLocalY = local[1];
537 minLocalY = local2[1];
539 maxLocalY = local2[1];
540 minLocalY = local[1];
542 if (local[2] > local2[2]) {
543 maxLocalZ = local[2];
544 minLocalZ = local2[2];
546 maxLocalZ = local2[2];
547 minLocalZ = local[2];
554 Hep3Vector point1(0, trkPar[0], trkPar[2]);
555 Hep3Vector point2(1, trkPar[0] + trkPar[1], trkPar[2] + trkPar[3]);
557 Hep3Vector refPoint1(0., 0., 0.); Hep3Vector refPoint2(0., 0., 0.);
561 Hep3Vector refSlope(refPoint2[0] - refPoint1[0], refPoint2[1] - refPoint1[1], refPoint2[2] - refPoint1[2]);
569 reflocalX = -reflocalX;
570 float X_coord = (reflocalX - refPoint1[0]) / refSlope[0];
571 float reflocalY = refPoint1[1] + refSlope[1] * X_coord;
572 float reflocalZ = refPoint1[2] + refSlope[2] * X_coord;
575 Hep3Vector reflocal(reflocalX, reflocalY, reflocalZ);
576 Hep3Vector global(0., 0., 0.);
580 float localX =
module->globalToLocal(global)[0];
581 float localY =
module->globalToLocal(global)[1];
582 float localZ =
module->globalToLocal(global)[2];
584 B2DEBUG(10,
"KLMTrackingModule:generateEffi: RefLocal " << reflocalX <<
" " << reflocalY <<
" " << reflocalZ);
585 B2DEBUG(10,
"KLMTrackingModule:generateEffi: Global " << global[0] <<
" " << global[1] <<
" " << global[2]);
586 B2DEBUG(10,
"KLMTrackingModule:generateEffi: Local " << localX <<
" " << localY <<
" " << localZ);
591 if (localY > minLocalY && localY < maxLocalY && localZ > minLocalZ && localZ < maxLocalZ) {
593 bool m_iffound =
false;
594 m_total[iSection][iSector]->Fill(iLayer + 1);
598 for (
int he = 0; he <
hits2D.getEntries(); ++he) {
600 B2DEBUG(11,
"not isLayerUnderStudy");
603 if (
hits2D[he]->isOutOfTime()) {
604 B2DEBUG(11,
"hit isOutOfTime");
608 if (m_pointUsed.find(he) != m_pointUsed.end()) {
609 B2DEBUG(11,
"passed unused");
612 B2DEBUG(11,
"KLMTrackingModule:generateEffi: Reached Distance Check");
615 float hitX =
hits2D[he]->getPositionX();
616 float hitY =
hits2D[he]->getPositionY();
617 float hitZ =
hits2D[he]->getPositionZ();
618 float deltaX = hitX - global[0];
float deltaY = hitY - global[1];
float deltaZ = hitZ - global[2];
619 float dist =
sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
620 B2DEBUG(10,
"dist w/ hit = " << dist <<
", dist func = " << distance <<
", error = " << error);
623 B2DEBUG(10,
"KLMTrackingModule:generateEffi: Hit found!");
626 m_pointUsed.insert(he);
627 m_pass[iSection][iSector]->Fill(iLayer + 1);
628 m_passYX->Fill(global[0], global[1]);
629 m_passYZ->Fill(global[2], global[1]);
634 m_effiVsLayer[iSection][iSector]->Fill(m_iffound, iLayer + 1);
671 double x, y, z, dx, dy, dz, distance;
676 TVectorD m_GlobalPar = track->getTrackParam();
690 x = (z - m_GlobalPar[ 2 ]) / m_GlobalPar[ 3 ];
691 y = m_GlobalPar[ 0 ] + x * m_GlobalPar[ 1 ];
698 double y2 = m_GlobalPar[ 0 ] + x2 * m_GlobalPar[ 1 ];
699 double z2 = m_GlobalPar[ 2 ] + x2 * m_GlobalPar[ 3 ];
705 double dist2 =
sqrt(dx2 * dx2 + dy2 * dy2 + dz2 * dz2);
708 distance =
sqrt(dx * dx + dy * dy + dz * dz);
714 B2DEBUG(11,
"Dist = " << distance <<
", error = " << error);
715 B2DEBUG(11,
"Dist2 = " << dist2 <<
", error = " << error);
726 x = (z - m_GlobalPar[ 2 ]) / m_GlobalPar[ 3 ];
727 y = m_GlobalPar[ 0 ] + x * m_GlobalPar[ 1 ];
733 distance =
sqrt(dx * dx + dy * dy + dz * dz);
747 B2WARNING(
"KLMTracking::distanceToHit Received KLMHit2d that's not from E/B-KLM. Setting distance to -1");
753 sigma = distance / error;
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
This dataobject is used only for EKLM alignment.
const StripGeometry * getStripGeometry() const
Get strip geometry data.
int getSubdetector() const
Get subdetector number.
int getYStripMin() const
Get first strip number for EKLM hit in the y-measuring plane.
int getLayer() const
Get layer number.
int getSection() const
Get section number.
float getPositionZ() const
Get hit global position z coordinate.
int getSector() const
Get sector number.
float getPositionX() const
Get hit global position x coordinate.
int getXStripMax() const
Get last strip number for EKLM hit in the x-measuring plane.
float getPositionY() const
Get hit global position y coordinate.
int getYStripMax() const
Get last strip number for EKLM hit in the y-measuring plane.
int getXStripMin() const
Get first strip number for EKLM hit in the x-measuring plane.
bool filter(const std::list< KLMHit2d * > &seed, std::list< KLMHit2d * > &hits, std::list< KLMHit2d * > &track, int iSubdetector)
find associated hits and do fit.
void registerFitter(KLMTrackFitter *fitter)
Register a fitter if not constructed with one.
float getChi2()
Chi square of the fit.
bool isGood()
Is fit good.
int getNumHit()
number of the hits on this track
CLHEP::HepSymMatrix getTrackParamErr()
Get invariance matrix of track parameters in the global system.
CLHEP::HepVector getTrackParam()
Get track parameters in the global system. y = p0 + p1 * x; z = p2 + p3 * x.
bool isValid()
Is fit valid.
Store one KLM Track as a ROOT object.
void setIsValid(const bool valid)
set the fit valid status
void setTrackChi2(const float chi2)
Set the fitted chi2 of the track.
void setTrackParamErr(const CLHEP::HepSymMatrix &trkParErr)
Set invariance matrix of track parameters in the global system.
void setNumHitOnTrack(const int NumHit)
Set the number of 2d hits on the track.
TVectorD getTrackParam()
Get track parameters in the global system. y = p0 + p1 * x; z = p2 + p3 * x.
void setIsGood(const bool good)
set the fit good status
void setInSubdetector(int nBKLM, int nEKLM)
setting whether track passes through E/B-KLM
void setTrackParam(const CLHEP::HepVector &trkPar)
Set track parameters in the global system. y = p0 + p1 * x; z = p2 + p3 * x.
void generateEffi(int iSubdetector, int section, int sector, int layer)
calculate efficiency
bool m_MatchToRecoTrack
whether match KLMTrack to RecoTrack
TEfficiency * m_effiVsLayer[2][8]
Efficieny of each layer.
std::vector< int > m_runNumber
run number
TH2F * m_passYZ
passed event at global position Y vs Z
TH2F * m_effiYX
Efficieny at global position Y vs X.
bool m_studyEffi
option for efficieny study mode, in this mode, the layer under study should not be used in tracking
double m_maxSigma
maximum sigma for hit acceptance during efficiency calculation
void runTracking(int mode, int iSubdetector, int section, int sector, int layer)
run the track finding and fitting
double m_maxAngleRequired
angle required between RecoTrack and KLMTrack, if openangle is larger than m_maxAngleRequired,...
int m_minNLayer
minimum number of layers for track finder to run
void initialize() override
Initialize at start of job.
unsigned int m_minHitList
minimum number of hits in sector for track finder to run (-2 from initial seed)
std::string m_outPath
output file name containing efficiencies plots
void event() override
Unpack one event and create digits.
Belle2::KLM::KLMGeometryPar * m_GeoPar
KLMGeometryPar to call on B/E-KLM.
bool findClosestRecoTrack(KLMTrack *klmTrk, RecoTrack *&closestTrack)
find the closest RecoTrack, match KLMTrack to RecoTrack, if the matched RecoTrack is found,...
void endRun() override
end run stuff
StoreArray< RecoTrack > recoTracks
RecoTrack StoreArray.
void terminate() override
Terminate at the end of job.
TH1F * m_pass[2][8]
Numerator of each layer.
TH2F * m_totalYX
total event at global position Y vs X
StoreArray< RecoHitInformation > recoHitInformation
RecoHitInformation StoreArray.
int m_runTotalEventsWithTracks
total number of processed events in the run with at lease one BKLMTrack
void beginRun() override
begin run stuff
KLMTrackingModule()
Constructor.
double m_maxDistance
maximum distance required between track and KLMHit2d to be accepted for efficiency calculation
StoreArray< KLMTrack > m_storeTracks
KLMTrack StoreArray.
StoreArray< KLMHit2d > hits2D
KLMHit2d StoreArray.
TFile * m_file
TFile that store efficieny plots.
std::vector< int > m_totalEvents
total number of processed events
bool isLayerUnderStudy(int section, int iSector, int iLayer, KLMHit2d *hit)
judge whether the current layer is understudy
TH1F * m_total[2][8]
Denominator of each layer.
double distanceToHit(KLMTrack *track, KLMHit2d *hit, double &error, double &sigma)
calculate distance from track to hit
unsigned int m_maxHitList
max number of hits in sector for track finder to run
std::vector< int > m_totalEventsWithTracks
total number of processed events with at least one BKLMTrack
TH2F * m_effiYZ
Efficieny at global position Y vs Z.
bool isSectorUnderStudy(int section, int iSector, KLMHit2d *hit)
judge whether the hits come from the sctor under study
static bool sortByLayer(KLMHit2d *hit1, KLMHit2d *hit2)
my defined sort function using layer number
TH2F * m_totalYZ
total event at global position Y vs Z
~KLMTrackingModule()
Destructor.
bool sameSector(KLMHit2d *hit1, KLMHit2d *hit2)
Judge if two hits come from the same sector.
int m_runTotalEvents
total number of processed events in the run
TH2F * m_passYX
passed event at global position Y vs X
void setDescription(const std::string &description)
Sets the description of the module.
This is the Reconstruction Event-Data Model Track.
bool addBKLMHit(const UsedBKLMHit *bklmHit, const unsigned int sortingParameter, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds a bklm hit with the given information to the reco track.
bool addEKLMHit(const UsedEKLMHit *eklmHit, const unsigned int sortingParameter, OriginTrackFinder foundByTrackFinder=OriginTrackFinder::c_undefinedTrackFinder)
Adds an eklm hit with the given information to the reco track.
unsigned int getNumberOfTotalHits() const
Return the number of cdc + svd + pxd + bklm + eklm hits.
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
RelationVector< FROM > getRelationsFrom(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from another store array to this object.
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Type-safe access to single objects in the data store.
static const double cm
Standard units with the value = 1.
Provides BKLM geometry parameters for simulation, reconstruction etc (from Gearbox or DataBase)
const Module * findModule(int section, int sector, int layer) const
Get the pointer to the definition of a module.
double getActiveMiddleRadius(int section, int sector, int layer) const
Get the radial midpoint of the detector module's active volume of specified layer.
Define the geometry of a BKLM module Each sector [octant] contains Modules.
const CLHEP::Hep3Vector globalToLocal(const CLHEP::Hep3Vector &v, bool reco=false) const
Transform space-point within this module from global to local coordinates.
double getPhiStripWidth() const
Get phi-strip width.
const CLHEP::Hep3Vector localToGlobal(const CLHEP::Hep3Vector &v, bool reco=false) const
Transform space-point within this module from local to global coordinates.
double getZStripWidth() const
Get z-strip width.
bool isFlipped() const
Determine if this module is flipped by 180 degrees about z axis within its air gap.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
constexpr T square(const T &x)
Calculate the square of the input.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
double sqrt(double a)
sqrt for double
ExpRunEvt getPosition(const std::vector< Evt > &events, double tEdge)
Get the exp-run-evt number from the event time [hours].
Abstract base class for different kinds of events.