10 #include <klm/bklm/modules/bklmTracking/BKLMTrackingModule.h>
13 #include <klm/bklm/geometry/GeometryPar.h>
14 #include <klm/bklm/modules/bklmTracking/BKLMTrackFinder.h>
17 #include <framework/dataobjects/EventMetaData.h>
18 #include <framework/datastore/StoreObjPtr.h>
19 #include <framework/logging/Logger.h>
23 using namespace Belle2::bklm;
24 using namespace CLHEP;
36 m_runTotalEventsWithTracks(0)
38 for (
int i = 0; i < 8; ++i) {
39 m_total[0][i] =
nullptr;
40 m_total[1][i] =
nullptr;
41 m_pass[0][i] =
nullptr;
42 m_pass[1][i] =
nullptr;
43 m_effiVsLayer[0][i] =
nullptr;
44 m_effiVsLayer[1][i] =
nullptr;
46 setDescription(
"Perform standard-alone straight line tracking for BKLM");
47 addParam(
"MatchToRecoTrack", m_MatchToRecoTrack,
"[bool], whether match BKLMTrack to RecoTrack; (default is false)",
false);
48 addParam(
"MaxAngleRequired", m_maxAngleRequired,
49 "[degree], match BKLMTrack to RecoTrack; angle between them is required to be smaller than (default 10)",
double(10.0));
50 addParam(
"fitGlobalBKLMTrack", m_globalFit,
51 "[bool], do the BKLMTrack fitting in global system (multi-sectors track) or local system (sector by sector) (default is false, local sys.)",
53 addParam(
"StudyEffiMode", m_studyEffi,
"[bool], run in efficieny study mode (default is false)",
false);
54 addParam(
"outputName", m_outPath ,
"[string], output file name containing efficiencies plots ",
string(
"bklmEffi.root"));
57 BKLMTrackingModule::~BKLMTrackingModule()
62 void BKLMTrackingModule::initialize()
66 m_storeTracks.registerInDataStore();
67 m_storeTracks.registerRelationTo(hits2D);
68 m_storeTracks.registerRelationTo(recoTracks);
69 recoHitInformation.registerRelationTo(hits2D);
70 hits2D.registerRelationTo(recoTracks);
73 B2INFO(
"BKLMTrackingModule:: this module is running in efficiency study mode!");
75 m_file =
new TFile(m_outPath.c_str(),
"recreate");
77 std::string labelFB[2] = {
"BB",
"BF"};
83 m_totalYX =
new TH2F(
"totalYX",
" denominator Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
84 m_passYX =
new TH2F(
"passYX",
" numerator Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
85 m_totalYZ =
new TH2F(
"totalYZ",
" denominator Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
86 m_passYZ =
new TH2F(
"passYZ",
" numerator Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
87 m_effiYX =
new TH2F(
"effiYX",
" effi. Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
88 m_effiYZ =
new TH2F(
"effiYZ",
" effi. Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
89 m_effiYX->GetXaxis()->SetTitle(
"x (cm)");
90 m_effiYX->GetYaxis()->SetTitle(
"y (cm)");
91 m_effiYZ->GetXaxis()->SetTitle(
"z (cm)");
92 m_effiYZ->GetYaxis()->SetTitle(
"y (cm)");
93 for (
int iF = 0; iF < 2; iF++) {
94 for (
int iS = 0; iS < 8; iS++) {
95 hname.Form(
"effi_%s%i", labelFB[iF].c_str(), iS);
96 m_effiVsLayer[iF][iS] =
new TEfficiency(hname, hname, Nbin, 0, 16);
97 hname.Form(
"total_%s%i", labelFB[iF].c_str(), iS);
98 m_total[iF][iS] =
new TH1F(hname, hname, Nbin, 0, 16);
99 hname.Form(
"pass_%s%i", labelFB[iF].c_str(), iS);
100 m_pass[iF][iS] =
new TH1F(hname, hname, Nbin, 0, 16);
106 void BKLMTrackingModule::beginRun()
109 m_runNumber.push_back((
int)eventMetaData->getRun());
110 m_runTotalEvents = 0;
111 m_runTotalEventsWithTracks = 0;
114 void BKLMTrackingModule::event()
116 m_storeTracks.clear();
117 bool thereIsATrack =
false;
120 runTracking(0, -1, -1, -1);
121 if (m_storeTracks.getEntries() > 0)
122 thereIsATrack =
true;
124 for (
int iSection = 0; iSection < 2; iSection++) {
125 for (
int iSector = 0; iSector < 8; iSector++) {
126 for (
int iLayer = 0; iLayer < 15; iLayer++) {
127 runTracking(1, iSection, iSector , iLayer);
128 if (m_storeTracks.getEntries() > 0)
129 thereIsATrack =
true;
130 generateEffi(iSection, iSector, iLayer);
132 m_storeTracks.clear();
140 m_runTotalEventsWithTracks++;
143 void BKLMTrackingModule::runTracking(
int mode,
int iSection,
int iSector,
int iLayer)
145 m_storeTracks.clear();
156 if (hits2D.getEntries() < 1)
159 for (
int j = 0; j < hits2D.getEntries(); j++) {
160 hits2D[j]->isOnStaTrack(
false);
164 for (
int hi = 0; hi < hits2D.getEntries() - 1; ++hi) {
166 if (mode == 1 && isLayerUnderStudy(iSection, iSector, iLayer, hits2D[hi]))
168 if (mode == 1 && !isSectorUnderStudy(iSection, iSector, hits2D[hi]))
170 if (hits2D[hi]->isOnStaTrack())
172 if (hits2D[hi]->isOutOfTime())
174 for (
int hj = hi + 1; hj < hits2D.getEntries(); ++hj) {
176 if (hits2D[hj]->isOnStaTrack())
178 if (hits2D[hj]->isOutOfTime())
180 if (!m_globalFit && !sameSector(hits2D[hi], hits2D[hj]))
182 if (sameSector(hits2D[hi], hits2D[hj]) && abs(hits2D[hi]->getLayer() - hits2D[hj]->getLayer()) < 3)
185 std::list<BKLMHit2d*> sectorHitList;
189 std::list<BKLMHit2d*> seed;
190 seed.push_back(hits2D[hi]);
191 seed.push_back(hits2D[hj]);
193 for (
int ho = 0; ho < hits2D.getEntries(); ++ho) {
196 if (ho == hi || ho == hj)
198 if (mode == 1 && isLayerUnderStudy(iSection, iSector, iLayer, hits2D[hj]))
200 if (mode == 1 && !isSectorUnderStudy(iSection, iSector, hits2D[hj]))
202 if (hits2D[ho]->isOnStaTrack())
204 if (!m_globalFit && !sameSector(hits2D[ho], hits2D[hi]))
208 if (hits2D[ho]->isOutOfTime())
210 sectorHitList.push_back(hits2D[ho]);
216 if (sectorHitList.size() < 2 || sectorHitList.size() > 60)
219 std::list<BKLMHit2d*> m_hits;
220 if (m_finder->
filter(seed, sectorHitList, m_hits)) {
221 BKLMTrack* m_track = m_storeTracks.appendNew();
230 std::list<BKLMHit2d*>::iterator j;
231 m_hits.sort(sortByLayer);
232 for (j = m_hits.begin(); j != m_hits.end(); ++j) {
233 (*j)->isOnStaTrack(
true);
242 if (m_MatchToRecoTrack) {
243 if (findClosestRecoTrack(m_track, closestTrack)) {
245 for (j = m_hits.begin(); j != m_hits.end(); ++j) {
247 closestTrack->
addBKLMHit((*j), sortingParameter, RecoHitInformation::OriginTrackFinder::c_LocalTrackFinder);
261 void BKLMTrackingModule::endRun()
263 m_totalEvents.push_back(m_runTotalEvents);
264 m_totalEventsWithTracks.push_back(m_runTotalEventsWithTracks);
267 void BKLMTrackingModule::terminate()
269 for (
long unsigned int i = 0; i < m_runNumber.size(); i++) {
270 float ratio = (float)m_totalEventsWithTracks.at(i) / (float)m_totalEvents.at(i);
271 B2INFO(
"BKLMTrackingModule:: run " << m_runNumber.at(i) <<
" --> " << ratio * 100 <<
"% of events has 1+ BKLMTracks");
275 for (
int iF = 0; iF < 2; iF++) {
276 for (
int iS = 0; iS < 8; iS++) {
277 m_effiVsLayer[iF][iS]->Write();
278 m_total[iF][iS]->Write();
279 m_pass[iF][iS]->Write();
283 for (
int i = 0; i < m_totalYX->GetNbinsX(); i++) {
284 for (
int j = 0; j < m_totalYX->GetNbinsY(); j++) {
285 float num = m_passYX->GetBinContent(i + 1, j + 1);
286 float denom = m_totalYX->GetBinContent(i + 1, j + 1);
288 m_effiYX->SetBinContent(i + 1, j + 1, num / denom);
289 m_effiYX->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
291 m_effiYX->SetBinContent(i + 1, j + 1, 0);
292 m_effiYX->SetBinError(i + 1, j + 1, 0);
295 num = m_passYZ->GetBinContent(i + 1, j + 1);
296 denom = m_totalYZ->GetBinContent(i + 1, j + 1);
298 m_effiYZ->SetBinContent(i + 1, j + 1, num / denom);
299 m_effiYZ->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
301 m_effiYZ->SetBinContent(i + 1, j + 1, 0);
302 m_effiYZ->SetBinError(i + 1, j + 1, 0);
331 if (bklmHits.
size() < 1) {
332 B2INFO(
"BKLMTrackingModule::something is wrong! there is BKLMTrack but no bklmHits");
335 if (recoTracks.getEntries() < 1) {
336 B2INFO(
"BKLMTrackingModule::there is no recoTrack");
339 double oldDistance = INFINITY;
340 double oldAngle = INFINITY;
341 closestTrack =
nullptr;
343 TVector3 firstBKLMHitPosition(0, 0, 0);
346 firstBKLMHitPosition = bklmHits[0]->getGlobalPosition();
349 TVector3 pos(0, 0, 0);
350 TVector3 mom(0, 0, 0);
356 state.getPosMomCov(pos, mom, cov);
357 if (mom.Y() * pos.Y() < 0)
358 { state = track.getMeasuredStateOnPlaneFromFirstHit(); }
360 const TVector3& distanceVec = firstBKLMHitPosition - pos;
361 state.extrapolateToPoint(firstBKLMHitPosition);
362 double newDistance = distanceVec.Mag2();
367 double angle = trkVec.Angle(mom);
370 if (newDistance < oldDistance) {
371 oldDistance = newDistance;
372 closestTrack = &track;
390 if (oldAngle > m_maxAngleRequired)
396 void BKLMTrackingModule::generateEffi(
int iSection,
int iSector,
int iLayer)
399 set<int> m_pointUsed;
401 if (m_storeTracks.getEntries() < 1)
404 for (
int it = 0; it < m_storeTracks.getEntries(); it++) {
411 for (
const BKLMHit2d& hit2D : relatedHit2D) {
412 if (hit2D.getLayer() > iLayer + 1)
414 if (hit2D.getLayer() < iLayer + 1)
418 if (iLayer != 0 && cnt2 < 1)
420 if (iLayer != 14 && cnt1 < 1)
422 m_GeoPar = GeometryPar::instance();
423 const bklm::Module* module = m_GeoPar->findModule(iSection, iSector + 1, iLayer + 1);
424 int minPhiStrip = module->getPhiStripMin();
425 int maxPhiStrip = module->getPhiStripMax();
426 int minZStrip = module->getZStripMin();
427 int maxZStrip = module->getZStripMax();
429 CLHEP::Hep3Vector local = module->getLocalPosition(minPhiStrip, minZStrip);
430 CLHEP::Hep3Vector local2 = module->getLocalPosition(maxPhiStrip, maxZStrip);
431 float minLocalY, maxLocalY;
432 float minLocalZ, maxLocalZ;
433 if (local[1] > local2[1]) {
434 maxLocalY = local[1];
435 minLocalY = local2[1];
437 maxLocalY = local2[1];
438 minLocalY = local[1];
440 if (local[2] > local2[2]) {
441 maxLocalZ = local[2];
442 minLocalZ = local2[2];
444 maxLocalZ = local2[2];
445 minLocalZ = local[2];
448 TVectorD trkPar = m_storeTracks[it]->getLocalTrackParam();
453 float reflocalX = fabs(m_GeoPar->getActiveMiddleRadius(iSection, iSector + 1,
454 iLayer + 1) - m_GeoPar->getActiveMiddleRadius(iSection, iSector + 1, 1));
458 float reflocalY = trkPar[0] + trkPar[1] * reflocalX;
459 float reflocalZ = trkPar[2] + trkPar[3] * reflocalX;
464 Hep3Vector reflocal(reflocalX, reflocalY, reflocalZ);
466 Hep3Vector global(0, 0, 0);
467 module = m_GeoPar->findModule(iSection, iSector + 1, iLayer + 1);
468 global = module->localToGlobal(reflocal);
470 float localY = module->globalToLocal(global)[1];
471 float localZ = module->globalToLocal(global)[2];
475 if (localY > minLocalY && localY < maxLocalY && localZ > minLocalZ && localZ < maxLocalZ) {
477 bool m_iffound =
false;
478 m_total[iSection][iSector]->Fill(iLayer + 1);
479 m_totalYX->Fill(global[0], global[1]);
480 m_totalYZ->Fill(global[2], global[1]);
482 for (
int he = 0; he < hits2D.getEntries(); ++he) {
483 if (!isLayerUnderStudy(iSection, iSector, iLayer, hits2D[he]))
485 if (hits2D[he]->isOutOfTime())
488 if (m_pointUsed.find(he) != m_pointUsed.end())
492 float distance = distanceToHit(m_storeTracks[it], hits2D[he], error, sigma);
494 if (distance < 10 && sigma < 5)
497 m_pointUsed.insert(he);
501 m_pass[iSection][iSector]->Fill(iLayer + 1);
502 m_passYX->Fill(global[0], global[1]);
503 m_passYZ->Fill(global[2], global[1]);
508 m_effiVsLayer[iSection][iSector]->Fill(m_iffound, iLayer + 1);
528 bool BKLMTrackingModule::isLayerUnderStudy(
int section,
int iSector,
int iLayer,
BKLMHit2d* hit)
530 if (hit->getSection() == section && hit->getSector() == iSector + 1 && hit->getLayer() == iLayer + 1)
535 bool BKLMTrackingModule::isSectorUnderStudy(
int section,
int iSector,
BKLMHit2d* hit)
537 if (hit->getSection() == section && hit->getSector() == iSector + 1)
547 double x, y, z, dy, dz;
552 TVectorD m_SectorPar = track->getLocalTrackParam();
554 m_GeoPar = GeometryPar::instance();
555 const Belle2::bklm::Module* refMod = m_GeoPar->findModule(hit->getSection(), hit->getSector(), 1);
556 const Belle2::bklm::Module* corMod = m_GeoPar->findModule(hit->getSection(), hit->getSector(), hit->getLayer());
558 CLHEP::Hep3Vector globalPos(hit->getGlobalPosition()[0], hit->getGlobalPosition()[1], hit->getGlobalPosition()[2]);
563 y = m_SectorPar[ 0 ] + x * m_SectorPar[ 1 ];
564 z = m_SectorPar[ 2 ] + x * m_SectorPar[ 3 ];
569 double distance = sqrt(dy * dy + dz * dz);
575 error = sqrt(pow(hit_localPhiErr, 2) +
576 pow(hit_localZErr, 2));
579 sigma = distance / error;
Store one BKLM strip hit as a ROOT object.
int getLayer() const
Get layer number.
int getSection() const
Get section number.
int getSector() const
Get sector number.
bool filter(const std::list< BKLMHit2d * > &seed, std::list< BKLMHit2d * > &hits, std::list< BKLMHit2d * > &track)
find associated hits and do fit.
void registerFitter(BKLMTrackFitter *fitter)
Register a fitter if not constructed with one.
void setGlobalFit(bool localOrGlobal)
set the fitting mode, local system or global system
CLHEP::HepVector getTrackParamSector()
Get track parameters in the sector locan system, y = p0 + p1 * x, z = p2 + p3 *x, where the first lay...
float getChi2()
Chi square of the fit.
bool isGood()
Is fit good.
CLHEP::HepSymMatrix getTrackParamSectorErr()
Get invariance matrix of track parameters in the sector local system, where the first layer of the se...
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; y = p2 + p3 * z, if in local sector fit m...
bool isValid()
Is fit valid.
Store one BKLM 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 setLocalTrackParam(const CLHEP::HepVector &trkPar)
Set track parameters in the sector local system, where the first layer of the sector is used as refer...
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 setLocalTrackParamErr(const CLHEP::HepSymMatrix &trkParErr)
Set invariance matrix of track parameters in the sector local system, where the first layer of the se...
void setTrackParam(const CLHEP::HepVector &trkPar)
Set track parameters in the global system. y = p0 + p1 * x; z = p2 + p3 * x.
This module perform straight line track finding and fitting for BKLM.
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.
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< 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.
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.
double getZStripWidth() const
Get z-strip width.
Exception class for error handling in GENFIT (provides storage for diagnostic information)
#StateOnPlane with additional covariance matrix.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.