From b19620198b252d30123a9dfd5bfd3417c21620a5 Mon Sep 17 00:00:00 2001
From: Norbert Herrmann <n.herrmann@physi.uni-heidelberg.de>
Date: Sat, 20 Mar 2021 08:59:03 +0100
Subject: [PATCH] add updated MC point matches to output

---
 reco/detectors/tof/CbmTofEventClusterizer.cxx | 99 +++++++++++++------
 reco/detectors/tof/CbmTofEventClusterizer.h   |  6 +-
 2 files changed, 71 insertions(+), 34 deletions(-)

diff --git a/reco/detectors/tof/CbmTofEventClusterizer.cxx b/reco/detectors/tof/CbmTofEventClusterizer.cxx
index cf390bdd77..d713de4b3b 100644
--- a/reco/detectors/tof/CbmTofEventClusterizer.cxx
+++ b/reco/detectors/tof/CbmTofEventClusterizer.cxx
@@ -102,8 +102,7 @@ CbmTofEventClusterizer::CbmTofEventClusterizer(const char* name,
   , fChannelInfo(NULL)
   , fDigiBdfPar(NULL)
   , fTrbHeader(NULL)
-  , fTofPointsColl(NULL)
-  , fMcTracksColl(NULL)
+  , fTofDigiPointMatches(NULL)
   , fDigiMan(nullptr)
   , fEventsColl(nullptr)
   , fbWriteHitsInOut(writeDataInOut)
@@ -112,6 +111,7 @@ CbmTofEventClusterizer::CbmTofEventClusterizer(const char* name,
   , fTofDigiMatchColl(NULL)
   , fTofHitsCollOut(NULL)
   , fTofDigiMatchCollOut(NULL)
+  , fTofDigiPointMatchesOut(NULL)
   , fiNbHits(0)
   , fVerbose(verbose)
   , fStorDigi()
@@ -510,21 +510,12 @@ Bool_t CbmTofEventClusterizer::RegisterInputs() {
     return kFALSE;
   }  // if( NULL == fTofDigisColl)
 
-  /*
-	 fTofPointsColl  = (TClonesArray *) fManager->GetObject("TofPoint");
-	 if( NULL == fTofPointsColl)
-	 {
-	 LOG(error)<<"CbmTofEventClusterizer::RegisterInputs => Could not get the TofPoint TClonesArray!!!";
-	 return kFALSE;
-	 } // if( NULL == fTofPointsColl)
-
-	 fMcTracksColl   = (TClonesArray *) fManager->GetObject("MCTrack");
-	 if( NULL == fMcTracksColl)
-	 {
-	 LOG(error)<<"CbmTofEventClusterizer::RegisterInputs => Could not get the MCTrack TClonesArray!!!";
-	 return kFALSE;
-	 } // if( NULL == fMcTracksColl)
-	 */
+  fTofDigiPointMatches = fManager->InitObjectAs<std::vector<CbmMatch> const *>("TofDigiMatch");
+  if (NULL == fTofDigiPointMatches)
+	    LOG(info) << "No tof digi to point matches in the input file";
+  else
+	    LOG(info) << "Found tof digi to point matches in the input file";
+
 
   fEventsColl = dynamic_cast<TClonesArray*>(fManager->GetObject("Event"));
   if (NULL == fEventsColl)
@@ -597,16 +588,27 @@ Bool_t CbmTofEventClusterizer::RegisterOutputs() {
 
     rootMgr->Register(
       tHitDigiMatchBranchName, "Tof", fTofDigiMatchColl, fbWriteHitsInOut);
+
+    if(NULL != fTofDigiPointMatches) {
+      rootMgr->RegisterAny("TofDigiMatch", fTofDigiPointMatchesOut, fbWriteDigisInOut);
+    }
+
   } else {  // CbmEvent - mode
     //fTofCalDigisCollOut  = new TClonesArray("CbmTofDigi");
     fTofCalDigiVecOut    = new std::vector<CbmTofDigi>();
     fTofHitsCollOut      = new TClonesArray("CbmTofHit");
     fTofDigiMatchCollOut = new TClonesArray("CbmMatch", 100);
+
     //rootMgr->Register( "TofCalDigi","Tof", fTofCalDigisCollOut, fbWriteDigisInOut);
     rootMgr->RegisterAny("TofCalDigi", fTofCalDigiVecOut, fbWriteDigisInOut);
     rootMgr->Register(tHitBranchName, "Tof", fTofHitsCollOut, fbWriteHitsInOut);
     rootMgr->Register(
       tHitDigiMatchBranchName, "Tof", fTofDigiMatchCollOut, fbWriteHitsInOut);
+
+    if(NULL != fTofDigiPointMatches) {
+      fTofDigiPointMatchesOut = new std::vector<CbmMatch>();
+      rootMgr->RegisterAny("TofDigiMatch", fTofDigiPointMatchesOut, fbWriteDigisInOut);
+    }
   }
   LOG(info) << "out branches: " << tHitBranchName << ", "
             << tHitDigiMatchBranchName;
@@ -1415,9 +1417,9 @@ Bool_t CbmTofEventClusterizer::CreateHistos() {
       fDigiBdfPar->GetNbChan(iSmType, iRpcId),
       0,
       fDigiBdfPar->GetNbChan(iSmType, iRpcId),
-      15,
+      20,
       0,
-      15.);
+      20.);
 
     fhRpcDigiStatus[iDetIndx] = new TH2F(
       Form("cl_SmT%01d_sm%03d_rpc%03d_DigiStatus", iSmType, iSmId, iRpcId),
@@ -1433,19 +1435,20 @@ Bool_t CbmTofEventClusterizer::CreateHistos() {
       0,
       10.);
 
+    const Int_t NLogbin=40;
+    Double_t edge[NLogbin+1];
+    for (Int_t i=0; i<NLogbin+1; i++) edge[i]=TMath::Power(2,i);
     fhRpcDigiDTLD[iDetIndx] = new TH2F(
       Form("cl_SmT%01d_sm%03d_rpc%03d_DigiDTLD", iSmType, iSmId, iRpcId),
       Form("Time distance to last digi of Rpc #%03d in Sm %03d of type %d; "
-           "channel; t_{digi} - t_{previous digi} (s)",
+           "channel; t_{digi} - t_{previous digi} (ns)",
            iRpcId,
            iSmId,
            iSmType),
       fDigiBdfPar->GetNbChan(iSmType, iRpcId) * 2,
       0,
       fDigiBdfPar->GetNbChan(iSmType, iRpcId) * 2,
-      1000.,
-      0.,
-      5.);
+      NLogbin, edge);
 
     fhRpcDigiDTFD[iDetIndx] = new TH2F(
       Form("cl_SmT%01d_sm%03d_rpc%03d_DigiDTFD", iSmType, iSmId, iRpcId),
@@ -1498,7 +1501,7 @@ Bool_t CbmTofEventClusterizer::CreateHistos() {
     fhRpcCluRate10s[iDetIndx] = new TH1D(
       Form("cl_SmT%01d_sm%03d_rpc%03d_rate10s", iSmType, iSmId, iRpcId),
       Form("            Clu rate of Rpc #%03d in Sm %03d of type %d in last "
-           "10s; Time (s); Rate (Hz)",
+           "10s; Time (s); Counts per 100 #mus",
            iRpcId,
            iSmId,
            iSmType),
@@ -2459,6 +2462,7 @@ Bool_t CbmTofEventClusterizer::FillHistos() {
           StartAnalysisTime = pHit->GetTime();
           LOG(info) << "StartAnalysisTime set to " << StartAnalysisTime / 1.E9
                     << " s. ";
+          fdStartAna10s=StartAnalysisTime;
         }
         Int_t iDetId = (pHit->GetAddress() & DetMask);
 
@@ -5509,7 +5513,7 @@ Bool_t CbmTofEventClusterizer::BuildClusters() {
   Int_t iNbTofDigi = fTofDigiVec.size();
   //Int_t iNbTofDigi = fTofDigisColl->GetEntries();
   if (iNbTofDigi > 100000) {
-    LOG(warning) << "Too many digis in event " << fiNevtBuild;
+    LOG(warning) << "Too many TOF digis in event " << fiNevtBuild;
     return kFALSE;
   }
   if (bAddBeamCounterSideDigi) {
@@ -5534,6 +5538,11 @@ Bool_t CbmTofEventClusterizer::BuildClusters() {
                            pDigi->GetType());
         LOG(debug) << "Duplicated digi " << fTofDigiVec.size()
                    << " with address 0x" << std::hex << pDigiN->GetAddress();
+
+        if( NULL != fTofDigiPointMatches ) { // copy MC Match Object
+          const CbmMatch digiMatch = (CbmMatch) fTofDigiPointMatches->at(iDigInd);
+          ((std::vector<CbmMatch>*) fTofDigiPointMatches)->push_back((CbmMatch)digiMatch);
+        }
       }
     }
     iNbTofDigi = fTofDigiVec.size();
@@ -5585,13 +5594,12 @@ Bool_t CbmTofEventClusterizer::BuildClusters() {
         size_t iDigiCh = pDigi->GetChannel() * 2 + pDigi->GetSide();
         if (iDigiCh < fvTimeLastDigi[iDetIndx].size()) {
           if (fvTimeLastDigi[iDetIndx][iDigiCh] > 0) {
-            if (fdStartAna10s > 0.) {
-              Double_t dTimeAna10s = (pDigi->GetTime() - fdStartAna10s) / 1.E9;
-              if (dTimeAna10s < fdSpillDuration)
+            if (kTRUE) { // fdStartAna10s > 0.) {
+              //Double_t dTimeAna10s = (pDigi->GetTime() - fdStartAna10s) / 1.E9;
+              //if (dTimeAna10s < fdSpillDuration)
                 fhRpcDigiDTLD[iDetIndx]->Fill(
                   iDigiCh,
-                  (pDigi->GetTime() - fvTimeLastDigi[iDetIndx][iDigiCh])
-                    / 1.E9);
+                  (pDigi->GetTime() - fvTimeLastDigi[iDetIndx][iDigiCh]) );
             }
           }
           fvTimeLastDigi[iDetIndx][iDigiCh] = pDigi->GetTime();
@@ -7710,6 +7718,11 @@ Bool_t CbmTofEventClusterizer::CalibRawDigis() {
   LOG(debug) << "CbmTofEventClusterizer::BuildClusters: Sort "
              << fTofCalDigiVec->size() << " calibrated digis ";
   if (iNbTofDigi > 1) {
+	  std::vector<CbmTofDigi>* tTofCalDigiVec=nullptr;
+	if(NULL != fTofDigiPointMatches) {  // temporary copy
+		tTofCalDigiVec=new std::vector<CbmTofDigi>(*fTofCalDigiVec);
+	}
+
     //    fTofCalDigisColl->Sort(iNbTofDigi); // Time order again, in case modified by the calibration
     /// Sort the buffers of hits due to the time offsets applied
     std::sort(fTofCalDigiVec->begin(),
@@ -7727,8 +7740,32 @@ Bool_t CbmTofEventClusterizer::CalibRawDigis() {
                         }))
       LOG(warning)
         << "CbmTofEventClusterizer::BuildClusters: Sorting not successful ";
+
+    if(NULL != fTofDigiPointMatches) {  // generate updated MC point Match Collection
+	  UInt_t iDigiOrg=0;
+      LOG(info)<<Form("Fill MC Point Matches for %3lu, %3lu digis ",fTofCalDigiVec->size(),tTofCalDigiVec->size());
+      for (UInt_t iDigi=0; iDigi<fTofCalDigiVec->size(); iDigi++){
+      // find original Digi
+      	CbmTofDigi* outDigi = &(fTofCalDigiVec->at(iDigi));
+        Bool_t bFound=kFALSE;
+        while ( !bFound ) {
+          for( ; iDigiOrg<tTofCalDigiVec->size(); iDigiOrg++) {
+          	CbmTofDigi* orgDigi = &(tTofCalDigiVec->at(iDigiOrg));
+    	    if ( outDigi->GetAddress() == orgDigi->GetAddress() ) {
+              CbmMatch digiMatch = (CbmMatch) fTofDigiPointMatches->at(iDigiOrg);
+              ((std::vector<CbmMatch>*) fTofDigiPointMatchesOut)->push_back((CbmMatch)digiMatch);
+              LOG(info)<<Form("Copy MC Point Match for 0x%08x, time %8.2f from %3d to %3d ",
+            		  orgDigi->GetAddress(),outDigi->GetTime(),iDigiOrg,iDigi);
+              bFound=kTRUE;
+              break;
+    	    }
+    	    if ( iDigiOrg == tTofCalDigiVec->size()-1 ) iDigiOrg=0;
+          }
+        }
+      }
+      delete tTofCalDigiVec; // cleanup temporary vector
+    }
   }
-  //  }
 
   return kTRUE;
 }
diff --git a/reco/detectors/tof/CbmTofEventClusterizer.h b/reco/detectors/tof/CbmTofEventClusterizer.h
index 81df1eb10d..ab8bd9f7d5 100644
--- a/reco/detectors/tof/CbmTofEventClusterizer.h
+++ b/reco/detectors/tof/CbmTofEventClusterizer.h
@@ -15,7 +15,6 @@
 // Input/Output
 //class CbmTofPoint;
 class CbmTofHit;
-class CbmMatch;
 class CbmEvent;
 class CbmVertex;
 // Geometry
@@ -33,6 +32,7 @@ class TTrbHeader;
 // FAIR classes and includes
 #include "CbmTofAddress.h"  // in cbmdata/tof
 #include "CbmTofDigi.h"
+#include "CbmMatch.h"
 #include "FairTask.h"
 
 // ROOT Classes and includes
@@ -254,8 +254,7 @@ private:
   TTrbHeader* fTrbHeader;
 
   // Input variables
-  TClonesArray* fTofPointsColl;  // TOF MC points
-  TClonesArray* fMcTracksColl;   // MC tracks
+  const std::vector<CbmMatch>* fTofDigiPointMatches=nullptr;  // TOF MC point matchec
   //TClonesArray          * fTofDigisColl;     // TOF Digis
   std::vector<CbmTofDigi> fTofDigiVec {};               //! TOF Digis
   const std::vector<CbmTofDigi>* fT0DigiVec = nullptr;  //! T0 Digis
@@ -274,6 +273,7 @@ private:
     nullptr;                           //! // Calibrated TOF Digis
   TClonesArray* fTofHitsCollOut;       // TOF hits
   TClonesArray* fTofDigiMatchCollOut;  // TOF Digi Links
+  std::vector<CbmMatch>* fTofDigiPointMatchesOut=nullptr; // TOF Digi MC Point Matches
   Int_t fiNbHits;                      // Index of the CbmTofHit TClonesArray
 
   // Generic
-- 
GitLab