00001 #include "RegisterModule.inc"
00002 #include "MakeDetectorPulses.h"
00003 #include "TVDetectorPulseGenerator.h"
00004 #include "MaxTimeDiffDPGenerator.h"
00005 #include "TDPGeneratorFactory.h"
00006 #include "TDPGeneratorOptions.h"
00007 #include "debug_tools.h"
00008
00009 #include <sstream>
00010 #include <algorithm>
00011 using std::endl;
00012 using std::cout;
00013
00014 extern SourceAnalPulseMap gAnalysedPulseMap;
00015 extern SourceDetPulseMap gDetectorPulseMap;
00016
00017 namespace{
00018 struct is_equal {
00019 is_equal( const IDs::channel& ch):fCh(ch){fCh.SlowFast(IDs::kAnySlowFast);};
00020 bool operator()(const std::string& str){return fCh.matches(str);}
00021 private:
00022 IDs::channel fCh;
00023 };
00024 }
00025
00026 MakeDetectorPulses::MakeDetectorPulses(modules::options* opts):
00027 BaseModule("MakeDetectorPulses",opts,false),fOptions(opts),fPassThruGenerator(NULL){
00028
00029
00030 opts->GetVectorStringsByWhiteSpace("analyse_detectors",fDetectorsToAnalyse);
00031 std::sort(fDetectorsToAnalyse.begin(), fDetectorsToAnalyse.end());
00032 fDefaultAlgorithm=opts->GetString("default_algorithm","MaxTimeDiff");
00033 fPassThruName=opts->GetString("pass_through_algorithm","PassThrough");
00034 }
00035
00036 MakeDetectorPulses::~MakeDetectorPulses(){
00037 }
00038
00039 int MakeDetectorPulses::BeforeFirstEntry(TGlobalData* gData, const TSetupData* setup){
00040
00041 bool analyse_all=fDetectorsToAnalyse.empty();
00042 if(!analyse_all) analyse_all= (std::find(fDetectorsToAnalyse.begin(), fDetectorsToAnalyse.end(), "all")!=fDetectorsToAnalyse.end());
00043
00044 const IDs::channel* ch;
00045 const IDs::generator* gen;
00046
00047
00048 for (SourceAnalPulseMap::const_iterator i_source = gAnalysedPulseMap.begin();
00049 i_source != gAnalysedPulseMap.end(); i_source++) {
00050 ch=&i_source->first.Channel();
00051 gen=&i_source->first.Generator();
00052
00053
00054 if( !analyse_all &&
00055 (std::find_if(fDetectorsToAnalyse.begin(),
00056 fDetectorsToAnalyse.end(),
00057 is_equal(*ch))==fDetectorsToAnalyse.end())) continue;
00058
00059
00060 IDs::source partner =i_source->first;
00061 for (SourceAnalPulseMap::const_iterator j_source = gAnalysedPulseMap.begin();
00062 j_source != gAnalysedPulseMap.end(); ++j_source) {
00063
00064 if (j_source->first.Generator().Type() == gen->Type()
00065 && j_source->first.Channel() == ch->GetCorrespondingFastSlow()) {
00066 partner = j_source->first;
00067 }
00068 }
00069
00070
00071 const std::string detname=IDs::channel::GetDetectorString(ch->Detector());
00072 if( fOptions->HasOption(detname) ){
00073
00074
00075 bool success=ParseGeneratorList(i_source->first, partner,fOptions->GetString(detname));
00076 if(! success) return 1;
00077 }else if(partner==i_source->first) {
00078
00079
00080 bool success=ParseGeneratorList(i_source->first, partner,fPassThruName);
00081 if(! success) return 1;
00082 } else {
00083
00084 bool success=ParseGeneratorList(i_source->first, partner,fDefaultAlgorithm);
00085 if(! success) return 1;
00086 }
00087 }
00088
00089
00090 for(ChannelPairing_t::const_iterator i_pair=fFastSlowPairs.begin();
00091 i_pair!=fFastSlowPairs.end();i_pair++){
00092 gDetectorPulseMap[i_pair->source];
00093 if(Debug()){
00094 std::cout<<"Paired: "<<i_pair->fast<<" with "<<i_pair->slow<<" processed by: "<<i_pair->generator->GetGeneratorID()<<std::endl;
00095 }
00096 }
00097 return 0;
00098 }
00099
00100 int MakeDetectorPulses::ProcessEntry(TGlobalData *gData, const TSetupData* gSetup){
00101 IDs::channel detector(IDs::kAnyDetector,IDs::kNotApplicable);
00102 IDs::source source;
00103 const AnalysedPulseList *slow_pulses;
00104 const AnalysedPulseList *fast_pulses;
00105 SourceAnalPulseMap::const_iterator current;
00106
00107
00108
00109 for (ChannelPairing_t::const_iterator i_detector = fFastSlowPairs.begin();
00110 i_detector != fFastSlowPairs.end(); i_detector++) {
00111 detector=i_detector->fast.Channel();
00112
00113
00114 current=gAnalysedPulseMap.find(i_detector->fast);
00115 if(current==gAnalysedPulseMap.end()){
00116 std::cout<<"Unable to find source for fast channel: "<<i_detector->fast<<std::endl;
00117 DumpgAnalysedPulseMap(gAnalysedPulseMap);
00118 return 1;
00119 }
00120 fast_pulses=&(current->second);
00121
00122
00123 if(fast_pulses->empty()) {
00124 if(Debug()) cout<< "List of TAPs for "<<i_detector->fast<<" is empty."<<endl;
00125 continue;
00126 }
00127
00128
00129
00130 if(i_detector->fast==i_detector->slow) slow_pulses=NULL;
00131 else{
00132 current=gAnalysedPulseMap.find(i_detector->slow);
00133 if(current==gAnalysedPulseMap.end()){
00134 std::cout<<"Unable to find source for slow channel: "<<i_detector->slow<<std::endl;
00135 DumpgAnalysedPulseMap(gAnalysedPulseMap);
00136 return 1;
00137 }
00138 slow_pulses=&(current->second);
00139
00140 if(slow_pulses->empty()) {
00141 if(Debug())
00142 cout<< "List of TAPs for "<<i_detector->slow<<" is empty."<<endl;
00143 continue;
00144 }
00145 }
00146
00147
00148 i_detector->generator->SetChannel(detector);
00149
00150 i_detector->generator->SetPulseLists(fast_pulses,slow_pulses);
00151
00152 source=i_detector->source;
00153 i_detector->generator->ProcessPulses(gDetectorPulseMap.at(source));
00154 }
00155 return 0;
00156 }
00157
00158 bool MakeDetectorPulses::ParseGeneratorList(const IDs::source& current_source, const IDs::source& partner_source,
00159 const std::string& generator_specs){
00160 std::vector<std::string> generatorList;
00161 modules::parser::TokeniseByDelimiter(generator_specs,generatorList,":");
00162
00163
00164 std::vector<std::string>::const_iterator gen;
00165 std::string arg;
00166 TDPGeneratorOptions* opts;
00167 modules::parser::Constructor_t generator_request;
00168 bool still_good=true;
00169 for(gen=generatorList.begin();gen!= generatorList.end();gen++){
00170
00171 generator_request=modules::parser::ParseConstructor(*gen,'(',')');
00172 std::stringstream sstream(generator_request.inside);
00173 opts=new TDPGeneratorOptions(generator_request.before);
00174 for(int count=0; std::getline(sstream, arg,','); count++){
00175 opts->AddArgument(count,arg);
00176 }
00177 still_good = MakeGenerator(current_source,partner_source, generator_request.before,opts);
00178
00179 if (!still_good) {
00180 return false;
00181 }
00182
00183 opts=NULL;
00184 }
00185
00186 return true;
00187 }
00188
00189
00190 TVDetectorPulseGenerator* MakeDetectorPulses::MakeGenerator(const IDs::source& current_source, const IDs::source& partner_source, const std::string& generatorType, TDPGeneratorOptions* opts ){
00191 const IDs::channel& ch=current_source.Channel();
00192
00193
00194 TVDetectorPulseGenerator* generator=TDPGeneratorFactory::Instance()->createModule(generatorType,opts);
00195 if (!generator){
00196 return NULL;
00197 }
00198
00199 generator->SetDebug(Debug());
00200
00201
00202 IDs::source tdp_source=current_source;
00203 tdp_source.Channel().SlowFast(IDs::kNotApplicable);
00204 tdp_source.Generator()=generator->GetGeneratorID();
00205
00206
00207 cout<< "MakeDetectorPulses::BeforeFirstEntry: Making generator '"<<generatorType<<"' with source ID: '"<<tdp_source<<"'" <<endl;
00208
00209 if(ch.isFast()) {
00210
00211 generator->SetPulseSources(current_source,partner_source);
00212 fFastSlowPairs.insert(Detector_t(tdp_source, current_source,partner_source,generator));
00213 }else{
00214
00215
00216
00217 generator->SetPulseSources(partner_source,current_source);
00218 fFastSlowPairs.insert(Detector_t(tdp_source, partner_source,current_source,generator));
00219 }
00220
00221 return generator;
00222 }
00223
00224 void MakeDetectorPulses::DumpgAnalysedPulseMap(const SourceAnalPulseMap& aMap){
00225 std::cout<<"gAnalysedPulseMap contains:"<<std::endl;
00226 for(SourceAnalPulseMap::const_iterator it=aMap.begin();
00227 it!=aMap.end(); it++){
00228 cout<<it->first<<" contains "<<it->second.size()<<" TAPs"<<endl;
00229 }
00230
00231 }
00232 ALCAP_REGISTER_MODULE(MakeDetectorPulses,default_algorithm);