00001
00002
00003
00004
00005
00007
00008 #include <cstdlib>
00009 #include <fstream>
00010
00011 #include "TFile.h"
00012 #include "TTree.h"
00013 #include "TClonesArray.h"
00014
00015 #include "Conventions/Detector.h"
00016 #include "MessageService/MsgService.h"
00017
00018 #include "MeuCal/MeuHitInfo.h"
00019 #include "MeuCal/MeuSummary.h"
00020 #include "MeuCal/MeuSummaryWriter.h"
00021
00022 using std::endl;
00023 using std::cout;
00024 using std::map;
00025 using std::vector;
00026
00027 CVSID("$Id: MeuSummaryWriter.cxx,v 1.4 2009/02/28 21:46:14 gmieg Exp $");
00028
00029
00030
00031 MeuSummaryWriter::MeuSummaryWriter()
00032 {
00033 MSG("MeuSummaryWriter",Msg::kDebug)
00034 <<"Running MeuSummaryWriter Constructor..."<<endl;
00035
00036 MSG("MeuSummaryWriter",Msg::kDebug)
00037 <<"Finished MeuSummaryWriter Constructor"<<endl;
00038 }
00039
00040
00041
00042 MeuSummaryWriter::~MeuSummaryWriter()
00043 {
00044 MSG("MeuSummaryWriter",Msg::kDebug)
00045 <<"Running MeuSummaryWriter Destructor..."<<endl;
00046
00047
00048 MSG("MeuSummaryWriter",Msg::kDebug)
00049 <<"Finished MeuSummaryWriter Destructor"<<endl;
00050 }
00051
00052
00053
00054 void MeuSummaryWriter::SummaryTreeSetup(Int_t run,Int_t subrun)
00055 {
00056 fSummaryFile=this->OpenFile(run,subrun,"MeuSummary");
00057 fSummaryTree=new TTree("s","s");
00058 cout<<"Creating new MeuSummary for output to tree"<<endl;
00059 fMeuSummary=new MeuSummary();
00060 fSummaryTree->Branch("s","MeuSummary",&fMeuSummary,32000,2);
00061 }
00062
00063
00064
00065 void MeuSummaryWriter::SummaryTreeFinish()
00066 {
00067 MSG("MeuSummaryWriter",Msg::kInfo)
00068 <<"Running MeuSummaryWriter::SummaryTreeFinish..."<<endl;
00069
00070 MSG("MeuSummaryWriter",Msg::kInfo)
00071 <<"Changing directory to fSummaryFile..."<<endl;
00072
00073 fSummaryFile->cd();
00074
00075 MSG("MeuSummaryWriter",Msg::kInfo)<<"Writing tree..."<<endl;
00076
00077
00078
00079
00080 fSummaryFile->Write();
00081
00082 MSG("MeuSummaryWriter",Msg::kInfo)
00083 <<"Closing file "<<fSummaryFile->GetName()<<" ..."<<endl;
00084
00085 fSummaryFile->Close();
00086
00087 MSG("MeuSummaryWriter",Msg::kInfo)
00088 <<"Completed MeuSummaryWriter::SummaryTreeFinish"<<endl;
00089 }
00090
00091
00092
00093 MeuSummary& MeuSummaryWriter::GetMeuSummaryToFill()
00094 {
00095 fMeuSummary->Reset();
00096 return *fMeuSummary;
00097 }
00098
00099
00100
00101 TFile* MeuSummaryWriter::OpenFile(Int_t run,Int_t subrun,
00102 std::string prefix) const
00103 {
00104
00105 TFile* outputFile=0;
00106
00107
00108 char *anaDir=getenv("MEUANA_DIR");
00109
00110
00111 string sAnaDir="";
00112
00113 if (anaDir!=NULL) {
00114 sAnaDir=anaDir;
00115 }
00116 else {
00117 MSG("MeuSummaryWriter",Msg::kInfo)
00118 <<"Environmental variable $MEUANA_DIR not set."
00119 <<" Writing file(s) to current directory"<<endl;
00120 sAnaDir=".";
00121 }
00122
00123
00124 string sRunNumber=Form("%d",run);
00125 string sSubrunNumber=Form("%d",subrun);
00126
00127 string sDetector="";
00128
00129 string sPrefix="";
00130 if (prefix!="") sPrefix+=prefix;
00131 string sBase=sAnaDir+"/"+sPrefix+sDetector+sRunNumber;
00132 sBase+="_"+sSubrunNumber;
00133 string sFileName=sBase+".root";
00134
00135
00136 ifstream Test(sFileName.c_str());
00137
00138
00139 if(!Test){
00140 outputFile=new TFile(sFileName.c_str(),"RECREATE");
00141 }
00142 else {
00143
00144 Int_t fred=1;
00145 while(Test) {
00146 Test.close();
00147 string sAppendage=Form("%d",fred);
00148 sFileName=sBase+"_"+sAppendage+".root";
00149 Test.open(sFileName.c_str());
00150 fred++;
00151 }
00152 outputFile=new TFile(sFileName.c_str(),"NEW");
00153 outputFile->SetCompressionLevel(9);
00154 }
00155
00156 string sTmp="No File!";
00157 if (outputFile) sTmp=outputFile->GetName();
00158
00159 MSG("MeuSummaryWriter",Msg::kInfo)
00160 <<"Output file opened: "<<sTmp<<endl;
00161 return outputFile;
00162 }
00163
00164
00165
00166 void MeuSummaryWriter::SummaryTreeFill
00167 (const std::map<Int_t,MeuHitInfo>& plInfo)
00168 {
00169 if (!fMeuSummary) cout<<"Ahhhhh no fMeuSummary object to put in tree"<<endl;
00170
00171 MSG("MeuSummaryWriter",Msg::kDebug)<<"Filling summaryTree..."<<endl;
00172
00173 static Int_t counter=0;
00174 fMeuSummary->Count=counter;
00175 counter++;
00176
00177 this->FillMeuHitInfo(plInfo);
00178
00179
00180 fSummaryTree->Fill();
00181 }
00182
00183
00184
00185 void MeuSummaryWriter::FillMeuHitInfo
00186 (const std::map<Int_t,MeuHitInfo>& plInfo)
00187 {
00188 MSG("MeuSummaryWriter",Msg::kDebug)<<" filling calibPos..."<<endl;
00189
00190 MeuSummary& s=*fMeuSummary;
00191
00192
00193 if (s.WinSigCor<=0) return;
00194
00195
00196 TClonesArray& calibPos = *(s.MeuHitInfo);
00197
00198
00199 Int_t i=0;
00200
00201 for (map<Int_t,MeuHitInfo>::const_iterator it=plInfo.begin();
00202 it!=plInfo.end();++it){
00203
00204 Int_t plane=it->second.Plane;
00205 if (plane<0) continue;
00206
00207
00208 if ((plane>=s.WinVtxSidePl && plane<=s.WinStopSidePl) ||
00209 (plane<=s.WinVtxSidePl && plane>=s.WinStopSidePl)) {
00210
00211 if (plane>120 && s.Detector==Detector::kNear) cout
00212 <<"plane>120!!!!!!!, pl="<<plane<<endl;
00213
00214 MeuHitInfo* cp=new(calibPos[i++]) MeuHitInfo();
00215 *cp=it->second;
00216
00217 MAXMSG("MeuSummaryWriter",Msg::kVerbose,1000)
00218 <<"Filling MeuHitInfo: pl="<<cp->Plane
00219 <<", sigMap="<<cp->SigMap<<endl;
00220 }
00221 }
00222 }
00223
00224