Belle II Software development
BKLMAnaModule.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 <klm/bklm/modules/bklmAna/BKLMAnaModule.h>
11
12/* Basf2 headers. */
13#include <framework/dataobjects/EventMetaData.h>
14#include <framework/datastore/StoreObjPtr.h>
15#include <mdst/dataobjects/Track.h>
16#include <mdst/dataobjects/TrackFitResult.h>
17#include <klm/dataobjects/KLMMuidLikelihood.h>
18
19/* ROOT headers. */
20#include <TMath.h>
21
22/* CLHEP headers. */
23#include <CLHEP/Units/SystemOfUnits.h>
24
25using namespace Belle2;
26using namespace CLHEP;
27
28//-----------------------------------------------------------------
29// Register the Module
30//-----------------------------------------------------------------
31REG_MODULE(BKLMAna);
32
33//-----------------------------------------------------------------
34// Implementation
35//-----------------------------------------------------------------
36
38 m_file(nullptr),
39 m_extTree(nullptr),
40 m_run(0),
41 m_nExtHit(0),
42 m_hdistance(nullptr),
43 m_totalMom(nullptr),
44 m_passMom(nullptr),
45 m_effiMom(nullptr),
46 m_totalYX(nullptr),
47 m_passYX(nullptr),
48 m_effiYX(nullptr),
49 m_totalYZ(nullptr),
50 m_passYZ(nullptr),
51 m_effiYZ(nullptr)
52{
53 for (int i = 0; i < 200; ++i) {
54 m_extx[i] = 0.0;
55 m_exty[i] = 0.0;
56 m_extz[i] = 0.0;
57 }
58 for (int i = 0; i < 15; ++i) {
59 m_totalThephi[i] = nullptr;
60 m_passThephi[i] = nullptr;
61 m_effiThephi[i] = nullptr;
62 m_totalTrkThephi[i] = nullptr;
63 m_passTrkThephi[i] = nullptr;
64 m_effiTrkThephi[i] = nullptr;
65 }
66 setDescription("analyze bklm efficiency associated to CDC, check performance of bklm et al.");
67 addParam("filename", m_filename, "Output root filename", std::string("bklmana.root"));
68}
69
73
75{
76 hits2D.isRequired();
77 extHits.isRequired();
78 tracks.isRequired();
79
80
81 m_file = new TFile(m_filename.c_str(), "RECREATE");
82
83 m_extTree = new TTree("exthit", "ext hit");
84 m_extTree->Branch("run", &m_run, "m_run/I");
85 m_extTree->Branch("nExtHit", &m_nExtHit, "m_nExtHit/I");
86 m_extTree->Branch("x", &m_extx, "m_extx[m_nExtHit]/F");
87 m_extTree->Branch("y", &m_exty, "m_exty[m_nExtHit]/F");
88 m_extTree->Branch("z", &m_extz, "m_extz[m_nExtHit]/F");
89
90 float phiBins = 36;
91 float phiMin = 0.0;
92 float phiMax = 360.0;
93 float thetaBins = 19;
94 float thetaMin = 35.0;
95 float thetaMax = 130.0;
96 char hname[50];
97 for (int iL = 0; iL < 15 ; iL ++) {
98 //based on theta phi of ExtHit position
99 sprintf(hname, "denominator_Layer%i", iL + 1);
100 m_totalThephi[iL] = new TH2F(hname, hname, phiBins, phiMin, phiMax, thetaBins, thetaMin, thetaMax);
101 m_totalThephi[iL]->GetXaxis()->SetTitle("phi");
102 m_totalThephi[iL]->GetYaxis()->SetTitle("theta");
103 sprintf(hname, "numerator_Layer%i", iL + 1);
104 m_passThephi[iL] = new TH2F(hname, hname, phiBins, phiMin, phiMax, thetaBins, thetaMin, thetaMax);
105 sprintf(hname, "effi_Layer%i", iL + 1);
106 m_passThephi[iL]->GetXaxis()->SetTitle("phi");
107 m_passThephi[iL]->GetYaxis()->SetTitle("theta");
108 m_effiThephi[iL] = new TH2F(hname, hname, phiBins, phiMin, phiMax, thetaBins, thetaMin, thetaMax);
109 m_effiThephi[iL]->GetXaxis()->SetTitle("phi");
110 m_effiThephi[iL]->GetYaxis()->SetTitle("theta");
111 //based on theta phi of trk
112 sprintf(hname, "Denominator_Layer%i", iL + 1);
113 m_totalTrkThephi[iL] = new TH2F(hname, hname, phiBins, phiMin, phiMax, thetaBins, thetaMin, thetaMax);
114 m_totalTrkThephi[iL]->GetXaxis()->SetTitle("trk.phi");
115 m_totalTrkThephi[iL]->GetYaxis()->SetTitle("trk.theta");
116 sprintf(hname, "Numerator_Layer%i", iL + 1);
117 m_passTrkThephi[iL] = new TH2F(hname, hname, phiBins, phiMin, phiMax, thetaBins, thetaMin, thetaMax);
118 sprintf(hname, "Effi_Layer%i", iL + 1);
119 m_passTrkThephi[iL]->GetXaxis()->SetTitle("trk.phi");
120 m_passTrkThephi[iL]->GetYaxis()->SetTitle("trk.theta");
121 m_effiTrkThephi[iL] = new TH2F(hname, hname, phiBins, phiMin, phiMax, thetaBins, thetaMin, thetaMax);
122 m_effiTrkThephi[iL]->GetXaxis()->SetTitle("trk.phi");
123 m_effiTrkThephi[iL]->GetYaxis()->SetTitle("trk.theta");
124 }
125 float gmin = -350;
126 float gmax = 350;
127 int gNbin = 150;
128 float mommin = 0.;
129 float mommax = 15;
130 int mNbin = 30;
131
132 m_hdistance = new TH1F("m_hdistance", " distance between matched extHit and bklmHit2d ", 100, 0, 30);
133
134 m_totalMom = new TH1F("m_totalMom", " denominator vs. p", mNbin, mommin, mommax);
135 m_passMom = new TH1F("m_passMom", " numerator vs. p", mNbin, mommin, mommax);
136 m_effiMom = new TH1F("m_effiMom", " effi. vs. p", mNbin, mommin, mommax);
137 m_effiMom->GetXaxis()->SetTitle("p (GeV)");
138 m_effiMom->GetYaxis()->SetTitle("Efficiency (GeV)");
139
140 m_totalYX = new TH2F("m_totalYX", " denominator Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
141 m_passYX = new TH2F("m_passYX", " numerator Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
142 m_totalYZ = new TH2F("m_totalYZ", " denominator Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
143 m_passYZ = new TH2F("m_passYZ", " numerator Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
144 m_effiYX = new TH2F("m_effiYX", " effi. Y vs. X", gNbin, gmin, gmax, gNbin, gmin, gmax);
145 m_effiYZ = new TH2F("m_effiYZ", " effi. Y vs. Z", gNbin, gmin, gmax, gNbin, gmin, gmax);
146 m_effiYX->GetXaxis()->SetTitle("x (cm)");
147 m_effiYX->GetYaxis()->SetTitle("y (cm)");
148 m_effiYZ->GetXaxis()->SetTitle("z (cm)");
149 m_effiYZ->GetYaxis()->SetTitle("y (cm)");
150
151}
152
154{
155 StoreObjPtr<EventMetaData> eventMetaData("EventMetaData", DataStore::c_Event);
156 //unsigned long eventNumber = eventMetaData->getEvent();
157 unsigned long runNumber = eventMetaData->getRun();
158 //unsigned long expNumber = eventMetaData->getExperiment();
159 //StoreArray<RecoTrack> recoTracks;
160 //numRecoTrk = recoTracks.getEntries();
161 //StoreArray<BKLMTrack> bklmtracks;
162 //StoreArray<TrackFitResult> trackFitResults;
163 //StoreArray<MuidHit> muidHits;
164 //StoreArray<Muid> muids;
165 //StoreArray<MCParticle> mcParticles;
166
167 //set<int> m_pointUsed;
168 //m_pointUsed.clear();
169 //check extHits
170 //all ExtHit in bklm scope in each event, should not be many
171 int nExtHit = 0;
172 for (int t = 0; t < extHits.getEntries(); t++) {
173 const ExtHit* exthit = extHits[t];
174 if (exthit->getDetectorID() != Const::EDetector::BKLM)
175 continue;
176 m_extx[nExtHit] = exthit->getPosition().X();
177 m_exty[nExtHit] = exthit->getPosition().Y();
178 m_extz[nExtHit] = exthit->getPosition().Z();
179 nExtHit++;
180 if (nExtHit > 199)
181 break;
182 }
183 m_run = runNumber;
184 m_nExtHit = nExtHit;
185
186//the second way, require muid
187 for (int k = 0; k < tracks.getEntries(); k++) {
188 Track* track = tracks[k];
189 // load the muon fit hypothesis or the hypothesis which is the clostes in mass to a muon
190 // the tracking will not always fit a muon hypothesis
191 const TrackFitResult* fitres = track->getTrackFitResultWithClosestMass(Belle2::Const::muon);
192 double mom = fitres->getMomentum().R();
193 //double pt = fitres->getTransverseMomentum();
194 ROOT::Math::XYZVector p3 = fitres->getMomentum();
195 double trkphi = p3.Phi() * TMath::RadToDeg();
196 double trktheta = p3.Theta() * TMath::RadToDeg();
197 if (trkphi < 0)
198 trkphi = trkphi + 360.0;
199 RelationVector<ExtHit> relatedExtHit = track->getRelationsTo<ExtHit>();
200 for (unsigned int t = 0; t < relatedExtHit.size(); t++) {
201 const ExtHit* exthit = relatedExtHit[t];
202 if (exthit->getDetectorID() != Const::EDetector::BKLM)
203 continue;
204 int module = exthit->getCopyID();
205 int section = BKLMElementNumbers::getSectionByModule(module);
206 int sector = BKLMElementNumbers::getSectorByModule(module);
207 int layer = BKLMElementNumbers::getLayerByModule(module);
208 bool crossed = false; // should be only once ?
209 const KLMMuidLikelihood* muid = track->getRelatedTo<KLMMuidLikelihood>();
210 if (!muid)
211 continue;
212 int extPattern = muid->getExtLayerPattern();
213 if ((extPattern & (1 << (layer - 1))) != 0)
214 crossed = true;
215 if (!crossed)
216 continue;
217
218 ROOT::Math::XYZVector extVec = exthit->getPosition();
219 bool matched = false;
220 m_totalYX->Fill(extVec.X(), extVec.Y());
221 m_totalYZ->Fill(extVec.Z(), extVec.Y());
222 float phi = extVec.Phi() * TMath::RadToDeg();
223 float theta = extVec.Theta() * TMath::RadToDeg();
224 if (phi < 0)
225 phi = phi + 360.0;
226 m_totalThephi[layer - 1]->Fill(phi, theta);
227 m_totalTrkThephi[layer - 1]->Fill(trkphi, trktheta);
228 m_totalMom->Fill(mom);
229 //look for mateched BKLM2dHit
230 //for (unsigned int mHit = 0; mHit < relatedHit2D.size(); mHit++) {
231 // KLMHit2d* hit = relatedHit2D[mHit];
232 for (int mHit = 0; mHit < hits2D.getEntries(); mHit++) {
233 const KLMHit2d* hit = hits2D[mHit];
235 continue;
236 //if(!hit->inRPC()) continue;
237 if (hit->getSection() != section)
238 continue;
239 if (hit->getSector() != sector)
240 continue;
241 if (hit->getLayer() != layer)
242 continue;
243 ROOT::Math::XYZVector position = hit->getPosition();
244 ROOT::Math::XYZVector distance = extVec - position;
245 //on same track, same sector, same layer, we should believe extHit and KLMHit2d are matched.
246 //let's record the distance to check, should be small
247 m_hdistance->Fill(distance.R());
248 if (distance.R() < 20)
249 matched = true;
250 //m_pointUsed.insert(m);
251 if (matched)
252 break;
253 }
254 if (matched) {
255 m_passYX->Fill(extVec.X(), extVec.Y());
256 m_passYZ->Fill(extVec.Z(), extVec.Y());
257 m_passTrkThephi[layer - 1]->Fill(trkphi, trktheta);
258 m_passThephi[layer - 1]->Fill(phi, theta);
259 m_passMom->Fill(mom);
260 }
261 }//end of loop ext hit
262 }//end of loop tracks
263 m_extTree->Fill();
264}
265
267{
268 for (int iL = 0; iL < 15; iL ++) {
269 for (int i = 0; i < m_totalThephi[iL]->GetNbinsX(); i++) {
270 for (int j = 0; j < m_totalThephi[iL]->GetNbinsY(); j++) {
271 float num = m_passThephi[iL]->GetBinContent(i + 1, j + 1);
272 float denom = m_totalThephi[iL]->GetBinContent(i + 1, j + 1);
273 if (num > 0) {
274 m_effiThephi[iL]->SetBinContent(i + 1, j + 1, num / denom);
275 m_effiThephi[iL]->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
276 } else {
277 m_effiThephi[iL]->SetBinContent(i + 1, j + 1, 0);
278 m_effiThephi[iL]->SetBinError(i + 1, j + 1, 0);
279 }
280 }
281 }
282 }
283
284 for (int iL = 0; iL < 15; iL ++) {
285 for (int i = 0; i < m_totalTrkThephi[iL]->GetNbinsX(); i++) {
286 for (int j = 0; j < m_totalTrkThephi[iL]->GetNbinsY(); j++) {
287 float num = m_passTrkThephi[iL]->GetBinContent(i + 1, j + 1);
288 float denom = m_totalTrkThephi[iL]->GetBinContent(i + 1, j + 1);
289 if (num > 0) {
290 m_effiTrkThephi[iL]->SetBinContent(i + 1, j + 1, num / denom);
291 m_effiTrkThephi[iL]->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
292 } else {
293 m_effiTrkThephi[iL]->SetBinContent(i + 1, j + 1, 0);
294 m_effiTrkThephi[iL]->SetBinError(i + 1, j + 1, 0);
295 }
296 }
297 }
298 }
299
300
301 for (int i = 0; i < m_totalYX->GetNbinsX(); i++) {
302 for (int j = 0; j < m_totalYX->GetNbinsY(); j++) {
303 float num = m_passYX->GetBinContent(i + 1, j + 1);
304 float denom = m_totalYX->GetBinContent(i + 1, j + 1);
305 if (num > 0) {
306 m_effiYX->SetBinContent(i + 1, j + 1, num / denom);
307 m_effiYX->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
308 } else {
309 m_effiYX->SetBinContent(i + 1, j + 1, 0);
310 m_effiYX->SetBinError(i + 1, j + 1, 0);
311 }
312
313 num = m_passYZ->GetBinContent(i + 1, j + 1);
314 denom = m_totalYZ->GetBinContent(i + 1, j + 1);
315 if (num > 0) {
316 m_effiYZ->SetBinContent(i + 1, j + 1, num / denom);
317 m_effiYZ->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
318 } else {
319 m_effiYZ->SetBinContent(i + 1, j + 1, 0);
320 m_effiYZ->SetBinError(i + 1, j + 1, 0);
321 }
322 }
323 }
324
325 for (int i = 0; i < m_totalMom->GetNbinsX(); i++) {
326 float num = m_passMom->GetBinContent(i + 1);
327 float denom = m_totalMom->GetBinContent(i + 1);
328 if (num > 0) {
329 m_effiMom->SetBinContent(i + 1, num / denom);
330 m_effiMom->SetBinError(i + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
331 } else {
332 m_effiMom->SetBinContent(i + 1, 0);
333 m_effiMom->SetBinError(i + 1, 0);
334 }
335 }
336
337 m_file->cd();
338 m_hdistance->Write();
339 m_totalYX->Write();
340 m_passYX->Write();
341 m_totalYZ->Write();
342 m_passYZ->Write();
343 m_effiYX->Write();
344 m_effiYZ->Write();
345 m_totalMom->Write();
346 m_passMom->Write();
347 m_effiMom->Write();
348 for (int i = 0; i < 15; i++) {
349 m_totalThephi[i]->Write();
350 m_passThephi[i]->Write();
351 m_effiThephi[i]->Write();
352 m_totalTrkThephi[i]->Write();
353 m_passTrkThephi[i]->Write();
354 m_effiTrkThephi[i]->Write();
355 }
356 m_extTree->Write();
357 m_file->Close();
358}
359
TH2F * m_passYZ
Y Z coordinate of extHit with matched bklmHit2d.
TH2F * m_effiYX
associated efficiencies vs glaoble y, x coordinate
float m_exty[200]
keep the global position of validated ExtHit in BKLM
void initialize() override
Initialize at start of job.
float m_extx[200]
keep the global position of validated ExtHit in BKLM
void event() override
This method is called for each event.
void terminate() override
Terminate at the end of job.
StoreArray< ExtHit > extHits
extHits StoreArray
BKLMAnaModule()
Constructor.
~BKLMAnaModule() override
Destructor.
TH2F * m_passThephi[15]
histogram of entries with matched bklmHit2d of extHit vs.theta phi angle of ExtHit
TH2F * m_totalTrkThephi[15]
histogram of total entries of extHit associating to ExtHit of each layer vs. theta phi angle of the t...
TH2F * m_totalYX
Y X coordinate of total entries of extHit that in klm region.
TH1F * m_hdistance
bklm GeometryPar
TH1F * m_totalMom
histogram of total entries of extHit vs. track momentum
StoreArray< KLMHit2d > hits2D
hits2D StoreArray
TFile * m_file
output TFile
int m_nExtHit
number of validated ExtHit in BKLM in one event
float m_extz[200]
keep the global position of validated ExtHit in BKLM
TH2F * m_totalThephi[15]
histogram of total entries of extHit associating to ExtHit of each layer vs.theta phi angle of ExtHit
TH2F * m_effiThephi[15]
efficiency associating to ExtHit of each layer vs.theta phi angle of ExtHit
std::string m_filename
name of the output TFile
TH2F * m_effiYZ
associated efficiencies vs glaoble y, z coordinate
TH1F * m_effiMom
efficiency associating to ExtHit vs. track momentum
StoreArray< Track > tracks
tracks StoreArray
TH2F * m_totalYZ
Y Z coordinate of total entries of extHit that in klm region.
TH2F * m_passTrkThephi[15]
histogram of extHit with matched bklmHit2d of each layer vs. theta phi angle of the track
TH2F * m_effiTrkThephi[15]
efficiency associating to ExtHit of each layer vs. theta phi angle of the track
TH2F * m_passYX
Y X coordinate of extHit with matched bklmHit2d.
TTree * m_extTree
TTree that holds several variable of interest.
TH1F * m_passMom
histogram of entries with matched bklmHit2d of extHit vs. track momentum
static int getSectorByModule(int module)
Get sector number by module identifier.
static int getLayerByModule(int module)
Get layer number by module identifier.
static int getSectionByModule(int module)
Get section number by module identifier.
static const ChargedStable muon
muon particle
Definition Const.h:661
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
Store one Ext hit as a ROOT object.
Definition ExtHit.h:31
int getCopyID() const
Get detector-element ID of sensitive element within detector.
Definition ExtHit.h:125
Const::EDetector getDetectorID() const
Get detector ID of this extrapolation hit.
Definition ExtHit.h:121
ROOT::Math::XYZVector getPosition() const
Get position of this extrapolation hit.
Definition ExtHit.h:144
KLM 2d hit.
Definition KLMHit2d.h:33
int getSubdetector() const
Get subdetector number.
Definition KLMHit2d.h:78
int getLayer() const
Get layer number.
Definition KLMHit2d.h:132
int getSection() const
Get section number.
Definition KLMHit2d.h:96
int getSector() const
Get sector number.
Definition KLMHit2d.h:114
ROOT::Math::XYZVector getPosition() const
Get hit global position.
Definition KLMHit2d.h:315
Class to store the likelihoods from KLM with additional information related to the extrapolation.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
Values of the result of a track fit with a given particle hypothesis.
ROOT::Math::XYZVector getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
Class that bundles various TrackFitResults.
Definition Track.h:25
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.