Skip to content
Snippets Groups Projects
Commit 28f9dfe6 authored by John Chapman's avatar John Chapman
Browse files

Cleaned-up `CaloHitAna`. Reduced `CaloHitAna` memory usage.

This is a manual sweep of the changes from !3978 into `master`.
Added an option to enable/disable recording of g4hits. The reason
for these changes is to fix the memory problems that were present
in the previous version of the code. There were minor problems with
the initialisation of new branches for the output tree, there were
some vectors that weren't getting cleared after each loop, and
there were a couple of objects that weren't being used anymore
which took up too much memory.

The initialisation of branches has been moved to the header file,
lines have been added to the source file to clean the vectors after
each loop and the unnecessary objects that accounted for most of the
memory problems have been removed.
parent c5a68b23
No related branches found
No related tags found
No related merge requests found
......@@ -21,12 +21,10 @@
#include <FCS_Cell.h>
#include <iostream>
// Fixed size dimensions of array or collections stored in the TTree if any.
// const int CaloHitAna::MAX_LAYER =25; //number of calorimeter layers/samplings, use last (MAX_LAYER-1) for invalid cells/hits
class CaloHitAna {
public :
class CaloHitAna
{
public :
TTree *fChain; //!pointer to the analyzed TTree or TChain
Int_t fCurrent; //!current Tree number in a TChain
TString fFilename;
......@@ -113,18 +111,13 @@ class CaloHitAna {
std::vector<FCS_truth>* truthcollection = new std::vector<FCS_truth>;
Float_t total_cell_e = 0;
Float_t total_hit_e = 0;
Float_t total_g4hit_e = 0;
// Float_t* p_total_cell_e = &total_cell_e;
// Float_t* p_total_hit_e = &total_hit_e;
// Float_t* p_total_g4hit_e = &total_g4hit_e;
std::vector<Float_t>* cell_energy = new std::vector<Float_t>(MAX_LAYER+1);
std::vector<Float_t>* hit_energy = new std::vector<Float_t>(MAX_LAYER+1);
std::vector<Float_t>* g4hit_energy = new std::vector<Float_t>(MAX_LAYER+1);
std::vector<Float_t>* cell_energy = new std::vector<Float_t>(MAX_LAYER + 1);
std::vector<Float_t>* hit_energy = new std::vector<Float_t>(MAX_LAYER + 1);
std::vector<Float_t>* g4hit_energy = new std::vector<Float_t>(MAX_LAYER + 1);
std::vector<Float_t>* new_truthE = new std::vector<Float_t>;
std::vector<Float_t>* new_truthPx = new std::vector<Float_t>;
......@@ -134,9 +127,6 @@ class CaloHitAna {
std::vector<int>* new_truthPDG = new std::vector<int>;
std::vector<int>* new_truthVtxBarcode = new std::vector<int>;
//If this won't work, we will have to change it... (memory??)
// std::vector<FCS_matchedcellvector*> m_all_cells; //hm, make it a vector of (vector of FCS_matchedcell) and have it for all 1000 events at once in memory??
// List of branches
TBranch *b_HitX; //!
TBranch *b_HitY; //!
......@@ -169,20 +159,20 @@ class CaloHitAna {
TBranch *b_G4HitSamplingFraction; //!
TBranch *b_G4HitSampling; //!
/*
TBranch *b_TTC_back_eta; //!
TBranch *b_TTC_back_phi; //!
TBranch *b_TTC_back_r; //!
TBranch *b_TTC_back_z; //!
TBranch *b_TTC_entrance_eta; //!
TBranch *b_TTC_entrance_phi; //!
TBranch *b_TTC_entrance_r; //!
TBranch *b_TTC_entrance_z; //!
TBranch *b_TTC_IDCaloBoundary_eta; //!
TBranch *b_TTC_IDCaloBoundary_phi; //!
TBranch *b_TTC_IDCaloBoundary_r; //!
TBranch *b_TTC_IDCaloBoundary_z; //!
TBranch *b_TTC_Angle3D; //!
TBranch *b_TTC_AngleEta; //!
TBranch *b_TTC_back_eta; //!
TBranch *b_TTC_back_phi; //!
TBranch *b_TTC_back_r; //!
TBranch *b_TTC_back_z; //!
TBranch *b_TTC_entrance_eta; //!
TBranch *b_TTC_entrance_phi; //!
TBranch *b_TTC_entrance_r; //!
TBranch *b_TTC_entrance_z; //!
TBranch *b_TTC_IDCaloBoundary_eta; //!
TBranch *b_TTC_IDCaloBoundary_phi; //!
TBranch *b_TTC_IDCaloBoundary_r; //!
TBranch *b_TTC_IDCaloBoundary_z; //!
TBranch *b_TTC_Angle3D; //!
TBranch *b_TTC_AngleEta; //!
*/
TBranch *b_newTTC_back_eta; //!
......@@ -201,7 +191,7 @@ class CaloHitAna {
TBranch *b_newTTC_AngleEta; //!
CaloHitAna(TString filename="ISF_HitAnalysispion_eta1.root", TString outputname="output1.root", std::vector<Int_t> settings=std::vector<Int_t>(), Float_t timingcut=999999., Int_t debug=0, TTree *tree=0);
CaloHitAna(TString filename = "ISF_HitAnalysispion_eta1.root", TString outputname = "output1.root", std::vector<Int_t> settings = std::vector<Int_t>(), Float_t timingcut = 999999., Int_t debug = 0, TTree *tree = 0);
virtual ~CaloHitAna();
virtual Int_t Cut(Long64_t entry);
virtual Int_t GetEntry(Long64_t entry);
......@@ -209,15 +199,14 @@ class CaloHitAna {
virtual void Init(TTree *tree);
virtual void InitOutTree();
virtual void Loop();
virtual void Finish(std::vector<Int_t> settings, TString outputname="output_cells.root");
virtual Bool_t Notify();
virtual void Show(Long64_t entry = -1);
void SetDebug(Int_t debug=0) {m_Debug=debug;};
void SetTimingCut(Int_t timingcut=999999) {m_TimingCut=timingcut;};
void SetDoAllCells(Int_t doit=0) {if(m_Settings.size()<1) m_Settings.resize(1,0);m_Settings[0]=doit;};
void SetDoLayers(Int_t doit=0) {if(m_Settings.size()<2) m_Settings.resize(2,0);m_Settings[1]=doit;};
void SetDoLayerSums(Int_t doit=0) {if(m_Settings.size()<3) m_Settings.resize(3,0);m_Settings[2]=doit;};
void SetDebug(Int_t debug = 0) {m_Debug = debug;};
void SetTimingCut(Int_t timingcut = 999999) {m_TimingCut = timingcut;};
void SetDoAllCells(Int_t doit = 0) {if (m_Settings.size() < 1) m_Settings.resize(1, 0); m_Settings[0] = doit;};
void SetDoLayers(Int_t doit = 0) {if (m_Settings.size() < 2) m_Settings.resize(2, 0); m_Settings[1] = doit;};
void SetDoLayerSums(Int_t doit = 0) {if (m_Settings.size() < 3) m_Settings.resize(3, 0); m_Settings[2] = doit;};
};
#endif
......@@ -225,29 +214,26 @@ class CaloHitAna {
#ifdef CaloHitAna_cxx
CaloHitAna::CaloHitAna(TString filename, TString outputname, std::vector<Int_t> settings, Float_t timingcut, Int_t debug, TTree *tree) : fChain(0)
{
fFilename= filename;
fFilename = filename;
// if parameter tree is not specified (or zero), connect the file
// used to generate this class and read the Tree.
if (tree == 0) {
TFile *f = TFile::Open(filename,"READ");
TString dirname=filename;
dirname+=":/ISF_HitAnalysis";
TFile *f = TFile::Open(filename, "READ");
TString dirname = filename;
dirname += ":/ISF_HitAnalysis";
TDirectory * dir = (TDirectory*)f->Get(dirname);
dir->GetObject("CaloHitAna",tree);
dir->GetObject("CaloHitAna", tree);
}
//tree->Print();
Init(tree);
m_Settings = settings;
m_Debug = debug;
m_TimingCut = timingcut;
m_OutputName = outputname;
m_PrintOutFrequency = 100;
m_max_nentries=-1;
m_max_nentries = -1;
m_Output = new TFile(m_OutputName, "RECREATE");
m_OutputTree = new TTree("FCS_ParametrizationInput","Output_Matched_cell_Tree");
m_OutputTree = new TTree("FCS_ParametrizationInput", "Output_Matched_cell_Tree");
InitOutTree();
//std::cout <<"Input: "<<fFilename<<" output: "<<m_OutputName<<" debug: "<<m_Debug<<" TC: "<<m_TimingCut<<std::endl;
}
CaloHitAna::~CaloHitAna()
......@@ -279,68 +265,64 @@ Long64_t CaloHitAna::LoadTree(Long64_t entry)
void CaloHitAna::InitOutTree()
{
m_OutputTree->Branch("TruthE",&new_truthE);
m_OutputTree->Branch("TruthPx",&new_truthPx);
m_OutputTree->Branch("TruthPy",&new_truthPy);
m_OutputTree->Branch("TruthPz",&new_truthPz);
m_OutputTree->Branch("TruthPDG",&new_truthPDG);
m_OutputTree->Branch("TruthBarcode",&new_truthBarcode);
m_OutputTree->Branch("TruthVtxBarcode",&new_truthVtxBarcode); //this is duplicate of what is in the truth collection, will be good to remove/hide at some point
m_OutputTree->Branch("newTTC_back_eta",&m_newTTC_back_eta);
m_OutputTree->Branch("newTTC_back_phi",&m_newTTC_back_phi);
m_OutputTree->Branch("TruthE", &new_truthE);
m_OutputTree->Branch("TruthPx", &new_truthPx);
m_OutputTree->Branch("TruthPy", &new_truthPy);
m_OutputTree->Branch("TruthPz", &new_truthPz);
m_OutputTree->Branch("TruthPDG", &new_truthPDG);
m_OutputTree->Branch("TruthBarcode", &new_truthBarcode);
m_OutputTree->Branch("TruthVtxBarcode", &new_truthVtxBarcode); //this is duplicate of what is in the truth collection, will be good to remove/hide at some point
m_OutputTree->Branch("newTTC_back_eta", &m_newTTC_back_eta);
m_OutputTree->Branch("newTTC_back_phi", &m_newTTC_back_phi);
m_OutputTree->Branch("newTTC_back_r", &m_newTTC_back_r);
m_OutputTree->Branch("newTTC_back_z", &m_newTTC_back_z);
m_OutputTree->Branch("newTTC_entrance_eta",&m_newTTC_entrance_eta);
m_OutputTree->Branch("newTTC_entrance_phi",&m_newTTC_entrance_phi);
m_OutputTree->Branch("newTTC_entrance_eta", &m_newTTC_entrance_eta);
m_OutputTree->Branch("newTTC_entrance_phi", &m_newTTC_entrance_phi);
m_OutputTree->Branch("newTTC_entrance_r", &m_newTTC_entrance_r);
m_OutputTree->Branch("newTTC_entrance_z", &m_newTTC_entrance_z);
m_OutputTree->Branch("newTTC_IDCaloBoundary_eta",&m_newTTC_IDCaloBoundary_eta);
m_OutputTree->Branch("newTTC_IDCaloBoundary_phi",&m_newTTC_IDCaloBoundary_phi);
m_OutputTree->Branch("newTTC_IDCaloBoundary_r",&m_newTTC_IDCaloBoundary_r);
m_OutputTree->Branch("newTTC_IDCaloBoundary_z",&m_newTTC_IDCaloBoundary_z);
m_OutputTree->Branch("newTTC_IDCaloBoundary_eta", &m_newTTC_IDCaloBoundary_eta);
m_OutputTree->Branch("newTTC_IDCaloBoundary_phi", &m_newTTC_IDCaloBoundary_phi);
m_OutputTree->Branch("newTTC_IDCaloBoundary_r", &m_newTTC_IDCaloBoundary_r);
m_OutputTree->Branch("newTTC_IDCaloBoundary_z", &m_newTTC_IDCaloBoundary_z);
m_OutputTree->Branch("newTTC_Angle3D", &m_newTTC_Angle3D);
m_OutputTree->Branch("newTTC_AngleEta",&m_newTTC_AngleEta);
m_OutputTree->Branch("newTTC_AngleEta", &m_newTTC_AngleEta);
//create branches in the output tree according to the settings vector
if(! m_Settings.size() || m_Settings[0]==1)
{
//Write all FCS_matchedcells
m_OutputTree->Branch("AllCells", &oneeventcells);
}
if (m_Settings.size()>=2 && m_Settings[1]==1)
{
//write cells per layer
for (Int_t i=0; i<MAX_LAYER; i++)
{
TString branchname="Sampling_";
branchname+=i;
// std::cout<< "fail?" << std::endl;
layercells[i] = new FCS_matchedcellvector;
m_OutputTree->Branch(branchname, &layercells[i]);
}
}
if (m_Settings.size()>=3 && m_Settings[2]==1)
if (! m_Settings.size() || m_Settings[0] == 1)
{
//Write all FCS_matchedcells
m_OutputTree->Branch("AllCells", &oneeventcells);
}
if (m_Settings.size() >= 2 && m_Settings[1] == 1)
{
//write cells per layer
for (Int_t i = 0; i < MAX_LAYER; i++)
{
//write also energies per layer:
m_OutputTree->Branch("cell_energy", &cell_energy);
m_OutputTree->Branch("hit_energy", &hit_energy);
m_OutputTree->Branch("g4hit_energy",&g4hit_energy);
//This is a duplicate of cell_energy[25]
m_OutputTree->Branch("total_cell_energy", &total_cell_e);
m_OutputTree->Branch("total_hit_energy", &total_hit_e);
m_OutputTree->Branch("total_g4hit_energy", &total_g4hit_e);
TString branchname = "Sampling_";
branchname += i;
// std::cout<< "fail?" << std::endl;
layercells[i] = new FCS_matchedcellvector;
m_OutputTree->Branch(branchname, &layercells[i]);
}
}
if (m_Settings.size() >= 3 && m_Settings[2] == 1)
{
//write also energies per layer:
m_OutputTree->Branch("cell_energy", &cell_energy);
m_OutputTree->Branch("hit_energy", &hit_energy);
m_OutputTree->Branch("g4hit_energy", &g4hit_energy);
//This is a duplicate of cell_energy[25]
m_OutputTree->Branch("total_cell_energy", &total_cell_e);
m_OutputTree->Branch("total_hit_energy", &total_hit_e);
m_OutputTree->Branch("total_g4hit_energy", &total_g4hit_e);
}
// Enable/Disable recording of g4hits
if (m_Settings.size()>=4 && m_Settings[3]==1){
if (m_Settings.size() >= 4 && m_Settings[3] == 1) {
std::cout << "do g4hits!" << std::endl;
m_do_g4_hits = true;
}
}
void CaloHitAna::Init(TTree *tree)
......@@ -385,20 +367,20 @@ void CaloHitAna::Init(TTree *tree)
G4HitSamplingFraction = 0;
G4HitSampling = 0;
/*
TTC_back_eta = 0;
TTC_back_phi = 0;
TTC_back_r = 0;
TTC_back_z = 0;
TTC_entrance_eta = 0;
TTC_entrance_phi = 0;
TTC_entrance_r = 0;
TTC_entrance_z = 0;
TTC_IDCaloBoundary_eta = 0;
TTC_IDCaloBoundary_phi = 0;
TTC_IDCaloBoundary_r = 0;
TTC_IDCaloBoundary_z = 0;
TTC_Angle3D = 0;
TTC_AngleEta = 0;
TTC_back_eta = 0;
TTC_back_phi = 0;
TTC_back_r = 0;
TTC_back_z = 0;
TTC_entrance_eta = 0;
TTC_entrance_phi = 0;
TTC_entrance_r = 0;
TTC_entrance_z = 0;
TTC_IDCaloBoundary_eta = 0;
TTC_IDCaloBoundary_phi = 0;
TTC_IDCaloBoundary_r = 0;
TTC_IDCaloBoundary_z = 0;
TTC_Angle3D = 0;
TTC_AngleEta = 0;
*/
newTTC_back_eta = 0;
newTTC_back_phi = 0;
......@@ -453,20 +435,20 @@ void CaloHitAna::Init(TTree *tree)
fChain->SetBranchAddress("G4HitSamplingFraction", &G4HitSamplingFraction, &b_G4HitSamplingFraction);
fChain->SetBranchAddress("G4HitSampling", &G4HitSampling, &b_G4HitSampling);
/*
fChain->SetBranchAddress("TTC_back_eta", &TTC_back_eta, &b_TTC_back_eta);
fChain->SetBranchAddress("TTC_back_phi", &TTC_back_phi, &b_TTC_back_phi);
fChain->SetBranchAddress("TTC_back_r", &TTC_back_r, &b_TTC_back_r);
fChain->SetBranchAddress("TTC_back_z", &TTC_back_z, &b_TTC_back_z);
fChain->SetBranchAddress("TTC_entrance_eta", &TTC_entrance_eta, &b_TTC_entrance_eta);
fChain->SetBranchAddress("TTC_entrance_phi", &TTC_entrance_phi, &b_TTC_entrance_phi);
fChain->SetBranchAddress("TTC_entrance_r", &TTC_entrance_r, &b_TTC_entrance_r);
fChain->SetBranchAddress("TTC_entrance_z", &TTC_entrance_z, &b_TTC_entrance_z);
fChain->SetBranchAddress("TTC_IDCaloBoundary_eta", &TTC_IDCaloBoundary_eta, &b_TTC_IDCaloBoundary_eta);
fChain->SetBranchAddress("TTC_IDCaloBoundary_phi", &TTC_IDCaloBoundary_phi, &b_TTC_IDCaloBoundary_phi);
fChain->SetBranchAddress("TTC_IDCaloBoundary_r", &TTC_IDCaloBoundary_r, &b_TTC_IDCaloBoundary_r);
fChain->SetBranchAddress("TTC_IDCaloBoundary_z", &TTC_IDCaloBoundary_z, &b_TTC_IDCaloBoundary_z);
fChain->SetBranchAddress("TTC_Angle3D", &TTC_Angle3D, &b_TTC_Angle3D);
fChain->SetBranchAddress("TTC_AngleEta", &TTC_AngleEta, &b_TTC_AngleEta);
fChain->SetBranchAddress("TTC_back_eta", &TTC_back_eta, &b_TTC_back_eta);
fChain->SetBranchAddress("TTC_back_phi", &TTC_back_phi, &b_TTC_back_phi);
fChain->SetBranchAddress("TTC_back_r", &TTC_back_r, &b_TTC_back_r);
fChain->SetBranchAddress("TTC_back_z", &TTC_back_z, &b_TTC_back_z);
fChain->SetBranchAddress("TTC_entrance_eta", &TTC_entrance_eta, &b_TTC_entrance_eta);
fChain->SetBranchAddress("TTC_entrance_phi", &TTC_entrance_phi, &b_TTC_entrance_phi);
fChain->SetBranchAddress("TTC_entrance_r", &TTC_entrance_r, &b_TTC_entrance_r);
fChain->SetBranchAddress("TTC_entrance_z", &TTC_entrance_z, &b_TTC_entrance_z);
fChain->SetBranchAddress("TTC_IDCaloBoundary_eta", &TTC_IDCaloBoundary_eta, &b_TTC_IDCaloBoundary_eta);
fChain->SetBranchAddress("TTC_IDCaloBoundary_phi", &TTC_IDCaloBoundary_phi, &b_TTC_IDCaloBoundary_phi);
fChain->SetBranchAddress("TTC_IDCaloBoundary_r", &TTC_IDCaloBoundary_r, &b_TTC_IDCaloBoundary_r);
fChain->SetBranchAddress("TTC_IDCaloBoundary_z", &TTC_IDCaloBoundary_z, &b_TTC_IDCaloBoundary_z);
fChain->SetBranchAddress("TTC_Angle3D", &TTC_Angle3D, &b_TTC_Angle3D);
fChain->SetBranchAddress("TTC_AngleEta", &TTC_AngleEta, &b_TTC_AngleEta);
*/
fChain->SetBranchAddress("newTTC_back_eta", &newTTC_back_eta, &b_newTTC_back_eta);
fChain->SetBranchAddress("newTTC_back_phi", &newTTC_back_phi, &b_newTTC_back_phi);
......@@ -509,7 +491,7 @@ Int_t CaloHitAna::Cut(Long64_t entry)
// This function may be called from Loop.
// returns 1 if entry is accepted.
// returns -1 otherwise.
std::cout <<entry<<std::endl;
std::cout << entry << std::endl;
return 1;
}
#endif // #ifdef CaloHitAna_cxx
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment