Belle II Software development
CDCCosmicAnalysisModule.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 "cdc/modules/cdcCosmicAnalysis/CDCCosmicAnalysisModule.h"
10#include <framework/geometry/BFieldManager.h>
11#include <framework/geometry/B2Vector3.h>
12#include <framework/gearbox/Const.h>
13#include <framework/datastore/RelationArray.h>
14
15#include <Math/ProbFuncMathCore.h>
16#include "algorithm"
17
18using namespace std;
19using namespace Belle2;
20using namespace CDC;
21
22
23//-----------------------------------------------------------------
24// Register the Module
25//-----------------------------------------------------------------
26REG_MODULE(CDCCosmicAnalysis);
27
28
29//-----------------------------------------------------------------
30// Implementation
31//-----------------------------------------------------------------
32
34{
35 setDescription("Module for harvesting parameters of the two half (up/down) of a cosmic track for performance study");
36 setPropertyFlags(c_ParallelProcessingCertified); // specify this flag if you need parallel processing
37 addParam("RecoTracksColName", m_recoTrackArrayName, "Name of collectrion hold RecoTracks", std::string(""));
38 addParam("Output", m_outputFileName, "output file name", string("twotracks.root"));
39 addParam("EventT0Extraction", m_eventT0Extraction, "use event t0 extract t0 or not", true);
40 addParam("treeName", m_treeName, "Output tree name", string("tree"));
41 addParam("phi0InRad", m_phi0InRad, "Phi0 in unit of radian, true: rad, false: deg", true);
42 addParam("StoreTrackParErrors", m_storeTrackParErrors,
43 "Store Track Parameter errors (true) or not (false)", false);
44}
45
47{
48}
50{
51 // Register histograms (calls back defineHisto)
52 m_Tracks.isRequired(m_trackArrayName);
56
57 m_relRecoTrackTrackName = relRecoTrackTrack.getName();
58
59 tfile = new TFile(m_outputFileName.c_str(), "RECREATE");
60 // tree = new TTree("treeTrk", "treeTrk");
61 tree = new TTree(m_treeName.c_str(), m_treeName.c_str());
62 tree->Branch("run", &run, "run/I");
63 tree->Branch("evtT0", &evtT0, "evtT0/D");
64 tree->Branch("charge", &charge, "charge/S");
65
66 tree->Branch("Pval1", &Pval1, "Pval1/D");
67 tree->Branch("ndf1", &ndf1, "ndf1/D");
68 tree->Branch("Pt1", &Pt1, "Pt1/D");
69 tree->Branch("D01", &D01, "D01/D");
70 tree->Branch("Phi01", &Phi01, "Phi01/D");
71 // tree->Branch("Om1", &Om1, "Om1/D");
72 tree->Branch("Z01", &Z01, "Z01/D");
73 tree->Branch("tanLambda1", &tanLambda1, "tanLambda1/D");
74 tree->Branch("posSeed1", "TVector3", &posSeed1);
75 tree->Branch("Omega1", &Omega1, "Omega1/D");
76 tree->Branch("Mom1", "TVector3", &Mom1);
77
78 tree->Branch("Pval2", &Pval2, "Pval2/D");
79 tree->Branch("ndf2", &ndf2, "ndf2/D");
80 tree->Branch("Pt2", &Pt2, "Pt2/D");
81 tree->Branch("D02", &D02, "D02/D");
82 tree->Branch("Phi02", &Phi02, "Phi02/D");
83 // tree->Branch("Om2", &Om2, "Om2/D");
84 tree->Branch("Z02", &Z02, "Z02/D");
85 tree->Branch("tanLambda2", &tanLambda2, "tanLambda2/D");
86 tree->Branch("posSeed2", "TVector3", &posSeed2);
87 tree->Branch("Omega2", &Omega2, "Omega2/D");
88 tree->Branch("Mom2", "TVector3", &Mom2);
89
91 tree->Branch("eD01", &eD01, "eD01/D");
92 tree->Branch("ePhi01", &ePhi01, "ePhi01/D");
93 tree->Branch("eOm1", &eOm1, "eOm1/D");
94 tree->Branch("eZ01", &eZ01, "eZ01/D");
95 tree->Branch("etanL1", &etanL1, "etanL1/D");
96
97 tree->Branch("eD02", &eD02, "eD02/D");
98 tree->Branch("ePhi02", &ePhi02, "ePhi02/D");
99 tree->Branch("eOm2", &eOm2, "eOm2/D");
100 tree->Branch("eZ02", &eZ02, "eZ02/D");
101 tree->Branch("etanL2", &etanL2, "etanL2/D");
102 }
103
104}
105
107{
108 ROOT::Math::XYZVector pos(0, 0, 0);
109 ROOT::Math::XYZVector bfield = BFieldManager::getFieldInTesla(pos);
110 if (bfield.Z() > 0.5) {
111 m_bField = true;
112 B2INFO("CDCCosmicAnalysis: Magnetic field is ON");
113 } else {
114 m_bField = false;
115 B2INFO("CDCCosmicAnalysis: Magnetic field is OFF");
116 }
117 B2INFO("CDCCosmicAnalysis: BField at (0,0,0) = " << bfield.R());
118}
119
121{
122 if (m_EventMetaData.isValid())
123 run = m_EventMetaData->getRun();
124
125 evtT0 = 0;
127 // event with is fail to extract event-t0 will be excluded
128 if (m_eventTimeStoreObject.isValid() && m_eventTimeStoreObject->hasEventT0()) {
129 evtT0 = m_eventTimeStoreObject->getEventT0();
130 } else {
131 return;
132 }
133 }
134
135 // Loop over Tracks
136 int nTr = m_Tracks.getEntries();
137
138 short charge2 = 0;
139 short charge1 = 0;
140 bool up(false), down(false);
141 for (int i = 0; i < nTr; ++i) {
142 const Belle2::Track* b2track = m_Tracks[i];
143 const Belle2::TrackFitResult* fitresult;
144 fitresult = b2track->getTrackFitResult(Const::muon);
145 if (!fitresult) {
146 B2WARNING("There is no track fit result for muon hypothesis; try with the closest mass hypothesis...");
147 fitresult = b2track->getTrackFitResultWithClosestMass(Const::muon);
148 if (!fitresult) {
149 B2WARNING("There is also no track fit reslt for the other mass hypothesis");
150 continue;
151 }
152 }
154 if (!recoTrack) {
155 B2WARNING("Can not access RecoTrack of this Belle2::Track");
156 continue;
157 }
158
159 B2Vector3D posSeed = recoTrack->getPositionSeed();
160 const genfit::FitStatus* fs = recoTrack->getTrackFitStatus();
161
162 double ndf = fs->getNdf();
163 if (!m_bField) // in case of no magnetic field, #track par=4 instead of 5.
164 ndf += 1;
165
166 double Chi2 = fs->getChi2();
167 double TrPval = std::max(0., ROOT::Math::chisquared_cdf_c(Chi2, ndf));
168 double Phi0 = fitresult->getPhi0();
169 if (m_phi0InRad == false) { // unit of degrees.
170 Phi0 *= 180 / M_PI;
171 }
172
173 /*** Two track case.***/
174 if ((posSeed.Y() > 0 && !up) ||
175 (up && posSeed.Y()*posSeed1.Y() > 0 && ndf > ndf1)) {
176 charge1 = fitresult->getChargeSign();
177 posSeed1 = posSeed;
178 D01 = fitresult->getD0();
179 eD01 = sqrt(fitresult->getCovariance5()[0][0]);
180 Phi01 = Phi0;
181 ePhi01 = sqrt(fitresult->getCovariance5()[1][1]);
182 if (m_phi0InRad == false) { // unit of degrees.
183 ePhi01 *= 180 / M_PI;
184 }
185
186 Omega1 = fitresult->getOmega();
187 Mom1 = B2Vector3D(fitresult->getMomentum());
188 eOm1 = sqrt(fitresult->getCovariance5()[2][2]);
189 Z01 = fitresult->getZ0();
190 eZ01 = sqrt(fitresult->getCovariance5()[3][3]);
191 tanLambda1 = fitresult->getTanLambda();
192 etanL1 = sqrt(fitresult->getCovariance5()[4][4]);
193 Pt1 = fitresult->getTransverseMomentum();
194 ndf1 = ndf;
195 Pval1 = TrPval;
196 up = true;
197 }
198
199 if ((posSeed.Y() < 0 && !down) ||
200 (down && posSeed.Y()*posSeed2.Y() > 0 && ndf > ndf2)) {
201 charge2 = fitresult->getChargeSign();
202 posSeed2 = posSeed;
203 D02 = fitresult->getD0();
204 eD02 = sqrt(fitresult->getCovariance5()[0][0]);
205 Phi02 = Phi0;
206 ePhi02 = sqrt(fitresult->getCovariance5()[1][1]);
207 if (m_phi0InRad == false) { // unit of degrees.
208 ePhi02 *= 180 / M_PI;
209 }
210 Omega2 = fitresult->getOmega();
211 Mom2 = B2Vector3D(fitresult->getMomentum());
212 eOm2 = sqrt(fitresult->getCovariance5()[2][2]);
213 Z02 = fitresult->getZ0();
214 eZ02 = sqrt(fitresult->getCovariance5()[3][3]);
215 tanLambda2 = fitresult->getTanLambda();
216 etanL2 = sqrt(fitresult->getCovariance5()[4][4]);
217 Pt2 = fitresult->getTransverseMomentum();
218 ndf2 = ndf;
219 Pval2 = TrPval;
220 down = true;
221 }
222 }
223 if (m_bField && charge1 * charge2 == 0) return;
224 if (charge1 * charge2 >= 0 && up && down) {
225 charge = charge1;
226 tree->Fill();
227 }
228
229}
230
232{
233}
234
236{
237 tfile->cd();
238 tree->Write();
239 tfile->Close();
240}
241
242
DataType Y() const
access variable Y (= .at(1) without boundary check)
Definition: B2Vector3.h:433
static ROOT::Math::XYZVector getFieldInTesla(const ROOT::Math::XYZVector &pos)
return the magnetic field at a given position in Tesla.
Definition: BFieldManager.h:61
StoreObjPtr< EventT0 > m_eventTimeStoreObject
Event t0.
std::string m_recoTrackArrayName
Belle2::RecoTrack StoreArray nam.e.
double eZ01
error on Z0 of 1st track.
void initialize() override
Initializes the Module.
StoreArray< TrackFitResult > m_TrackFitResults
Track fit results.
void event() override
Event action (main routine).
double eD01
error on D0 of 1st track.
double ndf1
degree of freedom of 1st track.
void terminate() override
Termination action.
bool m_phi0InRad
Unit of phi0, true: radian, false: degree.
double ndf2
degree of freedom of 2nd track.
double eD02
error on D0 of 2nd track.
std::string m_relRecoTrackTrackName
Releation between RecoTrack and Belle2:Track.
TVector3 posSeed1
seed position of the first track.
double etanL1
error on TanLambda of 1st track.
bool m_eventT0Extraction
run with event t0 extraction
void beginRun() override
Begin run action.
double eOm1
error on Omega of 1st track.
bool m_bField
Data are taken with B-field or not, if true, NDF=5 in cal P-value.
double eOm2
error on Omega of 2nd track.
double etanL2
error on TanLambda of 2nd track.
std::string m_trackArrayName
Belle2::Track StoreArray name.
TVector3 posSeed2
seed position of the second track.
double eZ02
error on Z0 of 2nd track.
TTree * tree
output tree, save info of each hit.
std::string m_trackFitResultArrayName
Belle2::TrackFitResult StoreArray name.
double tanLambda2
Tanlambda of 2nd track.
double ePhi02
error on Phi0 of 2nd track.
std::string m_treeName
output tree name.
StoreArray< RecoTrack > m_RecoTracks
Tracks.
double ePhi01
error on Phi0 of 1st track.
StoreObjPtr< EventMetaData > m_EventMetaData
Event metadata.
double tanLambda1
TanLambda of 1st track.
std::string m_outputFileName
Output file name.
bool m_storeTrackParErrors
Store error of track parameters or not.
static const ChargedStable muon
muon particle
Definition: Const.h:660
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:79
ROOT::Math::XYZVector getPositionSeed() const
Return the position seed stored in the reco track. ATTENTION: This is not the fitted position.
Definition: RecoTrack.h:480
const genfit::FitStatus * getTrackFitStatus(const genfit::AbsTrackRep *representation=nullptr) const
Return the track fit status for the given representation or for the cardinal one. You are not allowed...
Definition: RecoTrack.h:621
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
TO * getRelatedTo(const std::string &name="", const std::string &namedRelation="") const
Get the object to which this object has a relation.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Values of the result of a track fit with a given particle hypothesis.
TMatrixDSym getCovariance5() const
Getter for covariance matrix of perigee parameters in matrix form.
short getChargeSign() const
Return track charge (1 or -1).
double getOmega() const
Getter for omega.
double getD0() const
Getter for d0.
double getTransverseMomentum() const
Getter for the absolute value of the transverse momentum at the perigee.
double getTanLambda() const
Getter for tanLambda.
double getZ0() const
Getter for z0.
ROOT::Math::XYZVector getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
double getPhi0() const
Getter for phi0.
Class that bundles various TrackFitResults.
Definition: Track.h:25
const TrackFitResult * getTrackFitResult(const Const::ChargedStable &chargedStable) const
Default Access to TrackFitResults.
Definition: Track.cc:30
const TrackFitResult * getTrackFitResultWithClosestMass(const Const::ChargedStable &requestedType) const
Return the track fit for a fit hypothesis with the closest mass.
Definition: Track.cc:104
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
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition: B2Vector3.h:516
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Abstract base class for different kinds of events.
STL namespace.