Belle II Software development
BeamBkgNeutronModule.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#include <background/modules/BeamBkgNeutron/BeamBkgNeutronModule.h>
10
11#include <time.h>
12
13// Hit classes
14
15#include <simulation/dataobjects/BeamBackHit.h>
16#include <mdst/dataobjects/MCParticle.h>
17#include <pxd/dataobjects/PXDSimHit.h>
18#include <svd/dataobjects/SVDSimHit.h>
19#include <cdc/dataobjects/CDCSimHit.h>
20#include <arich/dataobjects/ARICHSimHit.h>
21#include <top/dataobjects/TOPSimHit.h>
22#include <ecl/dataobjects/ECLSimHit.h>
23#include <klm/dataobjects/KLMSimHit.h>
24#include <simulation/dataobjects/MCParticleTrajectory.h>
25
26// framework - DataStore
27#include <framework/datastore/StoreArray.h>
28#include <framework/datastore/RelationIndex.h>
29#include <framework/datastore/RelationArray.h>
30
31// framework aux
32#include <framework/logging/Logger.h>
33#include <framework/gearbox/Const.h>
34#include <framework/geometry/B2Vector3.h>
35
36using namespace std;
37using namespace boost;
38
39namespace Belle2 {
44 //-----------------------------------------------------------------
46 //-----------------------------------------------------------------
47
48 REG_MODULE(BeamBkgNeutron);
49
50
51 //-----------------------------------------------------------------
52 // Implementation
53 //-----------------------------------------------------------------
54
56 {
57 // Set description()
58 setDescription("BeamBkgNeutronModule module. Used to extract information relevant for the neutron background from background files");
59
60 // Add parameters
61 addParam("FileName", m_filename, "output file name", string("mytree.root"));
62 }
63
67
69 {
70 // create/open the file
71 m_ff = new TFile(m_filename.c_str(), "RECREATE");
72
73 // create trees
74 m_tree1 = new TTree("tree1", "BeamBackHit data");
75 m_tree2 = new TTree("tree2", "SimHits data");
76
77 // create branches
78 m_tree1->Branch("event", &m_iEvent, "event/I");
79 m_tree1->Branch("subDet", &m_subDet, "subDet/I");
80 m_tree1->Branch("iden", &m_iden, "iden/I");
81 m_tree1->Branch("PDG", &m_PDG, "PDG/I");
82 m_tree1->Branch("trackID", &m_trackID, "trackID/I");
83 m_tree1->Branch("momentumX", &m_momentumX, "momentumX/F");
84 m_tree1->Branch("momentumY", &m_momentumY, "momentumY/F");
85 m_tree1->Branch("momentumZ", &m_momentumZ, "momentumZ/F");
86 m_tree1->Branch("positionX", &m_positionX, "positionX/F");
87 m_tree1->Branch("positionY", &m_positionY, "positionY/F");
88 m_tree1->Branch("positionZ", &m_positionZ, "positionZ/F");
89 m_tree1->Branch("E_start", &m_E_start, "E_start/F");
90 m_tree1->Branch("E_end", &m_E_end, "E_end/F");
91 m_tree1->Branch("eDep", &m_eDep, "eDep/F");
92 m_tree1->Branch("trackLength", &m_trackLength, "trackLength/F");
93 m_tree1->Branch("nWeight", &m_nWeight, "nWeight/F");
94 m_tree1->Branch("E_init", &m_E_init, "E_init/F");
95 m_tree1->Branch("mass", &m_mass, "mass/F");
96 m_tree1->Branch("lifeTime", &m_lifeTime, "lifeTime/F");
97 m_tree1->Branch("vtxProdX", &m_vtxProdX, "vtxProdX/F");
98 m_tree1->Branch("vtxProdY", &m_vtxProdY, "vtxProdY/F");
99 m_tree1->Branch("vtxProdZ", &m_vtxProdZ, "vtxProdZ/F");
100 m_tree1->Branch("trj_x", &m_trj_x);
101 m_tree1->Branch("trj_y", &m_trj_y);
102 m_tree1->Branch("trj_z", &m_trj_z);
103 m_tree1->Branch("trj_px", &m_trj_px);
104 m_tree1->Branch("trj_py", &m_trj_py);
105 m_tree1->Branch("trj_pz", &m_trj_pz);
106
107 m_tree2->Branch("nSimHits", m_nSimHits, "nSimHits[13]/I");
108 m_tree2->Branch("hitPDG", m_hitPDG, "hitPDG[13]/I");
109 m_tree2->Branch("momPDG", m_momPDG, "momPDG[13]/I");
110// std::string xyzvector_typedef = "std::vector<ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>,ROOT::Math::DefaultCoordinateSystemTag>>";
111// tt->Branch("vtxProd", "xyzvector_typedef.c_str()", &m_vtxProd);
112 }
113
115 {
116 // Print run number
117 B2INFO("BeamBkgNeutron: Processing. ");
118 }
119
121 {
122 // MCParticles
123 StoreArray<MCParticle> McParticles;
124
125 // SimHits
126 StoreArray<PXDSimHit> PXDSimHits;
127 StoreArray<SVDSimHit> SVDSimHits;
128 StoreArray<CDCSimHit> CDCSimHits;
129 StoreArray<ARICHSimHit> ARICHSimHits;
130 StoreArray<TOPSimHit> TOPSimHits;
131 StoreArray<ECLSimHit> ECLSimHits;
132 StoreArray<KLMSimHit> KLMSimHits;
133
134 for (Int_t i = 0; i < 13; i++) {
135 m_nSimHits[i] = 0;
136 m_hitPDG[i] = 0;
137 m_momPDG[i] = 0;
138 }
139
140 m_nSimHits[1] = PXDSimHits.getEntries();
141 m_nSimHits[2] = SVDSimHits.getEntries();
142 m_nSimHits[3] = CDCSimHits.getEntries();
143 m_nSimHits[4] = ARICHSimHits.getEntries();
144 m_nSimHits[5] = TOPSimHits.getEntries();
145 m_nSimHits[6] = ECLSimHits.getEntries();
146 m_nSimHits[7] = KLMSimHits.getEntries();
147
148 // loop over KLM simHits
149 for (Int_t hit = 0; hit < m_nSimHits[7]; hit++) {
150 // get KLMSimHit
151// EKLMSimHit* simHit = EKLMSimHits[hit];
152 KLMSimHit* simHit = KLMSimHits[hit];
153 Float_t posZ = simHit->getPositionZ();
154 if (280.0 < posZ && posZ < 288.0) m_nSimHits[9]++; // FWD EKLM innermost layer
155 else if (400.0 < posZ && posZ < 406.0) m_nSimHits[10]++; // FWD EKLM outermost layer
156 else if (-194.0 < posZ && posZ < -186.0) m_nSimHits[11]++; // BWD EKLM innermost layer
157 else if (-294.0 < posZ && posZ < -286.0) m_nSimHits[12]++; // BWD EKLM outermost layer
158 }
159
160 RelationIndex<MCParticle, PXDSimHit> relPXDSimHitToMCParticle(McParticles, PXDSimHits);
161 RelationIndex<MCParticle, SVDSimHit> relSVDSimHitToMCParticle(McParticles, SVDSimHits);
162 RelationIndex<MCParticle, CDCSimHit> relCDCSimHitToMCParticle(McParticles, CDCSimHits);
163 RelationIndex<MCParticle, ARICHSimHit> relARICHSimHitToMCParticle(McParticles, ARICHSimHits);
164 RelationIndex<MCParticle, TOPSimHit> relTOPSimHitToMCParticle(McParticles, TOPSimHits);
165 RelationIndex<MCParticle, ECLSimHit> relECLSimHitToMCParticle(McParticles, ECLSimHits);
166 RelationIndex<MCParticle, KLMSimHit> relKLMSimHitToMCParticle(McParticles, KLMSimHits);
167
168 Int_t detID;
169
170 //--- PXD
171 detID = 1;
172 // loop over simhits
173 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
174 PXDSimHit* simHit = PXDSimHits[iHit];
175 // get related MCparticle
176 if (relPXDSimHitToMCParticle.getFirstElementTo(simHit)) {
177 const MCParticle* currParticle = relPXDSimHitToMCParticle.getFirstElementTo(simHit)->from;
178 m_hitPDG[detID] = currParticle->getPDG();
179 if (!currParticle->isPrimaryParticle()) {
180 const MCParticle* momParticle = currParticle->getMother();
181 m_momPDG[detID] = momParticle->getPDG();
182 }
183 }
184 }
185
186 //--- SVD
187 detID = 2;
188 // loop over simhits
189 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
190 SVDSimHit* simHit = SVDSimHits[iHit];
191 // get related MCparticle
192 if (relSVDSimHitToMCParticle.getFirstElementTo(simHit)) {
193 const MCParticle* currParticle = relSVDSimHitToMCParticle.getFirstElementTo(simHit)->from;
194 m_hitPDG[detID] = currParticle->getPDG();
195 if (!currParticle->isPrimaryParticle()) {
196 const MCParticle* momParticle = currParticle->getMother();
197 m_momPDG[detID] = momParticle->getPDG();
198 }
199 }
200 }
201
202 //--- CDC
203 detID = 3;
204 // loop over simhits
205 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
206 CDCSimHit* simHit = CDCSimHits[iHit];
207 // get related MCparticle
208 if (relCDCSimHitToMCParticle.getFirstElementTo(simHit)) {
209 const MCParticle* currParticle = relCDCSimHitToMCParticle.getFirstElementTo(simHit)->from;
210 m_hitPDG[detID] = currParticle->getPDG();
211 if (!currParticle->isPrimaryParticle()) {
212 const MCParticle* momParticle = currParticle->getMother();
213 m_momPDG[detID] = momParticle->getPDG();
214 }
215 }
216 }
217
218 //--- ARICH
219 detID = 4;
220 // loop over simhits
221 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
222 ARICHSimHit* simHit = ARICHSimHits[iHit];
223 // get related MCparticle
224 if (relARICHSimHitToMCParticle.getFirstElementTo(simHit)) {
225 const MCParticle* currParticle = relARICHSimHitToMCParticle.getFirstElementTo(simHit)->from;
226 m_hitPDG[detID] = currParticle->getPDG();
227 if (!currParticle->isPrimaryParticle()) {
228 const MCParticle* momParticle = currParticle->getMother();
229 m_momPDG[detID] = momParticle->getPDG();
230 }
231 }
232 }
233
234 //--- TOP
235 detID = 5;
236 // loop over simhits
237 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
238 TOPSimHit* simHit = TOPSimHits[iHit];
239 // get related MCparticle
240 if (relTOPSimHitToMCParticle.getFirstElementTo(simHit)) {
241 const MCParticle* currParticle = relTOPSimHitToMCParticle.getFirstElementTo(simHit)->from;
242 m_hitPDG[detID] = currParticle->getPDG();
243 if (!currParticle->isPrimaryParticle()) {
244 const MCParticle* momParticle = currParticle->getMother();
245 m_momPDG[detID] = momParticle->getPDG();
246 }
247 }
248 }
249
250 //--- ECL
251 detID = 6;
252 // loop over simhits
253 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
254 ECLSimHit* simHit = ECLSimHits[iHit];
255 // get related MCparticle
256 if (relECLSimHitToMCParticle.getFirstElementTo(simHit)) {
257 const MCParticle* currParticle = relECLSimHitToMCParticle.getFirstElementTo(simHit)->from;
258 m_hitPDG[detID] = currParticle->getPDG();
259 if (!currParticle->isPrimaryParticle()) {
260 const MCParticle* momParticle = currParticle->getMother();
261 m_momPDG[detID] = momParticle->getPDG();
262 }
263 }
264 }
265 //--- KLM
266 detID = 7;
267 // loop over simhits
268 for (Int_t iHit = 0; iHit < m_nSimHits[detID]; iHit++) {
269 KLMSimHit* simHit = KLMSimHits[iHit];
270 // get related MCparticle
271 if (relKLMSimHitToMCParticle.getFirstElementTo(simHit)) {
272 const MCParticle* currParticle = relKLMSimHitToMCParticle.getFirstElementTo(simHit)->from;
273 m_hitPDG[detID] = currParticle->getPDG();
274 if (!currParticle->isPrimaryParticle()) {
275 const MCParticle* momParticle = currParticle->getMother();
276 m_momPDG[detID] = momParticle->getPDG();
277 }
278 }
279 }
280
281 // fill the tree
282 m_tree2->Fill();
283
284 // BeamBkgHits
285 StoreArray<BeamBackHit> BeamBackHits;
286 RelationIndex<MCParticle, BeamBackHit> relBeamBackHitToMCParticle(McParticles, BeamBackHits);
287
288 Int_t nHits = BeamBackHits.getEntries();
289
290 // loop over bkgHits
291 for (Int_t iHit = 0; iHit < nHits; iHit++) {
292 // get one bkgHit
293 BeamBackHit* bkgHit = BeamBackHits[iHit];
294 if (bkgHit->getPDG() == 2112) { /*neutron*/
296 m_iden = bkgHit->getIdentifier();
297 m_subDet = bkgHit->getSubDet();
298 m_PDG = bkgHit->getPDG();
299 m_trackID = bkgHit->getTrackID();
300 auto position = bkgHit->getPosition();
301 m_positionX = position.X();
302 m_positionY = position.Y();
303 m_positionZ = position.Z();
304 auto momentum = bkgHit->getMomentum();
305 m_momentumX = momentum.X();
306 m_momentumY = momentum.Y();
307 m_momentumZ = momentum.Z();
308 m_E_start = bkgHit->getEnergy();
309 m_E_end = bkgHit->getEnergyAtExit();
310 m_eDep = bkgHit->getEnergyDeposit();
311 m_trackLength = bkgHit->getTrackLength();
312 m_nWeight = bkgHit->getNeutronWeight();
313
314 m_trj_x.clear();
315 m_trj_y.clear();
316 m_trj_z.clear();
317 m_trj_px.clear();
318 m_trj_py.clear();
319 m_trj_pz.clear();
320
321 // get related MCparticle
322 if (relBeamBackHitToMCParticle.getFirstElementTo(bkgHit)) {
323 const MCParticle* currParticle = relBeamBackHitToMCParticle.getFirstElementTo(bkgHit)->from;
324 auto m_vtxProd = currParticle->getVertex();
325 m_E_init = currParticle->getEnergy();
326 m_mass = currParticle->getMass();
327 m_lifeTime = currParticle->getLifetime();
328 m_vtxProdX = m_vtxProd.X();
329 m_vtxProdY = m_vtxProd.Y();
330 m_vtxProdZ = m_vtxProd.Z();
331
332 // get trajectory stored for this particle
333 const auto mcTrajectories = currParticle->getRelationsTo<MCParticleTrajectory>();
334 for (auto rel : mcTrajectories.relations()) {
335 // the trajectories with negative weight are from secondary daughters which were ignored so we don't use them
336 if (rel.weight <= 0) continue;
337 // get the trajectory
338 const MCParticleTrajectory& trajectory = dynamic_cast<const MCParticleTrajectory&>(*rel.object);
339 // loop over each point
340 for (const MCTrajectoryPoint& pt : trajectory) {
341 m_trj_x.push_back(pt.x);
342 m_trj_y.push_back(pt.y);
343 m_trj_z.push_back(pt.z);
344 m_trj_px.push_back(pt.px);
345 m_trj_py.push_back(pt.py);
346 m_trj_pz.push_back(pt.pz);
347 }
348 }
349 }
350
351 // fill the tree
352 m_tree1->Fill();
353 }
354 }
355 // increase the entry counter
356 m_iEntry++;
357 }
358
362
364 {
365 // CPU time end
366
367 // Announce
368 B2INFO("BeamBkgNeutron finished.");
369
370 // write
371 m_ff->cd();
372 m_tree1->Write();
373 m_tree2->Write();
374 // close the tree
375 m_ff->Close();
376 }
377
379} // end Belle2 namespace
Class ARICHSimHit - Geant4 simulated hit for ARICH.
Definition ARICHSimHit.h:29
Class BeamBackHit - Stores hits from beam background simulation.
Definition BeamBackHit.h:28
double getNeutronWeight() const
get the effective neutron weight
double getEnergy() const
Get energy of the particle.
int getTrackID() const
the traci ID of the particle
Definition BeamBackHit.h:92
double getEnergyDeposit() const
Get particle energy deposit in sensitive volume.
double getEnergyAtExit() const
Get energy of the particle.
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
ROOT::Math::XYZVector getMomentum() const
Get momentum of the particle hit.
ROOT::Math::XYZVector getPosition() const
Get global position of the particle hit.
Definition BeamBackHit.h:95
int getIdentifier() const
Get the identifier of subdetector component in which hit occurred.
Definition BeamBackHit.h:83
int getSubDet() const
Det the index of subdetector in which hit occurred.
Definition BeamBackHit.h:86
Float_t m_mass
McParticle mass [GeV].
std::vector< Float_t > m_trj_pz
Z component of trajectory momentum.
Float_t m_lifeTime
McParticle lifetime [ns].
Float_t m_E_init
McParticle energy [GeV].
Float_t m_vtxProdX
McParticle prod.
Int_t m_iEntry
Entry identifier.
std::vector< Float_t > m_trj_py
Y component of trajectory momentum.
Float_t m_momentumY
Y component of momentum.
std::vector< Float_t > m_trj_z
Z component of trajectory position.
std::vector< Float_t > m_trj_y
Y component of trajectory position.
Float_t m_positionY
Y component of position.
Float_t m_positionX
X component of position.
Float_t m_vtxProdZ
McParticle prod.
Float_t m_vtxProdY
McParticle prod.
Float_t m_momentumZ
Z component of momentum.
std::vector< Float_t > m_trj_x
X component of trajectory position.
Float_t m_E_start
Starting energy.
Int_t m_nSimHits[13]
Array with number of SimHits.
std::string m_filename
Output file name.
Int_t m_momPDG[13]
Array with PDG momentum.
Int_t m_hitPDG[13]
Array with PDG hits.
Float_t m_eDep
Deposited energy.
Int_t m_iEvent
Event identifier.
TFile * m_ff
Output root file.
Float_t m_positionZ
Z component of position.
std::vector< Float_t > m_trj_px
X component of trajectory momentum.
Float_t m_momentumX
X component of momentum.
Example Detector.
Definition CDCSimHit.h:21
ClassECLSimHit - Geant4 simulated hit for the ECL.
Definition ECLSimHit.h:29
KLM simulation hit.
Definition KLMSimHit.h:31
float getPositionZ() const
Get hit global position z coordinate.
Definition KLMSimHit.h:405
Class to save the full simulated trajectory of a particle.
A Class to store the Monte Carlo particle information.
Definition MCParticle.h:32
float getEnergy() const
Return particle energy in GeV.
Definition MCParticle.h:136
float getLifetime() const
Return the lifetime in ns.
Definition MCParticle.h:166
float getMass() const
Return the particle mass in GeV.
Definition MCParticle.h:124
ROOT::Math::XYZVector getVertex() const
Return production vertex position, shorthand for getProductionVertex().
Definition MCParticle.h:172
int getPDG() const
Return PDG code of particle.
Definition MCParticle.h:101
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
Class PXDSimHit - Geant4 simulated hit for the PXD.
Definition PXDSimHit.h:24
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
const Element * getFirstElementTo(const TO &to) const
Return a pointer to the first relation Element of the given object.
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Class SVDSimHit - Geant4 simulated hit for the SVD.
Definition SVDSimHit.h:26
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 simulated hits of Cherenkov photons on PMT's input for digitization module (TOPDigitiz...
Definition TOPSimHit.h:27
virtual void event()
Event processor.
virtual void initialize()
Initialize the Module.
virtual void beginRun()
Called when entering a new run.
virtual void terminate()
Termination action.
virtual void endRun()
End-of-run action.
virtual ~BeamBkgNeutronModule()
Destructor.
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition MCParticle.h:590
bool isPrimaryParticle() const
Check if particle is a primary particle which was created by the generator (and not,...
Definition MCParticle.h:585
Abstract base class for different kinds of events.
STL namespace.
Small struct to encode a position/momentum without additional overhead.
const FROM * from
pointer of the element from which the relation points.