00001 #include "CSMBuildRunMap.h"
00002
00003 #include "TH2.h"
00004 #include "TAxis.h"
00005 #include "TROOT.h"
00006 #include "TKey.h"
00007 #include "TIterator.h"
00008 #include "TFile.h"
00009 #include "TTree.h"
00010 #include "TSystem.h"
00011 #include "TF1.h"
00012 #include "TCanvas.h"
00013 #include "TStyle.h"
00014
00015 #include <iostream>
00016 #include <fstream>
00017 #include <iomanip>
00018 #include <map>
00019 #include <set>
00020 #include <string>
00021 #include <stdlib.h>
00022 #include <assert.h>
00023 #include <stdio.h>
00024
00025 using namespace std;
00026
00027
00028
00029
00030
00031
00032
00033
00034 Int_t
00035 CSMBuildRunMap::buildRunMap(const Char_t *directory, const Char_t* filter) {
00036
00037 cout << "CSMBuilRunMap::buildRunMap" << endl;
00038
00039 std::map<Int_t,std::set<std::string> > runFileMap;
00040 if (!directory || !filter) return 0;
00041 void *dir = NULL;
00042 if ((dir = gSystem->OpenDirectory(directory)) != NULL) {
00043 const Char_t *dirEntry;
00044 while ((dirEntry = gSystem->GetDirEntry(dir)) != NULL) {
00045 Char_t buffer[2048];
00046 strcpy(buffer,directory);
00047 if (buffer[strlen(buffer)-1] != '/') strcat(buffer,"/");
00048 strcat(buffer,dirEntry);
00049 if (!strstr(buffer,filter)) continue;
00050 TFile *file = new TFile(buffer,"READ");
00051 if (file) {
00052 cout << "Read File buffer Initiatiated" << endl;
00053 TList *keys = file->GetListOfKeys();
00054 if (keys) {
00055 TIterator* iter = keys->MakeIterator();
00056 TKey* key = NULL;
00057 while ((key = static_cast<TKey*>(iter->Next())) != NULL) {
00058 if (strstr(key->GetName(),"bemcStatusAdc") != NULL) {
00059 Char_t name[2048];
00060 Short_t keyCycle;
00061 file->DecodeNameCycle(key->GetName(),name,keyCycle);
00062 Char_t *runNumberString = name+14;
00063 cout << runNumberString << endl;
00064 string s(name);
00065 Int_t runNumber = atoi(runNumberString);
00066 runFileMap[runNumber].insert(string(buffer));
00067 }
00068 }
00069 }
00070 }
00071 file->Close();
00072 delete file;
00073 }
00074 }
00075
00076 cout << "found histograms for " << runFileMap.size() << " runs" << endl;
00077 cout << "dumping run->file map" << endl;
00078 for (std::map<Int_t,std::set<std::string> >::const_iterator iter = runFileMap.begin();
00079 iter != runFileMap.end(); ++iter) {
00080 cout << "run number " << iter->first << endl;
00081 for (set<string>::const_iterator files = iter->second.begin();
00082 files != iter->second.end(); ++files) {
00083 cout << *files << endl;
00084 }
00085 }
00086
00087 Int_t runDate, runTime, minirunDate, minirunTime;
00088 Float_t runFillNumber, minirunFillNumber;
00089 for (std::map<Int_t,set<string> >::const_iterator iter = runFileMap.begin();
00090 iter != runFileMap.end(); ++iter) {
00091 cout << "Creating status histogram for run " << iter->first << endl;
00092 Char_t buffer[2048];
00093 Char_t tempola[100];
00094 strcpy(buffer,directory);
00095 sprintf(tempola,"/run%d.cal.total.hist.root",iter->first);
00096 strcat(buffer,tempola);
00097 TFile* outFile = new TFile(buffer,"RECREATE");
00098 if (outFile) cout << "outFile buffer initiated" << endl;
00099 TTree* runTree = new TTree("calinfo","Extraneous Information");
00100 runTree->Branch("fillnum",&runFillNumber,"fillnum/F");
00101 runTree->Branch("thedate",&runDate,"thedate/I");
00102 runTree->Branch("thetime",&runTime,"thetime/I");
00103 runDate = 99999999;
00104 runTime = 999999;
00105 runFillNumber = 0;
00106 sprintf(buffer,"bemcStatusAdc_%d",iter->first);
00107 string BEMChistName(buffer);
00108 sprintf(buffer,"bemcStatusEnergy_%d",iter->first);
00109 string BEMCenergyHistName(buffer);
00110 sprintf(buffer,"eemcStatusAdc_%d",iter->first);
00111 string EEMChistName(buffer);
00112 sprintf(buffer,"eemcStatusEnergy_%d",iter->first);
00113 string EEMCenergyHistName(buffer);
00114
00115
00116
00117 TH2F* myBEMCRunHist = NULL;
00118
00119 for (set<string>::const_iterator filenames = iter->second.begin();
00120 filenames != iter->second.end(); ++filenames) {
00121 TFile *file = new TFile(filenames->c_str(),"READ");
00122 if (file->IsOpen()) {
00123 TTree* minirunTree = dynamic_cast<TTree*>(file->Get("calinfo"));
00124 if(minirunTree) {
00125 minirunTree->SetBranchAddress("thedate",&minirunDate);
00126 minirunTree->SetBranchAddress("thetime",&minirunTime);
00127 minirunTree->SetBranchAddress("fillnum",&minirunFillNumber);
00128 minirunTree->GetEvent(0);
00129 } else {
00130 assert(minirunTree);
00131 }
00132 if(runDate > minirunDate) {
00133 runDate = minirunDate;
00134 runTime = minirunTime;
00135 } else if(runDate == minirunDate && runTime > minirunTime) {
00136 runTime = minirunTime;
00137 }
00138 runFillNumber = minirunFillNumber;
00139 TH2F* BEMChist = dynamic_cast<TH2F*>(file->Get(BEMChistName.c_str()));
00140 if (BEMChist) {
00141 if (BEMChist->Integral(3,BEMChist->GetXaxis()->GetNbins(),1,2) >1e5) cout << "File " << filenames->c_str() << " has a ton of 0's" << endl;
00142 if (!myBEMCRunHist) {
00143 outFile->cd();
00144 myBEMCRunHist = new TH2F(BEMChistName.c_str(),BEMChistName.c_str(),
00145 BEMChist->GetXaxis()->GetNbins(),
00146 BEMChist->GetXaxis()->GetXmin(),
00147 BEMChist->GetXaxis()->GetXmax(),
00148 BEMChist->GetYaxis()->GetNbins(),
00149 BEMChist->GetYaxis()->GetXmin(),
00150 BEMChist->GetYaxis()->GetXmax());
00151 }
00152 myBEMCRunHist->Add(BEMChist);
00153 } else {
00154 cerr << "Didn't find histogram " << BEMChistName << " ???" << endl;
00155 cerr << "in file " << filenames->c_str() << " ???" << endl;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 }
00192 file->Close();
00193 delete file;
00194 }
00195 runTree->Fill();
00196 cout << "Writing myBEMCRunHist" << endl;
00197 myBEMCRunHist->Write();
00198
00199
00200 cout << "Writing outFile" << endl;
00201 outFile->Write();
00202 outFile->Close();
00203 delete outFile;
00204 }
00205 return 0;
00206 }
00207
00208 ClassImp(CSMBuildRunMap)