Belle II Software development
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#include <top/reconstruction_cpp/TOPRecoManager.h>
15
16// framework - DataStore
17#include <framework/datastore/StoreArray.h>
18#include <framework/datastore/StoreObjPtr.h>
19
20// framework aux
21#include <framework/gearbox/Const.h>
22#include <framework/logging/Logger.h>
23
24// DataStore classes
25#include <framework/dataobjects/EventMetaData.h>
26#include <framework/dataobjects/MCInitialParticles.h>
27#include <mdst/dataobjects/Track.h>
28#include <mdst/dataobjects/MCParticle.h>
29#include <top/dataobjects/TOPLikelihood.h>
30#include <top/dataobjects/TOPBarHit.h>
31#include <top/dataobjects/TOPDigit.h>
32#include <tracking/dataobjects/ExtHit.h>
33
34// ROOT
35#include <TDirectory.h>
36
37using namespace std;
38
39namespace Belle2 {
45 using namespace TOP;
46
47 //-----------------------------------------------------------------
49 //-----------------------------------------------------------------
50
51 REG_MODULE(TOPNtuple);
52
53 //-----------------------------------------------------------------
54 // Implementation
55 //-----------------------------------------------------------------
56
58 {
59 // set module description
60 setDescription("Writes ntuple of TOPLikelihoods with tracking info into a root file");
61
62 // Add parameters
63 addParam("outputFileName", m_outputFileName, "Output file name",
64 string("TOPNtuple.root"));
65
66 }
67
69 {
70 }
71
73 {
74 TDirectory::TContext context;
75 m_file = TFile::Open(m_outputFileName.c_str(), "RECREATE");
76 if (m_file->IsZombie()) {
77 B2FATAL("Couldn't open file '" << m_outputFileName << "' for writing!");
78 return;
79 }
80
81 m_tree = new TTree("top", "TOP validation ntuple");
82
83 m_tree->Branch("evt", &m_top.evt, "evt/I");
84 m_tree->Branch("run", &m_top.run, "run/I");
85
86 m_tree->Branch("p", &m_top.p, "p/F");
87 m_tree->Branch("cth", &m_top.cth, "cth/F");
88 m_tree->Branch("phi", &m_top.phi, "phi/F");
89 m_tree->Branch("pValue", &m_top.pValue, "pValue/F");
90
91 m_tree->Branch("PDG", &m_top.PDG, "PDG/I");
92 m_tree->Branch("motherPDG", &m_top.motherPDG, "motherPDG/I");
93 m_tree->Branch("primary", &m_top.primary, "primary/S");
94 m_tree->Branch("seen", &m_top.seen, "seen/S");
95 m_tree->Branch("rhoProd", &m_top.rhoProd, "rhoProd/F");
96 m_tree->Branch("zProd", &m_top.zProd, "zProd/F");
97 m_tree->Branch("phiProd", &m_top.phiProd, "phiProd/F");
98 m_tree->Branch("rhoDec", &m_top.rhoDec, "rhoDec/F");
99 m_tree->Branch("zDec", &m_top.zDec, "zDec/F");
100 m_tree->Branch("phiDec", &m_top.phiDec, "phiDec/F");
101 m_tree->Branch("yieldMC", &m_top.yieldMC, "yieldMC/I");
102
103 m_tree->Branch("numPhot", &m_top.numPhot, "numPhot/I");
104 m_tree->Branch("numBkg", &m_top.numBkg, "numBkg/F");
105 m_tree->Branch("moduleID", &m_top.moduleID, "moduleID/I");
106 m_tree->Branch("phot", &m_top.phot, "e/F:mu:pi:K:p:d");
107 m_tree->Branch("yield", &m_top.yield, "e/F:mu:pi:K:p:d");
108 m_tree->Branch("logL", &m_top.logL, "e/F:mu:pi:K:p:d");
109
110 m_tree->Branch("extHit", &m_top.extHit, "moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
111 m_tree->Branch("barHit", &m_top.barHit, "moduleID/I:PDG:x/F:y:z:p:theta:phi:time");
112
113 StoreArray<Track> tracks;
114 tracks.isRequired();
115 StoreArray<ExtHit> extHits;
116 extHits.isRequired();
117 StoreArray<TOPLikelihood> likelihoods;
118 likelihoods.isRequired();
119 StoreArray<MCParticle> mcParticles;
120 mcParticles.isOptional();
121 StoreArray<TOPBarHit> barHits;
122 barHits.isOptional();
123 StoreObjPtr<MCInitialParticles> mcInitialParticles;
124 mcInitialParticles.isOptional();
125 }
126
128 {
129 }
130
132 {
133
134 StoreObjPtr<EventMetaData> evtMetaData;
135 StoreArray<Track> tracks;
136 StoreObjPtr<MCInitialParticles> mcInitialParticles;
137 double trueEventT0 = 0;
138 if (mcInitialParticles.isValid()) trueEventT0 = mcInitialParticles->getTime();
139
140 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
141
142 for (const auto& track : tracks) {
143 const auto* trackFit = track.getTrackFitResultWithClosestMass(Const::pion);
144 if (!trackFit) continue;
145 const TOPLikelihood* top = track.getRelated<TOPLikelihood>();
146 if (!top) continue;
147 if (top->getFlag() != 1) continue;
148
149 const ExtHit* extHit = top->getRelated<ExtHit>();
150 const TOPBarHit* barHit = top->getRelated<TOPBarHit>();
151 const MCParticle* mcParticle = track.getRelated<MCParticle>();
152 const MCParticle* mother = 0;
153 if (mcParticle) {
154 mother = mcParticle->getMother();
155 if (not barHit) { // Track MC matching probably done after TOPReconstructor so no relation from TOPLikelihood
156 const auto barHits = mcParticle->getRelationsWith<TOPBarHit>();
157 for (const auto& bHit : barHits) {
158 if (bHit.getModuleID() == extHit->getCopyID()) barHit = &bHit;
159 }
160 }
161 }
162
163 m_top.clear();
164
165 m_top.evt = evtMetaData->getEvent();
166 m_top.run = evtMetaData->getRun();
167
168 ROOT::Math::XYZVector mom = trackFit->getMomentum();
169 m_top.p = mom.R();
170 m_top.cth = cos(mom.Theta());
171 m_top.phi = mom.Phi();
172 m_top.pValue = trackFit->getPValue();
173
174 if (mcParticle) {
175 m_top.PDG = mcParticle->getPDG();
176 if (mother) m_top.motherPDG = mother->getPDG();
177 m_top.primary = mcParticle->getStatus(MCParticle::c_PrimaryParticle);
178 m_top.seen = mcParticle->hasSeenInDetector(Const::TOP);
179 ROOT::Math::XYZVector prodVertex = mcParticle->getProductionVertex();
180 m_top.rhoProd = prodVertex.Rho();
181 m_top.zProd = prodVertex.Z();
182 m_top.phiProd = prodVertex.Phi();
183 ROOT::Math::XYZVector decVertex = mcParticle->getDecayVertex();
184 m_top.rhoDec = decVertex.Rho();
185 m_top.zDec = decVertex.Z();
186 m_top.phiDec = decVertex.Phi();
187 const auto digits = mcParticle->getRelationsWith<TOPDigit>();
188 for (size_t i = 0; i < digits.size(); i++) {
189 double wt = digits.weight(i);
190 if (wt <= 0) continue; // photon not from this MC particle
191 const auto* digit = digits[i];
192 if (digit->getHitQuality() != TOPDigit::c_Good) continue;
193 if (digit->getModuleID() != top->getModuleID()) continue;
194 if (digit->getTime() < TOPRecoManager::getMinTime() or digit->getTime() > TOPRecoManager::getMaxTime()) continue;
195 m_top.yieldMC++;
196 }
197 }
198
199 m_top.numPhot = top->getNphot();
200 m_top.numBkg = top->getEstBkg();
201 m_top.moduleID = top->getModuleID();
202
203 m_top.phot.e = top->getEstPhot(Const::electron);
204 m_top.phot.mu = top->getEstPhot(Const::muon);
205 m_top.phot.pi = top->getEstPhot(Const::pion);
206 m_top.phot.K = top->getEstPhot(Const::kaon);
207 m_top.phot.p = top->getEstPhot(Const::proton);
208 m_top.phot.d = top->getEstPhot(Const::deuteron);
209
210 m_top.yield.e = top->getEffectiveSignalYield(Const::electron);
211 m_top.yield.mu = top->getEffectiveSignalYield(Const::muon);
212 m_top.yield.pi = top->getEffectiveSignalYield(Const::pion);
213 m_top.yield.K = top->getEffectiveSignalYield(Const::kaon);
214 m_top.yield.p = top->getEffectiveSignalYield(Const::proton);
215 m_top.yield.d = top->getEffectiveSignalYield(Const::deuteron);
216
217 m_top.logL.e = top->getLogL(Const::electron);
218 m_top.logL.mu = top->getLogL(Const::muon);
219 m_top.logL.pi = top->getLogL(Const::pion);
220 m_top.logL.K = top->getLogL(Const::kaon);
221 m_top.logL.p = top->getLogL(Const::proton);
222 m_top.logL.d = top->getLogL(Const::deuteron);
223
224 if (extHit) {
225 int moduleID = extHit->getCopyID();
226 auto position = static_cast<ROOT::Math::XYZPoint>(extHit->getPosition());
227 auto momentum = extHit->getMomentum();
228 if (geo->isModuleIDValid(moduleID)) {
229 const auto& module = geo->getModule(moduleID);
230 position = module.pointToLocal(position);
231 momentum = module.momentumToLocal(momentum);
232 }
233 m_top.extHit.moduleID = moduleID;
234 m_top.extHit.PDG = extHit->getPdgCode();
235 m_top.extHit.x = position.X();
236 m_top.extHit.y = position.Y();
237 m_top.extHit.z = position.Z();
238 m_top.extHit.p = momentum.R();
239 m_top.extHit.theta = momentum.Theta();
240 m_top.extHit.phi = momentum.Phi();
241 m_top.extHit.time = extHit->getTOF();
242 }
243
244 if (barHit) {
245 int moduleID = barHit->getModuleID();
246 auto position = barHit->getPosition();
247 auto momentum = barHit->getMomentum();
248 if (geo->isModuleIDValid(moduleID)) {
249 const auto& module = geo->getModule(moduleID);
250 position = module.pointToLocal(position);
251 momentum = module.momentumToLocal(momentum);
252 }
253 m_top.barHit.moduleID = moduleID;
254 m_top.barHit.PDG = barHit->getPDG();
255 m_top.barHit.x = position.X();
256 m_top.barHit.y = position.Y();
257 m_top.barHit.z = position.Z();
258 m_top.barHit.p = momentum.R();
259 m_top.barHit.theta = momentum.Theta();
260 m_top.barHit.phi = momentum.Phi();
261 m_top.barHit.time = barHit->getTime() - trueEventT0;
262 }
263
264 m_tree->Fill();
265 }
266
267 }
268
269
271 {
272 }
273
275 {
276 TDirectory::TContext context;
277 m_file->cd();
278 m_tree->Write();
279 m_file->Close();
280 }
281
283} // end Belle2 namespace
284
static const ChargedStable muon
muon particle
Definition: Const.h:660
static const ChargedStable pion
charged pion particle
Definition: Const.h:661
static const ChargedStable proton
proton particle
Definition: Const.h:663
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:662
static const ChargedStable electron
electron particle
Definition: Const.h:659
static const ChargedStable deuteron
deuteron particle
Definition: Const.h:664
Store one Ext hit as a ROOT object.
Definition: ExtHit.h:31
int getPdgCode() const
Get PDG code of this extrapolation's hypothesis.
Definition: ExtHit.h:117
int getCopyID() const
Get detector-element ID of sensitive element within detector.
Definition: ExtHit.h:125
ROOT::Math::XYZVector getMomentum() const
Get momentum at this extrapolation hit.
Definition: ExtHit.h:151
ROOT::Math::XYZVector getPosition() const
Get position of this extrapolation hit.
Definition: ExtHit.h:144
double getTOF() const
Get time of flight from the point of closest approach near the origin to this hit.
Definition: ExtHit.h:136
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
RelationVector< T > getRelationsWith(const std::string &name="", const std::string &namedRelation="") const
Get the relations between this object and another store array.
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:27
Class to store TOP digitized hits (output of TOPDigitizer or raw data unpacker) relations to TOPSimHi...
Definition: TOPDigit.h:24
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.
static double getMaxTime()
Returns time window upper edge.
static double getMinTime()
Returns time window lower edge.
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
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.
TOPNtupleModule()
Constructor.
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.
STL namespace.
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 moduleID
module ID from TOPLikelihoods
Int_t run
run number
Short_t primary
is a primary particle (from related MCParticle)
Likelihoods yield
effective signal yields by sPlot
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)
Int_t yieldMC
signal yield MC truth
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