Belle II Software  release-05-01-25
BKLMSimHistogrammerModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Leo Piilonen *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 /* Own header. */
12 #include <klm/bklm/modules/bklmSimHistogrammer/BKLMSimHistogrammerModule.h>
13 
14 /* KLM headers. */
15 #include <klm/dataobjects/bklm/BKLMSimHit.h>
16 #include <klm/dataobjects/KLMDigit.h>
17 
18 /* Belle 2 headers. */
19 #include <framework/dataobjects/EventMetaData.h>
20 #include <framework/datastore/StoreArray.h>
21 #include <framework/datastore/StoreObjPtr.h>
22 
23 /* ROOT headers. */
24 #include <TMath.h>
25 
26 /* C++ headers. */
27 #include <iostream>
28 
29 using namespace std;
30 using namespace Belle2;
31 
32 //-----------------------------------------------------------------
33 // Register the Module
34 //-----------------------------------------------------------------
35 REG_MODULE(BKLMSimHistogrammer)
36 
37 //-----------------------------------------------------------------
38 // Implementation
39 //-----------------------------------------------------------------
40 
42  m_hSimHitPerChannelLayer(nullptr),
43  m_hEvt(nullptr),
44  m_hSimHitPhiRPC(nullptr),
45  m_bgSourcePerLayer(nullptr),
46  m_bgSourcePerLayer2D(nullptr),
47  m_bgSourceVsPhi(nullptr),
48  m_bgSourceVsTheta(nullptr),
49  m_bgSource(nullptr),
50  m_bgSource2D(nullptr),
51  m_hSimHitPhiScinti(nullptr),
52  m_hSimHitThetaRPC(nullptr),
53  m_hSimHitThetaScinti(nullptr),
54  m_hSimHit_layer(nullptr),
55  m_hSimHit_layer2D(nullptr),
56  m_hSimHitThetaPhiRPC(nullptr),
57  m_hSimHitThetaPhiScinti(nullptr),
58  m_file(nullptr),
59  m_weight(0.0)
60 {
61 
62  setDescription("Analyzes bg");
63 
64  addParam("filename", m_filename, "Output root filename", string("bg_output.root"));
65  addParam("realTime", m_realTime, "Time the simulation corresponds to", float(1.0));
66 
67 
68 }
69 
70 BKLMSimHistogrammerModule::~BKLMSimHistogrammerModule()
71 {
72 }
73 
74 void BKLMSimHistogrammerModule::initialize()
75 {
76 
77  hits2D.isRequired();
78  simHits.isRequired();
79 
80 
81  m_file = new TFile(m_filename.c_str(), "recreate");
82 
83  m_hEvt = new TH1D("hEvts", "hEvts", 10001, -1, 10000);
84 
85  m_hSimHitPhiRPC = new TH1D("simhitPhiRPC", "simHitPhiRPC", 100, -1 * TMath::Pi(), 1 * TMath::Pi());
86  m_hSimHitPhiScinti = new TH1D("simhitPhiScinti", "simHitPhiScinti", 100, -1 * TMath::Pi(), 1 * TMath::Pi());
87  m_hSimHitThetaRPC = new TH1D("simhitThetaRPC", "simHitThetaRPC", 100, 0, 1 * TMath::Pi());
88  m_hSimHitThetaScinti = new TH1D("simhitThetaScinti", "simHitThetaScinti", 100, 0, 1 * TMath::Pi());
89 
90  m_hSimHit_layer = new TH1I("simHitLayer", "simHitLayer", 16, 0, 15);
91  m_hSimHit_layer2D = new TH1I("simHitLayer2D", "simHitLayer2D", 16, 0, 15);
92 
93  m_bgSourcePerLayer = new TH2D("bgSourcPerLayer", "bgSourcePerLayer", 21, 0, 20, 16, 0, 15);
94  m_bgSourcePerLayer2D = new TH2D("bgSourcPerLayer2D", "bgSourcePerLayer2D", 21, 0, 20, 16, 0, 15);
95  m_bgSourceVsPhi = new TH2D("bgSourceVsPhi", "bgSourceVsPhi", 100, -1 * TMath::Pi(), TMath::Pi(), 21, 0, 20);
96  m_bgSourceVsTheta = new TH2D("bgSourceVsTheta", "bgSourceVsTheta", 100, 0, TMath::Pi(), 21, 0, 20);
97 
98  m_bgSource = new TH1D("bgSource", "bgSource", 21, 0, 20);
99  m_bgSource2D = new TH1D("bgSource2D", "bgSource2D", 21, 0, 20);
100 
101  m_hSimHitPerChannelLayer = new TH2D("posPerChannelLayer", "posPerChannelLayer", 5000, 0, 5000, 16, 0, 15);
102 
103 
104  // Force creation and persistence of BKLM output datastores
105 }
106 
107 void BKLMSimHistogrammerModule::beginRun()
108 {
109  StoreObjPtr<EventMetaData> evtMetaData;
110  B2INFO("BKLMSimHistogrammer: Experiment " << evtMetaData->getExperiment() << " run " << evtMetaData->getRun());
111 }
112 
113 void BKLMSimHistogrammerModule::event()
114 {
115  //---------------------------------------------
116  // Get BKLM hits collection from the data store
117  //---------------------------------------------
118 
119  int nSimHit = simHits.getEntries();
120 
121  //use 1D hits (otherwise too many simHits, also look up background info), only problem might be that the threshold in simulation is not correct
122 
123 
124  // unsigned int d = 0;
125 
126  if (m_realTime > 0)
127  m_weight = 1.0 / m_realTime;
128 
129  cout << "real time " << m_realTime << " weight: " << m_weight << endl;
130 
131  int n2DHits = hits2D.getEntries();
132  int n1DHits = hits1D.getEntries();
133  cout << "we have " << nSimHit << " sim hits " << n1DHits << " 1D hits " << n2DHits << " 2D hits " << endl;
134 
135  // cout <<"we have " << digits.getEntries() <<" digits " <<endl;
136 
137  m_hEvt->Fill(n2DHits, m_weight);
138  for (int i = 0; i < hits1D.getEntries(); i++) {
139  cout << "looking at 1DHit " << i << endl;
140  int scaledTag = -1;
141  RelationVector<KLMDigit> bklmDigits = hits1D[i]->getRelationsTo<KLMDigit>();
142  for (const auto& bklmDigit : bklmDigits) {
143  RelationVector<BKLMSimHit> relatedSimHits = bklmDigit.getRelationsWith<BKLMSimHit>();
144  for (const auto& simHit : relatedSimHits) {
145  auto bgTag = simHit.getBackgroundTag();
146  scaledTag = bgTag;
147  //other has numeric value of 99
148  if (scaledTag > 20)
149  scaledTag = 20;
150  cout << "scaledTag: " << scaledTag << endl;
151  break;
152 
153 
154 
155 // switch(bgTag)
156 // {
157 // case SimHitBase::bg_none:
158 // cout <<"this is no bg " <<endl;
159 // break;
160 // case SimHitBase::bg_Coulomb_LER:
161 // cout <<"coulomb ler" <<endl;
162 // break;
163 // case SimHitBase::bg_Coulomb_HER:
164 // cout <<"coulomb her" <<endl;
165 // break;
166 //
167 // case SimHitBase::bg_Touschek_LER:
168 // cout <<"touschek ler" <<endl;
169 // break;
170 // case SimHitBase::bg_Touschek_HER:
171 // cout <<"touschek her" <<endl;
172 // break;
173 //
174 // case SimHitBase::bg_RBB_LER:
175 // cout <<"rbb ler" <<endl;
176 // break;
177 //
178 // case SimHitBase::bg_RBB_HER:
179 // cout <<"rbb her" <<endl;
180 // break;
181 //
182 // case SimHitBase::bg_twoPhoton:
183 // cout <<" two phot" <<endl;
184 // break;
185 // case SimHitBase::bg_RBB_gamma:
186 // cout <<"rbb gamma" <<endl;
187 // break;
188 // case SimHitBase::bg_RBB_LER_far:
189 // cout <<"rbb ler far" <<endl;
190 // break;
191 // case SimHitBase::bg_RBB_HER_far:
192 // cout <<"rbb her far" <<endl;
193 // break;
194 // case SimHitBase::bg_Touschek_LER_far:
195 // cout <<"touschek ler far" <<endl;
196 // break;
197 // case SimHitBase::bg_Touschek_HER_far:
198 // cout <<"touschek her far" <<endl;
199 // break;
200 //
201 // case SimHitBase::bg_SynchRad_LER:
202 // cout <<"synchRad ler " <<endl;
203 // break;
204 // case SimHitBase::bg_SynchRad_HER:
205 // cout <<"synchRad her " <<endl;
206 // break;
207 // case SimHitBase::bg_BHWide_LER:
208 // cout <<"bh wide ler " <<endl;
209 // break;
210 // case SimHitBase::bg_BHWide_HER:
211 // cout <<"bh wide her " <<endl;
212 // break;
213 // case SimHitBase::bg_other:
214 // cout <<"bg other " <<endl;
215 // break;
216 //
217 // default:
218 // {
219 // cout <<"this bg code is not allowed! " << bgTag <<endl;
220 // break;
221 // }
222 //
223 // }
224  }
225  break;
226 
227  }
228  int channel = hits1D[i]->getSection() * 840 + hits1D[i]->getSector() * 105 + hits1D[i]->isPhiReadout() * 1680 +
229  hits1D[i]->getStripAve();
230  m_hSimHitPerChannelLayer->Fill(channel, hits1D[i]->getLayer(), m_weight);
231  m_bgSource->Fill(scaledTag, m_weight);
232  m_bgSourcePerLayer->Fill(scaledTag, hits1D[i]->getLayer(), m_weight);
233  cout << "filling layer with tag: " << scaledTag << " and layer " << hits1D[i]->getLayer() << endl;
234  }
235 
236  for (int i = 0; i < hits2D.getEntries(); i++) {
237  int scaledTag = -1;
238  TVector3 gHitPos = hits2D[i]->getGlobalPosition();
239  RelationVector<BKLMHit1d> related1DHits = hits2D[i]->getRelationsTo<BKLMHit1d>();
240  for (const auto& hit1d : related1DHits) {
241  RelationVector<KLMDigit> bklmDigits = hit1d.getRelationsTo<KLMDigit>();
242  for (const auto& bklmDigit : bklmDigits) {
243  RelationVector<BKLMSimHit> relatedSimHits = bklmDigit.getRelationsWith<BKLMSimHit>();
244  for (const auto& simHit : relatedSimHits) {
245  auto bgTag = simHit.getBackgroundTag();
246  scaledTag = bgTag;
247  //other has numeric value of 99
248  if (scaledTag > 20)
249  scaledTag = 20;
250 
251 
252  m_bgSourcePerLayer2D->Fill(scaledTag, hits2D[i]->getLayer(), m_weight);
253  m_bgSource2D->Fill(scaledTag, m_weight);
254  m_bgSourceVsTheta->Fill(gHitPos.Theta(), scaledTag, m_weight);
255  m_bgSourceVsPhi->Fill(gHitPos.Phi(), scaledTag, m_weight);
256  //one bg source is enough
257  break;
258  }
259  break;
260  }
261  break;
262  }
263  }
264 
265 
266 
267 
268 // for(int i=0;i<digits.getEntries();i++)
269 // {
270 // RelationVector<BKLMSimHit> simHits=digits[i]->getRelationsFrom<BKLMSimHit>();
271 // cout <<"digit " << i <<" has " << simHits.size() << " sim Hits associated " <<endl;
272 // }
273 // // RelationVector<BKLMSimHit> bklmSimHits = bklmDigits[i4]->getRelationsFrom<BKLMSimHit>();
274 //
275 // for (int i3 = 0; i3 < n1DHits; i3++) {
276 // RelationVector<BKLMDigit> bklmDigits =
277 // hits1D[i3]->getRelationsTo<BKLMDigit>();
278 // cout <<"the 1Dhit " << i3 << "has "<< bklmDigits.size()<< " digits" <<endl;
279 // int n4 = bklmDigits.size();
280 // for (int i4 = 0; i4 < n4; i4++) {
281 // RelationVector<BKLMSimHit> bklmSimHits =
282 // bklmDigits[i4]->getRelationsFrom<BKLMSimHit>();
283 // cout <<"and this digit is associated with " << bklmSimHits.size()<<" sim hits " <<endl;
284 // // n5 = bklmSimHits.size();
285 // }}
286 //
287 //
288 
289 
290  if (nSimHit == 0) return;
291  for (int i = 0; i < n2DHits; i++) {
292  BKLMHit2d* hit2D = hits2D[i];
293  TVector3 gHitPos = hit2D->getGlobalPosition();
294  if (hit2D->inRPC()) {
295  m_hSimHitPhiRPC->Fill(gHitPos.Phi(), m_weight);
296  m_hSimHitThetaRPC->Fill(gHitPos.Theta(), m_weight);
297  } else {
298  m_hSimHitPhiScinti->Fill(gHitPos.Phi(), m_weight);
299  m_hSimHitThetaScinti->Fill(gHitPos.Theta(), m_weight);
300  }
301 
302 
303 
304  }
305 
306  for (int h = 0; h < nSimHit; ++h) {
307  BKLMSimHit* simHit = simHits[h];
308 
309  RelationVector<MCParticle> bklmMCParticles = simHit->getRelationsFrom<MCParticle>();
310 // cout <<"did " << bklmMCParticles.size() <<" as relation from " <<endl;
311  if (bklmMCParticles.size() > 0) {
312  cout << "found MC particle!" << endl;
313  RelationVector<MCParticle> bklmMCParticlesTo = simHit->getRelationsTo<MCParticle>();
314  cout << "got " << bklmMCParticlesTo.size() << " as relation to " << endl;
315  }
316 
317 
318 
319 // //getMomentum, and getProductionVector getPDG
320  }
321 
322 
323 //
324 //td::map<int, std::vector<std::pair<int, BKLMSimHit*> > > volIDToSimHits;
325 //
326 //
327 // BKLMSimHit* simHit = simHits[h];
328 //
329 // RelationVector<MCParticle> bklmMCParticles =simHit->getRelationsFrom<MCParticle>();
330 // cout <<"did " << bklmMCParticles.size() <<" as relation from " <<endl;
331 //
332 // RelationVector<MCParticle> bklmMCParticlesTo =simHit->getRelationsTo<MCParticle>();
333 // // RelationVector<MCParticle> bklmMCParticlesTo =simHit->getRelationsTo<MCParticle>();
334 // cout <<"got " << bklmMCParticlesTo.size() <<" as relation to " <<endl;
335 // //getMomentum, and getProductionVector getPDG
336 //
337 //
338 // // cout <<" looking at sector: "<< simHit->getSector() << " get layer: " << simHit->getLayer()<<" strip: "<< simHit->getStrip()<<endl;
339 // m_hSimHit_layer->Fill(simHit->getLayer());
340 // // if (simHit->inRPC()) {
341 // {
342 // //get relations
343 // RelationVector<BKLMSimHitPosition> simPositionRelation=simHit->getRelationsTo<BKLMSimHitPosition>();
344 // cout <<" got " << simPositionRelation.size() <<" pos" <<endl;
345 //
346 //
347 //
348 // for(unsigned int iPos=0;iPos<simPositionRelation.size();iPos++)
349 // {
350 // BKLMSimHitPosition *simPos=simPositionRelation[iPos];
351 // if(simPos)
352 // cout <<"found simPos: "<< simPos->getPosition().Phi() <<" theta: "<< simPos->getPosition().Theta()<<endl;
353 // else
354 // cout <<"no sim pos " <<endl;
355 // }
356 //
357 // BKLMSimHitPosition* pos = simHit->getRelatedFrom<BKLMSimHitPosition>();
358 // if (!pos) {
359 // //nothing found, do some error handling here
360 // cout <<"no position found .." <<endl;
361 // }
362 // else
363 // {
364 // cout <<" found pos " << pos->getPosition().Phi() <<" theta: "<< pos->getPosition().Theta() <<endl;
365 // }
366 //
367 //
368 // }
369 //
370 
371 }
372 
373 void BKLMSimHistogrammerModule::endRun()
374 {
375 }
376 
377 void BKLMSimHistogrammerModule::terminate()
378 {
379  m_file->Write();
380  m_file->Close();
381 }
Belle2::RelationVector::size
size_t size() const
Get number of relations.
Definition: RelationVector.h:98
Belle2::BKLMHit1d
Store one reconstructed BKLM 1D hit as a ROOT object.
Definition: BKLMHit1d.h:39
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::BKLMSimHistogrammerModule
Convert BKLM raw simulation hits to digitizations.
Definition: BKLMSimHistogrammerModule.h:52
Belle2::KLMDigit
KLM digit (class representing a digitized hit in RPCs or scintillators).
Definition: KLMDigit.h:40
Belle2::RelationsInterface::getRelationsTo
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Definition: RelationsObject.h:199
Belle2::BKLMHit2d::getGlobalPosition
TVector3 getGlobalPosition(void) const
Get 3D hit position in global coordinates.
Definition: BKLMHit2d.h:187
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::RelationVector
Class for type safe access to objects that are referred to in relations.
Definition: DataStore.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::RelationsInterface::getRelationsFrom
RelationVector< FROM > getRelationsFrom(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from another store array to this object.
Definition: RelationsObject.h:214
Belle2::SimHitBase::getBackgroundTag
virtual unsigned short getBackgroundTag() const
Get background tag.
Definition: SimHitBase.h:56
Belle2::BKLMSimHit
Store one simulation hit as a ROOT object.
Definition: BKLMSimHit.h:35
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::BKLMHit2d::inRPC
bool inRPC() const
Determine whether this 2D hit is in RPC or scintillator.
Definition: BKLMHit2d.h:67
Belle2::BKLMHit2d
Store one BKLM strip hit as a ROOT object.
Definition: BKLMHit2d.h:42