From 36fe3daedc0b92eeed7bdbe42e12fba7228b934a Mon Sep 17 00:00:00 2001
From: soul <soul@se51-99.jinr.ru>
Date: Mon, 20 Mar 2017 00:07:48 +0300
Subject: [PATCH] corrections of memory errors shown by valgrind and possible
 concerned with some segfaults

---
 clustering/ClusterLinkDef.h           |   2 +-
 etof/MpdEtofMatching.cxx              |  24 ++-
 lhetrack/MpdEctTrackFinderCpc.cxx     |   7 +-
 lhetrack/MpdKfPrimaryVertexFinder.cxx |   2 +-
 macro/profiling/Makefile              |   4 +-
 macro/profiling/reco.cxx              | 188 --------------------
 macro/profiling/runRecoCpc30.C        | 238 ++++++++++++++++++++++++++
 macro/profiling/valgrind.supp         |  16 ++
 tof/MpdTofHitProducerIdeal.cxx        |   8 +-
 tof/MpdTofMatching.cxx                |   4 +-
 10 files changed, 288 insertions(+), 205 deletions(-)
 delete mode 100644 macro/profiling/reco.cxx
 create mode 100644 macro/profiling/runRecoCpc30.C
 create mode 100644 macro/profiling/valgrind.supp

diff --git a/clustering/ClusterLinkDef.h b/clustering/ClusterLinkDef.h
index 0397758..f0e50c4 100644
--- a/clustering/ClusterLinkDef.h
+++ b/clustering/ClusterLinkDef.h
@@ -11,7 +11,7 @@
 #pragma link off all classes;
 #pragma link off all functions;
 
-#pragma link C++ class ClusterElement;
+#pragma link C++ class ClusterElement+;
 #pragma link C++ class Cluster;
 #pragma link C++ class ClusterCollection;
 #pragma link C++ class ClManager;
diff --git a/etof/MpdEtofMatching.cxx b/etof/MpdEtofMatching.cxx
index 4205417..1def403 100644
--- a/etof/MpdEtofMatching.cxx
+++ b/etof/MpdEtofMatching.cxx
@@ -55,9 +55,15 @@ MpdEtofMatching::MpdEtofMatching(const char *name, Int_t verbose, Bool_t test, c
 //------------------------------------------------------------------------------------------------------------------------
 MpdEtofMatching::~MpdEtofMatching()
 {
-    	delete pMF;
-    	delete pRandom;    
-    	delete pMatchingQA;
+    delete pMF;
+    delete pRandom;
+    delete pMatchingQA;
+    if (aTofMatchings != nullptr)
+    {
+        aTofMatchings->Delete();
+        delete aTofMatchings;
+        aTofMatchings = nullptr;
+    }
 }
 //------------------------------------------------------------------------------------------------------------------------
 InitStatus	  MpdEtofMatching::Init()
@@ -65,9 +71,9 @@ InitStatus	  MpdEtofMatching::Init()
   	FairLogger::GetLogger()->Info(MESSAGE_ORIGIN, "[MpdEtofMatching::Init] Begin initialization.");
 
   	aMcPoints = 	(TClonesArray*) FairRootManager::Instance()->GetObject("ETOFPoint");
-   	aMcTracks   = 	(TClonesArray*) FairRootManager::Instance()->GetObject("MCTrack");  	
+    aMcTracks   = 	(TClonesArray*) FairRootManager::Instance()->GetObject("MCTrack");
 	aTofHits  = 	(TClonesArray*) FairRootManager::Instance()->GetObject("ETOFHit");
-	aKFectTracks   = (TClonesArray*) FairRootManager::Instance()->GetObject("EctTrack"); 	
+    aKFectTracks   = (TClonesArray*) FairRootManager::Instance()->GetObject("EctTrack");
 	
 	if(aMcPoints && aMcTracks) fUseMCData = true;
 	
@@ -91,7 +97,7 @@ return kSUCCESS;
 //------------------------------------------------------------------------------------------------------------------------
 void 		MpdEtofMatching::Exec(Option_t *option)
 {
-	fDoMCTest = fUseMCData && fDoTest;
+    fDoMCTest = fUseMCData && fDoTest;
 		
 	// Reset event
         aTofMatchings->Clear();
@@ -141,8 +147,8 @@ void 		MpdEtofMatching::Exec(Option_t *option)
         Int_t		mcTrackIndex = -1,  mcPID = -1;        
         TVector3 	mcPosition;			 
         Int_t		mcNpoints = -1; 			
-        bool		mcTofTouch, mcIsSameIDs, mcHaveCand, mcHaveTrueCand;
-        Int_t 		mcPadUID; 
+        bool		mcTofTouch, mcIsSameIDs, mcHaveCand = false, mcHaveTrueCand = false;
+        Int_t 		mcPadUID = -1;
    	 	
 //cout<<"\n ------------------------------------------------------------------------------------------------------------->> EVENT";  
 //mDetectorsR->dump("\n\n ----->>>	mDetectorsR INTERVALS");
@@ -239,7 +245,7 @@ void 		MpdEtofMatching::Exec(Option_t *option)
 			
 		} // have overlaped segments both R and Phi
 		
-		if(fDoMCTest && mcTofTouch) pMatchingQA->FillCandidates(mcHaveTrueCand, mcHaveCand, pKfTrack->Momentum3().Eta(), pKfTrack->Momentum());			
+        if(fDoMCTest && mcTofTouch) pMatchingQA->FillCandidates(mcHaveTrueCand, mcHaveCand, pKfTrack->Momentum3().Eta(), pKfTrack->Momentum());
 	
 	} // cycle by KF tracks
 
