Belle II Software development
CosmicsAlignmentValidation.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 <alignment/modules/CosmicsAlignmentValidation/CosmicsAlignmentValidation.h>
10
11#include <framework/datastore/RelationIndex.h>
12#include <mdst/dataobjects/MCParticle.h>
13
14#include <root/TFile.h>
15#include <root/TTree.h>
16
17//avoid having to wrap everything in the namespace explicitly
18//only permissible in .cc files!
19using namespace Belle2;
20
21//this line registers the module with the framework and actually makes it available
22//in steering files or the the module list (basf2 -m).
23//Note that the 'Module' part of the class name is missing, this is also the way it
24//will be called in the module list.
25
26REG_MODULE(CosmicsAlignmentValidation);
27
29 Module(),
30 file(NULL),
31 tree(NULL)
32{
33 setDescription("Alignment Validation with Cosmics");
34
35 addParam("gfTrackColName", m_gfTrackColName,
36 "Name of genfit::Track collection.", std::string(""));
37 addParam("outputFileName", m_outputFileName, "Name of the output file.",
38 std::string("cosmics.root"));
39}
40
44
46{
47 B2INFO(
48 "[CosmicsAlignmentValidation Module]: Starting initialization of CosmicsAlignmentValidation Module. Give me Cosmics!");
50 m_MCParticles.isRequired();
51 file = new TFile(m_outputFileName.c_str(), "RECREATE");
52 tree = new TTree("cosmics", "cosmics");
53 tree->Branch("p1", &t_p1);
54 tree->Branch("p2", &t_p2);
55 tree->Branch("p1MC", &t_p1MC);
56 tree->Branch("p2MC", &t_p2MC);
57 tree->Branch("pt1", &t_pt1);
58 tree->Branch("dz", &t_dz);
59 tree->Branch("dR", &t_dR);
60
61 tree->Branch("x1", &t_x1);
62 tree->Branch("x2", &t_x2);
63 tree->Branch("y1", &t_y1);
64 tree->Branch("y2", &t_y2);
65 tree->Branch("z1", &t_z1);
66 tree->Branch("z2", &t_z2);
67
68 tree->Branch("px1", &t_px1);
69 tree->Branch("px2", &t_px2);
70 tree->Branch("py1", &t_py1);
71 tree->Branch("py2", &t_py2);
72 tree->Branch("pz1", &t_pz1);
73 tree->Branch("pz2", &t_pz2);
74
75 tree->Branch("D01", &t_D01);
76 tree->Branch("Phi1", &t_Phi1);
77 tree->Branch("Omega1", &t_Omega1);
78 tree->Branch("Z01", &t_Z01);
79 tree->Branch("cotTheta1", &t_cotTheta1);
80
81 tree->Branch("D02", &t_D02);
82 tree->Branch("Phi2", &t_Phi2);
83 tree->Branch("Omega2", &t_Omega2);
84 tree->Branch("Z02", &t_Z02);
85 tree->Branch("cotTheta2", &t_cotTheta2);
86
87}
88
90{
91 B2DEBUG(99, "[CosmicsAlignmentValidationModule] begin event");
92
93 //genfit stuff
94 //StoreArray<genfit::Track> fittedTracks(""); // the results of the track fit
95 const int nFittedTracks = m_GenfitTracks.getEntries();
96
97 //RelationArray gfTracksToMCPart(fittedTracks, mcParticles);
98
99 t_p1 = t_p2 = t_pt1 = t_pt2 = t_p1MC = t_p2MC = -999;
100 t_x1 = t_x2 = t_y1 = t_y2 = t_z1 = t_z2 = -999;
101 t_px1 = t_px2 = t_py1 = t_py2 = t_pz1 = t_pz2 = -999;
102 t_D01 = t_Phi1 = t_Omega1 = t_Z01 = t_cotTheta1 - 999;
103 t_D02 = t_Phi2 = t_Omega2 = t_Z02 = t_cotTheta2 - 999;
104
105 if (nFittedTracks != 2) {
106 B2INFO(
107 "[CosmicsAlignmentValidationModule] no two tracks reconstructed, but "
108 << nFittedTracks);
109 return;
110 }
111
112 else {
113
114 const genfit::Track* tr1 = m_GenfitTracks[0];
115 const genfit::Track* tr2 = m_GenfitTracks[1];
116
117 const TrackFitResult* fitResult1 = findRelatedTrackFitResult(tr1);
118 const TrackFitResult* fitResult2 = findRelatedTrackFitResult(tr2);
119
120 if (fitResult1 == fitResult2)
121 B2WARNING(
122 "[CosmicsAlignmentValidationModule] Fit Results are from the same track!");
123
124 if (fitResult1 != NULL) { // valid TrackFitResult found
125 t_p1 = fitResult1->getMomentum().R();
126 t_pt1 = fitResult1->getMomentum().Rho();
127 t_x1 = fitResult1->getPosition().X();
128 t_y1 = fitResult1->getPosition().Y();
129 t_z1 = fitResult1->getPosition().Z();
130 t_px1 = fitResult1->getMomentum().X();
131 t_py1 = fitResult1->getMomentum().Y();
132 t_pz1 = fitResult1->getMomentum().Z();
133
134 t_D01 = fitResult1->getD0();
135 t_Phi1 = fitResult1->getPhi();
136 t_Omega1 = fitResult1->getOmega();
137 t_Z01 = fitResult1->getZ0();
138 t_cotTheta1 = fitResult1->getCotTheta();
139 }
140 if (fitResult2 != NULL) { // valid TrackFitResult found
141 t_p2 = fitResult2->getMomentum().R();
142 t_pt2 = fitResult2->getMomentum().Rho();
143 t_x2 = fitResult2->getPosition().X();
144 t_y2 = fitResult2->getPosition().Y();
145 t_z2 = fitResult2->getPosition().Z();
146 t_px2 = fitResult2->getMomentum().X();
147 t_py2 = fitResult2->getMomentum().Y();
148 t_pz2 = fitResult2->getMomentum().Z();
149
150 t_D02 = fitResult2->getD0();
151 t_Phi2 = fitResult2->getPhi();
152 t_Omega2 = fitResult2->getOmega();
153 t_Z02 = fitResult2->getZ0();
154 t_cotTheta2 = fitResult2->getCotTheta();
155 }
156
157 t_p1MC = m_MCParticles[0]->getMomentum().R();
158
159 if (fitResult2 != NULL && fitResult1 != NULL) {
160 t_dz = fitResult1->getPosition().Z()
161 - fitResult2->getPosition().Z();
162 t_dR = sqrt(
163 fitResult1->getPosition().X()
164 * fitResult1->getPosition().X()
165 + fitResult1->getPosition().Y()
166 * fitResult1->getPosition().Y())
167 - sqrt(
168 fitResult2->getPosition().X()
169 * fitResult2->getPosition().X()
170 + fitResult2->getPosition().Y()
171 * fitResult2->getPosition().Y());
172
173 }
174 tree->Fill();
175 }
176
177}
178
180{
181 B2INFO("[CosmicsAlignmentValidationModule] Saving tree.");
182 B2INFO(
183 "[CosmicsAlignmentValidationModule] Tree has " << tree->GetEntries()
184 << " entries");
185 file->cd();
186 tree->Write("cosmics");
187 file->Close();
188}
189
191 const genfit::Track* gfTrack)
192{
193 // search for a related TrackFitResult
194 RelationIndex < genfit::Track, TrackFitResult > relGfTracksToTrackFitResults;
195
197 std::vector<const TrackFitResult*> fitResults;
198
199 for (const relElement_t& relGfTrackToTrackFitResult : relGfTracksToTrackFitResults.getElementsFrom(gfTrack)) {
200 B2DEBUG(99, "----> Related TrackFitResult found!!!");
201 fitResults.push_back(relGfTrackToTrackFitResult.to);
202 }
203
204 int numberTrackFitResults = fitResults.size();
205
206 if (numberTrackFitResults == 1) {
207 return fitResults[0];
208 }
209 if (numberTrackFitResults == 0) {
210 return NULL;
211 }
212 if (numberTrackFitResults > 1) {
213 B2DEBUG(99,
214 "[CosmicsAlignmentValidationModule] genfit::Track has "
215 << numberTrackFitResults
216 << " related TrackFitResults. No TrackFitResult is returned.");
217 }
218
219 return NULL;
220}
virtual void initialize() override
Use this to initialize resources or memory your module needs.
virtual void event() override
Called once for each event.
virtual void terminate() override
Clean up anything you created in initialize().
const TrackFitResult * findRelatedTrackFitResult(const genfit::Track *gfTrack)
Find trackfit results in for the corresponding track.
CosmicsAlignmentValidationModule()
Constructor, for setting module description and parameters.
StoreArray< genfit::Track > m_GenfitTracks
Genfit tracks.
~CosmicsAlignmentValidationModule() override
Use to clean up anything you created in the constructor.
StoreArray< MCParticle > m_MCParticles
MC particles.
std::string m_outputFileName
output filename string
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.
RelationIndexContainer< FROM, TO >::Element Element
Struct representing a single element in the index.
Values of the result of a track fit with a given particle hypothesis.
double getPhi() const
Getter for phi0 with CDF naming convention.
double getCotTheta() const
Getter for tanLambda with CDF naming convention.
double getOmega() const
Getter for omega.
double getD0() const
Getter for d0.
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.
ROOT::Math::XYZVector getPosition() const
Getter for vector of position at closest approach of track in r/phi projection.
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
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.