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
71{
72}
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}
156
158{
159 StoreObjPtr<EventMetaData> eventMetaData("EventMetaData", DataStore::c_Event);
160 //unsigned long eventNumber = eventMetaData->getEvent();
161 unsigned long runNumber = eventMetaData->getRun();
162 //unsigned long expNumber = eventMetaData->getExperiment();
163 //StoreArray<RecoTrack> recoTracks;
164 //numRecoTrk = recoTracks.getEntries();
165 //StoreArray<BKLMTrack> bklmtracks;
166 //StoreArray<TrackFitResult> trackFitResults;
167 //StoreArray<MuidHit> muidHits;
168 //StoreArray<Muid> muids;
169 //StoreArray<MCParticle> mcParticles;
170
171 //set<int> m_pointUsed;
172 //m_pointUsed.clear();
173 //check extHits
174 //all ExtHit in bklm scope in each event, should not be many
175 int nExtHit = 0;
176 for (int t = 0; t < extHits.getEntries(); t++) {
177 ExtHit* exthit = extHits[t];
178 if (exthit->getDetectorID() != Const::EDetector::BKLM)
179 continue;
180 m_extx[nExtHit] = exthit->getPosition().X();
181 m_exty[nExtHit] = exthit->getPosition().Y();
182 m_extz[nExtHit] = exthit->getPosition().Z();
183 nExtHit++;
184 if (nExtHit > 199)
185 break;
186 }
187 m_run = runNumber;
188 m_nExtHit = nExtHit;
189
190//the second way, require muid
191 for (int k = 0; k < tracks.getEntries(); k++) {
192 Track* track = tracks[k];
193 // load the muon fit hypothesis or the hypothesis which is the clostes in mass to a muon
194 // the tracking will not always fit a muon hypothesis
195 const TrackFitResult* fitres = track->getTrackFitResultWithClosestMass(Belle2::Const::muon);
196 double mom = fitres->getMomentum().R();
197 //double pt = fitres->getTransverseMomentum();
198 ROOT::Math::XYZVector p3 = fitres->getMomentum();
199 double trkphi = p3.Phi() * TMath::RadToDeg();
200 double trktheta = p3.Theta() * TMath::RadToDeg();
201 if (trkphi < 0)
202 trkphi = trkphi + 360.0;
203 RelationVector<KLMHit2d> relatedHit2D = track->getRelationsTo<KLMHit2d>();
204 RelationVector<ExtHit> relatedExtHit = track->getRelationsTo<ExtHit>();
205 for (unsigned int t = 0; t < relatedExtHit.size(); t++) {
206 ExtHit* exthit = relatedExtHit[t];
207 if (exthit->getDetectorID() != Const::EDetector::BKLM)
208 continue;
209 int module = exthit->getCopyID();
210 int section = BKLMElementNumbers::getSectionByModule(module);
211 int sector = BKLMElementNumbers::getSectorByModule(module);
212 int layer = BKLMElementNumbers::getLayerByModule(module);
213 bool crossed = false; // should be only once ?
214 KLMMuidLikelihood* muid = track->getRelatedTo<KLMMuidLikelihood>();
215 if (!muid)
216 continue;
217 int extPattern = muid->getExtLayerPattern();
218 if ((extPattern & (1 << (layer - 1))) != 0)
219 crossed = true;
220 if (!crossed)
221 continue;
222
223 ROOT::Math::XYZVector extVec = exthit->getPosition();
224 bool matched = false;
225 m_totalYX->Fill(extVec.X(), extVec.Y());
226 m_totalYZ->Fill(extVec.Z(), extVec.Y());
227 float phi = extVec.Phi() * TMath::RadToDeg();
228 float theta = extVec.Theta() * TMath::RadToDeg();
229 if (phi < 0)
230 phi = phi + 360.0;
231 m_totalThephi[layer - 1]->Fill(phi, theta);
232 m_totalTrkThephi[layer - 1]->Fill(trkphi, trktheta);
233 m_totalMom->Fill(mom);
234 //look for mateched BKLM2dHit
235 //for (unsigned int mHit = 0; mHit < relatedHit2D.size(); mHit++) {
236 // KLMHit2d* hit = relatedHit2D[mHit];
237 for (int mHit = 0; mHit < hits2D.getEntries(); mHit++) {
238 KLMHit2d* hit = hits2D[mHit];
239 if (hit->getSubdetector() != KLMElementNumbers::c_BKLM)
240 continue;
241 //if(!hit->inRPC()) continue;
242 if (hit->getSection() != section)
243 continue;
244 if (hit->getSector() != sector)
245 continue;
246 if (hit->getLayer() != layer)
247 continue;
248 ROOT::Math::XYZVector position = hit->getPosition();
249 ROOT::Math::XYZVector distance = extVec - position;
250 //on same track, same sector, same layer, we should believe extHit and KLMHit2d are matched.
251 //let's record the distance to check, should be small
252 m_hdistance->Fill(distance.R());
253 if (distance.R() < 20)
254 matched = true;
255 //m_pointUsed.insert(m);
256 if (matched)
257 break;
258 }
259 if (matched) {
260 m_passYX->Fill(extVec.X(), extVec.Y());
261 m_passYZ->Fill(extVec.Z(), extVec.Y());
262 m_passTrkThephi[layer - 1]->Fill(trkphi, trktheta);
263 m_passThephi[layer - 1]->Fill(phi, theta);
264 m_passMom->Fill(mom);
265 }
266 }//end of loop ext hit
267 }//end of loop tracks
268 m_extTree->Fill();
269}
270
272{
273}
274
276{
277 for (int iL = 0; iL < 15; iL ++) {
278 for (int i = 0; i < m_totalThephi[iL]->GetNbinsX(); i++) {
279 for (int j = 0; j < m_totalThephi[iL]->GetNbinsY(); j++) {
280 float num = m_passThephi[iL]->GetBinContent(i + 1, j + 1);
281 float denom = m_totalThephi[iL]->GetBinContent(i + 1, j + 1);
282 if (num > 0) {
283 m_effiThephi[iL]->SetBinContent(i + 1, j + 1, num / denom);
284 m_effiThephi[iL]->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
285 } else {
286 m_effiThephi[iL]->SetBinContent(i + 1, j + 1, 0);
287 m_effiThephi[iL]->SetBinError(i + 1, j + 1, 0);
288 }
289 }
290 }
291 }
292
293 for (int iL = 0; iL < 15; iL ++) {
294 for (int i = 0; i < m_totalTrkThephi[iL]->GetNbinsX(); i++) {
295 for (int j = 0; j < m_totalTrkThephi[iL]->GetNbinsY(); j++) {
296 float num = m_passTrkThephi[iL]->GetBinContent(i + 1, j + 1);
297 float denom = m_totalTrkThephi[iL]->GetBinContent(i + 1, j + 1);
298 if (num > 0) {
299 m_effiTrkThephi[iL]->SetBinContent(i + 1, j + 1, num / denom);
300 m_effiTrkThephi[iL]->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
301 } else {
302 m_effiTrkThephi[iL]->SetBinContent(i + 1, j + 1, 0);
303 m_effiTrkThephi[iL]->SetBinError(i + 1, j + 1, 0);
304 }
305 }
306 }
307 }
308
309
310 for (int i = 0; i < m_totalYX->GetNbinsX(); i++) {
311 for (int j = 0; j < m_totalYX->GetNbinsY(); j++) {
312 float num = m_passYX->GetBinContent(i + 1, j + 1);
313 float denom = m_totalYX->GetBinContent(i + 1, j + 1);
314 if (num > 0) {
315 m_effiYX->SetBinContent(i + 1, j + 1, num / denom);
316 m_effiYX->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
317 } else {
318 m_effiYX->SetBinContent(i + 1, j + 1, 0);
319 m_effiYX->SetBinError(i + 1, j + 1, 0);
320 }
321
322 num = m_passYZ->GetBinContent(i + 1, j + 1);
323 denom = m_totalYZ->GetBinContent(i + 1, j + 1);
324 if (num > 0) {
325 m_effiYZ->SetBinContent(i + 1, j + 1, num / denom);
326 m_effiYZ->SetBinError(i + 1, j + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
327 } else {
328 m_effiYZ->SetBinContent(i + 1, j + 1, 0);
329 m_effiYZ->SetBinError(i + 1, j + 1, 0);
330 }
331 }
332 }
333
334 for (int i = 0; i < m_totalMom->GetNbinsX(); i++) {
335 float num = m_passMom->GetBinContent(i + 1);
336 float denom = m_totalMom->GetBinContent(i + 1);
337 if (num > 0) {
338 m_effiMom->SetBinContent(i + 1, num / denom);
339 m_effiMom->SetBinError(i + 1, sqrt(num * (denom - num) / (denom * denom * denom)));
340 } else {
341 m_effiMom->SetBinContent(i + 1, 0);
342 m_effiMom->SetBinError(i + 1, 0);
343 }
344 }
345
346 m_file->cd();
347 m_hdistance->Write();
348 m_totalYX->Write();
349 m_passYX->Write();
350 m_totalYZ->Write();
351 m_passYZ->Write();
352 m_effiYX->Write();
353 m_effiYZ->Write();
354 m_totalMom->Write();
355 m_passMom->Write();
356 m_effiMom->Write();
357 for (int i = 0; i < 15; i++) {
358 m_totalThephi[i]->Write();
359 m_passThephi[i]->Write();
360 m_effiThephi[i]->Write();
361 m_totalTrkThephi[i]->Write();
362 m_passTrkThephi[i]->Write();
363 m_effiTrkThephi[i]->Write();
364 }
365 m_extTree->Write();
366 m_file->Close();
367}
368
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
Definition: BKLMAnaModule.h:83
void initialize() override
Initialize at start of job.
float m_extx[200]
keep the global position of validated ExtHit in BKLM
Definition: BKLMAnaModule.h:80
void event() override
This method is called for each event.
void endRun() override
Do any needed actions at the end of a simulation run.
void terminate() override
Terminate at the end of job.
StoreArray< ExtHit > extHits
extHits StoreArray
BKLMAnaModule()
Constructor.
TH2F * m_passThephi[15]
histogram of entries with matched bklmHit2d of extHit vs.theta phi angle of ExtHit
~BKLMAnaModule()
Destructor.
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.
void beginRun() override
Do any needed actions at the start of a simulation run.
TH1F * m_hdistance
bklm GeometryPar
Definition: BKLMAnaModule.h:93
TH1F * m_totalMom
histogram of total entries of extHit vs. track momentum
Definition: BKLMAnaModule.h:96
StoreArray< KLMHit2d > hits2D
hits2D StoreArray
TFile * m_file
output TFile
Definition: BKLMAnaModule.h:62
int m_nExtHit
number of validated ExtHit in BKLM in one event
Definition: BKLMAnaModule.h:77
float m_extz[200]
keep the global position of validated ExtHit in BKLM
Definition: BKLMAnaModule.h:86
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
Definition: BKLMAnaModule.h:65
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.
int m_run
run number
Definition: BKLMAnaModule.h:74
TTree * m_extTree
TTree that holds several variable of interest.
Definition: BKLMAnaModule.h:68
TH1F * m_passMom
histogram of entries with matched bklmHit2d of extHit vs. track momentum
Definition: BKLMAnaModule.h:99
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:660
@ 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
Class to store the likelihoods from KLM with additional information related to the extrapolation.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Class for type safe access to objects that are referred to in 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:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Abstract base class for different kinds of events.