Belle II Software  release-06-02-00
TOPBackgroundModule.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 include
10 #include <top/modules/TOPBackground/TOPBackgroundModule.h>
11 
12 #include <top/dataobjects/TOPBarHit.h>
13 #include <top/dataobjects/TOPDigit.h>
14 #include <top/dataobjects/TOPSimHit.h>
15 #include <top/geometry/TOPGeometryPar.h>
16 
17 #include <mdst/dataobjects/MCParticle.h>
18 #include <simulation/dataobjects/BeamBackHit.h>
19 
20 // framework - DataStore
21 #include <framework/datastore/DataStore.h>
22 #include <framework/datastore/StoreArray.h>
23 
24 // framework aux
25 #include <framework/logging/Logger.h>
26 #include <framework/gearbox/Const.h>
27 
28 #include <TCanvas.h>
29 #include <TLegend.h>
30 #include <TLine.h>
31 #include <TPad.h>
32 #include <TROOT.h>
33 #include <TStyle.h>
34 
35 #include <geometry/Materials.h>
36 #include <G4Material.hh>
37 
38 using namespace std;
39 using namespace boost;
40 
41 namespace Belle2 {
46  //-----------------------------------------------------------------
47  // Register the Module
48  //-----------------------------------------------------------------
49 
50  REG_MODULE(TOPBackground)
51 
52 
53  //-----------------------------------------------------------------
54  // Implementation
55  //-----------------------------------------------------------------
56 
58  m_rootFile(0),
59  origingamma(0),
60  originpe(0),
61  peflux(0),
62  nflux(0),
63  rdose(0),
64  zdist(0),
65  genergy(0),
66  genergy2(0),
67  zdistg(0),
68  originpt(0),
69  nflux_bar(0),
70  gflux_bar(0),
71  cflux_bar(0),
72  norigin(0),
73  gorigin(0),
74  corigin(0),
75  nprim(0),
76  gprim(0),
77  cprim(0),
78  origin_zx(0),
79  origin_zy(0),
80  prim_zx(0),
81  prim_zy(0),
82  module_occupancy(0),
83  PCBmass(0),
84  PCBarea(0),
85  yearns(0),
86  evtoJ(0),
87  mtoc(0),
88  count(0),
89  count_occ(0),
90  origingamma_x(0),
91  origingamma_y(0),
92  origingamma_z(0),
93  originpe_x(0),
94  originpe_y(0),
95  originpe_z(0)
96  {
97  // Set description()
98  setDescription("A module to analyze beam background simulations regarding TOP");
99 
100  // Add parameters
101  addParam("Type", m_BkgType, "Backgound type" , string("Backgound"));
102  addParam("Output", m_OutputFileName, "Name of the output file",
103  string("Backgound.root"));
104  addParam("TimeOfSimulation", m_TimeOfSimulation,
105  "Real time in micro seconds that corresponds to simulated data", 5.);
106  }
107 
108  TOPBackgroundModule::~TOPBackgroundModule()
109  {
110 
111  }
112 
113  void TOPBackgroundModule::initialize()
114  {
115 
116  // CPU time start
117 
118  // Initializing the output root file
119  m_rootFile = TFile::Open(m_OutputFileName.c_str(), "RECREATE");
120  origingamma = new TTree("origingamma", "tree");
121  originpe = new TTree("originpe", "tree2");
122 
123  origingamma->Branch("x", &origingamma_x);
124  origingamma->Branch("y", &origingamma_y);
125  origingamma->Branch("z", &origingamma_z);
126  originpe->Branch("x", &originpe_x);
127  originpe->Branch("y", &originpe_y);
128  originpe->Branch("z", &originpe_z);
129 
130  peflux = new TH1F("Photoelectron flux", "Photoelectron flux", 33, -11.25, 360);
131  nflux = new TH1F("Neutron flux", "Neutron flux", 33, -11.25, 360);
132  rdose = new TH1F("Radiation dose", "Radiation dose", 33, -11.25, 360);
133  zdist = new TH1F("Photoelectron origin", "Photoelectron origin", 200, -400, 400);
134  genergy = new TH1F("Energy distribution of photons", "Energy distribution of photons", 150, 0, 5);
135  genergy2 = new TH1F("Energy distribution of gammas", "Energy distribution of gammas", 500, 0, 5);
136 
137  zdistg = new TH1F("Photoelectron flux z", "Photoelectron flux Z projection", 800, -400, 400);
138  originpt = new TH1F("P_t of origin electron", "P_t of origin electron", 300, 0.06, 0.14);
139 
140  nflux_bar = new TH2F("Neutron flux on bar", "Neutron flux on bar", 32, -114.8, 211.5, 16, -0, 360);
141  gflux_bar = new TH2F("Gamma flux on bar", "Gamma flux on bar MHz/cm^{2}", 32, -114.8, 211.5, 16, -0, 360);
142  cflux_bar = new TH2F("Charged flux on bar", "Charged flux on bar MHz/cm^{2}", 32, -114.8, 211.5, 16, -0, 360);
143 
144  norigin = new TH1F("neutron(BAR) origin", "neutron(BAR) origin", 200, -400, 400);
145  gorigin = new TH1F("gamma(BAR) origin", "gamma(BAR) origin", 200, -400, 400);
146  corigin = new TH1F("charged(BAR) origin", "charged(BAR) origin", 200, -400, 400);
147 
148  nprim = new TH1F("neutron(BAR) primary", "neutron(BAR) primary", 200, -400, 400);
149  gprim = new TH1F("gamma(BAR) primary", "gamma(BAR) primary", 200, -400, 400);
150  cprim = new TH1F("charged(BAR) primary", "charged(BAR) primary", 200, -400, 400);
151 
152  origin_zx = new TGraph();
153  origin_zy = new TGraph();
154 
155  prim_zx = new TGraph();
156  prim_zy = new TGraph();
157  module_occupancy = new TGraph();
158 
159  origin_zy->SetName("originZY");
160  origin_zx->SetName("originZX");
161  module_occupancy->SetName("occupancy");
162 
163  const auto& geo = TOP::TOPGeometryPar::Instance()->getGeometry()->getFrontEnd();
164  double S1 = geo.getFrontBoardWidth() * geo.getFrontBoardHeight();
165  double S2 = geo.getHVBoardWidth() * geo.getHVBoardLength();
166  double V1 = S1 * geo.getFrontBoardThickness();
167  double V2 = S2 * geo.getHVBoardThickness();
168  G4Material* material1 = geometry::Materials::get(geo.getFrontBoardMaterial());
169  if (!material1) B2FATAL("Material '" << geo.getFrontBoardMaterial() << "' not found");
170  G4Material* material2 = geometry::Materials::get(geo.getHVBoardMaterial());
171  if (!material2) B2FATAL("Material '" << geo.getHVBoardMaterial() << "' not found");
172  double density1 = material1->GetDensity() / CLHEP::kg * CLHEP::cm * CLHEP::cm * CLHEP::cm;
173  double density2 = material2->GetDensity() / CLHEP::kg * CLHEP::cm * CLHEP::cm * CLHEP::cm;
174 
175  PCBarea = S1 + S2; // [cm^2], old value was: 496.725
176  PCBmass = V1 * density1 + V2 * density2; // [kg], old value was: 0.417249
177 
178  yearns = 1.e13;
179  evtoJ = 1.60217653 * 1e-10;
180  mtoc = 1.97530864197531;
181  count = 0;
182  count_occ = 0;
183 
184  origingamma_x = 0;
185  origingamma_y = 0;
186  origingamma_z = 0;
187  originpe_x = 0;
188  originpe_y = 0;
189  originpe_z = 0;
190  }
191 
192  void TOPBackgroundModule::beginRun()
193  {
194  // Print run number
195  B2INFO("TOPBackground: Processing:");
196 
197  }
198 
199  void TOPBackgroundModule::event()
200  {
201  StoreArray<TOPSimHit> topSimhits;
202  StoreArray<MCParticle> mcParticles;
203  StoreArray<TOPDigit> topDigits;
204  StoreArray<TOPBarHit> topTracks;
205 
206  int nHits = topDigits.getEntries();
207  for (int i = 0; i < nHits; i++) {
208  TOPDigit* aDigit = topDigits[i];
209  int barID = aDigit->getModuleID();
210 
211  peflux->AddBinContent(barID * 2, 1. / m_TimeOfSimulation / 32.0);
212 
213  const TOPSimHit* simHit = DataStore::getRelated<TOPSimHit>(aDigit);
214  if (!simHit) continue;
215  int PMTID = simHit->getPmtID();
216 
217  module_occupancy->SetPoint(count_occ, PMTID , barID);
218  count_occ++;
219 
220  genergy->Fill(simHit->getEnergy());
221 
222  const MCParticle* particle = DataStore::getRelated<MCParticle>(simHit);
223 
224  if (particle) {
225  const MCParticle* currParticle = particle;
226 
227  const MCParticle* mother = currParticle->getMother();
228 
229  int mm = 0;
230  while (mother) {
231  const MCParticle* pommother = mother->getMother();
232  if (!pommother) {
233 
234  zdist->Fill(mother->getVertex().Z());
235  zdistg->Fill(mother->getVertex().Z(), 1. / m_TimeOfSimulation / 32.0 / 16.0);
236  originpe_x = mother->getVertex().X();
237  originpe_y = mother->getVertex().Y();
238  originpe_z = mother->getVertex().Z();
239  originpe->Fill();
240  TVector3 momentum = mother->getMomentum();
241  if (m_BkgType.at(m_BkgType.size() - 3) == 'L') momentum.RotateY(0.0415);
242  else if (m_BkgType.at(m_BkgType.size() - 3) == 'H') momentum.RotateY(-0.0415);
243  double px = momentum.X();
244  double py = momentum.Y();
245  double pt = sqrt(px * px + py * py);
246  originpt->Fill(pt);
247  }
248  mother = pommother;
249  mm++;
250  }
251  }
252  }
253 
254 
255  StoreArray<BeamBackHit> beamBackHits;
256  nHits = beamBackHits.getEntries();
257 
258  for (int iHit = 0; iHit < nHits; ++iHit) {
259  BeamBackHit* tophit = beamBackHits[iHit];
260  int subdet = tophit->getSubDet();
261  if (subdet != 5) continue;
262 
263  TVector3 pos = tophit->getPosition();
264  double phi = pos.XYvector().Phi_0_2pi(pos.XYvector().Phi()) / 3.14159265358979 * 180.;
265  int barID = int (phi / 22.5 + 0.5);
266  if (barID == 16) {
267  barID = 0;
268  }
269  barID++;
270 
271 
272  if (tophit->getPDG() == Const::neutron.getPDGCode()) {
273  double w = tophit->getNeutronWeight();
274  double tlen = tophit->getTrackLength();
275 
276  nflux->AddBinContent(barID * 2, w / m_TimeOfSimulation / PCBarea * yearns * tlen / 0.2);
277 
278  } else {
279  double edep = tophit->getEnergyDeposit();
280  rdose->AddBinContent(barID * 2, edep / m_TimeOfSimulation * yearns / PCBmass * evtoJ);
281  }
282  }
283 
284  nHits = topTracks.getEntries();
285  for (int iHit = 0; iHit < nHits; ++iHit) {
286  TOPBarHit* toptrk = topTracks[iHit];
287 
288  int PDG = toptrk->getPDG();
289  int barID = toptrk->getModuleID();
290 
291  if (PDG == Const::neutron.getPDGCode()) {
292  nflux_bar->Fill(toptrk->getPosition().Z(), (barID - 1) * 22.5,
293  1. / 917.65 / m_TimeOfSimulation * yearns * 2.);
294  norigin->Fill(toptrk->getProductionPoint().Z());
295  } else {
296  if (PDG == Const::photon.getPDGCode()) {
297  gflux_bar->Fill(toptrk->getPosition().Z(), (barID - 1) * 22.5,
298  1. / 917.65 / m_TimeOfSimulation * 2.);
299  gorigin->Fill(toptrk->getProductionPoint().Z());
300  genergy2->Fill(toptrk->getMomentum().Mag() * 1000);
301  origin_zx->SetPoint(count, toptrk->getProductionPoint().Z(),
302  toptrk->getProductionPoint().X());
303  origin_zy->SetPoint(count, toptrk->getProductionPoint().Z() / 0.999143,
304  toptrk->getProductionPoint().Y());
305  origingamma_x = toptrk->getProductionPoint().X();
306  origingamma_y = toptrk->getProductionPoint().Y();
307  origingamma_z = toptrk->getProductionPoint().Z();
308  origingamma->Fill();
309  count++;
310 
311  } else {
312  cflux_bar->Fill(toptrk->getPosition().Z(), (barID - 1) * 22.5,
313  1. / 917.65 / m_TimeOfSimulation * 2.);
314  corigin->Fill(toptrk->getProductionPoint().Z());
315  }
316  }
317 
318  }
319  }
320 
321 
322  void TOPBackgroundModule::myprint(TH1F* histo, const char* path, const char* xtit = "", const char* ytit = "", double tresh = 0)
323  {
324 
325  gROOT->Reset();
326  gStyle->SetOptStat("");
327  gStyle->SetOptFit(1111);
328 
329  gStyle->SetCanvasColor(-1);
330  gStyle->SetPadColor(-1);
331  gStyle->SetFrameFillColor(-1);
332  gStyle->SetHistFillColor(-1);
333  gStyle->SetTitleFillColor(-1);
334  gStyle->SetFillColor(-1);
335  gStyle->SetFillStyle(4000);
336  gStyle->SetStatStyle(0);
337  gStyle->SetTitleStyle(0);
338  gStyle->SetCanvasBorderSize(0);
339  gStyle->SetCanvasBorderMode(0);
340  gStyle->SetPadBorderMode(0);
341  // gStyle->SetTitleMode(0);
342  gStyle->SetFrameBorderSize(0);
343  gStyle->SetLegendBorderSize(0);
344  gStyle->SetStatBorderSize(0);
345  gStyle->SetTitleBorderSize(0);
346  //gROOT->ForceStyle();*/
347 
348 
349 
350  TCanvas* c1 = new TCanvas("c1", "", 1920, 1200);
351 
352  double x1 = histo->GetBinLowEdge(1);
353  double nb = histo->GetNbinsX();
354  double bin = histo->GetBinWidth(1);
355  double x2 = x1 + bin * nb;
356 
357  double max = histo->GetBinContent(histo->GetMaximumBin());
358 
359  if (max < tresh) {
360  histo->GetYaxis()->SetRangeUser(0, tresh * 1.1);
361  }
362 
363  TLine* line = new TLine(x1, tresh, x2, tresh);
364  line->SetLineColor(1);
365  line->SetLineWidth(3);
366  line->SetLineStyle(2);
367 
368 
369  histo->SetFillColor(2);
370  histo->SetLineColor(1);
371 
372  gPad->SetTopMargin(0.08);
373  gPad->SetBottomMargin(0.15);
374  gPad->SetGridy();
375 
376  histo->GetXaxis()->SetLabelSize(0.06);
377  histo->GetYaxis()->SetLabelSize(0.06);
378  histo->GetXaxis()->SetTitleSize(0.06);
379  histo->GetYaxis()->SetTitleSize(0.06);
380  histo->GetXaxis()->SetTitle(xtit);
381  histo->GetYaxis()->SetTitle(ytit);
382  histo->GetXaxis()->SetTitleOffset(0.9);
383  histo->GetYaxis()->SetTitleOffset(0.7);
384 
385  histo->Draw();
386 
387  TLegend* leg = new TLegend(0.75, 0.95, 0.90, 1.00);
388  leg->AddEntry(histo, m_BkgType.c_str(), "pf");
389  leg->Draw("SAME");
390  if (tresh > 0.01) {
391  line->Draw("SAME");
392  }
393 
394  c1->Print(path);
395  }
396 
397 
398 
399  void TOPBackgroundModule::endRun()
400  {
401  B2INFO("TOPBackground: Finished:");
402  }
403 
404  void TOPBackgroundModule::terminate()
405  {
406  /*
407  myprint(peflux, ("peflux_" + m_BkgType + ".pdf").c_str(), "#phi", "MHz / PMT", 1.);
408  myprint(zdist, ("zdist_" + m_BkgType + ".pdf").c_str(), "z[cm]", "", 0.0);
409  myprint(nflux, ("nflux_" + m_BkgType + ".pdf").c_str(), "#phi", "neutrons / cm^{2} / year", 0.0);
410  myprint(rdose, ("rdose_" + m_BkgType + ".pdf").c_str(), "#phi", "Gy/year", 0.0);
411  */
412 
413  m_rootFile->cd();
414  origingamma->Write();
415  originpe->Write();
416  peflux->Write();
417  zdist->Write();
418  nflux->Write();
419  rdose->Write();
420  genergy->Write();
421  genergy2->Write();
422  nflux_bar->Write();
423  gflux_bar->Write();
424  origin_zx->Write();
425  origin_zy->Write();
426  module_occupancy->Write();
427  gorigin->Write();
428  norigin->Write();
429  zdistg->Write();
430  originpt->Write();
431  m_rootFile->Close();
432 
433  // Announce
434  B2INFO("TOPBackground finished");
435 
436 
437  }
438 
439  void TOPBackgroundModule::printModuleParams() const
440  {
441 
442  }
443 
445 } // end Belle2 namespace
Class BeamBackHit - Stores hits from beam backgound simulation.
Definition: BeamBackHit.h:28
double getNeutronWeight() const
get the effective neutron weigth
Definition: BeamBackHit.h:111
const TVector3 & getPosition() const
Get global position of the particle hit.
Definition: BeamBackHit.h:90
double getEnergyDeposit() const
Get particle energy deposit in sensitive volume.
Definition: BeamBackHit.h:105
int getPDG() const
Get the lund code of the particle that hit the sensitive area.
Definition: BeamBackHit.h:84
double getTrackLength() const
the length of the track in the volume
Definition: BeamBackHit.h:108
int getSubDet() const
Det the index of subdetector in which hit occured.
Definition: BeamBackHit.h:81
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
Class to store track parameters of incoming MC particles relation to MCParticle filled in top/simulat...
Definition: TOPBarHit.h:26
TVector3 getMomentum() const
Returns impact momentum.
Definition: TOPBarHit.h:115
int getModuleID() const
Returns module ID.
Definition: TOPBarHit.h:85
int getPDG() const
Returns PDG code of particle.
Definition: TOPBarHit.h:91
TVector3 getPosition() const
Returns impact point.
Definition: TOPBarHit.h:103
TVector3 getProductionPoint() const
Returns production point.
Definition: TOPBarHit.h:97
Class to store TOP digitized hits (output of TOPDigitizer or raw data unpacker) relations to TOPSimHi...
Definition: TOPDigit.h:24
int getModuleID() const
Returns module ID.
Definition: TOPDigit.h:270
Class to store simulated hits of Cherenkov photons on PMT's input for digitization module (TOPDigitiz...
Definition: TOPSimHit.h:27
double getEnergy() const
Returns photon energy.
Definition: TOPSimHit.h:108
int getPmtID() const
Returns PMT ID.
Definition: TOPSimHit.h:72
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:582
Abstract base class for different kinds of events.