From 89c72128b743f0f40a554851cc937c4c67e48aab Mon Sep 17 00:00:00 2001
From: Dominik Smith <smith@th.physik.uni-frankfurt.de>
Date: Wed, 10 Mar 2021 17:14:52 +0100
Subject: [PATCH] CbmBuildEventsQA: Added histograms to display percentage of
 correct digis per event and percentage of found digis per event.

---
 reco/eventbuilder/digis/CbmBuildEventsQA.cxx | 47 ++++++++++++++++++--
 reco/eventbuilder/digis/CbmBuildEventsQA.h   |  9 ++++
 2 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/reco/eventbuilder/digis/CbmBuildEventsQA.cxx b/reco/eventbuilder/digis/CbmBuildEventsQA.cxx
index fe1ebea8f1..a7a3ddbd09 100644
--- a/reco/eventbuilder/digis/CbmBuildEventsQA.cxx
+++ b/reco/eventbuilder/digis/CbmBuildEventsQA.cxx
@@ -16,6 +16,7 @@
 #include "FairRootManager.h"
 
 #include "TClonesArray.h"
+#include "TH1.h"
 #include "TStopwatch.h"
 
 #include <cassert>
@@ -27,18 +28,40 @@ using namespace std;
 
 
 // =====   Constructor   =====================================================
-CbmBuildEventsQA::CbmBuildEventsQA() : FairTask("BuildEventsQA"), fEvents(NULL), fNofEntries(0) {}
+CbmBuildEventsQA::CbmBuildEventsQA()
+  : FairTask("BuildEventsQA")
+  , fEvents(NULL)
+  , fNofEntries(0)
+  , fOutFolder("BuildEventsQA", "Build Events QA")
+{
+}
 // ===========================================================================
 
 
 // =====   Destructor   ======================================================
-CbmBuildEventsQA::~CbmBuildEventsQA() {}
+CbmBuildEventsQA::~CbmBuildEventsQA() { DeInit(); }
 // ===========================================================================
 
+// =====   De-initialisation   =============================================
+void CbmBuildEventsQA::DeInit()
+{
+  fOutFolder.Clear();
+  histFolder = nullptr;
+  SafeDelete(fhCorrectDigiRatioAll);
+  SafeDelete(fhFoundDigiRatioAll);
+}
 
 // =====   Task initialisation   =============================================
 InitStatus CbmBuildEventsQA::Init()
 {
+  DeInit();
+
+  // --- Init histograms
+  histFolder            = fOutFolder.AddFolder("hist", "Histogramms");
+  fhCorrectDigiRatioAll = new TH1F("fhCorrectDigiRatioAll", "\% correct digis per event", 1001, 0., 100.1);
+  fhFoundDigiRatioAll   = new TH1F("fhFoundDigiRatioAll", "\% found digis per event", 1001, 0., 100.1);
+  histFolder->Add(fhCorrectDigiRatioAll);
+  histFolder->Add(fhFoundDigiRatioAll);
 
   // --- Get FairRootManager instance
   FairRootManager* ioman = FairRootManager::Instance();
@@ -75,7 +98,6 @@ InitStatus CbmBuildEventsQA::Init()
 // =====   Task execution   ==================================================
 void CbmBuildEventsQA::Exec(Option_t*)
 {
-
   // --- Time and counters
   TStopwatch timer;
   timer.Start();
@@ -155,6 +177,9 @@ void CbmBuildEventsQA::Exec(Option_t*)
                   << 100. * Double_t(nLinksCorrect) / Double_t(nLinks) << " % ";
         LOG(info) << "Digi percentage found " << nDigiCorrect << " / " << totEventDigis << " = "
                   << 100. * Double_t(nDigiCorrect) / Double_t(totEventDigis) << " % ";
+
+        fhCorrectDigiRatioAll->Fill(100. * Double_t(nLinksCorrect) / Double_t(nLinks));
+        fhFoundDigiRatioAll->Fill(100. * Double_t(nDigiCorrect) / Double_t(totEventDigis));
       }
       else {
         LOG(info) << GetName() << ": Detector " << CbmModuleList::GetModuleNameCaps(system)
@@ -227,6 +252,22 @@ void CbmBuildEventsQA::MatchEvent(CbmEvent* event)
 // ===========================================================================
 
 
+// =====  Finish task  =======================================================
+void CbmBuildEventsQA::Finish()
+{
+  fhCorrectDigiRatioAll->DrawCopy("colz", "");
+  fhFoundDigiRatioAll->DrawCopy("colz", "");
+
+  if (!FairRootManager::Instance() || !FairRootManager::Instance()->GetSink()) {
+    LOG(error) << "No sink found";
+    return;
+  }
+  FairSink* sink = FairRootManager::Instance()->GetSink();
+  sink->WriteObject(&fOutFolder, nullptr);
+}
+// ===========================================================================
+
+
 // =====  Get digi type  =====================================================
 ECbmDataType CbmBuildEventsQA::GetDigiType(ECbmModuleId system)
 {
diff --git a/reco/eventbuilder/digis/CbmBuildEventsQA.h b/reco/eventbuilder/digis/CbmBuildEventsQA.h
index 33c20d02b1..d7e3013b15 100644
--- a/reco/eventbuilder/digis/CbmBuildEventsQA.h
+++ b/reco/eventbuilder/digis/CbmBuildEventsQA.h
@@ -10,6 +10,7 @@
 #include <FairTask.h>
 
 class TClonesArray;
+class TH1F;
 class CbmDigiManager;
 class CbmEvent;
 
@@ -34,6 +35,7 @@ public:
 
   /** Task execution **/
   virtual void Exec(Option_t* opt);
+  void Finish();
 
   /** Add a reference detector **/
   void AddRefDetector(ECbmModuleId RefDetector) { fRefDetectors.push_back(RefDetector); }
@@ -44,9 +46,16 @@ private:
   TClonesArray* fEvents;                  ///< Input array (class CbmEvent)
   Int_t fNofEntries = 0;                  ///< Number of processed entries
 
+  TFolder* histFolder = nullptr;  /// subfolder for histograms
+  TFolder fOutFolder;             /// output folder with histos and canvases
+
   /** Task initialisation **/
   virtual InitStatus Init();
+  void DeInit();
 
+  /** Histograms **/
+  TH1F* fhCorrectDigiRatioAll = nullptr;  /// correct digis per event for all detectors
+  TH1F* fhFoundDigiRatioAll   = nullptr;  /// digis found per event for all detectors
 
   /** Match a reconstructed event to MC events+
 		 ** @param event Pointer to reconstructed event
-- 
GitLab