00001 #include "TMEGeneratorFactory.h"
00002 #include "FixedWindowMEGenerator.h"
00003 #include "TDetectorPulse.h"
00004 #include "TMuonEvent.h"
00005
00006 #include "debug_tools.h"
00007
00008 #include <iostream>
00009 using std::cout;
00010 using std::endl;
00011
00012 FixedWindowMEGenerator::FixedWindowMEGenerator(TMEGeneratorOptions* opts):
00013 TVMuonEventGenerator("FixedWindow",opts),fInit(true){
00014 fEventWindow=opts->GetDouble("event_window",1e4);
00015 }
00016
00017 int FixedWindowMEGenerator::Init(const SourceDetPulseMap& detectorPulsesIn){
00018 for(SourceDetPulseMap::const_iterator i_source=detectorPulsesIn.begin();
00019 i_source!=detectorPulsesIn.end();++i_source){
00020
00021 if(i_source->first.matches(IDs::channel(IDs::kMuSc))){
00022
00023 if(!fMuSc.source || fMuSc.source->isWildCardChannel()){
00024 fMuSc=Detector_t(i_source);
00025 }else{
00026 cout<<"FixedWindowMEGenerator: Error: Multiple muSc TDP sources found"<<endl;
00027 return 1;
00028 }
00029 }else{
00030 fDetectors.push_back(Detector_t(i_source));
00031 }
00032 }
00033 return 0;
00034 }
00035
00036 void FixedWindowMEGenerator::Reset(){
00037 fMuSc.Reset();
00038 for(SourceList::iterator i_det=fDetectors.begin();
00039 i_det!=fDetectors.end(); ++i_det){
00040 i_det->Reset();
00041 }
00042 }
00043
00044 int FixedWindowMEGenerator::ProcessPulses(MuonEventList& muonEventsOut,
00045 const SourceDetPulseMap& detectorPulsesIn){
00046 if(fInit) {
00047 int error=Init(detectorPulsesIn);
00048 if(error) return error;
00049 fInit=false;
00050 }else{
00051 Reset();
00052 }
00053
00054
00055 for(DetectorPulseList::const_iterator i_muSc=fMuSc.pulses->begin();
00056 i_muSc!=fMuSc.pulses->end(); ++i_muSc){
00057
00058 TMuonEvent* tme=new TMuonEvent(*i_muSc, fEventWindow);
00059
00060
00061 AddPulsesInWindow(tme,fEventWindow,fMuSc);
00062
00063 for(SourceList::iterator i_det=fDetectors.begin();
00064 i_det!=fDetectors.end(); ++i_det){
00065
00066 AddPulsesInWindow(tme,fEventWindow,*i_det);
00067 }
00068
00069
00070
00071
00072 muonEventsOut.push_back(tme);
00073 }
00074 return 0;
00075 }
00076
00077 void FixedWindowMEGenerator::AddPulsesInWindow(
00078 TMuonEvent* tme, double window, Detector_t& detector){
00079
00080 double central_time=tme->GetTime();
00081 double early_edge=central_time-window;
00082 double late_edge=central_time+window;
00083
00084
00085
00086 DetectorPulseList::const_iterator& start=detector.start_window;
00087 DetectorPulseList::const_iterator& stop=detector.end_window;
00088 const DetectorPulseList::const_iterator& end=detector.pulses->end();
00089
00090
00091
00092 if(start==end) return;
00093
00094
00095 DetectorPulseList::const_iterator i_tmp=start;
00096 while( (i_tmp !=end)){
00097
00098 while( (i_tmp!=end) && !(*i_tmp)->IsGood() ) ++i_tmp;
00099
00100 if(i_tmp==end) return;
00101 double time=(*i_tmp)->GetTime(TDetectorPulse::kFast);
00102 if(time<early_edge) ++i_tmp;
00103 else break;
00104 }
00105 start=i_tmp;
00106
00107 while( (i_tmp !=end)){
00108
00109 while( (i_tmp!=end) && !(*i_tmp)->IsGood() ) ++i_tmp;
00110
00111 if(i_tmp==end) break;
00112 double time=(*i_tmp)->GetTime(TDetectorPulse::kFast);
00113 if(time<late_edge) ++i_tmp;
00114 else break;
00115 }
00116 stop=i_tmp;
00117
00118
00119 tme->AddPulses(*detector.source,start,stop);
00120
00121
00122 if(stop==detector.pulses->end()) tme->AllPulsesUsed(*detector.source);
00123 }
00124
00125 ALCAP_TME_GENERATOR(FixedWindow, event_window);