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
42{
43}
44
46{
47 B2INFO(
48 "[CosmicsAlignmentValidation Module]: Starting initialization of CosmicsAlignmentValidation Module. Give me Cosmics!");
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}
92
94{
95 B2DEBUG(99, "[CosmicsAlignmentValidationModule] begin event");
96
97 //genfit stuff
98 //StoreArray<genfit::Track> fittedTracks(""); // the results of the track fit
99 const int nFittedTracks = m_GenfitTracks.getEntries();
100
101 //RelationArray gfTracksToMCPart(fittedTracks, mcParticles);
102
103 t_p1 = t_p2 = t_pt1 = t_pt2 = t_p1MC = t_p2MC = -999;
104 t_x1 = t_x2 = t_y1 = t_y2 = t_z1 = t_z2 = -999;
105 t_px1 = t_px2 = t_py1 = t_py2 = t_pz1 = t_pz2 = -999;
106 t_D01 = t_Phi1 = t_Omega1 = t_Z01 = t_cotTheta1 - 999;
107 t_D02 = t_Phi2 = t_Omega2 = t_Z02 = t_cotTheta2 - 999;
108
109 if (nFittedTracks != 2) {
110 B2INFO(
111 "[CosmicsAlignmentValidationModule] no two tracks reconstructed, but "
112 << nFittedTracks);
113 return;
114 }
115
116 else {
117
118 genfit::Track* tr1 = m_GenfitTracks[0];
119 genfit::Track* tr2 = m_GenfitTracks[1];
120
121 const TrackFitResult* fitResult1 = findRelatedTrackFitResult(tr1);
122 const TrackFitResult* fitResult2 = findRelatedTrackFitResult(tr2);
123
124 if (fitResult1 == fitResult2)
125 B2WARNING(
126 "[CosmicsAlignmentValidationModule] Fit Results are from the same track!");
127
128 if (fitResult1 != NULL) { // valid TrackFitResult found
129 t_p1 = fitResult1->getMomentum().R();
130 t_pt1 = fitResult1->getMomentum().Rho();
131 t_x1 = fitResult1->getPosition().X();
132 t_y1 = fitResult1->getPosition().Y();
133 t_z1 = fitResult1->getPosition().Z();
134 t_px1 = fitResult1->getMomentum().X();
135 t_py1 = fitResult1->getMomentum().Y();
136 t_pz1 = fitResult1->getMomentum().Z();
137
138 t_D01 = fitResult1->getD0();
139 t_Phi1 = fitResult1->getPhi();
140 t_Omega1 = fitResult1->getOmega();
141 t_Z01 = fitResult1->getZ0();
142 t_cotTheta1 = fitResult1->getCotTheta();
143 }
144 if (fitResult2 != NULL) { // valid TrackFitResult found
145 t_p2 = fitResult2->getMomentum().R();
146 t_pt2 = fitResult2->getMomentum().Rho();
147 t_x2 = fitResult2->getPosition().X();
148 t_y2 = fitResult2->getPosition().Y();
149 t_z2 = fitResult2->getPosition().Z();
150 t_px2 = fitResult2->getMomentum().X();
151 t_py2 = fitResult2->getMomentum().Y();
152 t_pz2 = fitResult2->getMomentum().Z();
153
154 t_D02 = fitResult2->getD0();
155 t_Phi2 = fitResult2->getPhi();
156 t_Omega2 = fitResult2->getOmega();
157 t_Z02 = fitResult2->getZ0();
158 t_cotTheta2 = fitResult2->getCotTheta();
159 }
160
161 t_p1MC = m_MCParticles[0]->getMomentum().R();
162
163 if (fitResult2 != NULL && fitResult1 != NULL) {
164 t_dz = fitResult1->getPosition().Z()
165 - fitResult2->getPosition().Z();
166 t_dR = sqrt(
167 fitResult1->getPosition().X()
168 * fitResult1->getPosition().X()
169 + fitResult1->getPosition().Y()
170 * fitResult1->getPosition().Y())
171 - sqrt(
172 fitResult2->getPosition().X()
173 * fitResult2->getPosition().X()
174 + fitResult2->getPosition().Y()
175 * fitResult2->getPosition().Y());
176
177 }
178 tree->Fill();
179 }
180
181}
182
184{
185}
186
188{
189 B2INFO("[CosmicsAlignmentValidationModule] Saving tree.");
190 B2INFO(
191 "[CosmicsAlignmentValidationModule] Tree has " << tree->GetEntries()
192 << " entries");
193 file->cd();
194 tree->Write("cosmics");
195 file->Close();
196}
197
199 const genfit::Track* gfTrack)
200{
201 // search for a related TrackFitResult
202 RelationIndex < genfit::Track, TrackFitResult > relGfTracksToTrackFitResults;
203
205 std::vector<const TrackFitResult*> fitResults;
206
207 for (const relElement_t& relGfTrackToTrackFitResult : relGfTracksToTrackFitResults.getElementsFrom(gfTrack)) {
208 B2DEBUG(99, "----> Related TrackFitResult found!!!");
209 fitResults.push_back(relGfTrackToTrackFitResult.to);
210 }
211
212 int numberTrackFitResults = fitResults.size();
213
214 if (numberTrackFitResults == 1) {
215 return fitResults[0];
216 }
217 if (numberTrackFitResults == 0) {
218 return NULL;
219 }
220 if (numberTrackFitResults > 1) {
221 B2DEBUG(99,
222 "[CosmicsAlignmentValidationModule] genfit::Track has "
223 << numberTrackFitResults
224 << " related TrackFitResults. No TrackFitResult is returned.");
225 }
226
227 return NULL;
228}
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
output 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: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.