ATLAS Offline Software
MonitoringFile_PPFileWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3  */
4 
6 //File Wrapper class to keep track of histograms and manage memory
7 //Required due to problems with root's intrinsic memory management
8 //Author: Matt King (matthew.king@cern.ch)
9 // Copied from code by Justin Griffiths
10 //Date: May 2010
12 
14 
15 #include <iostream>
16 #include <iomanip>
17 #include <algorithm>
18 #include <fstream>
19 #include <cmath>
20 #include <cstdlib>
21 #include <sstream>
22 #include <vector>
23 #include <utility>
24 #include <map>
25 #include <string>
26 
27 #include "TH1F.h"
28 #include "TH2F.h"
29 #include "TFile.h"
30 #include "TClass.h"
31 #include "TKey.h"
32 #include "TMath.h"
33 #include "TTree.h"
34 #include "TBranch.h"
35 #include "TGraph.h"
36 
37 namespace dqutils {
39  const std::string& name) :
40  TFile(inFilename.c_str(), "UPDATE"),
41  m_name(name),
42  m_error(0) {
43  if (this->IsOpen()) {
44  //std::cout << "Begin " << m_name << " MonitoringFile: " << this->GetName() << " Size: " << this->GetSize() <<
45  // std::endl;
46  } else {
47  std::cerr << "ERROR " << m_name << " Could not open MonitoringFile: " << inFilename << std::endl;
48  }
49  }
50 
52  //std::cout << "Destructing " << m_name << " Collected " << m_garbage.size() << " Objects to be deleted" <<
53  // std::endl;
54  if (!m_error) {
55  this->Write();
56  } else {
57  std::cerr << "ERROR " << m_name << " Chose Not To Write " << this->GetName() << std::endl;
58  }
59  this->Close();
60  for (std::set<TObject*>::const_iterator git = m_garbage.begin(); git != m_garbage.end(); ++git) {
61  delete *git;
62  }
63  //std::cout << "\n";
64  }
65 
66  /*
67  // get() Defined in header file, otherwise it can't use a template.
68  // If it can't use a template, then it can't pass a reference to an arbitrary object pointer,
69  // this solution isn't perfect, but it seems to be the best available.
70  template <class T>
71  void MonitoringFile::PostProcessorFileWrapper::getOriginal(const char* namecycle, T* &ptr, TDirectory* baseDir,
72  TDirectory* target){
73 
74  if (baseDir) baseDir->GetObject( namecycle, ptr ); // suggested root function
75  else this->GetObject( namecycle, ptr ); //suggested root function
76  if(!ptr) return;
77 
78  if((T::Class())->InheritsFrom("TH1")){
79  TH1 *obj = reinterpret_cast<TH1*> (ptr);
80 
81  obj->SetDirectory(target);
82  if(target == 0) m_garbage.insert(obj);
83  }
84  else{
85  TObject *obj = reinterpret_cast<TObject*>(ptr);
86 
87  if(target == 0) m_garbage.insert(obj);
88  }
89  }
90  */
91 
93 
95  h->SetDirectory(dir);
96  if (dir == 0) {
97  m_garbage.insert(h);
98  }
99  }
100 
102  h->SetDirectory(dir);
103  if (dir == 0) {
104  m_garbage.insert(h);
105  }
106  }
107 
109  // Cannot set directory for TGraph
110  if (dir == 0) {
111  m_garbage.insert(g);
112  }
113  }
114 
115  void MonitoringFile::PostProcessorFileWrapper::setMetaData(TDirectory* targetDir, TH1* h1, TH1* h2, TH1* h3) {
116  //we will collect the garbage and set the directory to zero
117  h1->SetDirectory(0);
118  m_garbage.insert(h1);
119  if (h2) {
120  h2->SetDirectory(0);
121  m_garbage.insert(h2);
122  }
123  if (h3) {
124  h3->SetDirectory(0);
125  m_garbage.insert(h3);
126  }
127  //now call old function being sure not to fill a garbage set
128  return MonitoringFile::TGCSetMetaData(targetDir, h1, h2, h3);
129  }
130 
131  void MonitoringFile::TGCSetMetaData(TDirectory* targetDir, TH1* h1, TH1* h2, TH1* h3) {
132  TTree* metadata_overview_tr = 0;
133 
134  if (!targetDir || !h1) return;
135 
136  targetDir->GetObject("metadata", metadata_overview_tr);
137  //Fill Metadata for new objs.
138  std::string name_;
139  std::string intervalData_ = "run";
140  std::string chainData_ = "<none>";
141  std::string mergeData_ = "<default>";
142  if (metadata_overview_tr) {
143  OutputMetadata metadata_overview(metadata_overview_tr);
144  char name[99];
145  char intervalData[99];
146  char chainData[99];
147  char mergeData[99];
148  TBranch* b_name;
149  TBranch* b_intervalData;
150  TBranch* b_chainData;
151  TBranch* b_mergeData;
152  TTree* tClone = (TTree*) metadata_overview_tr->Clone();
153  metadata_overview_tr->SetDirectory(0);
154  tClone->SetBranchAddress("Name", name, &b_name);
155  tClone->SetBranchAddress("Interval", intervalData, &b_intervalData);
156  tClone->SetBranchAddress("TriggerChain", chainData, &b_chainData);
157  tClone->SetBranchAddress("MergeMethod", mergeData, &b_mergeData);
158  //Check if Metadata Already Exists
159  bool isMeta = false;
160  for (int i = 0; i != tClone->GetEntries(); ++i) {
161  tClone->GetEntry(i);
162  intervalData_ = intervalData;
163  chainData_ = chainData;
164  mergeData_ = mergeData;
165  name_ = name;
166  if (name_ == (std::string) h1->GetName()) isMeta = true;
167  }
168 
169  delete tClone;
170  if (isMeta) {
171  metadata_overview_tr->SetDirectory(0);
172  delete metadata_overview_tr;
173  return;
174  }
175  metadata_overview.fill(h1->GetName(), intervalData_, chainData_, mergeData_);
176  if (h2) metadata_overview.fill(h2->GetName(), intervalData_, chainData_, mergeData_);
177  if (h3) metadata_overview.fill(h3->GetName(), intervalData_, chainData_, mergeData_);
178  //set new hists to 0 dir
179  TDirectory* tempDir = gDirectory;
180  targetDir->cd();
181  metadata_overview_tr->Write("", TObject::kOverwrite);
182  tempDir->cd();
183  } else {
184  TTree* metadata_overview_tr = new TTree("metadata", "Monitoring Metadata");
185  OutputMetadata metadata_overview(metadata_overview_tr);
186  if (h1) metadata_overview.fill(h1->GetName(), intervalData_, chainData_, mergeData_);
187  TDirectory* tempDir = gDirectory;
188  targetDir->cd();
189  h1->SetDirectory(0);
190  metadata_overview_tr->Write("", TObject::kOverwrite);
191  metadata_overview_tr->SetDirectory(0);//make this safe to delete
192  delete metadata_overview_tr;
193  tempDir->cd();
194  }
195  }
196 }//namespace
read_hist_ntuple.h1
h1
Definition: read_hist_ntuple.py:21
dqutils::MonitoringFile::PostProcessorFileWrapper::PostProcessorFileWrapper
PostProcessorFileWrapper(const std::string &inFilename, const std::string &name)
Definition: MonitoringFile_PPFileWrapper.cxx:38
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
dqutils::MonitoringFile::OutputMetadata::fill
virtual void fill(const std::string &name, const std::string &interval, const std::string &chain, const std::string &merge)
lumiFormat.i
int i
Definition: lumiFormat.py:85
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
dqutils::MonitoringFile::PostProcessorFileWrapper::~PostProcessorFileWrapper
~PostProcessorFileWrapper()
Definition: MonitoringFile_PPFileWrapper.cxx:51
dqutils::MonitoringFile::PostProcessorFileWrapper::error
void error()
Definition: MonitoringFile_PPFileWrapper.cxx:92
dqutils
Definition: CoolMdt.h:76
beamspotman.dir
string dir
Definition: beamspotman.py:623
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
dqutils::MonitoringFile::OutputMetadata
Definition: MonitoringFile.h:67
dqutils::MonitoringFile::PostProcessorFileWrapper::setDirectory
void setDirectory(TH1 *h, TDirectory *dir=0)
Definition: MonitoringFile_PPFileWrapper.cxx:94
MonitoringFile.h
h
dqutils::MonitoringFile::PostProcessorFileWrapper::m_name
std::string m_name
Definition: MonitoringFile.h:235
dqutils::MonitoringFile::TGCSetMetaData
static void TGCSetMetaData(TDirectory *targetDir, TH1 *h1, TH1 *h2=0, TH1 *h3=0)
Definition: MonitoringFile_PPFileWrapper.cxx:131
dqutils::MonitoringFile::PostProcessorFileWrapper::setMetaData
void setMetaData(TDirectory *targetDir, TH1 *h1, TH1 *h2=0, TH1 *h3=0)
Definition: MonitoringFile_PPFileWrapper.cxx:115