NearlineSummariser.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <memory>
5 
6 //ROOT
7 
8 #include "TFile.h"
9 #include "TTree.h"
10 #include "TList.h"
11 #include "TKey.h"
12 #include "TH1.h"
13 
16 
17 const std::string NearlineInstanceLabel = "nearlineana";
18 
19 bool checkFileHasDir(TFile* fp, std::string dirName);
20 TH1* getHistogram(TFile* fp, std::string histName, std::string newHistName);
21 
23 
24  bool HasHeader;
25  unsigned int Run;
26  unsigned int Subrun;
28  int LastEvent;
29  int Nevents;
30  unsigned int StartYear;
31  unsigned int EndYear;
32  unsigned int StartMonth;
33  unsigned int EndMonth;
34  unsigned int StartDay;
35  unsigned int EndDay;
36  double StartHour;
37  double EndHour;
38  unsigned long long int StartTime;
39  unsigned long long int EndTime;
40 
41  //NearlineVersionNumbers
44 
45  NearlineFileInfo(TFile *fp, std::string headerTreeName = NearlineInstanceLabel + "/Header", std::string versionHistName = NearlineInstanceLabel + "/hist_nearline_version"):
46  HasHeader(false),
47  Run(0),
48  Subrun(0),
49  FirstEvent(1e9),
50  LastEvent(-1),
51  Nevents(0),
52  StartYear(0),
53  EndYear(0),
54  StartMonth(0),
55  EndMonth(0),
56  StartDay(0),
57  EndDay(0),
58  StartHour(0.0),
59  EndHour(0.0),
60  StartTime(-1), // this is an unsigned int so it will default to a huge number
61  EndTime(0),
62  ThisNearlineMinorVersion(0),
63  ThisNearlineMajorVersion(0)
64  {
65 
66  getHeaderInfo(fp, headerTreeName);
67  getNearlineVersion(fp, versionHistName);
68 
69  }//constructor
70 
71  void getHeaderInfo(TFile *fp, std::string headerTreeName){
72  TTree *Header = (TTree*) fp->Get(headerTreeName.c_str());
73  if(!Header){
74  std::cout << "NearlineSummariser: ERROR: " << fp->GetName() << " doesn't contain header tree: " << headerTreeName << std::endl;
75  return;
76  }
77 
78  Header->SetBranchAddress("Run",&Run);
79  Header->SetBranchAddress("Subrun",&Subrun);
80  Header->SetBranchAddress("FirstEvent",&FirstEvent);
81  Header->SetBranchAddress("LastEvent",&LastEvent);
82  Header->SetBranchAddress("Nevents",&Nevents);
83  Header->SetBranchAddress("StartYear",&StartYear);
84  Header->SetBranchAddress("StartMonth",&StartMonth);
85  Header->SetBranchAddress("StartDay",&StartDay);
86  Header->SetBranchAddress("StartHour",&StartHour);
87  Header->SetBranchAddress("EndYear",&EndYear);
88  Header->SetBranchAddress("EndMonth",&EndMonth);
89  Header->SetBranchAddress("EndDay",&EndDay);
90  Header->SetBranchAddress("EndHour",&EndHour);
91 
92  Header->GetEntry(0);
93 
94  HasHeader = true;
95 
96  }//getHeaderInfo
97 
98  void getNearlineVersion(TFile* fp, std::string versionHistName){
99 
100  TH1* hist = (TH1*) fp->Get(versionHistName.c_str());
101 
102  if(!hist){
103  std::cout << "NearlineSummariser: ERROR: " << fp->GetName() << " doesn't contain version number hist: " << versionHistName << std::endl;
104  return;
105  }
106 
107  ThisNearlineMinorVersion = hist->GetBinContent(1);
108  ThisNearlineMajorVersion = hist->GetBinContent(2);
109 
110  }//getNearlineVersion
111 
112  friend std::ostream & operator << (std::ostream &os, NearlineFileInfo &rhs){
113  os << "Run " << rhs.Run
114  << " StartYear " << rhs.StartYear
115  << " StartMonth " << rhs.StartMonth
116  << " StartDay " << rhs.StartDay
117  << " StartHour " << rhs.StartHour
118  << " StartTime " << rhs.StartTime;
119  return os;
120  }//operator <<
121 
122 };//NearlineFileInfo
123 
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 
128 
129  std::map<std::string,std::shared_ptr<TH1>> MapNameHists;
131  TFile *File;
133  FileInfo(fp),
134  File(fp)
135  {
136 
137  }//NearlinePlots
138  void loadHistogram(std::string dirName, std::string histName){
139  std::string newHistName = histName + "_run" + std::to_string(FileInfo.Run);
140  std::string histFullName;
141  if(dirName=="") histFullName = histName;
142  else histFullName = dirName + "/" + histName;
143  std::shared_ptr<TH1> thisHisto(getHistogram(File, histFullName, newHistName));
144  MapNameHists.insert(std::pair<std::string,std::shared_ptr<TH1>>(histName, thisHisto));
145  }
146 };//NearlineRunPlotSet
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 
153 int main(int argc, char** argv){
154 
155  std::vector<std::string> vecFileNames;
156  for(int i=1;i<argc;i++){
157  std::string thisFileName(argv[i]);
158  vecFileNames.push_back(thisFileName);
159  }
160 
161  std::vector<NearlineRunPlotSet> VecRunPlotSet;
162  std::vector<std::string> VecPlotNames;
163  VecPlotNames.push_back("hped_per_event_chan_0");
164  VecPlotNames.push_back("hped_per_event_chan_128");
165  VecPlotNames.push_back("hped_per_event_chan_256");
166  VecPlotNames.push_back("hped_per_event_chan_384");
167  VecPlotNames.push_back("hped_per_event_chan_512");
168  VecPlotNames.push_back("hped_per_event_chan_640");
169  VecPlotNames.push_back("hped_per_event_chan_768");
170  VecPlotNames.push_back("hped_per_event_chan_896");
171  VecPlotNames.push_back("hped_per_event_chan_1024");
172  VecPlotNames.push_back("hped_per_event_chan_1152");
173  VecPlotNames.push_back("hped_per_event_chan_1280");
174  VecPlotNames.push_back("hped_per_event_chan_1408");
175  VecPlotNames.push_back("hped_per_event_chan_1536");
176  VecPlotNames.push_back("hped_per_event_chan_1664");
177  VecPlotNames.push_back("hped_per_event_chan_1792");
178  VecPlotNames.push_back("hped_per_event_chan_1920");
179 
180 
181 
182  for(auto thisFileName: vecFileNames){
183  TFile *fp = TFile::Open(thisFileName.c_str());
184  if(fp){
185  if(!checkFileHasDir(fp, NearlineInstanceLabel)) continue;
186  }
187 
188  NearlineRunPlotSet thisRunPlotSet(fp);
189 
190  //DEBUG
191  std::cout << "NearlineSummariser: INFO: "
192  << thisRunPlotSet.FileInfo
193  // << thisRunPlotSet.FileInfo.StartYear
194  // << " " << thisRunPlotSet.FileInfo.StartMonth
195  // << " " << thisRunPlotSet.FileInfo.StartDay
196  // << " " << thisRunPlotSet.FileInfo.StartHour
197  // << " " << thisRunPlotSet.FileInfo.StartTime
198  << "\n";
199 
200  std::cout << "NearlineSummariser: INFO: ThisNearlineVersion: "
201  << thisRunPlotSet.FileInfo.ThisNearlineMajorVersion
202  << "." << thisRunPlotSet.FileInfo.ThisNearlineMinorVersion
203  << " NearlineVersion (Summariser): "
205  << "." << NearlineMinorVersion
206  << std::endl;
207 
208  for(auto thisPlotName: VecPlotNames){
209  thisRunPlotSet.loadHistogram(NearlineInstanceLabel, thisPlotName);
210  }//VecPlotNames
211 
212  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_0");
213  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_128");
214  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_256");
215  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_384");
216  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_512");
217  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_640");
218  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_768");
219  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_896");
220  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1024");
221  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1152");
222  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1280");
223  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1408");
224  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1536");
225  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1664");
226  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1792");
227  // thisRunPlotSet.loadHistogram(NearlineInstanceLabel , "hped_per_event_chan_1920");
228 
229  VecRunPlotSet.push_back(thisRunPlotSet);
230 
231  // if(fp) checkFileHasDir(fp, "foo");
232  // listHistogramsInFile(thisFileName);
233 
234  }//vecFileNames
235 
236  for(auto thisRunPlotSet: VecRunPlotSet){
237  std::cout << "NearlineSummariser: INFO: " << thisRunPlotSet.FileInfo << std::endl;
238 
239  // for(auto thisPlotName: VecPlotNames){
240  // auto thisHist =thisRunPlotSet.MapNameHists.at(thisPlotName);
241  // double mean = (thisHist)->GetMean();
242  // double rms = (thisHist)->GetRMS();
243  // std::cout << "NearlineSummariser: INFO: Name " << thisPlotName << " count " << thisHist.use_count() << " mean " << mean << " rms " << rms << std::endl;
244  // }//thisPlotName
245 
246  // for(auto thisNameHistPair: thisRunPlotSet.MapNameHists){
247  // double mean = (thisNameHistPair.second)->GetMean();
248  // double rms = (thisNameHistPair.second)->GetRMS();
249  // std::cout << "NearlineSummariser: INFO: Name " << thisNameHistPair.first << " mean " << mean << " rms " << rms << std::endl;
250  // }//MapNameHists
251 
252  }//VecRunPlotSet
253 
254 
255  return 0;
256 }
257 
258 ////////////////////////////////////////////////////////////////////////////////
259 
260 bool checkFileHasDir(TFile* fp, std::string dirName){
261 
262  TList* listOfKeys = fp->GetListOfKeys();
263  TIter next(listOfKeys);
264  TKey* key;
265  while(( key = (TKey*)next() )){
266  std::string thisKeyName(key->GetName());
267  if(thisKeyName == dirName){
268  // std::cout << "NearlineSummariser: INFO: Got dirName: " << dirName << std::endl;
269  return true;
270  }
271  }
272  std::cout << "NearlineSummariser: ERROR: " << fp->GetName() << " doesn't contain a dir: " << dirName << std::endl;
273  return false;
274 }
275 
276 
277 ////////////////////////////////////////////////////////////////////////////////
278 
279 TH1* getHistogram(TFile* fp, std::string histName, std::string newHistName){
280 
281  TH1* hist;
282  TH1* outhist;
283 
284  // std::cout << "NearlineSummariser: INFO: Getting: " << histName << std::endl;
285 
286  fp->GetObject(histName.c_str(), hist);
287 
288  if(!hist){
289  std::cout << "NearlineSummariser: ERROR: Failed to get histogram: " << histName << "\n";
290  return NULL;
291  }
292 
293  // std::cout << "NearlineSummariser: INFO: Got: " << hist->GetName() << std::endl;
294 
295  outhist = (TH1*) hist->Clone(newHistName.c_str());
296  outhist->SetDirectory(0);
297 
298  // std::cout << "NearlineSummariser: INFO: Returning: " << outhist->GetName() << std::endl;
299 
300  return outhist;
301 }
302 ////////////////////////////////////////////////////////////////////////////////
std::map< std::string, std::shared_ptr< TH1 > > MapNameHists
unsigned long long int EndTime
const int NearlineMajorVersion
std::string string
Definition: nybbler.cc:12
const int NearlineMinorVersion
friend std::ostream & operator<<(std::ostream &os, NearlineFileInfo &rhs)
int main(int argc, char **argv)
def key(type, name=None)
Definition: graph.py:13
unsigned long long int StartTime
void getHeaderInfo(TFile *fp, std::string headerTreeName)
NearlineFileInfo(TFile *fp, std::string headerTreeName=NearlineInstanceLabel+"/Header", std::string versionHistName=NearlineInstanceLabel+"/hist_nearline_version")
bool checkFileHasDir(TFile *fp, std::string dirName)
const std::string NearlineInstanceLabel
TH1 * getHistogram(TFile *fp, std::string histName, std::string newHistName)
void getNearlineVersion(TFile *fp, std::string versionHistName)
NearlineFileInfo FileInfo
void loadHistogram(std::string dirName, std::string histName)
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
QTextStream & endl(QTextStream &s)