Belle II Software  release-08-01-10
SVDEventT0PerformanceTTreeModule.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 <svd/modules/svdPerformance/SVDEventT0PerformanceTTreeModule.h>
10 #include <framework/gearbox/Unit.h>
11 #include <framework/core/Environment.h>
12 #include <framework/dataobjects/EventMetaData.h>
13 #include <svd/dataobjects/SVDShaperDigit.h>
14 #include <svd/dataobjects/SVDTrueHit.h>
15 #include <svd/dataobjects/SVDEventInfo.h>
16 #include <vxd/dataobjects/VxdID.h>
17 #include <vxd/geometry/SensorInfoBase.h>
18 #include <tracking/dataobjects/RecoTrack.h>
19 #include <tracking/dataobjects/RecoHitInformation.h>
20 #include <genfit/TrackPoint.h>
21 #include <TVector3.h>
22 #include <TDirectory.h>
23 #include <Math/Boost.h>
24 #include <math.h>
25 #include <iostream>
26 #include <algorithm>
27 #include <mdst/dataobjects/Track.h>
28 #include <mdst/dataobjects/TrackFitResult.h>
29 #include <mdst/dataobjects/MCParticle.h>
30 #include <framework/datastore/RelationArray.h>
31 
32 // Unpackers
33 #include <trg/ecl/dataobjects/TRGECLUnpackerStore.h>
34 
35 // OnlineEventT0
36 #include <hlt/dataobjects/OnlineEventT0.h>
37 
38 using namespace Belle2;
39 using namespace std;
40 
41 
42 //-----------------------------------------------------------------
43 // Register the Module
44 //-----------------------------------------------------------------
45 REG_MODULE(SVDEventT0PerformanceTTree);
46 
47 //-----------------------------------------------------------------
48 // Implementation
49 //-----------------------------------------------------------------
50 
52 {
53  //Set module properties
54  setDescription("This module is used to create a TTree to study SVD eventT0 using clusters associated to tracks.");
55  //Parameter to take only specific RecoTracks as input
56  addParam("outputFileName", m_rootFileName, "Name of output root file.", std::string("SVDEventT0PerformanceTTree.root"));
57  addParam("recoTracksStoreArrayName", m_recoTracksStoreArrayName, "StoreArray name of the input and output RecoTracks.",
59 }
60 
62 {
64  recoTracks.isOptional();
66  m_EventT0.isOptional();
67 
68  TDirectory::TContext context;
69 
70  m_rootFilePtr = new TFile(m_rootFileName.c_str(), "RECREATE");
71 
72  //Tree for SVD clusters
73  //one fill per event!
74  m_t = new TTree("tree", "Tree for SVD clusters related to tracks");
75  m_t->Branch("exp", &m_exp, "exp/i");
76  m_t->Branch("run", &m_run, "run/i");
77  m_t->Branch("event", &m_event, "event/i");
78  m_t->Branch("trueEventT0", &m_trueEventT0, "trueEventT0/F");
79  m_t->Branch("eventT0", &m_eventT0, "eventT0/F");
80  m_t->Branch("eventT0Err", &m_eventT0Err, "eventT0Err/F");
81  m_t->Branch("svdEventT0", &m_svdEventT0, "svdEventT0/F");
82  m_t->Branch("svdEventT0Err", &m_svdEventT0Err, "svdEventT0Err/F");
83  m_t->Branch("svdOnlineEventT0", &m_svdOnlineEventT0, "svdOnlineEventT0/F");
84  m_t->Branch("svdOnlineEventT0Err", &m_svdOnlineEventT0Err, "svdOnlineEventT0Err/F");
85  m_t->Branch("cdcEventT0", &m_cdcEventT0, "cdcEventT0/F");
86  m_t->Branch("cdcEventT0Err", &m_cdcEventT0Err, "cdcEventT0Err/F");
87  m_t->Branch("cdcOnlineEventT0", &m_cdcOnlineEventT0, "cdcOnlineEventT0/F");
88  m_t->Branch("cdcOnlineEventT0Err", &m_cdcOnlineEventT0Err, "cdcOnlineEventT0Err/F");
89  m_t->Branch("topEventT0", &m_topEventT0, "topEventT0/F");
90  m_t->Branch("topEventT0Err", &m_topEventT0Err, "topEventT0Err/F");
91  m_t->Branch("topOnlineEventT0", &m_topOnlineEventT0, "topOnlineEventT0/F");
92  m_t->Branch("topOnlineEventT0Err", &m_topOnlineEventT0Err, "topOnlineEventT0Err/F");
93  m_t->Branch("eclEventT0", &m_eclEventT0, "eclEventT0/F");
94  m_t->Branch("eclEventT0Err", &m_eclEventT0Err, "eclEventT0Err/F");
95  m_t->Branch("eclOnlineEventT0", &m_eclOnlineEventT0, "eclOnlineEventT0/F");
96  m_t->Branch("eclOnlineEventT0Err", &m_eclOnlineEventT0Err, "eclOnlineEventT0Err/F");
97  m_t->Branch("eclTCEmax", &m_eclTCEmax, "eclTCEmax/I");
98  m_t->Branch("eclTCid", &m_eclTCid, "eclTCid/I");
99  m_t->Branch("totTracks", &m_nTracks, "totTracks/i");
100  m_t->Branch("TB", &m_svdTB, "TB/i");
101  m_t->Branch("trkNumb", &m_trkNumber);
102  m_t->Branch("trkd0", &m_svdTrkd0);
103  m_t->Branch("trkz0", &m_svdTrkz0);
104  m_t->Branch("trkp", &m_svdTrkp);
105  m_t->Branch("trkpT", &m_svdTrkpT);
106  m_t->Branch("trkpCM", &m_svdTrkpCM);
107  m_t->Branch("trkTheta", &m_svdTrkTheta);
108  m_t->Branch("trkPhi", &m_svdTrkPhi);
109  m_t->Branch("trkCharge", &m_svdTrkCharge);
110  m_t->Branch("trkPValue", &m_svdTrkPValue);
111  m_t->Branch("trkNDF", &m_svdTrkNDF);
112  m_t->Branch("trkPXDHits", &m_svdTrkPXDHits);
113  m_t->Branch("trkSVDHits", &m_svdTrkSVDHits);
114  m_t->Branch("trkCDCHits", &m_svdTrkCDCHits);
115  m_t->Branch("layer", &m_svdLayer);
116  m_t->Branch("ladder", &m_svdLadder);
117  m_t->Branch("sensor", &m_svdSensor);
118  m_t->Branch("clsSize", &m_svdSize);
119  m_t->Branch("clsIsU", &m_svdisUside);
120  m_t->Branch("clsCharge", &m_svdClCharge);
121  m_t->Branch("clsSNR", &m_svdClSNR);
122  m_t->Branch("clsPos", &m_svdClPos);
123  m_t->Branch("clsPosErr", &m_svdClPosErr);
124  m_t->Branch("clsTime", &m_svdClTime);
125  m_t->Branch("clsTimeErr", &m_svdClTimeErr);
126  m_t->Branch("trueTime", &m_svdTrueTime);
127 
128 
129 
130 }
131 
133 {
134  m_trueEventT0 = std::numeric_limits<float>::quiet_NaN();
135  m_eventT0 = std::numeric_limits<float>::quiet_NaN();
136  m_eventT0Err = std::numeric_limits<float>::quiet_NaN();
137  m_svdEventT0 = std::numeric_limits<float>::quiet_NaN();
138  m_svdEventT0Err = std::numeric_limits<float>::quiet_NaN();
139  m_svdOnlineEventT0 = std::numeric_limits<float>::quiet_NaN();
140  m_svdOnlineEventT0Err = std::numeric_limits<float>::quiet_NaN();
141  m_cdcEventT0 = std::numeric_limits<float>::quiet_NaN();
142  m_cdcEventT0Err = std::numeric_limits<float>::quiet_NaN();
143  m_cdcOnlineEventT0 = std::numeric_limits<float>::quiet_NaN();
144  m_cdcOnlineEventT0Err = std::numeric_limits<float>::quiet_NaN();
145  m_topEventT0 = std::numeric_limits<float>::quiet_NaN();
146  m_topEventT0Err = std::numeric_limits<float>::quiet_NaN();
147  m_topOnlineEventT0 = std::numeric_limits<float>::quiet_NaN();
148  m_topOnlineEventT0Err = std::numeric_limits<float>::quiet_NaN();
149  m_eclEventT0 = std::numeric_limits<float>::quiet_NaN();
150  m_eclEventT0Err = std::numeric_limits<float>::quiet_NaN();
151  m_eclOnlineEventT0 = std::numeric_limits<float>::quiet_NaN();
152  m_eclOnlineEventT0Err = std::numeric_limits<float>::quiet_NaN();
153  m_eclTCEmax = std::numeric_limits<int>::quiet_NaN();
154  m_eclTCid = std::numeric_limits<int>::quiet_NaN();
155 
156  StoreObjPtr<EventMetaData> evtMetaData;
157  m_exp = evtMetaData->getExperiment();
158  m_run = evtMetaData->getRun();
159  m_event = evtMetaData->getEvent();
160 
161  if (m_EventT0.isValid())
162  if (m_EventT0->hasEventT0()) {
163  m_eventT0 = (float)m_EventT0->getEventT0();
164  m_eventT0Err = (float)m_EventT0->getEventT0Uncertainty();
165 
166  if (m_EventT0->hasTemporaryEventT0(Const::EDetector::SVD)) {
167  const auto bestSVDEvtT0 = m_EventT0->getBestSVDTemporaryEventT0();
168  m_svdEventT0 = bestSVDEvtT0->eventT0 ;
169  m_svdEventT0Err = bestSVDEvtT0->eventT0Uncertainty;
170  }
171 
172  if (m_EventT0->hasTemporaryEventT0(Const::EDetector::CDC)) {
173  const auto bestCDCEvtT0 = m_EventT0->getBestCDCTemporaryEventT0();
174  m_cdcEventT0 = bestCDCEvtT0->eventT0 ;
175  m_cdcEventT0Err = bestCDCEvtT0->eventT0Uncertainty;
176  }
177 
178  if (m_EventT0->hasTemporaryEventT0(Const::EDetector::TOP)) {
179  const auto bestTOPEvtT0 = m_EventT0->getBestTOPTemporaryEventT0();
180  m_topEventT0 = bestTOPEvtT0->eventT0 ;
181  m_topEventT0Err = bestTOPEvtT0->eventT0Uncertainty;
182  }
183 
184  if (m_EventT0->hasTemporaryEventT0(Const::EDetector::ECL)) {
185  const auto bestECLEvtT0 = m_EventT0->getBestECLTemporaryEventT0();
186  m_eclEventT0 = bestECLEvtT0->eventT0 ;
187  m_eclEventT0Err = bestECLEvtT0->eventT0Uncertainty;
188  }
189  }
190 
191  StoreArray<OnlineEventT0> onlineEventT0;
192  for (auto& evt : onlineEventT0) {
193  if (evt.getOnlineEventT0Detector() == Const::EDetector::SVD) {
194  B2DEBUG(40, "OnlineEventT0 given by SVD");
195  m_svdOnlineEventT0 = evt.getOnlineEventT0();
196  m_svdOnlineEventT0Err = evt.getOnlineEventT0Uncertainty();
197  }
198 
199  if (evt.getOnlineEventT0Detector() == Const::EDetector::CDC) {
200  B2DEBUG(40, "OnlineEventT0 given by CDC");
201  m_cdcOnlineEventT0 = evt.getOnlineEventT0();
202  m_cdcOnlineEventT0Err = evt.getOnlineEventT0Uncertainty();
203  }
204 
205  if (evt.getOnlineEventT0Detector() == Const::EDetector::ECL) {
206  B2DEBUG(40, "OnlineEventT0 given by ECL");
207  m_eclOnlineEventT0 = evt.getOnlineEventT0();
208  m_eclOnlineEventT0Err = evt.getOnlineEventT0Uncertainty();
209  }
210 
211  if (evt.getOnlineEventT0Detector() == Const::EDetector::TOP) {
212  B2DEBUG(40, "OnlineEventT0 given by TOP");
213  m_topOnlineEventT0 = evt.getOnlineEventT0();
214  m_topOnlineEventT0Err = evt.getOnlineEventT0Uncertainty();
215  }
216  }
217 
218 
219  // clear all vectors
220  m_trkNumber.clear();
221  m_svdisUside.clear();
222  m_svdSize.clear();
223  m_svdSensor.clear();
224  m_svdLadder.clear();
225  m_svdLadder.clear();
226  m_svdTrkPhi.clear();
227  m_svdTrkTheta.clear();
228  m_svdTrkpCM.clear();
229  m_svdTrkp.clear();
230  m_svdTrkpT.clear();
231  m_svdTrkz0.clear();
232  m_svdTrkd0.clear();
233  m_svdTrkPValue.clear();
234  m_svdTrkCharge.clear();
235  m_svdTrkNDF.clear();
236  m_svdTrkCDCHits.clear();
237  m_svdTrkSVDHits.clear();
238  m_svdTrkPXDHits.clear();
239  m_svdTrueTime.clear();
240  m_svdClPosErr.clear();
241  m_svdClPos.clear();
242  m_svdClTimeErr.clear();
243  m_svdClTime.clear();
244  m_svdClSNR.clear();
245  m_svdClCharge.clear();
246 
247 
248  //first check SVDEventInfo name
249  StoreObjPtr<SVDEventInfo> temp_eventinfo("SVDEventInfo");
250  std::string m_svdEventInfoName = "SVDEventInfo";
251  if (!temp_eventinfo.isValid())
252  m_svdEventInfoName = "SVDEventInfoSim";
253  StoreObjPtr<SVDEventInfo> eventinfo(m_svdEventInfoName);
254  if (!eventinfo) B2ERROR("No SVDEventInfo!");
255  m_svdTB = eventinfo->getModeByte().getTriggerBin();
256 
257  bool isMC = Environment::Instance().isMC();
258 
259  int trkNumber = 0;
261  m_nTracks = recoTracks.getEntries();
262  for (const auto& trk : recoTracks) {
263  if (! trk.wasFitSuccessful()) {
264  m_nTracks -= 1;
265  continue;
266  }
267 
268 
269  RelationVector<Track> theTK = DataStore::getRelationsWithObj<Track>(&trk);
270 
271  if (theTK.size() == 0) {
272  m_nTracks -= 1;
273  continue;
274  }
275 
276  trkNumber ++;
277 
278  const vector<SVDCluster* > svdClusters = trk.getSVDHitList();
279  B2DEBUG(40, "FITTED TRACK: NUMBER OF SVD HITS = " << svdClusters.size());
280 
281  for (unsigned int i = 0; i < svdClusters.size(); i++) {
282 
283  const TrackFitResult* tfr = theTK[0]->getTrackFitResultWithClosestMass(Const::pion);
284 
285  if (tfr) {
286  m_svdTrkTheta.push_back(tfr->getMomentum().Theta());
287  m_svdTrkPhi.push_back(tfr->getMomentum().Phi());
288  m_svdTrkd0.push_back(tfr->getD0());
289  m_svdTrkz0.push_back(tfr->getZ0());
290  m_svdTrkp.push_back(tfr->getMomentum().R());
291  m_svdTrkpT.push_back(tfr->getMomentum().Rho());
292  m_svdTrkPValue.push_back(tfr->getPValue());
293  m_svdTrkCharge.push_back(tfr->getChargeSign());
294  m_svdTrkNDF.push_back(tfr->getNDF());
295  ROOT::Math::PxPyPzEVector pStar = tfr->get4Momentum();
296  ROOT::Math::BoostZ boost(3. / 11);
297  pStar = boost(pStar);
298  m_svdTrkpCM.push_back(pStar.P());
299  }
300 
301 
302 
303  m_svdTrkPXDHits.push_back((trk.getPXDHitList()).size());
304  m_svdTrkSVDHits.push_back((trk.getSVDHitList()).size());
305  m_svdTrkCDCHits.push_back((trk.getCDCHitList()).size());
306  m_trkNumber.push_back(trkNumber);
307 
308  const SVDCluster* svd_1 = svdClusters[i];
309 
310  //get true hits, used only if isMC
311  RelationVector<SVDTrueHit> trueHit_1 = DataStore::getRelationsWithObj<SVDTrueHit>(svd_1);
312 
313 
314 
315 
316  const RecoHitInformation* infoSVD_1 = trk.getRecoHitInformation(svd_1);
317  if (!infoSVD_1) {
318  continue;
319  }
320 
321  const VxdID svd_id_1 = svd_1->getSensorID();
322  m_svdLayer.push_back(svd_id_1.getLayerNumber());
323  m_svdLadder.push_back(svd_id_1.getLadderNumber());
324  m_svdSensor.push_back(svd_id_1.getSensorNumber());
325 
326  m_svdisUside.push_back(svd_1->isUCluster());
327 
328  m_svdSize.push_back(svd_1->getSize());
329 
330  m_svdClTime.push_back(svd_1->getClsTime());
331  m_svdClTimeErr.push_back(svd_1->getClsTimeSigma());
332  m_svdClSNR.push_back(svd_1->getSNR());
333  m_svdClCharge.push_back(svd_1->getCharge());
334  m_svdClPos.push_back(svd_1->getPosition());
335  m_svdClPosErr.push_back(svd_1->getPositionSigma());
336  if (isMC && trueHit_1.size() > 0) {
337 
338  m_svdTrueTime.push_back(trueHit_1[0]->getGlobalTime());
339 
340  RelationVector<MCParticle> mcParticle_1 = trueHit_1[0]->getRelationsFrom<MCParticle>();
341  if (mcParticle_1.size() > 0) {
342  if (mcParticle_1[0]->isPrimaryParticle())
343  m_trueEventT0 = mcParticle_1[0]->getProductionTime();
344  }
345  } else
346  m_svdTrueTime.push_back(std::numeric_limits<float>::quiet_NaN());
347  }
348  }
349 
351  for (const auto& trgHit : TRGECLData) {
352  int hitWin = trgHit.getHitWin();
353  B2DEBUG(40, "hitWin = " << hitWin);
354  if (hitWin != 3 && hitWin != 4) { continue; }
355  if (trgHit.getTCEnergy() > m_eclTCEmax) {
356  m_eclTCid = trgHit.getTCId();
357  m_eclTCEmax = trgHit.getTCEnergy();
358  }
359  }
360 
361  m_t->Fill();
362 
363 }
364 
365 
367 {
368 
369  if (m_rootFilePtr != nullptr) {
370 
371  TDirectory::TContext context;
372 
373  m_rootFilePtr->cd();
374  m_t->Write();
375  m_rootFilePtr->Close();
376  }
377 }
static const ChargedStable pion
charged pion particle
Definition: Const.h:652
bool isMC() const
Do we have generated, not real data?
Definition: Environment.cc:54
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
This class stores additional information to every CDC/SVD/PXD hit stored in a RecoTrack.
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Definition: SVDCluster.h:29
float getClsTime() const
Get average of waveform maximum times of cluster strip signals.
Definition: SVDCluster.h:134
float getSNR() const
Get cluster SNR.
Definition: SVDCluster.h:159
unsigned short getSize() const
Get cluster size.
Definition: SVDCluster.h:154
float getCharge() const
Get collected charge.
Definition: SVDCluster.h:144
VxdID getSensorID() const
Get the sensor ID.
Definition: SVDCluster.h:102
bool isUCluster() const
Get the direction of strips.
Definition: SVDCluster.h:110
float getPosition(double v=0) const
Get the coordinate of reconstructed hit.
Definition: SVDCluster.h:117
float getClsTimeSigma() const
Get the error of the reconstructed hit time.
Definition: SVDCluster.h:139
float getPositionSigma() const
Get the error of the reconstructed hit coordinate.
Definition: SVDCluster.h:129
std::vector< float > m_svdTrkTheta
polar angle of the track
std::vector< float > m_svdTrkPValue
pValue of the track
std::vector< float > m_svdTrkCharge
charge of the track
std::vector< float > m_svdClPosErr
cluster position error
std::vector< int > m_svdTrkSVDHits
number of SVD hits on the track
void initialize() override
Register input and output data.
std::vector< float > m_svdTrkPhi
azimuthal angle of the track
void event() override
Compute the variables and fill the tree.
std::vector< int > m_svdTrkPXDHits
number of PXD hits on the track
void terminate() override
Write the TTree to the file.
std::vector< float > m_svdTrkpCM
pCM of the track
std::vector< float > m_svdTrkNDF
pValue of the track
std::vector< int > m_svdTrkCDCHits
number of CDC hits on the track
std::vector< float > m_svdClTimeErr
cluster time error
TTree * m_t
tree containing info related to the clusters related to tracks
std::string m_recoTracksStoreArrayName
storeArray name of the input and output RecoTracks
TFile * m_rootFilePtr
pointer at root file used for storing histograms
std::vector< int > m_trkNumber
track number in the event
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:111
Values of the result of a track fit with a given particle hypothesis.
float getNDF() const
Getter for number of degrees of freedom of the track fit.
short getChargeSign() const
Return track charge (1 or -1).
double getPValue() const
Getter for Chi2 Probability of the track fit.
ROOT::Math::PxPyPzEVector get4Momentum() const
Getter for the 4Momentum at the closest approach of the track in the r/phi projection.
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.
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
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.