Belle II Software  release-06-01-15
ARICHBackgroundModule.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 // Own include
9 
10 #include <arich/modules/arichBackground/ARICHBackgroundModule.h>
11 
12 #include <time.h>
13 
14 // Hit classes
15 
16 #include <simulation/dataobjects/BeamBackHit.h>
17 #include <arich/dataobjects/ARICHSimHit.h>
18 #include <arich/dataobjects/ARICHDigit.h>
19 
20 #include <mdst/dataobjects/MCParticle.h>
21 
22 // framework - DataStore
23 #include <framework/datastore/StoreArray.h>
24 #include <framework/datastore/RelationIndex.h>
25 #include <framework/datastore/RelationArray.h>
26 
27 // framework aux
28 #include <framework/logging/Logger.h>
29 #include <framework/gearbox/Const.h>
30 
31 using namespace std;
32 using namespace boost;
33 
34 namespace Belle2 {
39  namespace arich {
40  //-----------------------------------------------------------------
41  // Register the Module
42  //-----------------------------------------------------------------
43 
44  REG_MODULE(ARICHBackground)
45 
46 
47  //-----------------------------------------------------------------
48  // Implementation
49  //-----------------------------------------------------------------
50 
51  ARICHBackgroundModule::ARICHBackgroundModule() : Module(), phpos(TVector3()), phmom(TVector3()),
52  phVtx(TVector3()), phMmom(TVector3()), phMvtx(TVector3()), phPvtx(TVector3()), phPmom(TVector3()),
53  phGMvtx(TVector3()), phGMmom(TVector3()), modOrig(TVector3()), source(0), phPDG(0), phMPDG(0), phPPDG(0),
54  phGMPDG(0), type(0), edep(0.0), ttime(0.0), moduleID(0), phnw(0.0), trlen(0.0), en(0.0), ff(NULL),
55  TrHits(NULL)
56  {
57  // Set description()
58  setDescription("ARICHBackground module. Used to extract information relevant for ARICH background from background files");
59 
60  // Add parameters
61  addParam("FileName", m_filename, "output file name", string("mytree.root"));
62  addParam("BkgTag", m_bkgTag, "background type tag (appended to hits in the output tree", 0);
63 
64  }
65 
66  ARICHBackgroundModule::~ARICHBackgroundModule()
67  {
68 
69  }
70 
71  void ARICHBackgroundModule::initialize()
72  {
73  // Print set parameters
74  printModuleParams();
75 
76  ff = new TFile(m_filename.c_str(), "RECREATE");
77 
78  TrHits = new TTree("TrHits", "tree of arich background hits");
79  TrHits->Branch("source", &source, "source/I");
80  TrHits->Branch("phpos", "TVector3", &phpos);
81  TrHits->Branch("phmom", "TVector3", &phmom);
82  TrHits->Branch("phVtx", "TVector3", &phVtx);
83  TrHits->Branch("phMmom", "TVector3", &phMmom);
84  TrHits->Branch("phMvtx", "TVector3", &phMvtx);
85  TrHits->Branch("phPvtx", "TVector3", &phPvtx);
86  TrHits->Branch("phPmom", "TVector3", &phPmom);
87  TrHits->Branch("phGMvtx", "TVector3", &phGMvtx);
88  TrHits->Branch("phGMmom", "TVector3", &phGMmom);
89  TrHits->Branch("modOrig", "TVector3", &modOrig);
90  TrHits->Branch("phPDG", &phPDG, "phPDG/I");
91  TrHits->Branch("phMPDG", &phMPDG, "phMPDG/I");
92  TrHits->Branch("phPPDG", &phPPDG, "phPPDG/I");
93  TrHits->Branch("phGMPDG", &phGMPDG, "phGMPDG/I");
94  TrHits->Branch("type", &type, "type/I");
95  TrHits->Branch("edep", &edep, "edep/D");
96  TrHits->Branch("ttime", &ttime, "ttime/D");
97  TrHits->Branch("moduleID", &moduleID, "moduleID/I");
98  TrHits->Branch("phnw", &phnw, "phnw/D");
99  TrHits->Branch("trlen", &trlen, "trlen/D");
100  TrHits->Branch("en", &en, "en/D");
101 
102  source = m_bkgTag;
103  }
104 
105  void ARICHBackgroundModule::beginRun()
106  {
107  // Print run number
108  B2INFO("ARICHBackground: Processing. ");
109 
110  }
111 
112  void ARICHBackgroundModule::event()
113  {
114 
115  StoreArray<MCParticle> mcParticles;
116 
117  StoreArray<ARICHSimHit> arichSimHits;
118 
119  StoreArray<BeamBackHit> beamBackHits;
120 
121  StoreArray<ARICHDigit> arichDigits;
122 
123  int nHits = beamBackHits.getEntries();
124 
125  for (int iHit = 0; iHit < nHits; ++iHit) {
126 
127  BeamBackHit* arichhit = beamBackHits[iHit];
128  int subdet = arichhit->getSubDet();
129  if (subdet != 4) continue;
130  type = 0;
131  if (arichhit->getIdentifier() == 1) type = 1;
132  edep = arichhit->getEnergyDeposit();
133  ttime = arichhit->getTime();
134  phPDG = arichhit->getPDG();
135  phpos = arichhit->getPosition();
136  phmom = arichhit->getMomentum();
137  moduleID = m_arichgp->getDetectorPlane().pointSlotID(phpos.X(), phpos.Y());
138  double r = m_arichgp->getDetectorPlane().getSlotR(moduleID);
139  double phi = m_arichgp->getDetectorPlane().getSlotPhi(moduleID);
140  modOrig = TVector3(r * cos(phi), r * sin(phi), 0);
141  en = arichhit->getEnergy();
142 
143  if (phPDG == Const::neutron.getPDGCode()) {
144  phnw = arichhit->getNeutronWeight();
145  trlen = arichhit->getTrackLength();
146  }
147  phVtx = TVector3(0, 0, 0); phMvtx = TVector3(0, 0, 0); phMmom = TVector3(0, 0, 0);
148  phMPDG = -1; phPPDG = -1; phGMPDG = -1;
149  phPvtx = TVector3(0, 0, 0); phPmom = TVector3(0, 0, 0); phGMvtx = TVector3(0, 0, 0); phGMmom = TVector3(0, 0, 0);
150 
151  RelationIndex<MCParticle, BeamBackHit> relBeamBackHitToMCParticle(mcParticles, beamBackHits);
152  if (relBeamBackHitToMCParticle.getFirstElementTo(arichhit)) {
153  const MCParticle* currParticle = relBeamBackHitToMCParticle.getFirstElementTo(arichhit)->from;
154  phVtx = currParticle->getVertex();
155  const MCParticle* mother = currParticle->getMother();
156  int mm = 0;
157  while (mother) {
158  if (mm == 0) {
159  phMPDG = mother->getPDG();
160  phMvtx = mother->getVertex();
161  phMmom = mother->getMomentum();
162  }
163  if (mm == 1) {
164  phGMPDG = mother->getPDG();
165  phGMvtx = mother->getVertex();
166  phGMmom = mother->getMomentum();
167  }
168  const MCParticle* pommother = mother->getMother();
169  if (!pommother) {
170  phPPDG = mother->getPDG();
171  phPvtx = mother->getVertex();
172  phPmom = mother->getMomentum();
173  }
174  mother = pommother;
175  mm++;
176  }
177 
178  }
179  TrHits->Fill();
180  }
181 
182  nHits = arichSimHits.getEntries();
183 
184  for (int iHit = 0; iHit < nHits; ++iHit) {
185  ARICHSimHit* simHit = arichSimHits[iHit];
186  moduleID = simHit->getModuleID();
187  type = 2;
188  phPDG = 0;
189  edep = 0;
190  ttime = simHit->getGlobalTime();
191 
192  phVtx = TVector3(0, 0, 0); phMvtx = TVector3(0, 0, 0); phMmom = TVector3(0, 0, 0);
193  phMPDG = -1; phPPDG = -1; phGMPDG = -1; phmom = TVector3(0, 0, 0);
194  phPvtx = TVector3(0, 0, 0); phPmom = TVector3(0, 0, 0); phGMvtx = TVector3(0, 0, 0); phGMmom = TVector3(0, 0, 0);
195  RelationIndex<MCParticle, ARICHSimHit> relSimHitToMCParticle(mcParticles, arichSimHits);
196  if (relSimHitToMCParticle.getFirstElementTo(simHit)) {
197  const MCParticle* currParticle = relSimHitToMCParticle.getFirstElementTo(simHit)->from;
198  phVtx = currParticle->getVertex();
199  const MCParticle* mother = currParticle->getMother();
200  int mm = 0;
201  while (mother) {
202  if (mm == 0) {
203  phMPDG = mother->getPDG();
204  phMvtx = mother->getVertex();
205  phMmom = mother->getMomentum();
206  }
207  if (mm == 1) {
208  phGMPDG = mother->getPDG();
209  phGMvtx = mother->getVertex();
210  phGMmom = mother->getMomentum();
211  }
212  const MCParticle* pommother = mother->getMother();
213  if (!pommother) {
214  phPPDG = mother->getPDG();
215  phPvtx = mother->getVertex();
216  phPmom = mother->getMomentum();
217  }
218  mother = pommother;
219  mm++;
220  }
221 
222  }
223  TrHits->Fill();
224  }
225  }
226 
227  void ARICHBackgroundModule::endRun()
228  {
229  }
230 
231  void ARICHBackgroundModule::terminate()
232  {
233  // CPU time end
234 
235  // Announce
236  B2INFO("ARICHBackground finished.");
237 
238  //TrHits->Write();
239  ff->Write();
240  ff->Close();
241 
242  }
243 
244  void ARICHBackgroundModule::printModuleParams() const
245  {
246 
247  }
248 
249 
250  } // end arich namespace
252 } // end Belle2 namespace
Class ARICHSimHit - Geant4 simulated hit for ARICH.
Definition: ARICHSimHit.h:29
float getGlobalTime() const override
Get global time of hit.
Definition: ARICHSimHit.h:72
int getModuleID() const
Get ID number of module that registered hit.
Definition: ARICHSimHit.h:66
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 getEnergy() const
Get energy of the particle.
Definition: BeamBackHit.h:99
const TVector3 & getMomentum() const
Get momentum of the particle hit.
Definition: BeamBackHit.h:93
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 getIdentifier() const
Get the identifier of subdetector component in which hit occured.
Definition: BeamBackHit.h:78
int getSubDet() const
Det the index of subdetector in which hit occured.
Definition: BeamBackHit.h:81
double getTime() const
Get the time at which the hit occured.
Definition: BeamBackHit.h:96
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
TVector3 getVertex() const
Return production vertex position, shorthand for getProductionVertex().
Definition: MCParticle.h:183
Base class for Modules.
Definition: Module.h:72
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
Definition: RelationIndex.h:76
const Element * getFirstElementTo(const TO &to) const
Return a pointer to the first relation Element of the given object.
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
#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.
const FROM * from
pointer of the element from which the relation points.