Belle II Software development
CDCCalibrationCollector.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/cdcCalibrationCollector/CDCCalibrationCollector.h"
10#include <cdc/translators/RealisticTDCCountTranslator.h>
11#include <framework/datastore/RelationArray.h>
12
13#include <tracking/trackingUtilities/eventdata/hits/CDCWireHit.h>
14#include <cdc/topology/CDCWireTopology.h>
15
16#include <genfit/TrackPoint.h>
17#include <genfit/KalmanFitterInfo.h>
18#include <genfit/MeasurementOnPlane.h>
19#include <genfit/MeasuredStateOnPlane.h>
20
21#include <Math/ProbFuncMathCore.h>
22
23#include <cdc/dataobjects/WireID.h>
24#include <cdc/geometry/CDCGeometryPar.h>
25
26#include <TH1F.h>
27
28#include <cmath>
29
30//using namespace std;
31using namespace Belle2;
32using namespace CDC;
33using namespace genfit;
34using namespace TrackingUtilities;
35
36
37REG_MODULE(CDCCalibrationCollector);
38
39
41{
42 setDescription("Collector module for cdc calibration");
43 setPropertyFlags(c_ParallelProcessingCertified); // specify this flag if you need parallel processing
44 addParam("recoTracksColName", m_recoTrackArrayName, "Name of collection hold genfit::Track", std::string(""));
45 //addParam("bField", m_bField, "If true -> #Params ==5 else #params ==4 for calculate P-Val", false);
46 addParam("calExpectedDriftTime", m_calExpectedDriftTime, "if true module will calculate expected drift time, it take a time",
47 true);
48 addParam("storeTrackParams", m_storeTrackParams, "Store Track Parameter or not, it will be multicount for each hit", false);
49 addParam("eventT0Extraction", m_eventT0Extraction, "use event t0 extract t0 or not", true);
50 addParam("minimumPt", m_minimumPt, "Tracks with transverse momentum smaller than this value will not used", 0.15);
51 addParam("minimumNDF", m_minimumNDF, "Discard tracks whose degree-of-freedom below this value", 5.);
52 addParam("isCosmic", m_isCosmic, "True when we process cosmic events, else False (collision)", m_isCosmic);
53 addParam("effStudy", m_effStudy, "When true module collects info only necessary for wire eff study", false);
54}
55
59
61{
62 m_Tracks.isRequired(m_trackArrayName);
65 m_CDCHits.isRequired(m_cdcHitArrayName);
68 //Store names to speed up creation later
69 m_relRecoTrackTrackName = relRecoTrackTrack.getName();
70
71 if (!m_effStudy) { // by default collects calibration data
72 auto m_tree = new TTree(m_treeName.c_str(), "tree for cdc calibration");
73 m_tree->Branch<Float_t>("x_mea", &x_mea);
74 m_tree->Branch<Float_t>("x_u", &x_u);
75 m_tree->Branch<Float_t>("x_b", &x_b);
76 m_tree->Branch<Float_t>("alpha", &alpha);
77 m_tree->Branch<Float_t>("theta", &theta);
78 m_tree->Branch<Float_t>("t", &t);
79 m_tree->Branch<UShort_t>("adc", &adc);
80 // m_tree->Branch<int>("boardID", &boardID);
81 m_tree->Branch<UChar_t>("lay", &lay);
82 m_tree->Branch<Float_t>("weight", &weight);
83 m_tree->Branch<UShort_t>("IWire", &IWire);
84 m_tree->Branch<Float_t>("Pval", &Pval);
85 m_tree->Branch<Float_t>("ndf", &ndf);
87 m_tree->Branch<Float_t>("d0", &d0);
88 m_tree->Branch<Float_t>("z0", &z0);
89 m_tree->Branch<Float_t>("phi0", &phi0);
90 m_tree->Branch<Float_t>("tanL", &tanL);
91 m_tree->Branch<Float_t>("omega", &omega);
92 }
93
94 if (m_calExpectedDriftTime) { // expected drift time, calculated form xfit
95 m_tree->Branch<Float_t>("t_fit", &t_fit);
96 }
97
98 registerObject<TTree>("tree", m_tree);
99 }
100 if (m_effStudy) { //if m_effStudy is changed to true prepares to only run wire efficiency study
101 auto m_efftree = new TTree(m_effTreeName.c_str(), "tree for wire efficiency");
102 m_efftree->Branch<unsigned short>("layerID", &layerID);
103 m_efftree->Branch<unsigned short>("wireID", &wireID);
104 m_efftree->Branch<float>("z", &z);
105 m_efftree->Branch<bool>("isFound", &isFound);
106
107 registerObject<TTree>("efftree", m_efftree);
108 }
109
110 auto m_hNDF = new TH1F("hNDF", "NDF of fitted track;NDF;Tracks", 71, -1, 70);
111 auto m_hPval = new TH1F("hPval", "p-values of tracks;pVal;Tracks", 1000, 0, 1);
112 auto m_hEventT0 = new TH1F("hEventT0", "Event T0", 1000, -100, 100);
113 auto m_hNTracks = new TH1F("hNTracks", "Number of tracks", 50, 0, 10);
114 auto m_hOccupancy = new TH1F("hOccupancy", "occupancy", 100, 0, 1.0);
115
116 registerObject<TH1F>("hNDF", m_hNDF);
117 registerObject<TH1F>("hPval", m_hPval);
118 registerObject<TH1F>("hEventT0", m_hEventT0);
119 registerObject<TH1F>("hNTracks", m_hNTracks);
120 registerObject<TH1F>("hOccupancy", m_hOccupancy);
121
122 ROOT::Math::XYZVector pos(0, 0, 0);
123 ROOT::Math::XYZVector bfield = BFieldManager::getFieldInTesla(pos);
124 if (bfield.Z() > 0.5) {
125 m_bField = true;
126 B2INFO("CDCCalibrationCollector: Magnetic field is ON");
127 } else {
128 m_bField = false;
129 B2INFO("CDCCalibrationCollector: Magnetic field is OFF");
130 }
131 B2INFO("BField at (0,0,0) = " << bfield.R());
132
133}
134
136{
138
139 /* CDCHit distribution */
140 // make evt t0 incase we dont use evt t0
141 evtT0 = 0;
142
143 //reject events don't have eventT0
145 // event with is fail to extract t0 will be exclude from analysis
146 if (m_eventTimeStoreObject.isValid() && m_eventTimeStoreObject->hasEventT0()) {
147 evtT0 = m_eventTimeStoreObject->getEventT0();
148 getObjectPtr<TH1F>("hEventT0")->Fill(evtT0);
149 } else {
150 return;
151 }
152 }
153 // Collects the WireID and Layer of every hit in this event
154 // Used in wire efficiency building
155 std::vector<unsigned short> wiresInCDCTrack;
156
157 for (CDCTrack& cdcTrack : *m_CDCTracks) {
158 for (CDCRecoHit3D& cdcHit : cdcTrack) {
159 unsigned short eWireID = cdcHit.getWire().getEWire();
160 wiresInCDCTrack.push_back(eWireID);
161 }
162 }
163 // WireID collection finished
164
165 const int nTr = m_Tracks.getEntries();
166 const int nHits = m_CDCHits.getEntries();
167 const int nWires = 14336;
168 float oc = static_cast<float>(nHits) / static_cast<float>(nWires);
169 getObjectPtr<TH1F>("hNTracks")->Fill(nTr);
170 getObjectPtr<TH1F>("hOccupancy")->Fill(oc);
171
172 for (int i = 0; i < nTr; ++i) {
173 const Belle2::Track* b2track = m_Tracks[i];
175 if (!fitresult) {
176 B2WARNING("No track fit result found.");
177 continue;
178 }
179
181 if (!recoTrack) {
182 B2WARNING("Can not access RecoTrack of this Belle2::Track");
183 continue;
184 }
185 const genfit::FitStatus* fs = recoTrack->getTrackFitStatus();
186 if (!fs) continue;
187 ndf = fs->getNdf();
188 if (!m_bField) {
189 ndf += 1;
190 }
191
192 getObjectPtr<TH1F>("hPval")->Fill(Pval);
193 getObjectPtr<TH1F>("hNDF")->Fill(ndf);
194 B2DEBUG(99, "ndf = " << ndf);
195 B2DEBUG(99, "Pval = " << Pval);
196
197 if (ndf < m_minimumNDF) continue;
198 double Chi2 = fs->getChi2();
199 Pval = std::max(0., ROOT::Math::chisquared_cdf_c(Chi2, ndf));
200 //store track parameters
201
202 d0 = fitresult->getD0();
203 z0 = fitresult->getZ0();
204 tanL = fitresult->getTanLambda();
205 omega = fitresult->getOmega();
206 phi0 = fitresult->getPhi0() * 180 / M_PI;
207
208 // Rejection of suspicious cosmic tracks.
209 // phi0 of cosmic track must be negative in our definition!
210 if (m_isCosmic == true && phi0 > 0.0) continue;
211
212 //cut at Pt
213 if (fitresult->getTransverseMomentum() < m_minimumPt) continue;
214 if (!m_effStudy) { // all harvest to fill the tree if collecting calibration info
215 try {
216 harvest(recoTrack);
217 } catch (const genfit::Exception& e) {
218 B2ERROR("Exception when harvest information from recotrack: " << e.what());
219 }
220 }
221 if (m_effStudy) { // call buildEfficiencies for efficiency study
222 // Request tracks coming from IP
223 if (fitresult->getD0() > 2 || fitresult->getZ0() > 5) continue;
224 const Helix helixFit = fitresult->getHelix();
225 buildEfficiencies(wiresInCDCTrack, helixFit);
226 }
227 }
228
229}
230
234
236{
237 B2DEBUG(99, "start collect hit");
238 static CDCGeometryPar& cdcgeo = CDCGeometryPar::Instance();
240
241 for (const RecoHitInformation::UsedCDCHit* hit : track->getCDCHitList()) {
242 const genfit::TrackPoint* tp = track->getCreatedTrackPoint(track->getRecoHitInformation(hit));
243 if (!tp) continue;
244 lay = hit->getICLayer();
245 IWire = hit->getIWire();
246 adc = hit->getADCCount();
247 unsigned short tdc = hit->getTDCCount();
248 WireID wireid(lay, IWire);
249 const genfit::KalmanFitterInfo* kfi = tp->getKalmanFitterInfo();
250 if (!kfi) {B2DEBUG(199, "No Fitter Info: Layer " << hit->getICLayer()); continue;}
251 for (unsigned int iMeas = 0; iMeas < kfi->getNumMeasurements(); ++iMeas) {
252 if ((kfi->getWeights().at(iMeas)) > 0.5) {
253 // int boardID = cdcgeo.getBoardID(WireID(lay,IWire));
254 const genfit::MeasuredStateOnPlane& mop = kfi->getFittedState();
255 const TVector3 pocaOnWire = mop.getPlane()->getO();//Local wire position
256 const TVector3 pocaOnTrack = mop.getPlane()->getU();//residual direction
257 const TVector3 pocaMom = mop.getMom();
258 alpha = cdcgeo.getAlpha(pocaOnWire, pocaMom) ;
259 theta = cdcgeo.getTheta(pocaMom);
260 x_mea = kfi->getMeasurementOnPlane(iMeas)->getState()(0);
261 x_b = kfi->getFittedState(true).getState()(3);// x fit biased
262 x_u = kfi->getFittedState(false).getState()(3);//x fit unbiased
263 weight = kfi->getWeights().at(iMeas);
264
265 int lr;
266 if (x_u > 0) lr = 1;
267 else lr = 0;
268
269 //Convert to outgoing
270 if (std::abs(alpha) > M_PI / 2) {
271 x_b *= -1;
272 x_u *= -1;
273 }
274 x_mea = copysign(x_mea, x_b);
275 lr = cdcgeo.getOutgoingLR(lr, alpha);
277 alpha = cdcgeo.getOutgoingAlpha(alpha);
278
279 B2DEBUG(99, "x_unbiased " << x_u << " |left_right " << lr);
280 if (m_calExpectedDriftTime) { t_fit = cdcgeo.getDriftTime(std::abs(x_u), lay, lr, alpha, theta);}
281 alpha *= 180 / M_PI;
282 theta *= 180 / M_PI;
283 //estimate drift time
284 t = tdcTrans->getDriftTime(tdc, wireid, mop.getTime(), pocaOnWire.Z(), adc);
285 getObjectPtr<TTree>("tree")->Fill();
286 } //NDF
287 // }//end of if isU
288 }//end of for
289 }//end of for tp
290}//end of func
291
292const CDCWire& CDCCalibrationCollectorModule::getIntersectingWire(const ROOT::Math::XYZVector& xyz, const CDCWireLayer& layer,
293 const Helix& helixFit) const
294{
295 ROOT::Math::XYZVector crosspoint;
296 if (layer.isAxial())
297 crosspoint = xyz;
298 else {
299 const CDCWire& oneWire = layer.getWire(1);
300 double newR = oneWire.getWirePos2DAtZ(xyz.Z()).R();
301 double arcLength = helixFit.getArcLength2DAtCylindricalR(newR);
302 crosspoint = helixFit.getPositionAtArcLength2D(arcLength);
303 }
304
305 const CDCWire& wire = layer.getClosestWire(crosspoint);
306
307 return wire;
308}
309
310void CDCCalibrationCollectorModule::buildEfficiencies(std::vector<unsigned short> wireHits, const Helix helixFit)
311{
312 static const CDCWireTopology& wireTopology = CDCWireTopology::getInstance();
313 for (const CDCWireLayer& wireLayer : wireTopology.getWireLayers()) {
314 const double radiusofLayer = wireLayer.getRefCylindricalR();
315 //simple extrapolation of fit
316 const double arcLength = helixFit.getArcLength2DAtCylindricalR(radiusofLayer);
317 const ROOT::Math::XYZVector xyz = B2Vector3D(helixFit.getPositionAtArcLength2D(arcLength));
318 if (!xyz.X()) continue;
319 const CDCWire& wireIntersected = getIntersectingWire(xyz, wireLayer, helixFit);
320 unsigned short crossedWire = wireIntersected.getEWire();
321 unsigned short crossedCWire = wireIntersected.getNeighborCW()->getEWire();
322 unsigned short crossedCCWire = wireIntersected.getNeighborCCW()->getEWire();
323
324 if (find(wireHits.begin(), wireHits.end(), crossedWire) != wireHits.end()
325 || find(wireHits.begin(), wireHits.end(), crossedCWire) != wireHits.end()
326 || find(wireHits.begin(), wireHits.end(), crossedCCWire) != wireHits.end())
327 isFound = true;
328 else
329 isFound = false;
330
331 wireID = wireIntersected.getIWire();
332 layerID = wireIntersected.getICLayer();
333 z = xyz.Z();
334 getObjectPtr<TTree>("efftree")->Fill();
335 }
336}
337
338
339
static ROOT::Math::XYZVector getFieldInTesla(const ROOT::Math::XYZVector &pos)
return the magnetic field at a given position in Tesla.
StoreObjPtr< EventT0 > m_eventTimeStoreObject
Event t0 object.
std::string m_recoTrackArrayName
Belle2::RecoTrack StoreArray name.e.
bool m_calExpectedDriftTime
Calculate expected drift time from x_fit or not.
double m_minimumNDF
minimum NDF required for track
std::string m_effTreeName
Name of efficiency tree for the output file.
TrackingUtilities::StoreWrappedObjPtr< std::vector< TrackingUtilities::CDCTrack > > m_CDCTracks
CDC tracks.
std::string m_cdcTrackVectorName
Belle2::CDCTrack vectorpointer name.
bool m_storeTrackParams
Store Track parameter or not.
StoreArray< TrackFitResult > m_TrackFitResults
Track fit results.
void harvest(Belle2::RecoTrack *track)
collect hit information of fitted track.
std::string m_cdcHitArrayName
Belle2::CDCHit StoreArray name.
Float_t theta
Entrance Polar angle of hit (degree).
std::string m_relRecoTrackTrackName
Relation between RecoTrack and Belle2:Track.
unsigned short wireID
wireID for hit-level wire monitoring
void collect() override
Event action, collect information for calibration.
bool m_isCosmic
true when we process cosmic events, else false (collision).
Float_t t_fit
Drift time calculated from x_fit.
bool m_eventT0Extraction
use Event T0 extract t0 or not.
void buildEfficiencies(std::vector< unsigned short > wireHits, const Helix helixFit)
fills efficiency objects
bool m_bField
fit incase no magnetic Field of not, if false, NDF=4 in cal P-value
std::string m_trackArrayName
Belle2::Track StoreArray name.
void prepare() override
Initializes the Module.
Float_t x_u
X_fit for unbiased track fit.
std::string m_trackFitResultArrayName
Belle2::TrackFitResult StoreArray name.
unsigned short layerID
layerID for hit-level wire monitoring
Float_t x_mea
measure drift length (signed by left right).
std::string m_treeName
Name of tree for the output file.
Float_t alpha
Entrance Azimuthal angle of hit (degree).
double m_minimumPt
minimum pt required for track
bool m_effStudy
When true module collects info only necessary for wire eff study.
float z
z of hit for hit-level wire monitoring
const CDCWire & getIntersectingWire(const ROOT::Math::XYZVector &xyz, const CDCWireLayer &layer, const Helix &helixFit) const
extrapolates the helix fit to a given layer and finds the wire which it would be hitting
bool isFound
flag for a hit that has been found near a track as expected by extrapolation
The Class for CDC Geometry Parameters.
double getTheta(const B2Vector3D &momentum) const
Returns track incident angle (theta in rad.).
double getAlpha(const B2Vector3D &posOnWire, const B2Vector3D &momentum) const
Returns track incident angle in rphi plane (alpha in rad.).
double getOutgoingAlpha(const double alpha) const
Converts incoming- to outgoing-alpha.
unsigned short getOutgoingLR(const unsigned short lr, const double alpha) const
Converts incoming-lr to outgoing-lr.
double getOutgoingTheta(const double alpha, const double theta) const
Converts incoming- to outgoing-theta.
double getDriftTime(double dist, unsigned short layer, unsigned short lr, double alpha, double theta) const
Return the drift time to the sense wire.
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
Class representing a sense wire layer in the central drift chamber.
Class representing the sense wire arrangement in the whole of the central drift chamber.
const std::vector< CDCWireLayer > & getWireLayers() const
Getter for the underlying storing layer vector.
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
Class representing a sense wire in the central drift chamber.
Definition CDCWire.h:50
TrackingUtilities::MayBePtr< const CDCWire > getNeighborCCW() const
Gives the closest neighbor in the counterclockwise direction - always exists.
Definition CDCWire.cc:159
IWire getIWire() const
Getter for the wire id within its layer.
Definition CDCWire.h:138
ROOT::Math::XYVector getWirePos2DAtZ(const double z) const
Gives the xy projected position of the wire at the given z coordinate.
Definition CDCWire.h:184
unsigned short getEWire() const
Getter for the encoded wire number.
Definition CDCWire.h:123
ILayer getICLayer() const
Getter for the continuous layer id ranging from 0 - 55.
Definition CDCWire.h:142
TrackingUtilities::MayBePtr< const CDCWire > getNeighborCW() const
Gives the closest neighbor in the clockwise direction - always exists.
Definition CDCWire.cc:164
Helix parameter class.
Definition Helix.h:48
Translator mirroring the realistic Digitization.
double getDriftTime(unsigned short tdcCount, const WireID &wireID, double timeOfFlightEstimator, double z, unsigned short adcCount) override
Get Drift time.
void registerObject(std::string name, T *obj)
Register object with a name, takes ownership, do not access the pointer beyond prepare()
CalibrationCollectorModule()
Constructor. Sets the default prefix for calibration dataobjects.
T * getObjectPtr(std::string name)
Calls the CalibObjManager to get the requested stored collector data.
static const ChargedStable muon
muon particle
Definition Const.h:660
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
CDCHit UsedCDCHit
Define, use of CDC hits as CDC hits (for symmetry).
This is the Reconstruction Event-Data Model Track.
Definition RecoTrack.h:79
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.
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.
Values of the result of a track fit with a given particle hypothesis.
Helix getHelix() const
Conversion to framework Helix (without covariance).
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.
double getPhi0() const
Getter for phi0.
Class that bundles various TrackFitResults.
Definition Track.h:25
const TrackFitResult * getTrackFitResultWithClosestMass(const Const::ChargedStable &requestedType) const
Return the track fit for the fit hypothesis with the closest mass.
Definition Track.cc:104
Class representing a three dimensional reconstructed hit.
Class representing a sequence of three dimensional reconstructed hits.
Definition CDCTrack.h:39
Class to identify a wire inside the CDC.
Definition WireID.h:34
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
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition B2Vector3.h:516
Abstract base class for different kinds of events.