Belle II Software  release-08-01-10
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 header.
10 #include <top/modules/TOPNtuple/TOPNtupleModule.h>
11 
12 // TOP headers.
13 #include <top/geometry/TOPGeometryPar.h>
14 
15 // framework - DataStore
16 #include <framework/datastore/StoreArray.h>
17 #include <framework/datastore/StoreObjPtr.h>
18 
19 // framework aux
20 #include <framework/gearbox/Const.h>
21 #include <framework/logging/Logger.h>
22 
23 // DataStore classes
24 #include <framework/dataobjects/EventMetaData.h>
25 #include <framework/dataobjects/MCInitialParticles.h>
26 #include <mdst/dataobjects/Track.h>
27 #include <mdst/dataobjects/MCParticle.h>
28 #include <top/dataobjects/TOPLikelihood.h>
29 #include <top/dataobjects/TOPBarHit.h>
30 #include <tracking/dataobjects/ExtHit.h>
31 
32 // ROOT
33 #include <TDirectory.h>
34 
35 using namespace std;
36 
37 namespace Belle2 {
43  using namespace TOP;
44 
45  //-----------------------------------------------------------------
47  //-----------------------------------------------------------------
48 
49  REG_MODULE(TOPNtuple);
50 
51  //-----------------------------------------------------------------
52  // Implementation
53  //-----------------------------------------------------------------
54 
55  TOPNtupleModule::TOPNtupleModule() : Module()
56  {
57  // set module description
58  setDescription("Writes ntuple of TOPLikelihoods with tracking info into a root file");
59 
60  // Add parameters
61  addParam("outputFileName", m_outputFileName, "Output file name",
62  string("TOPNtuple.root"));
63 
64  }
65 
67  {
68  }
69 
71  {
72  TDirectory::TContext context;
73  m_file = TFile::Open(m_outputFileName.c_str(), "RECREATE");
74  if (m_file->IsZombie()) {
75  B2FATAL("Couldn't open file '" << m_outputFileName << "' for writing!");
76  return;
77  }
78 
79  m_tree = new TTree("top", "TOP validation ntuple");
80 
81  m_tree->Branch("evt", &m_top.evt, "evt/I");
82  m_tree->Branch("run", &m_top.run, "run/I");
83 
84  m_tree->Branch("p", &m_top.p, "p/F");
85  m_tree->Branch("cth", &m_top.cth, "cth/F");
86  m_tree->Branch("phi", &m_top.phi, "phi/F");
87  m_tree->Branch("pValue", &m_top.pValue, "pValue/F");
88 
89  m_tree->Branch("PDG", &m_top.PDG, "PDG/I");
90  m_tree->Branch("motherPDG", &m_top.motherPDG, "motherPDG/I");
91  m_tree->Branch("primary", &m_top.primary, "primary/S");
92  m_tree->Branch("seen", &m_top.seen, "seen/S");
93  m_tree->Branch("rhoProd", &m_top.rhoProd, "rhoProd/F");
94  m_tree->Branch("zProd", &m_top.zProd, "zProd/F");
95  m_tree->Branch("phiProd", &m_top.phiProd, "phiProd/F");
96  m_tree->Branch("rhoDec", &m_top.rhoDec, "rhoDec/F");
97  m_tree->Branch("zDec", &m_top.zDec, "zDec/F");
98  m_tree->Branch("phiDec", &m_top.phiDec, "phiDec/F");
99 
100  m_tree->Branch("numPhot", &m_top.numPhot, "numPhot/I");
101  m_tree->Branch("numBkg", &m_top.numBkg, "numBkg/F");
102  m_tree->Branch("phot", &m_top.phot, "e/F:mu:pi:K:p:d");
103  m_tree->Branch("logL", &m_top.logL, "e/F:mu:pi:K:p:d");
104 
105  m_tree->Branch("extHit", &m_top.extHit, "moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
106  m_tree->Branch("barHit", &m_top.barHit, "moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
107 
108  StoreArray<Track> tracks;
109  tracks.isRequired();
110  StoreArray<ExtHit> extHits;
111  extHits.isRequired();
112  StoreArray<TOPLikelihood> likelihoods;
113  likelihoods.isRequired();
114  StoreArray<MCParticle> mcParticles;
115  mcParticles.isOptional();
116  StoreArray<TOPBarHit> barHits;
117  barHits.isOptional();
118  StoreObjPtr<MCInitialParticles> mcInitialParticles;
119  mcInitialParticles.isOptional();
120  }
121 
123  {
124  }
125 
127  {
128 
129  StoreObjPtr<EventMetaData> evtMetaData;
130  StoreArray<Track> tracks;
131  StoreObjPtr<MCInitialParticles> mcInitialParticles;
132  double trueEventT0 = 0;
133  if (mcInitialParticles.isValid()) trueEventT0 = mcInitialParticles->getTime();
134 
135  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
136 
137  for (const auto& track : tracks) {
138  const auto* trackFit = track.getTrackFitResultWithClosestMass(Const::pion);
139  if (!trackFit) continue;
140  const TOPLikelihood* top = track.getRelated<TOPLikelihood>();
141  if (!top) continue;
142  if (top->getFlag() != 1) continue;
143 
144  const ExtHit* extHit = top->getRelated<ExtHit>();
145  const TOPBarHit* barHit = top->getRelated<TOPBarHit>();
146  const MCParticle* mcParticle = track.getRelated<MCParticle>();
147  const MCParticle* mother = 0;
148  if (mcParticle) {
149  mother = mcParticle->getMother();
150  if (not barHit) { // Track MC matching probably done after TOPReconstructor so no relation from TOPLikelihood
151  const auto barHits = mcParticle->getRelationsWith<TOPBarHit>();
152  for (const auto& bHit : barHits) {
153  if (bHit.getModuleID() == extHit->getCopyID()) barHit = &bHit;
154  }
155  }
156  }
157 
158  m_top.clear();
159 
160  m_top.evt = evtMetaData->getEvent();
161  m_top.run = evtMetaData->getRun();
162 
163  ROOT::Math::XYZVector mom = trackFit->getMomentum();
164  m_top.p = mom.R();
165  m_top.cth = cos(mom.Theta());
166  m_top.phi = mom.Phi();
167  m_top.pValue = trackFit->getPValue();
168 
169  if (mcParticle) {
170  m_top.PDG = mcParticle->getPDG();
171  if (mother) m_top.motherPDG = mother->getPDG();
172  m_top.primary = mcParticle->getStatus(MCParticle::c_PrimaryParticle);
173  m_top.seen = mcParticle->hasSeenInDetector(Const::TOP);
174  ROOT::Math::XYZVector prodVertex = mcParticle->getProductionVertex();
175  m_top.rhoProd = prodVertex.Rho();
176  m_top.zProd = prodVertex.Z();
177  m_top.phiProd = prodVertex.Phi();
178  ROOT::Math::XYZVector decVertex = mcParticle->getDecayVertex();
179  m_top.rhoDec = decVertex.Rho();
180  m_top.zDec = decVertex.Z();
181  m_top.phiDec = decVertex.Phi();
182  }
183 
184  m_top.numPhot = top->getNphot();
185  m_top.numBkg = top->getEstBkg();
186  m_top.phot.e = top->getEstPhot(Const::electron);
187  m_top.phot.mu = top->getEstPhot(Const::muon);
188  m_top.phot.pi = top->getEstPhot(Const::pion);
189  m_top.phot.K = top->getEstPhot(Const::kaon);
190  m_top.phot.p = top->getEstPhot(Const::proton);
191  m_top.phot.d = top->getEstPhot(Const::deuteron);
192 
193  m_top.logL.e = top->getLogL(Const::electron);
194  m_top.logL.mu = top->getLogL(Const::muon);
195  m_top.logL.pi = top->getLogL(Const::pion);
196  m_top.logL.K = top->getLogL(Const::kaon);
197  m_top.logL.p = top->getLogL(Const::proton);
198  m_top.logL.d = top->getLogL(Const::deuteron);
199 
200  if (extHit) {
201  int moduleID = extHit->getCopyID();
202  auto position = static_cast<ROOT::Math::XYZPoint>(extHit->getPosition());
203  auto momentum = extHit->getMomentum();
204  if (geo->isModuleIDValid(moduleID)) {
205  const auto& module = geo->getModule(moduleID);
206  position = module.pointToLocal(position);
207  momentum = module.momentumToLocal(momentum);
208  }
209  m_top.extHit.moduleID = moduleID;
210  m_top.extHit.PDG = extHit->getPdgCode();
211  m_top.extHit.x = position.X();
212  m_top.extHit.y = position.Y();
213  m_top.extHit.z = position.Z();
214  m_top.extHit.p = momentum.R();
215  m_top.extHit.theta = momentum.Theta();
216  m_top.extHit.phi = momentum.Phi();
217  m_top.extHit.time = extHit->getTOF();
218  }
219 
220  if (barHit) {
221  int moduleID = barHit->getModuleID();
222  auto position = barHit->getPosition();
223  auto momentum = barHit->getMomentum();
224  if (geo->isModuleIDValid(moduleID)) {
225  const auto& module = geo->getModule(moduleID);
226  position = module.pointToLocal(position);
227  momentum = module.momentumToLocal(momentum);
228  }
229  m_top.barHit.moduleID = moduleID;
230  m_top.barHit.PDG = barHit->getPDG();
231  m_top.barHit.x = position.X();
232  m_top.barHit.y = position.Y();
233  m_top.barHit.z = position.Z();
234  m_top.barHit.p = momentum.R();
235  m_top.barHit.theta = momentum.Theta();
236  m_top.barHit.phi = momentum.Phi();
237  m_top.barHit.time = barHit->getTime() - trueEventT0;
238  }
239 
240  m_tree->Fill();
241  }
242 
243  }
244 
245 
247  {
248  }
249 
251  {
252  TDirectory::TContext context;
253  m_file->cd();
254  m_tree->Write();
255  m_file->Close();
256  }
257 
259 } // end Belle2 namespace
260 
static const ChargedStable muon
muon particle
Definition: Const.h:651
static const ChargedStable pion
charged pion particle
Definition: Const.h:652
static const ChargedStable proton
proton particle
Definition: Const.h:654
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:653
static const ChargedStable electron
electron particle
Definition: Const.h:650
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:655
Store one Ext hit as a ROOT object.
Definition: ExtHit.h:32
int getPdgCode() const
Get PDG code of this extrapolation's hypothesis.
Definition: ExtHit.h:118
int getCopyID() const
Get detector-element ID of sensitive element within detector.
Definition: ExtHit.h:126
ROOT::Math::XYZVector getMomentum() const
Get momentum at this extrapolation hit.
Definition: ExtHit.h:159
ROOT::Math::XYZVector getPosition() const
Get position of this extrapolation hit.
Definition: ExtHit.h:145
double getTOF() const
Get time of flight from the point of closest approach near the origin to this hit.
Definition: ExtHit.h:137
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition: MCParticle.h:47
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
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:96
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
Class to store track parameters of incoming MC particles relation to MCParticle filled in top/simulat...
Definition: TOPBarHit.h:28
Class to store TOP log likelihoods (output of TOPReconstructor).
Definition: TOPLikelihood.h:26
TTree * m_tree
TTree with TOPTree structure.
TOP::TOPTree m_top
ntuple structure
std::string m_outputFileName
output file name (root file)
const TOPGeometry * getGeometry() const
Returns pointer to geometry object using basf2 units.
static TOPGeometryPar * Instance()
Static method to obtain the pointer to its instance.
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
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
virtual void endRun() override
End-of-run action.
virtual void terminate() override
Termination action.
virtual ~TOPNtupleModule()
Destructor.
virtual void beginRun() override
Called when entering a new run.
Abstract base class for different kinds of events.
Float_t e
for electron
Float_t d
for deuteron
Float_t zDec
decay vertex (cylindrical coordinate z) of MCParticle
Float_t p
momentum magnitude of Track
Likelihoods logL
log likelihoods
Float_t rhoDec
decay vertex (cylindrical coordinate r) of MCParticle
Int_t numPhot
number of detected photons
Int_t run
run number
Short_t primary
is a primary particle (from related MCParticle)
Int_t evt
event number
Float_t phi
azimuthal angle of Track
Float_t numBkg
number of expected background photons
Short_t seen
is seen in TOP (from related MCParticle)
TrackHit extHit
extrapolated Track hit (in local module frame)
Int_t motherPDG
PDG code of related mother MCParticle.
Float_t phiProd
production vertex (cylindrical coordinate phi) of MCParticle
Float_t cth
cosine of polar angle of Track
Float_t rhoProd
production vertex (cylindrical coordinate r) of MCParticle
void clear()
Clear the structure: set elements to zero.
Float_t phiDec
decay vertex (cylindrical coordinate phi) of MCParticle
Int_t PDG
PDG code of related MCParticle.
Float_t pValue
p-value of Track fit
Likelihoods phot
number of expected photons (signal + bkg)
Float_t zProd
production vertex (cylindrical coordinate z) of MCParticle
TrackHit barHit
related MC particle hit (in local module frame)
Float_t p
momentum magnitude
Float_t z
impact point, z component
Int_t moduleID
module ID
Float_t theta
momentum polar angle
Float_t time
impact time
Float_t phi
momentum azimuthal angle
Float_t x
impact point, x component
Float_t y
impact point, y component