Belle II Software  release-08-01-10
PXDDQMTrackRawNtupleModule.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 <pxd/modules/pxdDQM/PXDDQMTrackRawNtupleModule.h>
10 #include <tracking/dataobjects/ROIid.h>
11 
12 #include <pxd/reconstruction/PXDPixelMasker.h>
13 #include <mdst/dataobjects/Track.h>
14 #include <framework/gearbox/Const.h>
15 
16 #include "TMatrixDSym.h"
17 using namespace Belle2;
18 
19 //-----------------------------------------------------------------
20 // Register the Module
21 //-----------------------------------------------------------------
22 REG_MODULE(PXDDQMTrackRawNtuple);
23 
24 //-----------------------------------------------------------------
25 // Implementation
26 //-----------------------------------------------------------------
27 
28 PXDDQMTrackRawNtupleModule::PXDDQMTrackRawNtupleModule() : Module(), m_vxdGeometry(VXD::GeoCache::getInstance())
29 {
30  // Set module properties
31  setDescription("Create tuple PXD trigger studies");
32 
33  // setPropertyFlags(c_ParallelProcessingCertified);// for ntuple not certified!!!
34 
35  // Parameter definitions
36  addParam("ntupleName", m_ntupleName, "name of ntuple file", std::string("rawhits.root"));
37  addParam("pxdHitsName", m_pxdHitsName, "name of StoreArray with PXD raw hits", std::string(""));
38  addParam("recoTracksName", m_recoTracksName, "name of StoreArray with RecoTracks", std::string(""));
39  addParam("tracksName", m_tracksName, "name of StoreArray with Tracks", std::string(""));
40  addParam("PXDInterceptListName", m_PXDInterceptListName, "name of the list of interceptions", std::string(""));
41  addParam("useAlignment", m_useAlignment, "if true the alignment will be used", true);
42  addParam("pCut", m_pcut, "Set a cut on the track fit p-value (0=no cut)", double(1e-20));
43  addParam("minSVDHits", m_minSVDHits, "Number of SVD hits required in a track to be considered", 5u);
44  addParam("momCut", m_momCut, "Set a cut on the track momentum in GeV/c, 0 disables", double(0.3));
45  addParam("pTCut", m_pTCut, "Set a cut on the track transverse momentum (<=0 disable)", double(0.3));
46  addParam("uDist", m_uDist, "distance in ucell to intercept to accept hit", int(10));
47  addParam("vDist", m_vDist, "distance in vcell to intercept to accept hit", int(10));
48 }
49 
50 
52 {
53  auto dir = gDirectory;
54  if (m_tuple) {
55  if (m_file) { // no file -> no write to file
56  m_file->cd();
57  }
58  m_tuple->Write();
59  delete m_tuple;
60  m_tuple = nullptr;
61  }
62  if (m_file) {
63  m_file->Write();
64  m_file->Close();
65  delete m_file;
66  m_file = nullptr;
67  }
68  dir->cd();
69 }
70 
71 
73 {
74  m_file = new TFile(m_ntupleName.data(), "recreate");
75  if (m_file) m_file->cd();
76  m_tuple = new TNtuple("trackraw", "trackraw", "vxdid:u:v:p:pt:framenr:triggergate");
77 
78  //register the required arrays
79  //Register as optional so validation for cases where they are not available still succeeds, but module will not do any meaningful work without them
80  m_pxdhits.isOptional(m_pxdHitsName);
82  m_tracks.isOptional(m_tracksName);
84  m_storeDAQEvtStats.isRequired();
85 }
86 
87 
89 {
90  if (!m_pxdhits.isValid()) {
91  B2INFO("PXDHits array is missing, will not do anything");
92  return;
93  }
94  if (!m_recoTracks.isValid()) {
95  B2INFO("RecoTrack array is missing, will not do anything");
96  return;
97  }
98  if (!m_tracks.isValid()) {
99  B2INFO("Track array is missing, will not do anything");
100  return;
101  }
102  if (!m_intercepts.isValid()) {
103  B2INFO("Intercept array is missing, will not do anything");
104  return;
105  }
106 
107  std::map<unsigned int, int> triggergate;
108  auto evt = *m_storeDAQEvtStats;
109  for (auto& pkt : evt) {
110  for (auto& dhc : pkt) {
111  for (auto& dhe : dhc) {
112  triggergate[(unsigned int)dhe.getSensorID()] = dhe.getTriggerGate();
113  // dhe.getFirstDataGate());
114  }
115  }
116  }
117 
118  for (auto& track : m_tracks) {
119  RelationVector<RecoTrack> recoTrack = track.getRelationsTo<RecoTrack>(m_recoTracksName);
120  if (!recoTrack.size()) continue;
121 
122  auto a_track = recoTrack[0];
123  //If fit failed assume position pointed to is useless anyway
124  if (!a_track->wasFitSuccessful()) continue;
125 
126  if (a_track->getNumberOfSVDHits() < m_minSVDHits) continue;
127 
128  RelationVector<PXDIntercept> interceptList = a_track->getRelationsTo<PXDIntercept>(m_PXDInterceptListName);
129  if (!interceptList.size()) continue;
130 
131  const genfit::FitStatus* fitstatus = a_track->getTrackFitStatus();
132  if (fitstatus->getPVal() < m_pcut) continue;
133 
134  genfit::MeasuredStateOnPlane trackstate;
135  trackstate = a_track->getMeasuredStateOnPlaneFromFirstHit();
136  if (trackstate.getMom().Mag() < m_momCut) continue;
137  if (trackstate.getMom().Pt() < m_pTCut) continue;
138 
139  const TrackFitResult* ptr2 = track.getTrackFitResultWithClosestMass(Const::pion);
140  if (!ptr2) {
141  B2ERROR("expect a track fit result for mass");
142  continue;
143  }
144 
145  //loop over all PXD sensors to get the intersections
146  std::vector<VxdID> sensors = m_vxdGeometry.getListOfSensors();
147  for (auto intercept : interceptList) {
148  VxdID aVxdID = intercept.getSensorID();
149  auto& info = m_vxdGeometry.getSensorInfo(aVxdID);
150  //Search for intersections of the track with all PXD layers
151  //Traditional (aka the person before did it like this) method
152  //If there is a way to find out sensors crossed by a track directly, that would most likely be faster
153 
154  double u_fit = intercept.getCoorU();
155  double v_fit = intercept.getCoorV();
156 
157  int ucell_fit = info.getUCellID(u_fit); // check wie overflow!!!
158  int vcell_fit = info.getVCellID(v_fit); // Check wie overflow
159 
160  //loop the hits
161  for (auto& hit : m_pxdhits) {
162  //Do not consider as different if only segment differs!
163  //As of this writing segment is never filled for hits, but just to be sure
164  VxdID hitID = hit.getSensorID();
165  if (aVxdID.getLayerNumber() != hitID.getLayerNumber() ||
166  aVxdID.getLadderNumber() != hitID.getLadderNumber() ||
167  aVxdID.getSensorNumber() != hitID.getSensorNumber()) {
168  continue;
169  }
170  //only hit on the correct sensor and direction should survive
171 
172  auto u = hit.getUCellID();
173  auto v = hit.getVCellID();
174  if (abs(ucell_fit - u) < m_uDist && abs(vcell_fit - v) < m_vDist) {
175 
176  float fill[7] = {float((int)aVxdID), float(u), float(v), float(trackstate.getMom().Mag()), float(trackstate.getMom().Pt()), float(hit.getFrameNr()), float(triggergate[hitID])};
177  m_tuple->Fill(fill);
178  }
179  }
180  }
181  }
182 }
static const ChargedStable pion
charged pion particle
Definition: Const.h:652
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
StoreArray< PXDRawHit > m_pxdhits
store array of pxd clusters
void initialize() override final
initializes the need store arrays, trees and histograms
StoreObjPtr< PXDDAQStatus > m_storeDAQEvtStats
Input array for DAQ Status.
unsigned int m_minSVDHits
Required hits in SVD strips for tracks.
std::string m_PXDInterceptListName
intercept list name
double m_momCut
Cut on fitted track momentum.
void terminate() override final
terminate , save tuple to file if needed
void event() override final
main function which fills trees and histograms
StoreArray< Track > m_tracks
store array of tracks
std::string m_recoTracksName
name of the store array of recotracks
std::string m_pxdHitsName
name of the store array of pxd clusters
PXDDQMTrackRawNtupleModule()
Constructor: Sets the description, the properties and the parameters of the module.
StoreArray< PXDIntercept > m_intercepts
store array of PXD Intercepts
StoreArray< RecoTrack > m_recoTracks
store array of reco tracks
bool m_useAlignment
if true alignment will be used!
int m_uDist
distance in ucell to intercept to accept hit
TNtuple * m_tuple
pointer to opened tuple
int m_vDist
distance in vcell to intercept to accept hit
std::string m_tracksName
name of the store array of tracks
PXDIntercept stores the U,V coordinates and uncertainties of the intersection of a track with an PXD ...
Definition: PXDIntercept.h:22
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:79
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:288
Values of the result of a track fit with a given particle hypothesis.
const std::vector< VxdID > getListOfSensors() const
Get list of all sensors.
Definition: GeoCache.cc:59
const SensorInfoBase & getSensorInfo(Belle2::VxdID id) const
Return a referecne to the SensorInfo of a given SensorID.
Definition: GeoCache.cc:67
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:33
baseType getSensorNumber() const
Get the sensor id.
Definition: VxdID.h:100
baseType getLadderNumber() const
Get the ladder id.
Definition: VxdID.h:98
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:96
Class where important numbers and properties of a fit can be stored.
Definition: FitStatus.h:80
#StateOnPlane with additional covariance matrix.
REG_MODULE(arichBtest)
Register the Module.
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
Abstract base class for different kinds of events.