00001 #include "CalcCoarseTimeOffset.h"
00002 #include <iostream>
00003 #include <string>
00004 #include <sstream>
00005 #include "TGlobalData.h"
00006 #include "TSetupData.h"
00007
00008 #include "TAnalysedPulse.h"
00009 #include "TDetectorPulse.h"
00010 #include "RegisterModule.inc"
00011 #include "definitions.h"
00012 #include "SetupNavigator.h"
00013
00014
00015
00016 #include <cmath>
00017 #include <TH1F.h>
00018 #include <TH2F.h>
00019
00020
00021
00022 using modules::parser::GetOneWord;
00023 using namespace std;
00024 using std::string;
00025
00026 extern SourceAnalPulseMap gAnalysedPulseMap;
00027
00028 CalcCoarseTimeOffset::CalcCoarseTimeOffset(modules::options* opts):
00029 BaseModule("CalcCoarseTimeOffset",opts){
00030 fDetNameA = opts->GetString("det1");
00031 fDetNameB = opts->GetString("det2");
00032 }
00033
00034
00035
00036 CalcCoarseTimeOffset::~CalcCoarseTimeOffset(){
00037 }
00038
00040
00041 int CalcCoarseTimeOffset::BeforeFirstEntry(TGlobalData* gData,const TSetupData *setup){
00042
00043
00044
00045
00046
00047
00048
00049 for(SourceAnalPulseMap::const_iterator sourceIt = gAnalysedPulseMap.begin();
00050 sourceIt != gAnalysedPulseMap.end(); sourceIt++) {
00051 if( sourceIt->first.Channel() != fDetNameA) {
00052 continue;
00053 }
00054
00055
00056 AnalysedPulseList pulses = sourceIt->second;
00057
00058 for(SourceAnalPulseMap::const_iterator sourceIt2 = gAnalysedPulseMap.begin(); sourceIt2 != gAnalysedPulseMap.end(); sourceIt2++) {
00059 if(sourceIt2->first.Channel() != fDetNameB)
00060 continue;
00061 if(sourceIt2->first.Generator() != sourceIt->first.Generator())
00062 continue;
00063
00064
00065 fDetASources.push_back(sourceIt->first);
00066 fDetBSources.push_back(sourceIt2->first);
00067 }
00068 }
00069 return 0;
00070 }
00071
00072
00073
00074 int CalcCoarseTimeOffset::ProcessEntry(TGlobalData* gData,const TSetupData *setup) {
00075 for(SourceVector::const_iterator sourceIt = fDetASources.begin(); sourceIt != fDetASources.end(); sourceIt++) {
00076 const AnalysedPulseList& detAPulses = gAnalysedPulseMap[*sourceIt];
00077 const AnalysedPulseList& detBPulses = gAnalysedPulseMap[fDetBSources.at(sourceIt - fDetASources.begin())];
00078
00079 const std::string keyname = sourceIt->str();
00080 CreateHistogramsIfDontExist(keyname, (*sourceIt).Generator().str());
00081
00082 for(AnalysedPulseList::const_iterator pulseIt = detAPulses.begin();
00083 pulseIt != detAPulses.end(); ++pulseIt) {
00084 double timeA = (*pulseIt)->GetTime();
00085 for(AnalysedPulseList::const_iterator pulseIt2 = detBPulses.begin();
00086 pulseIt2 != detBPulses.end(); ++pulseIt2) {
00087
00088 double timeB = (*pulseIt2)->GetTime();
00089 double tDiff = timeA - timeB;
00090
00091 oned_plots[keyname]->Fill(tDiff);
00092
00093 }
00094 }
00095 }
00096
00097 return 0;
00098 }
00099
00100
00101
00102
00103 int CalcCoarseTimeOffset::AfterLastEntry(TGlobalData* gData, const TSetupData *setup){
00104 for (unsigned int i = 0; i < fDetASources.size(); ++i) {
00105 const std::string keyname = fDetASources[i].str();
00106 const double dt = oned_plots[keyname]->GetBinCenter(oned_plots[keyname]->GetMaximumBin());
00107 SetupNavigator::Instance()->SetCoarseTimeOffset(fDetASources[i], dt);
00108 }
00109 return 0;
00110 }
00111
00112 void CalcCoarseTimeOffset::CreateHistogramsIfDontExist(const std::string& src, const std::string& gen) {
00113 if( oned_plots.find(src) == oned_plots.end() ) {
00114 std::string histname = "h" + fDetNameB + "_" + src + "TDiff_1D";
00115 std::string histtitle = "Time difference between " + fDetNameA + " and " + fDetNameB + " detectors with the " + gen + " generator";
00116 TH1F* dplots = new TH1F(histname.c_str(), histtitle.c_str(), 500, -10000, 10000);
00117 dplots->GetXaxis()->SetTitle("Time Difference (ns)");
00118 dplots->GetYaxis()->SetTitle("Count");
00119 oned_plots[src] = dplots;
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128 ALCAP_REGISTER_MODULE(CalcCoarseTimeOffset,det1,det2);