00001
00002
00003
00004
00009
00014
00019
00026
00027
00028
00029
00030
00031 #include <iostream>
00032 #include <string>
00033
00034 #include <map>
00035
00036 #include "SetupNavigator.h"
00037 #include "definitions.h"
00038
00039
00040 #include "TTree.h"
00041 #include "TBranch.h"
00042 #include "TFile.h"
00043
00044
00045 #include "CommandLine.h"
00046 #include "ModulesFactory.h"
00047 #include "ModulesNavigator.h"
00048 #include "BaseModule.h"
00049 #include "TGlobalData.h"
00050 #include "TSetupData.h"
00051 #include "TAnalysedPulse.h"
00052 #include "TDetectorPulse.h"
00053 #include "TMuonEvent.h"
00054
00055 #include "TAnalysedPulseMapWrapper.h"
00056 #include "EventNavigator.h"
00057
00058 #include "debug_tools.h"
00059
00060
00061 Int_t Main_event_loop(TTree* dataTree, ARGUMENTS& arguments);
00062 void ClearGlobalData(TGlobalData*);
00063 TTree* GetTree(TFile* inFile, const char* t_name);
00064 Int_t PrepareAnalysedPulseMap(TFile* fileOut);
00065 Int_t PrepareSingletonObjects(const ARGUMENTS&);
00066 void Finish(TFile*);
00067
00068
00069
00070
00071
00072 SourceAnalPulseMap gAnalysedPulseMap;
00073 SourceDetPulseMap gDetectorPulseMap;
00074 MuonEventList gMuonEvents;
00075
00076 int main(int argc, char **argv)
00077 {
00078
00079
00080
00081 ARGUMENTS arguments;
00082 int ret = analyze_command_line (argc, argv,arguments);
00083 if(ret!=0) return ret;
00084 std::cout << "Starting event" << std::endl;
00085
00086 SetupNavigator::Instance()->SetCommandLineArgs(arguments);
00087 SetupNavigator::Instance()->CacheCalibDB();
00088
00089
00090 ret= modules::navigator::Instance()->LoadConfigFile(arguments.mod_file.c_str());
00091 if(ret!=0) {
00092 std::cout<<"Error: Problem loading MODULES file"<<std::endl;
00093 return ret;
00094 }
00095
00096
00097 EventNavigator& navi = EventNavigator::Instance();
00098 if ( ! navi.ConnectInput(arguments.infile.c_str()) ){
00099 std::cout << "Failed to open input file '"
00100 << arguments.infile <<"'. Exiting." << std::endl;
00101 return 1;
00102 }
00103 if( modules::navigator::Instance()->DumpInputFile()){
00104 navi.DumpRawTree();
00105 }
00106
00107
00108 ret = PrepareSingletonObjects(arguments);
00109 if(ret!=0) {
00110 std::cout<<"Error: Problem creating a singleton object..."<<std::endl;
00111 return ret;
00112 }
00113
00114
00115
00116
00117
00118
00119 TTree* eventTree = navi.GetRawTree();
00120
00121
00122 TFile *fileOut = new TFile(arguments.outfile.c_str(), "RECREATE");
00123 if(!fileOut->IsOpen()){
00124 std::cout << "Could not open ROOT output file "
00125 << arguments.outfile << std::endl;
00126 return 1;
00127 }
00128 modules::navigator::Instance()->SetOutFile(fileOut);
00129
00130
00131
00132
00133 ret= modules::navigator::Instance()->MakeModules();
00134 if(ret!=0) {
00135 std::cout << "Problem creating analysis modules." << std::endl;
00136 return ret;
00137 }
00138
00139
00140 ret = PrepareAnalysedPulseMap(fileOut);
00141 if(ret!=0) {
00142 std::cout << "Problem preparing analysed pulse map." << std::endl;
00143 return ret;
00144 }
00145
00146
00147 fileOut->cd();
00148 try {
00149 Main_event_loop(eventTree,arguments);
00150 }
00151 catch (std::exception& e){
00152
00153 try{ Finish(fileOut); }catch(...){}
00154 std::cout << "Terminating due to unexpected exception:\n"
00155 << e.what() << std::endl;
00156 throw;
00157 }
00158 catch (...){
00159
00160 try{ Finish(fileOut); }catch(...){}
00161 std::cout << "Terminating due to unknown exception" << std::endl;
00162 throw;
00163 }
00164
00165 Finish(fileOut);
00166 return 0;
00167 }
00168
00169 void Finish(TFile* fileOut){
00170
00171 fileOut->cd();
00172
00173 fileOut->Write();
00174 fileOut->Close();
00175 EventNavigator::Instance().Close();
00176 SetupNavigator::Instance()->Close();
00177 }
00178
00179
00180 Int_t Main_event_loop(TTree* dataTree,ARGUMENTS& arguments)
00181 {
00182
00183 EventNavigator& enav = EventNavigator::Instance();
00184 Long64_t nEntries = enav.GetInputNEntries();
00185 std::cout << "Raw data tree contains " << nEntries
00186 << " entries." << std::endl
00187 << "Processing file, which may take a while..."
00188 << std::endl;
00189
00190
00191 TGlobalData* raw_data = enav.GetRawData();
00192 if ( raw_data ){
00193 raw_data->Clear("C");
00194 }
00195
00196 enav.MakeLoopSequence(arguments).Run();
00197 std::cout << "Finished processing data" << std::endl;
00198 return 0;
00199 }
00200
00201
00202
00203 void ClearGlobalData(TGlobalData* data)
00204 {
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 static bool should_delete_TAPS=!modules::navigator::Instance()->GetModule("LoadPulses");
00229 for(SourceAnalPulseMap::iterator mapIter=gAnalysedPulseMap.begin();
00230 mapIter != gAnalysedPulseMap.end(); ++mapIter) {
00231
00232
00233 AnalysedPulseList& pulse_vector= mapIter->second;
00234 if(should_delete_TAPS){
00235 for(size_t i=0; i<pulse_vector.size(); i++){
00236 delete pulse_vector[i];
00237 pulse_vector[i] = NULL;
00238 }
00239 }
00240 pulse_vector.clear();
00241 }
00242
00243
00244 for(SourceDetPulseMap::iterator mapIter = gDetectorPulseMap.begin();
00245 mapIter != gDetectorPulseMap.end(); ++mapIter) {
00246
00247 DetectorPulseList& pulse_vector= mapIter->second;
00248 for(size_t i=0; i<pulse_vector.size(); i++){
00249 delete pulse_vector[i];
00250 pulse_vector[i] = NULL;
00251 }
00252 pulse_vector.clear();
00253 }
00254
00255
00256 for(MuonEventList::iterator muonEvent = gMuonEvents.begin();
00257 muonEvent != gMuonEvents.end(); ++muonEvent) {
00258 delete *muonEvent;
00259 }
00260 gMuonEvents.clear();
00261 }
00262
00263
00264
00265 void PrintSetupData(TSetupData* s_data)
00266 {
00267 if(!s_data) return;
00268
00269 std::map<std::string, std::string>::iterator it_info;
00270 std::cout << "### TSetupData ###\n"
00271 << "Bank Detector Name Clock Tick (ns) Pedestal (ADC)"
00272 << " Trigger Polarity Time Shift (ns) No. of Bits\n"
00273 << "---- ------------- --------------- --------------"
00274 << " ---------------- --------------- -----------"
00275 << std::endl;
00276
00277 for (it_info=s_data->fBankToDetectorMap.begin();
00278 it_info!=s_data->fBankToDetectorMap.end(); ++it_info){
00279 const std::string& bankname = it_info->first;
00280 std::cout << bankname << " "
00281 << s_data->GetDetectorName(bankname) << "\t\t"
00282 << s_data->GetClockTick(bankname) << "\t"
00283 << s_data->GetPedestal(bankname) << "\t\t"
00284 << s_data->GetTriggerPolarity(bankname) << "\t\t"
00285 << s_data->GetTimeShift(bankname) << "\t\t"
00286 << s_data->GetNBits(bankname)
00287 << std::endl;
00288 }
00289 }
00290
00291
00292 Int_t PrepareAnalysedPulseMap(TFile* fileOut)
00293 {
00294
00295
00296
00297 int split = 1;
00298
00299 Int_t branchstyle = 1;
00300
00301 if (split < 0) {branchstyle = 0; split = -1-split;}
00302 TTree::SetBranchStyle(branchstyle);
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 return 0;
00313 }
00314
00315
00316 Int_t PrepareSingletonObjects(const ARGUMENTS&){
00317
00318 if(!modules::navigator::Instance()) return 1;
00319
00320
00321
00322 if( !EventNavigator::Instance().GetSetupData() ) return 2;
00323
00324 return 0;
00325 }
00326