StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CSMBuildRunMap.cxx
1 #include "CSMBuildRunMap.h"
2 
3 #include "TH2.h"
4 #include "TAxis.h"
5 #include "TROOT.h"
6 #include "TKey.h"
7 #include "TIterator.h"
8 #include "TFile.h"
9 #include "TTree.h"
10 #include "TSystem.h"
11 #include "TF1.h"
12 #include "TCanvas.h"
13 #include "TStyle.h"
14 
15 #include <iostream>
16 #include <fstream>
17 #include <iomanip>
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <stdio.h>
24 
25 using namespace std;
26 
27 //this opens ALL(!!) of the files in the directory specified
28 //and looks for histograms containing the phrase bemcStatusAdc
29 //It then adds up all of the 2d histograms to make one 2d histogram
30 //for that particular run FOR EACH DETECTOR
31 //and puts all of those 2d histograms in an output file called
32 //directory/run##.calorimeters.hist.root
33 
34 Int_t
35 CSMBuildRunMap::buildRunMap(const Char_t *directory, const Char_t* filter) {
36 
37  cout << "CSMBuilRunMap::buildRunMap" << endl;
38 
39  std::map<Int_t,std::set<std::string> > runFileMap;
40  if (!directory || !filter) return 0;
41  void *dir = NULL;
42  if ((dir = gSystem->OpenDirectory(directory)) != NULL) {
43  const Char_t *dirEntry;
44  while ((dirEntry = gSystem->GetDirEntry(dir)) != NULL) {
45  Char_t buffer[2048];
46  strcpy(buffer,directory);
47  if (buffer[strlen(buffer)-1] != '/') strcat(buffer,"/");
48  strcat(buffer,dirEntry);
49  if (!strstr(buffer,filter)) continue;
50  TFile *file = new TFile(buffer,"READ");
51  if (file) {
52  cout << "Read File buffer Initiatiated" << endl;
53  TList *keys = file->GetListOfKeys();
54  if (keys) {
55  TIterator* iter = keys->MakeIterator();
56  TKey* key = NULL;
57  while ((key = static_cast<TKey*>(iter->Next())) != NULL) {
58  if (strstr(key->GetName(),"bemcStatusAdc") != NULL) {
59  Char_t name[2048];
60  Short_t keyCycle;
61  file->DecodeNameCycle(key->GetName(),name,keyCycle);
62  Char_t *runNumberString = name+14;
63  cout << runNumberString << endl;
64  string s(name);
65  Int_t runNumber = atoi(runNumberString);
66  runFileMap[runNumber].insert(string(buffer));
67  }
68  }
69  }
70  }
71  file->Close();
72  delete file;
73  }
74  }
75 //this just couts the entire map you just acquired
76  cout << "found histograms for " << runFileMap.size() << " runs" << endl;
77  cout << "dumping run->file map" << endl;
78  for (std::map<Int_t,std::set<std::string> >::const_iterator iter = runFileMap.begin();
79  iter != runFileMap.end(); ++iter) {
80  cout << "run number " << iter->first << endl;
81  for (set<string>::const_iterator files = iter->second.begin();
82  files != iter->second.end(); ++files) {
83  cout << *files << endl;
84  }
85  }
86 
87  Int_t runDate, runTime, minirunDate, minirunTime;
88  Float_t runFillNumber, minirunFillNumber;
89  for (std::map<Int_t,set<string> >::const_iterator iter = runFileMap.begin();
90  iter != runFileMap.end(); ++iter) {
91  cout << "Creating status histogram for run " << iter->first << endl;
92  Char_t buffer[2048];
93  Char_t tempola[100];
94  strcpy(buffer,directory);
95  sprintf(tempola,"/run%d.cal.total.hist.root",iter->first);
96  strcat(buffer,tempola);
97  TFile* outFile = new TFile(buffer,"RECREATE");
98  if (outFile) cout << "outFile buffer initiated" << endl;
99  TTree* runTree = new TTree("calinfo","Extraneous Information");
100  runTree->Branch("fillnum",&runFillNumber,"fillnum/F");
101  runTree->Branch("thedate",&runDate,"thedate/I");
102  runTree->Branch("thetime",&runTime,"thetime/I");
103  runDate = 99999999;
104  runTime = 999999;
105  runFillNumber = 0;
106  sprintf(buffer,"bemcStatusAdc_%d",iter->first);
107  string BEMChistName(buffer);
108  sprintf(buffer,"bemcStatusEnergy_%d",iter->first);
109  string BEMCenergyHistName(buffer);
110  sprintf(buffer,"eemcStatusAdc_%d",iter->first);
111  string EEMChistName(buffer);
112  sprintf(buffer,"eemcStatusEnergy_%d",iter->first);
113  string EEMCenergyHistName(buffer);
114  //cout << "reading " << bemchistName << " and " << bemcenergyHistName << endl;
115  // TH2F* myEEMCRunHist = NULL; //D.Staszak
116  // TH2F* myEEMCEnergyRunHist = NULL;
117  TH2F* myBEMCRunHist = NULL;
118  // TH2F* myBEMCEnergyRunHist = NULL;
119  for (set<string>::const_iterator filenames = iter->second.begin();
120  filenames != iter->second.end(); ++filenames) {
121  TFile *file = new TFile(filenames->c_str(),"READ");
122  if (file->IsOpen()) {
123  TTree* minirunTree = dynamic_cast<TTree*>(file->Get("calinfo"));
124  if(minirunTree) {
125  minirunTree->SetBranchAddress("thedate",&minirunDate);
126  minirunTree->SetBranchAddress("thetime",&minirunTime);
127  minirunTree->SetBranchAddress("fillnum",&minirunFillNumber);
128  minirunTree->GetEvent(0);
129  } else {
130  assert(minirunTree); //uh, where's the tree?
131  }
132  if(runDate > minirunDate) {
133  runDate = minirunDate;
134  runTime = minirunTime;
135  } else if(runDate == minirunDate && runTime > minirunTime) {
136  runTime = minirunTime;
137  }
138  runFillNumber = minirunFillNumber;
139  TH2F* BEMChist = dynamic_cast<TH2F*>(file->Get(BEMChistName.c_str()));
140  if (BEMChist) {
141  if (BEMChist->Integral(3,BEMChist->GetXaxis()->GetNbins(),1,2) >1e5) cout << "File " << filenames->c_str() << " has a ton of 0's" << endl; // D.Staszak
142  if (!myBEMCRunHist) {
143  outFile->cd();
144  myBEMCRunHist = new TH2F(BEMChistName.c_str(),BEMChistName.c_str(),
145  BEMChist->GetXaxis()->GetNbins(),
146  BEMChist->GetXaxis()->GetXmin(),
147  BEMChist->GetXaxis()->GetXmax(),
148  BEMChist->GetYaxis()->GetNbins(),
149  BEMChist->GetYaxis()->GetXmin(),
150  BEMChist->GetYaxis()->GetXmax());
151  }
152  myBEMCRunHist->Add(BEMChist);
153  } else {
154  cerr << "Didn't find histogram " << BEMChistName << " ???" << endl;
155  cerr << "in file " << filenames->c_str() << " ???" << endl;
156  }
157  /*
158  TH2F* BEMCenergyHist = dynamic_cast<TH2F*>(file->Get(BEMCenergyHistName.c_str()));
159  if (BEMCenergyHist) {
160  if (!myBEMCEnergyRunHist) {
161  outFile->cd();
162  myBEMCEnergyRunHist = new TH2F(BEMCenergyHistName.c_str(),BEMCenergyHistName.c_str(),
163  BEMCenergyHist->GetXaxis()->GetNbins(),
164  BEMCenergyHist->GetXaxis()->GetXmin(),
165  BEMCenergyHist->GetXaxis()->GetXmax(),
166  BEMCenergyHist->GetYaxis()->GetNbins(),
167  BEMCenergyHist->GetYaxis()->GetXmin(),
168  BEMCenergyHist->GetYaxis()->GetXmax());
169  }
170  myBEMCEnergyRunHist->Add(BEMCenergyHist);
171  } else {
172  cerr << "Didn't find histogram " << BEMCenergyHistName << " ???" << endl;
173  }
174  TH2F* EEMChist = dynamic_cast<TH2F*>(file->Get(EEMChistName.c_str()));
175  if (EEMChist) {
176  if (!myEEMCRunHist) {
177  outFile->cd();
178  myEEMCRunHist = new TH2F(EEMChistName.c_str(),EEMChistName.c_str(),
179  EEMChist->GetXaxis()->GetNbins(),
180  EEMChist->GetXaxis()->GetXmin(),
181  EEMChist->GetXaxis()->GetXmax(),
182  EEMChist->GetYaxis()->GetNbins(),
183  EEMChist->GetYaxis()->GetXmin(),
184  EEMChist->GetYaxis()->GetXmax());
185  }
186  myEEMCRunHist->Add(EEMChist);
187  } else {
188  cerr << "Didn't find histogram " << EEMChistName << " ???" << endl;
189  }
190  */
191  }
192  file->Close();
193  delete file;
194  }
195  runTree->Fill();
196  cout << "Writing myBEMCRunHist" << endl;
197  myBEMCRunHist->Write();
198  // cout << "Writing myEEMCRunHist" << endl;
199  // myEEMCRunHist->Write();
200  cout << "Writing outFile" << endl;
201  outFile->Write();
202  outFile->Close();
203  delete outFile;
204  }
205  return 0;
206 }
207 
208 ClassImp(CSMBuildRunMap)