CbmRoot
Loading...
Searching...
No Matches
CbmRecoQa.cxx
Go to the documentation of this file.
1/* Copyright (C) 2019-2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Tim Fauerbach, Florian Uhlig [committer] */
4
10#include "CbmRecoQa.h"
11
12#include "CbmHit.h"
13#include "CbmLink.h"
14#include "CbmMCDataArray.h"
15#include "CbmMatch.h"
16#include "CbmMuchPixelHit.h"
17#include "CbmMuchPoint.h"
18#include "CbmMvdHit.h"
19#include "CbmMvdPoint.h"
20#include "CbmStsDigi.h"
21#include "CbmStsHit.h"
22#include "CbmStsPoint.h"
23#include "CbmTofHit.h"
24#include "CbmTofPoint.h"
25#include "CbmTrdHit.h"
26#include "CbmTrdPoint.h"
27#include "FairDetector.h"
28#include "FairMCPoint.h"
29#include "FairTask.h"
30#include "TClonesArray.h"
31#include "TFile.h"
32#include "TH1.h"
33#include "TH1F.h"
34#include "TList.h"
35#include "TROOT.h"
36#include "TString.h"
37#include "TTask.h"
38#include "TVector3.h"
39
40#include <Logger.h>
41
42#include <iostream>
43#include <string>
44#include <vector>
45
47
54CbmRecoQa::CbmRecoQa(std::vector<std::pair<std::string, std::array<int, 4>>> decNames, std::string outName,
55 int verbose_l)
56 : FairTask("CbmRecoQa")
57 , pullresfile(nullptr)
58 , verbosity(verbose_l)
59 , detectors(decNames)
60 , hists(std::vector<std::vector<TH1F*>>(detectors.size(), std::vector<TH1F*>(6)))
61 , eventList(nullptr)
62 , fManager(nullptr)
63 , mcManager(nullptr)
64 , outname(outName)
65{
66 if (!instance) {
67 instance = this;
68 };
69};
70
71// --- Destructor
73
74// --- Init and Re-Init
75InitStatus CbmRecoQa::ReInit() { return kSUCCESS; };
76
77InitStatus CbmRecoQa::Init()
78{
79
80 fManager = FairRootManager::Instance();
81 mcManager = (CbmMCDataManager*) fManager->GetObject("MCDataManager");
82 eventList = (CbmMCEventList*) fManager->GetObject("MCEventList.");
83
84 if (!mcManager) {
85 LOG(error) << "Could not initialise MCDataManager" << std::endl;
86 return kERROR;
87 }
88 if (!fManager) {
89 LOG(warn) << "Could not initialise FairRootManager" << std::endl;
90 return kERROR;
91 }
92
93 for (unsigned int i = 0; i < detectors.size(); i++) {
94
95 // Histogram Setup
96 std::string decName = detectors[i].first;
97 std::string px = "Px_" + decName;
98 std::string py = "Py_" + decName;
99 std::string pt = "Pt_" + decName;
100 std::string rx = "x_" + decName;
101 std::string ry = "y_" + decName;
102 std::string rt = "t_" + decName;
103 std::string pullx = decName + " Pull x";
104 std::string pully = decName + " Pull y";
105 std::string pullt = decName + " Pull t";
106 std::string resx = decName + " Residual x";
107 std::string resy = decName + " Residual y";
108 std::string rest = decName + " Residual t";
109
110 TH1F* pullxHist = new TH1F(px.c_str(), pullx.c_str(), 100, -1 * detectors[i].second[0], detectors[i].second[0]);
111 TH1F* pullyHist = new TH1F(py.c_str(), pully.c_str(), 100, -1 * detectors[i].second[0], detectors[i].second[0]);
112 TH1F* pulltHist = new TH1F(pt.c_str(), pullt.c_str(), 100, -1 * detectors[i].second[0], detectors[i].second[0]);
113
114 TH1F* residualxHist = new TH1F(rx.c_str(), resx.c_str(), 100, -1 * detectors[i].second[1], detectors[i].second[1]);
115 TH1F* residualyHist = new TH1F(ry.c_str(), resy.c_str(), 100, -1 * detectors[i].second[2], detectors[i].second[2]);
116 TH1F* residualtHist = new TH1F(rt.c_str(), rest.c_str(), 100, -1 * detectors[i].second[3], detectors[i].second[3]);
117
118 // pullxHist->SetCanExtend(TH1::kAllAxes);
119 // pullyHist->SetCanExtend(TH1::kAllAxes);
120 // pulltHist->SetCanExtend(TH1::kAllAxes);
121 // residualxHist->SetCanExtend(TH1::kAllAxes);
122 // residualyHist->SetCanExtend(TH1::kAllAxes);
123 // residualtHist->SetCanExtend(TH1::kAllAxes);
124
125 pullxHist->GetXaxis()->SetTitle("Pull x");
126 pullyHist->GetXaxis()->SetTitle("Pull y");
127 pulltHist->GetXaxis()->SetTitle("Pull t");
128 residualxHist->GetXaxis()->SetTitle("Residual x");
129 residualyHist->GetXaxis()->SetTitle("Residual y");
130 residualtHist->GetXaxis()->SetTitle("Residual t");
131
132 hists[i][0] = pullxHist;
133 hists[i][1] = pullyHist;
134 hists[i][2] = pulltHist;
135 hists[i][3] = residualxHist;
136 hists[i][4] = residualyHist;
137 hists[i][5] = residualtHist;
138
139 if (verbosity > 1) LOG(info) << "CbmRecoQa Success Initiliasing Histograms for: " << decName;
140 }
141
142 return kSUCCESS;
143}
144
145// --- Finish Event
146// Record all Data for Detectors defined
148{
149
150 static int event = 0;
151 LOG(info) << "CbmRecoQa for Event " << event++;
152 for (unsigned int k = 0; k < detectors.size(); k++) {
154 }
155}
156
157// --- Finish Task
158// Save Data in File
160{
162 TFile* oldFile = gFile;
163 TDirectory* oldDir = gDirectory;
164
165 std::string filename = outname + ".qa.hists.root";
166 pullresfile = new TFile(filename.c_str(), "Recreate");
167
168 for (unsigned int i = 0; i < hists.size(); i++) {
169 gDirectory->mkdir(detectors[i].first.c_str());
170 gDirectory->cd(detectors[i].first.c_str());
171 for (unsigned int k = 0; k < hists[i].size(); k++) {
172 if (verbosity > 2)
173 LOG(info) << "CbmRecoQa Histogramm " << hists[i][k]->GetName() << " Entries " << hists[i][k]->GetEntries();
174 if (hists[i][k]->GetEntries() > 0) hists[i][k]->Write();
175 }
176 gDirectory->cd("..");
177 }
178
179 pullresfile->Close();
180
182 gFile = oldFile;
183 gDirectory = oldDir;
184}
185
186// --- Write Data in Historgrams, depending on detectorw
187void CbmRecoQa::record(std::string decName, int decNum)
188{
189
190 if (verbosity > 1) LOG(info) << "CbmRecoQa Record called for: " << decName;
191
192 // Data aquasition
193
194 TClonesArray* listHits = nullptr;
195 TClonesArray* listHitMatches = nullptr;
196 // TClonesArray* listDigiMatches = nullptr;
197 CbmMCDataArray* listPoints = nullptr;
198 // TClonesArray* listClusters = nullptr; (FU) unused
199 TClonesArray* listClusterMatches = nullptr;
200
201 if (decName == "sts") {
202 listHits = (TClonesArray*) (fManager->GetObject("StsHit"));
203 listHitMatches = (TClonesArray*) (fManager->GetObject("StsHitMatch"));
204 // listClusters= (TClonesArray*)(fManager->GetObject("StsCluster")); (FU) unused
205 listClusterMatches = (TClonesArray*) (fManager->GetObject("StsClusterMatch"));
206 listPoints = mcManager->InitBranch("StsPoint");
207 if (listPoints == nullptr) {
208 LOG(warn) << "No StsPoint data!";
209 }
210
211
212 if (listHits && listHitMatches) {
213
214 TVector3 hitPos(.0, .0, .0);
215 TVector3 mcPos(.0, .0, .0);
216 TVector3 hitErr(.0, .0, .0);
217
218 int nEnt = listHits->GetEntriesFast();
219 if (verbosity > 2) LOG(info) << "CbmRecoQa for " << decName << " found " << nEnt << " Hit Entries";
220 for (int j = 0; j < nEnt; j++) {
221
222 float bestWeight = 0.f;
223 // int iMCPoint = -1; (FU) unused
224 CbmLink link;
225
226 CbmStsHit* curr_hit = dynamic_cast<CbmStsHit*>(listHits->At(j));
227 CbmStsPoint* curr_point = 0;
228
229 if (listClusterMatches) {
230
231 const CbmMatch* front_match =
232 static_cast<const CbmMatch*>(listClusterMatches->At(curr_hit->GetFrontClusterId()));
233 const CbmMatch* back_match =
234 static_cast<const CbmMatch*>(listClusterMatches->At(curr_hit->GetBackClusterId()));
235 CbmMatch curr_match;
236 if (verbosity > 3)
237 LOG(info) << "Front Match: " << front_match->ToString() << " Back Match: " << back_match->ToString();
238
239 for (int frontlink_c = 0; frontlink_c < front_match->GetNofLinks(); frontlink_c++) {
240
241 const CbmLink& frontLink = front_match->GetLink(frontlink_c);
242
243 for (int backlink_c = 0; backlink_c < back_match->GetNofLinks(); backlink_c++) {
244
245 const CbmLink& backLink = back_match->GetLink(backlink_c);
246 if (verbosity > 3)
247 LOG(info) << "FrontLink: " << frontLink.ToString() << " BackLink: " << backLink.ToString();
248 if (frontLink == backLink) {
249 curr_match.AddLink(frontLink);
250 curr_match.AddLink(backLink);
251 }
252 }
253 }
254
255 //LOG(info) << curr_match.ToString();
256
257 for (int iLink = 0; iLink < curr_match.GetNofLinks(); iLink++) {
258 float tmpweight = curr_match.GetLink(iLink).GetWeight();
259 //LOG(info) << " Match Link Nr.: " << iLink;
260 if (tmpweight > bestWeight) {
261 bestWeight = tmpweight;
262 // iMCPoint = curr_match.GetLink(iLink).GetIndex(); (FU) unused
263 link = curr_match.GetLink(iLink);
264 if (verbosity > 3) LOG(info) << "Found Link for current HIT";
265 }
266 }
267
268 curr_point = (CbmStsPoint*) (listPoints->Get(link.GetFile(), link.GetEntry(), link.GetIndex()));
269
270 if (curr_point == 0) continue;
271 double mcTime = curr_point->GetTime() + eventList->GetEventTime(link.GetEntry(), link.GetFile());
272
273 curr_hit->Position(hitPos);
274 curr_hit->PositionError(hitErr);
275 if (verbosity > 3) LOG(info) << "Calculated Position Error";
276
277 mcPos.SetX(curr_point->GetX(hitPos.Z()));
278 mcPos.SetY(curr_point->GetY(hitPos.Z()));
279 mcPos.SetZ(hitPos.Z());
280 if (verbosity > 3) LOG(info) << "Calculated MCPos";
281
282
283 if (hitErr.X() != 0) hists[decNum][0]->Fill((hitPos.X() - mcPos.X()) / curr_hit->GetDx());
284 if (hitErr.Y() != 0) hists[decNum][1]->Fill((hitPos.Y() - mcPos.Y()) / curr_hit->GetDy());
285 hists[decNum][2]->Fill((curr_hit->GetTime() - mcTime) / curr_hit->GetTimeError());
286
287 hists[decNum][3]->Fill((hitPos.X() - mcPos.X()) * 10 * 1000);
288 hists[decNum][4]->Fill((hitPos.Y() - mcPos.Y()) * 10 * 1000);
289 hists[decNum][5]->Fill(curr_hit->GetTime() - mcTime);
290 }
291 else {
292 LOG(warn) << "CBMRECOQA WARNING :-- No Sts Cluster Matches found!" << std::endl;
293 }
294 }
295 }
296 else {
297 LOG(warn) << "CBMRECOQA WARNING :-- NO Data for Reco QA found! "
298 << "Detector: " << decName << std::endl;
299 }
300 }
301 else if (decName == "mvd") {
302 listHits = (TClonesArray*) (fManager->GetObject("MvdHit"));
303 listHitMatches = (TClonesArray*) (fManager->GetObject("MvdHitMatch"));
304 //listClusters= (TClonesArray*)(fManager->GetObject("MvdCluster"));
305 //listClusterMatches = (TClonesArray*)(fManager->GetObject("MvdClusterMatch"));
306 TClonesArray* listMvdPoints = (TClonesArray*) (fManager->GetObject("MvdPoint"));
307
308 if (listHits && listHitMatches) {
309
310 TVector3 hitPos(.0, .0, .0);
311 TVector3 mcPos(.0, .0, .0);
312 TVector3 hitErr(.0, .0, .0);
313
314 int nEnt = listHits->GetEntriesFast();
315 if (verbosity > 2) LOG(info) << "CbmRecoQa for " << decName << " found " << nEnt << " Hit Entries";
316 for (int j = 0; j < nEnt; j++) {
317
318 int iMC = -1;
319
320 CbmMvdHit* curr_hit = dynamic_cast<CbmMvdHit*>(listHits->At(j));
321 CbmMatch* curr_match = dynamic_cast<CbmMatch*>(listHitMatches->At(j));
322 CbmMvdPoint* curr_point = 0;
323
324 if (curr_match->GetNofLinks() > 0) iMC = curr_match->GetLink(0).GetIndex();
325 if (iMC < 0) continue;
326
327 curr_point = (CbmMvdPoint*) (listMvdPoints->At(iMC));
328
329 if (curr_point == 0) continue;
330
331 curr_hit->Position(hitPos);
332 curr_hit->PositionError(hitErr);
333 if (verbosity > 3) LOG(info) << "Calculated Position Error";
334
335 mcPos.SetX((curr_point->GetX() + curr_point->GetXOut()) / 2.);
336 mcPos.SetY((curr_point->GetY() + curr_point->GetYOut()) / 2.);
337 mcPos.SetZ(hitPos.Z());
338 if (verbosity > 3) LOG(info) << "Calculated MCPos";
339
340
341 if (hitErr.X() != 0) hists[decNum][0]->Fill((hitPos.X() - mcPos.X()) / curr_hit->GetDx());
342 if (hitErr.Y() != 0) hists[decNum][1]->Fill((hitPos.Y() - mcPos.Y()) / curr_hit->GetDy());
343
344 hists[decNum][3]->Fill((hitPos.X() - mcPos.X()) * 10 * 1000);
345 hists[decNum][4]->Fill((hitPos.Y() - mcPos.Y()) * 10 * 1000);
346 }
347 }
348 else {
349 LOG(warn) << "CBMRECOQA WARNING :-- NO Data for Reco QA found! "
350 << "Detector: " << decName << std::endl;
351 }
352 }
353 else if (decName == "much") {
354
355 listHits = (TClonesArray*) (fManager->GetObject("MuchPixelHit"));
356 listHitMatches = (TClonesArray*) (fManager->GetObject("MuchPixelHitMatch"));
357 listPoints = mcManager->InitBranch("MuchPoint");
358 if (listPoints == nullptr) {
359 LOG(warn) << "No MuchPoint data!";
360 }
361
362 if (listHits && listHitMatches) {
363
364 TVector3 hitPos(.0, .0, .0);
365 TVector3 mcPos(.0, .0, .0);
366 TVector3 hitErr(.0, .0, .0);
367
368 int nEnt = listHits->GetEntriesFast();
369 if (verbosity > 2) LOG(info) << "CbmRecoQa for " << decName << " found " << nEnt << " Hit Entries";
370 for (int j = 0; j < nEnt; j++) {
371
372 float bestWeight = 0.f;
373 int iMC = -1;
374 CbmLink link;
375
376 CbmMuchPixelHit* curr_hit = dynamic_cast<CbmMuchPixelHit*>(listHits->At(j));
377 CbmMatch* curr_match = dynamic_cast<CbmMatch*>(listHitMatches->At(j));
378
379 for (int iLink = 0; iLink < curr_match->GetNofLinks(); iLink++) {
380 float tmpweight = curr_match->GetLink(iLink).GetWeight();
381 //LOG(info) << " Match Link Nr.: " << iLink;
382 if (tmpweight > bestWeight) {
383 bestWeight = tmpweight;
384 iMC = curr_match->GetLink(iLink).GetIndex();
385 link = curr_match->GetLink(iLink);
386 if (verbosity > 3) LOG(info) << "Found Link for current HIT";
387 }
388 }
389
390 if (iMC < 0) continue;
391
392 CbmMuchPoint* curr_point = (CbmMuchPoint*) (listPoints->Get(link.GetFile(), link.GetEntry(), link.GetEntry()));
393 double mcTime = curr_point->GetTime() + eventList->GetEventTime(link.GetEntry(), link.GetFile());
394 //LOG(info) << "Much Hit " << j << " Time: " << mcTime << " " << curr_hit->GetTime() << " " << curr_hit->GetTimeError();
395
396 if (curr_point == 0) continue;
397
398 curr_hit->Position(hitPos);
399 curr_hit->PositionError(hitErr);
400 if (verbosity > 3) LOG(info) << "Calculated Position Error";
401
402 mcPos.SetX((curr_point->GetXIn() + curr_point->GetXOut()) / 2.);
403 mcPos.SetY((curr_point->GetYIn() + curr_point->GetYOut()) / 2.);
404 mcPos.SetZ(hitPos.Z());
405 if (verbosity > 3) LOG(info) << "Calculated MCPos";
406
407 curr_hit->Position(hitPos);
408
409 if (hitErr.X() != 0) hists[decNum][0]->Fill((hitPos.X() - mcPos.X()) / curr_hit->GetDx());
410 if (hitErr.Y() != 0) hists[decNum][1]->Fill((hitPos.Y() - mcPos.Y()) / curr_hit->GetDy());
411 if (hitErr.Y() != 0) hists[decNum][2]->Fill(((curr_hit->GetTime() - mcTime) / curr_hit->GetTimeError()));
412
413 hists[decNum][3]->Fill((hitPos.X() - mcPos.X()));
414 hists[decNum][4]->Fill((hitPos.Y() - mcPos.Y()));
415 hists[decNum][5]->Fill(curr_hit->GetTime() - mcTime);
416 }
417 }
418 else {
419 LOG(warn) << "CBMRECOQA WARNING :-- NO Data for Reco QA found! "
420 << "Detector: " << decName << std::endl;
421 }
422 }
423 else {
424 LOG(warn) << "CBMRECOQA WARNING :-- NO matching Detector found ! " << std::endl;
425 }
426}
ClassImp(CbmConverterManager)
Class for pixel hits in MUCH detector.
Data class for a reconstructed hit in the STS.
Class for hits in TRD detector.
static constexpr size_t size()
Definition KfSimdPseudo.h:2
bool first
double GetTimeError() const
Definition CbmHit.h:77
double GetTime() const
Definition CbmHit.h:76
Access to a MC data branch for time-based analysis.
TObject * Get(const CbmLink *lnk)
Task class creating and managing CbmMCDataArray objects.
CbmMCDataObject * GetObject(const char *name)
CbmMCDataArray * InitBranch(const char *name)
Container class for MC events with number, file and start time.
double GetEventTime(uint32_t event, uint32_t file)
Event start time.
const CbmLink & GetLink(int32_t i) const
Definition CbmMatch.h:39
int32_t GetNofLinks() const
Definition CbmMatch.h:42
void AddLink(const CbmLink &newLink)
Definition CbmMatch.cxx:47
virtual std::string ToString() const
Return string representation of the object.
Definition CbmMatch.cxx:25
double GetYOut() const
double GetYIn() const
double GetXOut() const
double GetXIn() const
double GetXOut() const
Definition CbmMvdPoint.h:67
double GetYOut() const
Definition CbmMvdPoint.h:68
double GetDy() const
Definition CbmPixelHit.h:76
void Position(TVector3 &pos) const
Copies hit position to pos.
double GetDx() const
Definition CbmPixelHit.h:75
void PositionError(TVector3 &dpos) const
Copies hit position error to pos.
int verbosity
Definition CbmRecoQa.h:29
virtual void FinishEvent()
CbmMCDataManager * mcManager
Definition CbmRecoQa.h:34
std::vector< std::vector< TH1F * > > hists
Definition CbmRecoQa.h:31
TFile * pullresfile
Definition CbmRecoQa.h:28
CbmRecoQa(std::vector< std::pair< std::string, std::array< int, 4 > > > decNames, std::string outName="test", int verbose_l=0)
Definition CbmRecoQa.cxx:54
FairRootManager * fManager
Definition CbmRecoQa.h:33
void record(std::string decName, int i)
virtual void FinishTask()
virtual InitStatus Init()
Definition CbmRecoQa.cxx:77
virtual InitStatus ReInit()
Definition CbmRecoQa.cxx:75
std::string outname
Definition CbmRecoQa.h:35
CbmMCEventList * eventList
Definition CbmRecoQa.h:32
std::vector< std::pair< std::string, std::array< int, 4 > > > detectors
Definition CbmRecoQa.h:30
static CbmRecoQa * instance
Definition CbmRecoQa.h:41
data class for a reconstructed 3-d hit in the STS
Definition CbmStsHit.h:35
int32_t GetFrontClusterId() const
Definition CbmStsHit.h:105
int32_t GetBackClusterId() const
Definition CbmStsHit.h:65
double GetX(double z) const
double GetY(double z) const
Hash for CbmL1LinkKey.