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