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