diff --git a/lhetrack/MpdEctTrackFinderCpc.cxx b/lhetrack/MpdEctTrackFinderCpc.cxx
index dafe212..99a00e2 100644
--- a/lhetrack/MpdEctTrackFinderCpc.cxx
+++ b/lhetrack/MpdEctTrackFinderCpc.cxx
@@ -129,6 +129,7 @@ void MpdEctTrackFinderCpc::Reset()
   }
   fTrackCand->Delete();
   delete [] fLayPointers;
+  fLayPointers = 0x0;   // memory segmentation fault was in 'delete' above because of double deletion ('Exec' function with return before 'new')
 }
 
 //__________________________________________________________________________
@@ -1668,7 +1669,11 @@ void MpdEctTrackFinderCpc::SelectTracks(Int_t iPass)
   index = new Int_t [nFound];
   for (Int_t i = 0; i < nFound; ++i) {
     MpdEctKalmanTrack *track = (MpdEctKalmanTrack*) fTracks->UncheckedAt(i);
-    if (track->GetTofIndex() < 0) continue; // track from TPC
+    if (track->GetTofIndex() < 0)   // track from TPC
+    {
+        nh[i] = 0;
+        continue;
+    }
     nh[i] = track->GetNofHits();
     etas[i] = TMath::Abs (track->Momentum3().Eta());
   }
diff --git a/lhetrack/MpdKfPrimaryVertexFinder.cxx b/lhetrack/MpdKfPrimaryVertexFinder.cxx
index d9b01b1..2294fbb 100644
--- a/lhetrack/MpdKfPrimaryVertexFinder.cxx
+++ b/lhetrack/MpdKfPrimaryVertexFinder.cxx
@@ -140,7 +140,7 @@ void MpdKfPrimaryVertexFinder::EvalVertex()
   /// Primary vertex position evaluator
 
   Int_t nTracks = fTracks->GetEntriesFast();
