Belle II Software  release-06-02-00
TOPNtupleModule.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/TOPNtuple/TOPNtupleModule.h>
11 
12 #include <top/geometry/TOPGeometryPar.h>
13 
14 // framework - DataStore
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 
18 // framework aux
19 #include <framework/gearbox/Const.h>
20 #include <framework/logging/Logger.h>
21 
22 // DataStore classes
23 #include <framework/dataobjects/EventMetaData.h>
24 #include <framework/dataobjects/MCInitialParticles.h>
25 #include <mdst/dataobjects/Track.h>
26 #include <mdst/dataobjects/MCParticle.h>
27 #include <top/dataobjects/TOPLikelihood.h>
28 #include <top/dataobjects/TOPBarHit.h>
29 #include <tracking/dataobjects/ExtHit.h>
30 
31 // ROOT
32 #include <TDirectory.h>
33 
34 using namespace std;
35 
36 namespace Belle2 {
42  using namespace TOP;
43 
44  //-----------------------------------------------------------------
45  // Register module
46  //-----------------------------------------------------------------
47 
48  REG_MODULE(TOPNtuple)
49 
50  //-----------------------------------------------------------------
51  // Implementation
52  //-----------------------------------------------------------------
53 
55  {
56  // set module description
57  setDescription("Writes ntuple of TOPLikelihoods with tracking info into a root file");
58 
59  // Add parameters
60  addParam("outputFileName", m_outputFileName, "Output file name",
61  string("TOPNtuple.root"));
62 
63  }
64 
65  TOPNtupleModule::~TOPNtupleModule()
66  {
67  }
68 
69  void TOPNtupleModule::initialize()
70  {
71  TDirectory::TContext context;
72  m_file = TFile::Open(m_outputFileName.c_str(), "RECREATE");
73  if (m_file->IsZombie()) {
74  B2FATAL("Couldn't open file '" << m_outputFileName << "' for writing!");
75  return;
76  }
77 
78  m_tree = new TTree("top", "TOP validation ntuple");
79 
80  m_tree->Branch("evt", &m_top.evt, "evt/I");
81  m_tree->Branch("run", &m_top.run, "run/I");
82 
83  m_tree->Branch("p", &m_top.p, "p/F");
84  m_tree->Branch("cth", &m_top.cth, "cth/F");
85  m_tree->Branch("phi", &m_top.phi, "phi/F");
86  m_tree->Branch("pValue", &m_top.pValue, "pValue/F");
87 
88  m_tree->Branch("PDG", &m_top.PDG, "PDG/I");
89  m_tree->Branch("motherPDG", &m_top.motherPDG, "motherPDG/I");
90  m_tree->Branch("primary", &m_top.primary, "primary/S");
91  m_tree->Branch("seen", &m_top.seen, "seen/S");
92  m_tree->Branch("rhoProd", &m_top.rhoProd, "rhoProd/F");
93  m_tree->Branch("zProd", &m_top.zProd, "zProd/F");
94  m_tree->Branch("phiProd", &m_top.phiProd, "phiProd/F");
95  m_tree->Branch("rhoDec", &m_top.rhoDec, "rhoDec/F");
96  m_tree->Branch("zDec", &m_top.zDec, "zDec/F");
97  m_tree->Branch("phiDec", &m_top.phiDec, "phiDec/F");
98 
99  m_tree->Branch("numPhot", &m_top.numPhot, "numPhot/I");
100  m_tree->Branch("numBkg", &m_top.numBkg, "numBkg/F");
101  m_tree->Branch("phot", &m_top.phot, "e/F:mu:pi:K:p:d");
102  m_tree->Branch("logL", &m_top.logL, "e/F:mu:pi:K:p:d");
103 
104  m_tree->Branch("extHit", &m_top.extHit, "moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
105  m_tree->Branch("barHit", &m_top.barHit, "moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
106 
107  StoreArray<Track> tracks;
108  tracks.isRequired();
109  StoreArray<ExtHit> extHits;
110  extHits.isRequired();
111  StoreArray<TOPLikelihood> likelihoods;
112  likelihoods.isRequired();
113  StoreArray<MCParticle> mcParticles;
114  mcParticles.isOptional();
115  StoreArray<TOPBarHit> barHits;
116  barHits.isOptional();
117  StoreObjPtr<MCInitialParticles> mcInitialParticles;
118  mcInitialParticles.isOptional();
119  }
120 
121  void TOPNtupleModule::beginRun()
122  {
123  }
124 
125  void TOPNtupleModule::event()
126  {
127 
128  StoreObjPtr<EventMetaData> evtMetaData;
129  StoreArray<Track> tracks;
130  StoreObjPtr<MCInitialParticles> mcInitialParticles;
131  double trueEventT0 = 0;
132  if (mcInitialParticles.isValid()) trueEventT0 = mcInitialParticles->getTime();
133 
134  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
135 
136  for (const auto& track : tracks) {
137  const auto* trackFit = track.getTrackFitResultWithClosestMass(Const::pion);
138  if (!trackFit) continue;
139  const TOPLikelihood* top = track.getRelated<TOPLikelihood>();
140  if (!top) continue;
141  if (top->getFlag() != 1) continue;
142 
143  const ExtHit* extHit = top->getRelated<ExtHit>();
144  const TOPBarHit* barHit = top->getRelated<TOPBarHit>();
145  const MCParticle* mcParticle = track.getRelated<MCParticle>();
146  const MCParticle* mother = 0;
147  if (mcParticle) mother = mcParticle->getMother();
148 
149  m_top.clear();
150 
151  m_top.evt = evtMetaData->getEvent();
152  m_top.run = evtMetaData->getRun();
153 
154  TVector3 mom = trackFit->getMomentum();
155  m_top.p = mom.Mag();
156  m_top.cth = mom.CosTheta();
157  m_top.phi = mom.Phi();
158  m_top.pValue = trackFit->getPValue();
159 
160  if (mcParticle) {
161  m_top.PDG = mcParticle->getPDG();
162  if (mother) m_top.motherPDG = mother->getPDG();
163  m_top.primary = mcParticle->getStatus(MCParticle::c_PrimaryParticle);
164  m_top.seen = mcParticle->hasSeenInDetector(Const::TOP);
165  TVector3 prodVertex = mcParticle->getProductionVertex();
166  m_top.rhoProd = prodVertex.Perp();
167  m_top.zProd = prodVertex.Z();
168  m_top.phiProd = prodVertex.Phi();
169  TVector3 decVertex = mcParticle->getDecayVertex();
170  m_top.rhoDec = decVertex.Perp();
171  m_top.zDec = decVertex.Z();
172  m_top.phiDec = decVertex.Phi();
173  }
174 
175  m_top.numPhot = top->getNphot();
176  m_top.numBkg = top->getEstBkg();
177  m_top.phot.e = top->getEstPhot(Const::electron);
178  m_top.phot.mu = top->getEstPhot(Const::muon);
179  m_top.phot.pi = top->getEstPhot(Const::pion);
180  m_top.phot.K = top->getEstPhot(Const::kaon);
181  m_top.phot.p = top->getEstPhot(Const::proton);
182  m_top.phot.d = top->getEstPhot(Const::deuteron);
183 
184  m_top.logL.e = top->getLogL(Const::electron);
185  m_top.logL.mu = top->getLogL(Const::muon);
186  m_top.logL.pi = top->getLogL(Const::pion);
187  m_top.logL.K = top->getLogL(Const::kaon);
188  m_top.logL.p = top->getLogL(Const::proton);
189  m_top.logL.d = top->getLogL(Const::deuteron);
190 
191  if (extHit) {
192  int moduleID = extHit->getCopyID();
193  TVector3 position = extHit->getPosition();
194  TVector3 momentum = extHit->getMomentum();
195  if (geo->isModuleIDValid(moduleID)) {
196  const auto& module = geo->getModule(moduleID);
197  position = module.pointToLocal(position);
198  momentum = module.momentumToLocal(momentum);
199  }
200  m_top.extHit.moduleID = moduleID;
201  m_top.extHit.PDG = extHit->getPdgCode();
202  m_top.extHit.x = position.X();
203  m_top.extHit.y = position.Y();
204  m_top.extHit.z = position.Z();
205  m_top.extHit.p = momentum.Mag();
206  m_top.extHit.theta = momentum.Theta();
207  m_top.extHit.phi = momentum.Phi();
208  m_top.extHit.time = extHit->getTOF();
209  }
210 
211  if (barHit) {
212  int moduleID = barHit->getModuleID();
213  TVector3 position = barHit->getPosition();
214  TVector3 momentum = barHit->getMomentum();
215  if (geo->isModuleIDValid(moduleID)) {
216  const auto& module = geo->getModule(moduleID);
217  position = module.pointToLocal(position);
218  momentum = module.momentumToLocal(momentum);
219  }
220  m_top.barHit.moduleID = moduleID;
221  m_top.barHit.PDG = barHit->getPDG();
222  m_top.barHit.x = position.X();
223  m_top.barHit.y = position.Y();
224  m_top.barHit.z = position.Z();
225  m_top.barHit.p = momentum.Mag();
226  m_top.barHit.theta = momentum.Theta();
227  m_top.barHit.phi = momentum.Phi();
228  m_top.barHit.time = barHit->getTime() - trueEventT0;
229  }
230 
231  m_tree->Fill();
232  }
233 
234  }
235 
236 
237  void TOPNtupleModule::endRun()
238  {
239  }
240 
241  void TOPNtupleModule::terminate()
242  {
243  TDirectory::TContext context;
244  m_file->cd();
245  m_tree->Write();
246  m_file->Close();
247  }
248 
250 } // end Belle2 namespace
251 
Store one Ext hit as a ROOT object.
Definition: ExtHit.h:30
int getPdgCode() const
Get PDG code of this extrapolation's hypothesis.
Definition: ExtHit.h:116
TVector3 getMomentum() const
Get momentum at this extrapolation hit.
Definition: ExtHit.h:147
int getCopyID() const
Get detector-element ID of sensitive element within detector.
Definition: ExtHit.h:124
TVector3 getPosition() const
Get position of this extrapolation hit.
Definition: ExtHit.h:143
double getTOF() const
Get time of flight from the point of closest approach near the origin to this hit.
Definition: ExtHit.h:135
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:110
Class to store track parameters of incoming MC particles relation to MCParticle filled in top/simulat...
Definition: TOPBarHit.h:26
Class to store TOP log likelihoods (output of TOPReconstructor).
Definition: TOPLikelihood.h:26
Module to write out a ntuple summarizing TOP reconstruction output.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.