13 #include "FairRunAna.h"
16 #include "KFMCTrack.h"
18 #include "KFParticleTopoReconstructor.h"
19 #include "KFTopoPerformance.h"
23 #include "TClonesArray.h"
24 #include "TDatabasePDG.h"
42 const KFParticleTopoReconstructor* tr,
44 : FairTask(name, iVerbose)
45 , fMCTracksBranchName(
"MCTrack")
46 , fTrackMatchBranchName(
"StsTrackMatch")
47 , fMCTrackArray(nullptr)
48 , fMCTrackArrayEvent(nullptr)
50 , fTrackMatchArray(nullptr)
51 , fRecParticles(nullptr)
52 , fMCParticles(nullptr)
53 , fMatchParticles(nullptr)
54 , fSaveParticles(false)
55 , fSaveMCParticles(false)
56 , fTimeSliceMode(false)
57 , fOutFileName(outFileName)
59 , fEfffileName(
"Efficiency.txt")
60 , fTopoPerformance(nullptr)
61 , fPrintFrequency(100)
64 , fSuperEventAnalysis(false)
65 , fReferenceResults(
"./")
67 , fCheckDecayQA(false)
69 for (Int_t
i = 0;
i < 5;
i++)
75 TFile* curFile = gFile;
76 TDirectory* curDirectory = gDirectory;
83 "KFTopoReconstructor",
85 tr->GetKFParticleFinder()->GetReconstructionList());
88 gDirectory = curDirectory;
103 FairRootManager* ioman = FairRootManager::Instance();
106 Error(
"CbmKFParticleFinderQA::Init",
"RootManager not instantiated!");
116 FairRootManager* fManger = FairRootManager::Instance();
120 Error(
"CbmKFParticleFinderQA::Init",
"MC Data Manager not found!");
125 Error(
"CbmKFParticleFinderQA::Init",
"mc track array not found!");
131 Error(
"CbmKFParticleFinderQA::Init",
"MC Event List not found!");
141 Error(
"CbmKFParticleFinderQA::Init",
"track match array not found!");
148 ioman->Register(
"RecoParticles",
151 IsOutputBranchPersistent(
"RecoParticles"));
157 ioman->Register(
"KFMCParticles",
160 IsOutputBranchPersistent(
"KFMCParticles"));
164 ioman->Register(
"KFParticleMatch",
167 IsOutputBranchPersistent(
"KFParticleMatch"));
184 vector<vector<vector<int>>> indexMap(1);
185 indexMap[0].resize(nMCEvents);
187 int mcIndexOffset = 0;
189 for (
int iMCEvent = 0; iMCEvent < nMCEvents; iMCEvent++) {
198 for (Int_t iMC = 0; iMC <
nMCTracks; iMC++) {
209 kfMCTrack.SetPx(cbmMCTrack->
GetPx());
210 kfMCTrack.SetPy(cbmMCTrack->
GetPy());
211 kfMCTrack.SetPz(cbmMCTrack->
GetPz());
216 && ((TParticlePDG*) TDatabasePDG::Instance()->GetParticle(pdg)))
217 q = TDatabasePDG::Instance()->GetParticle(pdg)->Charge() / 3.0;
218 else if (pdg == 1000010020)
220 else if (pdg == -1000010020)
222 else if (pdg == 1000010030)
224 else if (pdg == -1000010030)
226 else if (pdg == 1000020030)
228 else if (pdg == -1000020030)
230 else if (pdg == 1000020040)
232 else if (pdg == -1000020040)
236 Double_t p = cbmMCTrack->
GetP();
239 kfMCTrack.SetMotherId(cbmMCTrack->
GetMotherId() + mcIndexOffset);
241 kfMCTrack.SetMotherId(-iMCEvent - 1);
242 kfMCTrack.SetQP(q / p);
243 kfMCTrack.SetPDG(pdg);
244 kfMCTrack.SetNMCPoints(0);
255 vector<int> trackMatch(ntrackMatches, -1);
257 for (
int iTr = 0; iTr < ntrackMatches; iTr++) {
262 Float_t bestWeight = 0.f;
263 Float_t totalWeight = 0.f;
264 Int_t mcTrackId = -1;
266 for (
int iLink = 0; iLink < stsTrackMatch->
GetNofLinks(); iLink++) {
271 link = stsTrackMatch->
GetLink(iLink);
279 if (bestWeight / totalWeight < 0.7)
continue;
286 if (TMath::Abs(
mcTracks[mcTrackId].PDG()) > 4000
287 && !(TMath::Abs(
mcTracks[mcTrackId].PDG()) == 1000010020
288 || TMath::Abs(
mcTracks[mcTrackId].PDG()) == 1000010030
289 || TMath::Abs(
mcTracks[mcTrackId].PDG()) == 1000020030
290 || TMath::Abs(
mcTracks[mcTrackId].PDG()) == 1000020040))
292 mcTracks[mcTrackId].SetReconstructed();
293 trackMatch[iTr] = mcTrackId;
305 for (
int iT = 0; iT < 4; iT++)
308 std::cout <<
"Topo reconstruction time"
310 <<
" ms" << std::endl;
312 <<
" ms" << std::endl;
314 <<
" ms" << std::endl;
316 <<
" ms" << std::endl;
317 std::cout <<
" KF Particle Finder " <<
fTime[3] /
fNEvents * 1.e3
318 <<
" ms" << std::endl;
323 for (
unsigned int iP = 0;
326 new ((*fRecParticles)[iP]) KFParticle(
332 for (
unsigned int iP = 0;
338 Short_t matchType = 0;
341 .IsMatchedWithPdg()))
360 new ((*fMCParticles)[iP])
372 Int_t ntrackMatches =
374 vector<int> trackMatch(ntrackMatches, -1);
383 for (
int iT = 0; iT < 4; iT++)
386 std::cout <<
"Topo reconstruction time"
387 <<
" Real = " << std::setw(10) <<
fTime[4] * 1.e3 <<
" ms"
389 std::cout <<
" Init " <<
fTime[0] * 1.e3 <<
" ms"
391 std::cout <<
" PV Finder " <<
fTime[1] * 1.e3 <<
" ms"
393 std::cout <<
" Sort Tracks " <<
fTime[2] * 1.e3 <<
" ms"
395 std::cout <<
" KF Particle Finder " <<
fTime[3] * 1.e3 <<
" ms"
399 TDirectory* curr = gDirectory;
400 TFile* currentFile = gFile;
408 Error(
"CbmKFParticleFinderQA::Finish",
409 "Please specify the decay to be analysed.");
421 std::fstream eff(
fEfffileName.Data(), std::fstream::out);
427 if (!obj->IsFolder())
430 TDirectory* cur = gDirectory;
431 TFile* currentFile = gFile;
433 TDirectory* sub = cur->GetDirectory(obj->GetName());
435 TList* listSub = (
static_cast<TDirectory*
>(obj))->GetList();
437 while (TObject* obj1 = it())
452 const bool saveReferenceResults)
const {
453 static const int nParameters = 7;
454 TString parameterName[nParameters] = {
"X",
"Y",
"Z",
"Px",
"Py",
"Pz",
"E"};
456 TH1F* histogram[nParameters * 2];
457 TF1* fit[nParameters * 2];
459 for (
int iParameter = 0; iParameter < nParameters; iParameter++) {
460 TString cloneResidualName =
461 TString(
"hResidual") + parameterName[iParameter];
462 histogram[iParameter] =
464 ->Clone(cloneResidualName.Data()));
466 new TF1(TString(
"fitResidual") + parameterName[iParameter],
468 histogram[iParameter]->GetXaxis()->GetXmin(),
469 histogram[iParameter]->GetXaxis()->GetXmax());
470 fit[iParameter]->SetLineColor(kRed);
471 histogram[iParameter]->Fit(TString(
"fitResidual")
472 + parameterName[iParameter],
475 histogram[iParameter]->GetXaxis()->GetXmin(),
476 histogram[iParameter]->GetXaxis()->GetXmax());
477 sigma[iParameter] = fit[iParameter]->GetParameter(2);
479 TString clonePullName = TString(
"hPull") + parameterName[iParameter];
480 histogram[iParameter + nParameters] =
482 ->Clone(clonePullName.Data()));
483 fit[iParameter + nParameters] =
484 new TF1(TString(
"fitPull") + parameterName[iParameter],
486 histogram[iParameter + nParameters]->GetXaxis()->GetXmin(),
487 histogram[iParameter + nParameters]->GetXaxis()->GetXmax());
488 fit[iParameter + nParameters]->SetLineColor(kRed);
489 histogram[iParameter + nParameters]->Fit(
490 TString(
"fitPull") + parameterName[iParameter],
493 histogram[iParameter + nParameters]->GetXaxis()->GetXmin(),
494 histogram[iParameter + nParameters]->GetXaxis()->GetXmax());
495 sigma[iParameter + nParameters] =
496 fit[iParameter + nParameters]->GetParameter(2);
499 if (saveReferenceResults) {
500 TCanvas fitCanvas(
"fitCanvas",
"fitCanvas", 1600, 800);
501 fitCanvas.Divide(4, 4);
503 int padMap[nParameters * 2] = {
504 1, 2, 3, 9, 10, 11, 12, 5, 6, 7, 13, 14, 15, 16};
505 for (
int iHisto = 0; iHisto < nParameters * 2; iHisto++) {
506 fitCanvas.cd(padMap[iHisto]);
507 histogram[iHisto]->Draw();
508 fit[iHisto]->Draw(
"same");
511 TString canvasFile = TString(
"FitQA_")
514 fitCanvas.SaveAs(canvasFile.Data());
517 for (
int iHisto = 0; iHisto < nParameters * 2; iHisto++)
518 if (fit[iHisto])
delete fit[iHisto];
525 TString referenceFileName =
528 TString qaFileName = TString(
"qa_")
533 while (!gSystem->AccessPathName(qaFileName)) {
536 qaFileName += iQAFile;
537 qaFileName += TString(
".root");
541 TFile* curFile = gFile;
542 TDirectory* curDirectory = gDirectory;
543 TFile* qaFile =
new TFile(qaFileName.Data(),
"RECREATE");
545 TString qaHistoName =
547 TH1F* qaHisto =
new TH1F(qaHistoName.Data(), qaHistoName.Data(), 16, 0, 16);
549 TString binLabel[16] = {
"#sigma_{x}",
563 "#varepsilon_{4#pi}",
564 "#varepsilon_{KFP}"};
565 for (
int iBin = 0; iBin < 16; iBin++)
566 qaHisto->GetXaxis()->SetBinLabel(iBin + 1, binLabel[iBin].Data());
568 for (
int iSigma = 0; iSigma < 14; iSigma++)
569 qaHisto->SetBinContent(iSigma + 1, sigma[iSigma]);
571 qaHisto->SetBinContent(
573 qaHisto->SetBinContent(
579 TFile* referenceFile =
new TFile(referenceFileName.Data(),
"READ");
580 if (referenceFile->IsOpen()) {
581 TH1F* referenceHisto = (TH1F*) referenceFile->Get(qaHistoName);
582 if (referenceHisto) {
584 for (
int iBin = 1; iBin <= 7; iBin++)
585 fTestOk &=
fabs(referenceHisto->GetBinContent(iBin)
586 - qaHisto->GetBinContent(iBin))
587 / referenceHisto->GetBinContent(iBin)
589 for (
int iBin = 8; iBin <= 14; iBin++)
590 fTestOk &=
fabs(referenceHisto->GetBinContent(iBin)
591 - qaHisto->GetBinContent(iBin))
592 / referenceHisto->GetBinContent(iBin)
594 for (
int iBin = 15; iBin <= 16; iBin++)
595 fTestOk &=
fabs(referenceHisto->GetBinContent(iBin)
596 - qaHisto->GetBinContent(iBin))
597 / referenceHisto->GetBinContent(iBin)
600 referenceFile->Close();
601 referenceFile->Delete();
610 gDirectory = curDirectory;