-  Double_t dMax = 0., dMin = 0.1, xyzM[3], xyzF[3], xyz[3];
+  Double_t dMax = 0., dMin = 0.1, xyzM[3], xyzF[3] = {0,0,0}, xyz[3];
   TAxis *axis[3];
   for (Int_t i = 0; i < 3; ++i) {
     axis[i] = fHist[i]->GetXaxis();
diff --git a/macro/profiling/Makefile b/macro/profiling/Makefile
index 22d526b..24bcbd4 100644
--- a/macro/profiling/Makefile
+++ b/macro/profiling/Makefile
@@ -1,5 +1,5 @@
-runMC:
-	gcc -g -o $@ runMC.cxx -I`root-config --incdir` -I$(VMCWORKDIR)/base -I$(VMCWORKDIR)/geobase -I$(VMCWORKDIR)/parbase -I$(VMCWORKDIR)/mcstack -I$(VMCWORKDIR)/field -I$(VMCWORKDIR)/generators -I$(VMCWORKDIR)/passive -I$(VMCWORKDIR)/trackbase -I$(VMCWORKDIR)/mpdbase -I$(VMCWORKDIR)/mpddata -I$(VMCWORKDIR)/bbc -I$(VMCWORKDIR)/cpc -I$(VMCWORKDIR)/fsa -I$(VMCWORKDIR)/ffd -I$(VMCWORKDIR)/kalman -I$(VMCWORKDIR)/lhetrack -I$(VMCWORKDIR)/mpdgenerators -I$(VMCWORKDIR)/geane -I$(VMCWORKDIR)/strawendcap -I$(VMCWORKDIR)/sts -I$(VMCWORKDIR)/bbc -I$(VMCWORKDIR)/fsa -I$(VMCWORKDIR)/ffd -I$(VMCWORKDIR)/cpc -I$(VMCWORKDIR)/ndet -I$(VMCWORKDIR)/etof -I$(VMCWORKDIR)/emc -I$(VMCWORKDIR)/stt -I$(VMCWORKDIR)/tpc -I$(VMCWORKDIR)/tof -I$(VMCWORKDIR)/zdc -I$(VMCWORKDIR)/fairtools -L`root-config --libdir` -L"/usr/lib64" -L$(VMCWORKDIR)/build/lib -lMpdData -lBbc -lCpc -lEmc -lEtof -lFfd -lFsa -lLHETrack -lSft -lStrawendcap -lSts -lStt -ltpc -lZdc -lTof -lNDet -lPassive -lKalman -lMpdgenerators -lMpdBase -lBase -lMpdBase -lFairTools -lField -lMCStack -lTrkBase -lParBase -lGeoBase -lGeane -lGen -lXMLIO -lMinuit -lGeom -lGeomPainter -lVMC -lEG `root-config --glibs` -liomp5 -lpthread -lirc -limf -lsvml -lintlc -ltcmalloc
+runReco:
+	g++ -g -O0 -o $@ runRecoCpc30.C -I`root-config --incdir` -I/opt/fairsoft/install/include -I$(VMCWORKDIR)/base/event -I$(VMCWORKDIR)/base/field -I$(VMCWORKDIR)/base/sim -I$(VMCWORKDIR)/base/source -I$(VMCWORKDIR)/base/steer -I$(VMCWORKDIR)/geobase -I$(VMCWORKDIR)/parbase -I$(VMCWORKDIR)/mcstack -I$(VMCWORKDIR)/mpdfield -I$(VMCWORKDIR)/generators -I$(VMCWORKDIR)/passive -I$(VMCWORKDIR)/trackbase -I$(VMCWORKDIR)/clustering -I$(VMCWORKDIR)/mpdbase -I$(VMCWORKDIR)/mpddata -I$(VMCWORKDIR)/cpc -I$(VMCWORKDIR)/ffd -I$(VMCWORKDIR)/kalman -I$(VMCWORKDIR)/lhetrack -I$(VMCWORKDIR)/geane -I$(VMCWORKDIR)/strawendcap -I$(VMCWORKDIR)/sts -I$(VMCWORKDIR)/ndet -I$(VMCWORKDIR)/etof -I$(VMCWORKDIR)/emc -I$(VMCWORKDIR)/tpc -I$(VMCWORKDIR)/tof -I$(VMCWORKDIR)/zdc -I$(VMCWORKDIR)/fairtools -L`root-config --libdir` -L"/usr/lib64" -L/opt/fairsoft/install/lib -L$(VMCWORKDIR)/build/lib -lMpdData -lCpc -lEmc -lCluster -lEtof -lFfd -lLHETrack -lStrawendcap -lSts -ltpc -lZdc -lTof -lPassive -lKalman -lGen -lMpdBase -lBase -lMpdBase -lFairTools -lMpdField -lMCStack -lTrkBase -lParBase -lGeoBase -lGeane -lGen -lXMLIO -lMinuit -lGeom -lGeomPainter -lVMC -lEG `root-config --glibs` -lpthread
 
 
 #librunMC.a: $(patsubst %.cxx,%.o,$(wildcard *.cxx))
diff --git a/macro/profiling/reco.cxx b/macro/profiling/reco.cxx
deleted file mode 100644
index 29cbcb9..0000000
--- a/macro/profiling/reco.cxx
+++ /dev/null
@@ -1,188 +0,0 @@
-// Macro for running reconstruction
-
-#if !defined(__CINT__) || defined(__MAKECINT__)
-// ROOT includes
-#include "TString.h"
-#include "TStopwatch.h"
-#include "TSystem.h"
-
-// Fair includes
-#include "FairRunAna.h"
-#include "FairRuntimeDb.h"
-#include "FairParRootFileIo.h"
-#include "FairTask.h"
-#include "FairField.h"
-#include "FairTrackParP.h"
-
-// MPD includes
-#include "MpdTpcHitProducer.h"
-#include "TpcHitFinderTask.h"
-#include "MpdKalmanFilter.h"
-#include "MpdTpcHitProducer.h"
-#include "TpcLheHitsMaker.h"
-#include "MpdVertexZfinder.h"
-#include "MpdTpcKalmanFilter.h"
-#include "MpdKfPrimaryVertexFinder.h"
-#include "MpdTofHitProducer.h"
-#include "MpdEtofHitProducer.h"
-#include "MpdEctTrackFinderTpc.h"
-#include "MpdEctTrackFinderTof.h"
-#include "MpdTofMatching.h"
-#include "MpdEtofMatching.h"
-#include "MpdFillDstTask.h"
-#include "MpdEmc.h"
-#include "MpdCpc.h"
-#include "MpdZdc.h"
-#include "MpdFsa.h"
-#include "MpdStrawendcap.h"
-#include "FairUrqmdGenerator.h"
-#include "FairCave.h"
-#include "FairMagnet.h"
-
-//#include "google/heap-profiler.h"
-//#include "google/heap-checker.h"
-
-#include <iostream>
-using namespace std;
-#endif
-
-// inFile - input file with MC data, default: evetest.root
-// nStartEvent - number (start with zero) of first event to process, default: 0
-// nEvents - number of events to process, default: 1
-// outFile - output file with reconstructed data, default: mpddst.root
-// run_type - proof execution, default "local". e.g.:
-//      "proof" - run on proof-lite with "CPU" count workers,
-//      "proof:workers=3" - run on proof-lite with 3 workers
-//      "proof:pod://proof.server:21001" - to run on the PROOF cluster created with PoD
-void reco(TString inFile = "$VMCWORKDIR/macro/mpd/evetest.root", Int_t nStartEvent = 0, Int_t nEvents = 1, TString outFile = "mpddst.root", TString run_type="local")
-{
-
-  // ========================================================================
-  // Verbosity level (0=quiet, 1=event level, 2=track level, 3=debug)
-  Int_t iVerbose = 1;
-
-  // Parameter file
-  TString parFile = inFile;
-
-  // -----   Timer   --------------------------------------------------------
-  TStopwatch timer;
-  timer.Start();
-  // ------------------------------------------------------------------------
-
-  // -----   Digitization run   -------------------------------------------
-  int ind = run_type.Index(':');
-  TString proof_name = "";
-  if (ind >= 0){
-      proof_name = run_type(ind+1,run_type.Length()-ind-1);
-      run_type = run_type(0, ind);
-  }
-
-  FairRunAna *fRun= new FairRunAna(run_type, proof_name);
-  fRun->SetInputFile(inFile);
-  //fRun->AddFriend(inFile);
-  fRun->SetOutputFile(outFile);
-  fRun->SetProofParName("$VMCWORKDIR/gconfig/libMpdRoot.par");
-  // ------------------------------------------------------------------------
-
-  // -----  Parameter database   --------------------------------------------
-  FairRuntimeDb* rtdb = fRun->GetRuntimeDb();
-  FairParRootFileIo* parInput1 = new FairParRootFileIo();
-  parInput1->open(parFile.Data());
-  rtdb->setFirstInput(parInput1);
-
-  // fRun->LoadGeometry();  // EL
-
-  // ------------------------------------------------------------------------
-
-  MpdEmc* pEmc = new MpdEmc();
-  MpdCpc* pCpc = new MpdCpc();
-  MpdZdc* pZdc = new MpdZdc();
-  MpdFsa* pFsa = new MpdFsa();
-  MpdStrawendcap* pStrawEndcap = new MpdStrawendcap();
-  FairCave* pFairCave = new FairCave();
-  FairMagnet* pFairMagnet = new FairMagnet();
-  FairUrqmdGenerator* pFairUrqmdGenerator = new FairUrqmdGenerator();
-
-  MpdKalmanFilter *kalman = MpdKalmanFilter::Instance("KF");
-  fRun->AddTask(kalman);
-
-  //FairTask* trackMS = new TpcLheHitsMaker("Hit producer");
-  //fRun->AddTask(trackMS);
-
-//  TpcDistributor* tpcDistributor = new TpcDistributor(10000, kTRUE, kTRUE);
-//  fRun->AddTask(tpcDistributor);
-
-//  MpdTpcClusterFinderTask *tpcClusterFinder = new MpdTpcClusterFinderTask();
-//  fRun->AddTask( tpcClusterFinder );
-
-  MpdTpcHitProducer* hitPr = new MpdTpcHitProducer();
-  hitPr->SetModular(0);
-  fRun->AddTask(hitPr);
-
-  FairTask* vertZ = new MpdVertexZfinder();
-  fRun->AddTask(vertZ);
-
-  FairTask* recoKF = new MpdTpcKalmanFilter("Kalman filter");
-  fRun->AddTask(recoKF);
-
-  FairTask* findVtx = new MpdKfPrimaryVertexFinder("Vertex finder");
-  fRun->AddTask(findVtx);
-
-  // TOF hit producers
-  MpdTofHitProducer* tofHit = new MpdTofHitProducer("Hit producer");
-  fRun->AddTask(tofHit);
-
-  MpdEtofHitProducer* etofHitProd = new MpdEtofHitProducer("ETOF HitProducer");
-  etofHitProd->SetParamFlnm("etof.geo.par.xml");
-  fRun->AddTask(etofHitProd);
-
-  // Endcap tracking
-  FairTask* tpcECT = new MpdEctTrackFinderTpc();
-  fRun->AddTask(tpcECT);
-
-  MpdEctTrackFinderTof* tofECT = new MpdEctTrackFinderTof();
-  tofECT->SetTpc(kTRUE);
-  fRun->AddTask(tofECT);
-
-  // TOF matching
-  MpdTofMatching* tofMatch = new MpdTofMatching("TOF matching");
-  fRun->AddTask(tofMatch);
-
-  // ETOF matching
-  MpdEtofMatching* etofMatch = new MpdEtofMatching("ETOF matching");
-  fRun->AddTask(etofMatch);
-
-  MpdFillDstTask* fillDST = new MpdFillDstTask("MpdDst task");
-  fRun->AddTask(fillDST);
-
-  // -----   Intialise   ----------------------------------------------------
-  fRun->Init();
-  if (run_type != "proof") cout << "Field: " << fRun->GetField()->GetBz(0.,0.,0.) << endl;
-
-  // -----   Run   ______________--------------------------------------------
-  fRun->Run(nStartEvent, nStartEvent+nEvents);
-  // ------------------------------------------------------------------------
-
-  // -----   Finish   -------------------------------------------------------
-
-  delete fRun;
-
-  timer.Stop();
-  Double_t rtime = timer.RealTime();
-  Double_t ctime = timer.CpuTime();
-  cout << endl << endl;
-  cout << "Macro finished succesfully." << endl;
-  cout << "Output file is "    << outFile << endl;
-  cout << "Parameter file is " << parFile << endl;
-  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
-  cout << endl;
-  // ------------------------------------------------------------------------
-
-  //HeapLeakChecker::NoGlobalLeaks();
-  //HeapLeakChecker::CancelGlobalCheck();
-}
-
-int main(int argc, char** arg)
-{
-   reco();
-}
diff --git a/macro/profiling/runRecoCpc30.C b/macro/profiling/runRecoCpc30.C
new file mode 100644
index 0000000..a2d4f26
--- /dev/null
+++ b/macro/profiling/runRecoCpc30.C
@@ -0,0 +1,238 @@
+// Macro for running reconstruction
+
+#if !defined(__CINT__) || defined(__MAKECINT__)
+// ROOT includes
+#include "TString.h"
+#include "TStopwatch.h"
+#include "TSystem.h"
+#include "TROOT.h"
+
+// Fair includes
+#include "FairRunAna.h"
+#include "FairRuntimeDb.h"
+#include "FairParRootFileIo.h"
+#include "FairTask.h"
+#include "FairField.h"
+#include "FairTrackParP.h"
+#include "FairCave.h"
+#include "FairPipe.h"
+#include "FairMagnet.h"
+
+// MPD includes
+#include "MpdTpcHitProducer.h"
+#include "MpdKalmanFilter.h"
+#include "TpcLheHitsMaker.h"
+#include "MpdVertexZfinder.h"
+#include "MpdTpcKalmanFilter.h"
+#include "MpdKfPrimaryVertexFinder.h"
+#include "MpdTofHitProducer.h"
+#include "MpdEtofHitProducer.h"
+#include "MpdEctTrackFinderTpc.h"
+#include "MpdEctTrackFinderTof.h"
+#include "MpdTofMatching.h"
+#include "MpdEtofMatching.h"
+#include "MpdFillDstTask.h"
+#include "MpdEmc.h"
+#include "MpdCpc.h"
+#include "MpdZdc.h"
+#include "MpdStrawendcap.h"
+#include "FairUrqmdGenerator.h"
+#include "FairFileSource.h"
+#include "MpdTpcDigitizerAZ.h"
+#include "MpdTpcClusterFinderAZ.h"
+#include "MpdEctTrackFinderCpc.h"
+#include "MpdStrawendcapPoint.h"
+#include "MpdStrawendcapGeoPar.h"
+#include "MpdLAQGSMGenerator.h"
+#include "MpdCpcGeoPar.h"
+#include "MpdCpcPoint.h"
+#include "MpdEmcPoint.h"
+
+//#include "google/heap-profiler.h"
+//#include "google/heap-checker.h"
+
+#include <iostream>
+using namespace std;
+#endif
+
+void runRecoCpc30(TString inFile = "mc.30.root")
+{
+    // for adding ROOT dictionary description of classes below
+    MpdStrawendcapPoint* pMpdStrawendcapPoint = new MpdStrawendcapPoint();
+    MpdCpcPoint* pMpdCpcPoint = new MpdCpcPoint();
+    MpdEmcPoint* pMpdEmcPoint = new MpdEmcPoint();
+    FairCave* pFairCave = new FairCave();
+    FairPipe* pFairPipe = new FairPipe();
+    FairMagnet* pFairMagnet = new FairMagnet();
+    MpdStrawendcap* pMpdStrawendcap = new MpdStrawendcap();
+    MpdCpc* pCpc = new MpdCpc();
+    MpdEmc* pEmc = new MpdEmc();
+    MpdLAQGSMGenerator* pMpdLAQGSMGenerator = new MpdLAQGSMGenerator();
+    MpdStrawendcapGeoPar* pMpdStrawendcapGeoPar = new MpdStrawendcapGeoPar();
+    MpdCpcGeoPar* pMpdCpcGeoPar = new MpdCpcGeoPar();
+
+    delete pMpdStrawendcapPoint;
+    delete pMpdCpcPoint;
+    delete pMpdEmcPoint;
+    delete pFairCave;
+    delete pFairPipe;
+    delete pFairMagnet;
+    delete pMpdStrawendcap;
+    delete pCpc;
+    delete pEmc;
+    delete pMpdLAQGSMGenerator;
+    delete pMpdStrawendcapGeoPar;
+    delete pMpdCpcGeoPar;
+
+
+  // ========================================================================
+  // Verbosity level (0=quiet, 1=event level, 2=track level, 3=debug)
+  Int_t iVerbose = 1;
+
+  // Input file (MC events)
+  //TString inFile = "mc.root";
+  //TString inFile = "mc_10.root";
+  TString mcFile = TString(gSystem->Getenv("MCFILE"));
+  if (mcFile != "") inFile = mcFile;
+
+  // Parameter file
+  //TString parFile = "testparams.root";
+  TString parFile = inFile;
+
+  // Output file
+  //TString outFile = "test.raw.1251-1500.root";
+  TString outFile = "tpc.reco.30.root";
+  TString recoFile = TString(gSystem->Getenv("RECOFILE"));
+  if (recoFile != "") outFile = recoFile;
+
+  // ------------------------------------------------------------------------
+
+  // ---  Now choose concrete engines for the different tasks   -------------
+  // ------------------------------------------------------------------------
+
+  // In general, the following parts need not be touched
+  // ========================================================================
+
+  // -----   Timer   --------------------------------------------------------
+  TStopwatch timer;
+  timer.Start();
+  // ------------------------------------------------------------------------
+
+  // -----   Digitization run   -------------------------------------------
+  FairRunAna *fRun= new FairRunAna();
+
+  FairSource* fFileSource = new FairFileSource(inFile);
+  fRun->SetSource(fFileSource);
+  fRun->SetOutputFile(outFile);
+  fRun->SetGenerateRunInfo(false);
+  fRun->SetUseFairLinks(true);
+
+  // -----  Parameter database   --------------------------------------------
+  FairRuntimeDb* rtdb = fRun->GetRuntimeDb();
+  FairParRootFileIo* parInput1 = new FairParRootFileIo();
+  parInput1->open(parFile.Data());
+  //FairParAsciiFileIo* parInput2 = new FairParAsciiFileIo();
+  //TString stsDigiFile = gSystem->Getenv("VMCWORKDIR");
+  //stsDigiFile += "/parameters/sts/sts_digi_new_standard.par";
+  //parInput2->open(stsDigiFile.Data(),"in");
+  rtdb->setFirstInput(parInput1);
+  //rtdb->setSecondInput(parInput2);
+  // ------------------------------------------------------------------------
+  
+  MpdKalmanFilter *kalman = MpdKalmanFilter::Instance("KF");
+  fRun->AddTask(kalman);
+  //kalman->SetNumer(0);
+
+  MpdTpcDigitizerAZ* tpcDigitizer = new MpdTpcDigitizerAZ();
+  tpcDigitizer->SetPersistence(kFALSE);
+  //fRun->AddTask(tpcDigitizer);
+
+  /*
+  MpdTpcClusterFinderTask *tpcClusterFinder = new MpdTpcClusterFinderTask();
+  //  tpcClusterFinder->SetDebug(kFALSE);
+  //  tpcClusterFinder->SetMakeQA(kTRUE);
+  //  tpcClusterFinder->SetCalcResiduals(kFALSE);
+  fRun->AddTask(tpcClusterFinder);
+  */
+
+  MpdTpcClusterFinderAZ *tpcClusAZ = new MpdTpcClusterFinderAZ();
+  //MpdTpcClusterFinderMlem *tpcClusAZ = new MpdTpcClusterFinderMlem();
+  tpcClusAZ->SetPersistence();
+  //fRun->AddTask(tpcClusAZ);
+
+  MpdTpcHitProducer* hitPr = new MpdTpcHitProducer();
+  //hitPr->SetModular(1);
+  //hitPr->SetModular(0);
+  fRun->AddTask(hitPr);
+
+  FairTask* vertZ = new MpdVertexZfinder();
+  fRun->AddTask(vertZ);
+
+  MpdTpcKalmanFilter* recoKF = new MpdTpcKalmanFilter("Kalman filter");
+  //recoKF->UseTpcHit(kFALSE); // do not use hits from the hit producer
+  fRun->AddTask(recoKF);
+  
+  MpdKfPrimaryVertexFinder* findVtx = new MpdKfPrimaryVertexFinder("Vertex finder");
+  //findVtx->SetConstrFlag();
+  fRun->AddTask(findVtx);
+
+  // TOF hit producers
+  MpdTofHitProducer* tofHit = new MpdTofHitProducer("TOF Hit producer", true, 1, true, "QA.MpdTofHitProducer.30.root");
+  fRun->AddTask(tofHit);
+
+  MpdEtofHitProducer* etofHitProd = new MpdEtofHitProducer("ETOF HitProducer", true, 1, true, "QA.MpdEtofHitProducer.30.root");
+  fRun->AddTask(etofHitProd);
+
+  // Endcap tracking
+  //*
+  FairTask* tpcECT = new MpdEctTrackFinderTpc();
+  fRun->AddTask(tpcECT);
+  //*/
+
+  //MpdEctTrackFinderTof* tofECT = new MpdEctTrackFinderTof();
+  //*
+  MpdEctTrackFinderCpc* tofECT = new MpdEctTrackFinderCpc();
+  tofECT->SetTpc(kTRUE);
+  fRun->AddTask(tofECT);
+  //*/
+
+  // TOF matching
+  MpdTofMatching* tofMatch = new MpdTofMatching("TOF matching", 1, true, "QA.MpdTofMatching.30.root");
+  fRun->AddTask(tofMatch);
+
+  // ETOF matching
+  MpdEtofMatching* etofMatch = new MpdEtofMatching("ETOF matching", 1, true, "QA.MpdEtofMatching.30.root");
+  fRun->AddTask(etofMatch);
+  
+
+  FairTask* fillDST = new  MpdFillDstTask("MpdDst task");
+  fRun->AddTask(fillDST);
+
+  // Number of events to process
+  Int_t nEvents = 30; //55; //50; //250; //90;
+  
+  // -----   Intialise and run   --------------------------------------------
+  fRun->Init();
+  cout << "Field: " << fRun->GetField()->GetBz(0.,0.,0.) << endl;
+  fRun->Run(0, nEvents);
+  // ------------------------------------------------------------------------
+
+  // -----   Finish   -------------------------------------------------------
+
+  timer.Stop();
+  Double_t rtime = timer.RealTime();
+  Double_t ctime = timer.CpuTime();
+  cout << endl << endl;
+  cout << "Macro finished succesfully." << endl;
+  cout << "Output file is "    << outFile << endl;
+  cout << "Parameter file is " << parFile << endl;
+  cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
+  cout << endl;
+  // ------------------------------------------------------------------------
+
+}
+
+int main(int argc, char** arg)
+{
+   runRecoCpc30();
+}
diff --git a/macro/profiling/valgrind.supp b/macro/profiling/valgrind.supp
new file mode 100644
index 0000000..359c382
--- /dev/null
+++ b/macro/profiling/valgrind.supp
@@ -0,0 +1,16 @@
+# MPDRoot Suppressions
+{
+   obj1
+   Memcheck:Cond
+   fun:_ZN7TObjectC1Ev
+}
+{
+   obj2
+   Memcheck:Cond
+   fun:_ZN7TObjectC1ERKS_
+}
+{
+   gas1
+   Memcheck:Cond
+   fun:_ZN6TpcGasC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEd
+}
diff --git a/tof/MpdTofHitProducerIdeal.cxx b/tof/MpdTofHitProducerIdeal.cxx
index 5d79c46..d4aa3f4 100644
--- a/tof/MpdTofHitProducerIdeal.cxx
+++ b/tof/MpdTofHitProducerIdeal.cxx
@@ -26,7 +26,13 @@ MpdTofHitProducerIdeal::MpdTofHitProducerIdeal(const char *name, Bool_t useMCdat
 //------------------------------------------------------------------------------------------------------------------------
 MpdTofHitProducerIdeal::~MpdTofHitProducerIdeal() 
 { 
-	delete pHitProducerQA;
+    if (pHitProducerQA != nullptr) delete pHitProducerQA;
+    if (aTofHits != nullptr)
+    {
+        aTofHits->Delete();
+        delete aTofHits;
+        aTofHits = nullptr;
+    }
 }
 //------------------------------------------------------------------------------------------------------------------------
 InitStatus	MpdTofHitProducerIdeal::Init() 
diff --git a/tof/MpdTofMatching.cxx b/tof/MpdTofMatching.cxx
index 0ef3428..14b0ed0 100644
--- a/tof/MpdTofMatching.cxx
+++ b/tof/MpdTofMatching.cxx
@@ -160,8 +160,8 @@ Int_t		MpdTofMatching::ExecByIntervalTree(const TsPt& tids, const TmmD2H& dets,
         Int_t		mcTrackIndex = -1,  mcPID = -1;        
         TVector3 	mcPosition;			 
         Int_t		mcNpoints = -1; 			
-        bool		mcTofTouch, mcIsSameIDs, mcHaveCand, mcHaveTrueCand;
-        Int_t 		mcPadUID;
+        bool		mcTofTouch, mcIsSameIDs, mcHaveCand = false, mcHaveTrueCand = false;
+        Int_t 		mcPadUID = -1;
      
 //cout<<"\n ------------------------------------------------------------------------------------------------------------->> EVENT";    
 //mDetectorsZ->dump("\n\n ----->>>	mDetectorsZ INTERVALS");
-- 
GitLab