00001 #include "PlotTDPs.h"
00002 #include "RegisterModule.inc"
00003 #include "TGlobalData.h"
00004 #include "TSetupData.h"
00005 #include "TDetectorPulse.h"
00006 #include "ModulesOptions.h"
00007 #include "ModulesNavigator.h"
00008 #include "ModulesParser.h"
00009 #include "definitions.h"
00010 #include "IdSource.h"
00011 #include "MakeDetectorPulses.h"
00012 #include "PassThroughDPGenerator.h"
00013 #include "SetupNavigator.h"
00014
00015 #include "TH2F.h"
00016 #include "TH1F.h"
00017 #include "TProfile.h"
00018 #include "TFitResult.h"
00019
00020 #include <cmath>
00021 #include <iostream>
00022 #include <sstream>
00023 #include <string>
00024 using std::cout;
00025 using std::endl;
00026
00027 using namespace std::rel_ops;
00028
00029 extern SourceDetPulseMap gDetectorPulseMap;
00030
00031 PlotTDPs::PlotTDPs(modules::options* opts):
00032 BaseModule("PlotTDPs",opts),fCutHists(false){
00033 fCutHists=!opts->GetFlag("no_cut_histos");
00034 fFastCut=opts->GetDouble("fast_ch_cut",0);
00035 fSlowCut=opts->GetDouble("slow_ch_cut",0);
00036 }
00037
00038 PlotTDPs::~PlotTDPs(){
00039 }
00040
00041 int PlotTDPs::BeforeFirstEntry(TGlobalData* gData,const TSetupData *setup){
00042
00043 GetDirectory()->cd();
00044
00045
00046 MakeDetectorPulses* makeTDPs=modules::navigator::Instance()->GetModule<MakeDetectorPulses>("MakeDetectorPulses");
00047 if(!makeTDPs){
00048 cout<<"It's meaningless to use the PlotTDPs module without MakeDetectorPulses as well"<<endl;
00049 return 1;
00050 }
00051
00052 if(!modules::navigator::Instance()->Before("MakeDetectorPulses",GetName())){
00053 cout<<"MakeDetectorPulses must be used before PlotTDPs"<<endl;
00054 return 2;
00055 }
00056
00057
00058 IDs::generator passThruId=PassThroughDPGenerator::GetStaticId();
00059 Detector_t tmp;
00060 std::string name;
00061 std::string title;
00062 std::stringstream full_title;
00063 IDs::channel fast_id;
00064 IDs::channel slow_id;
00065 int n_bits;
00066 double fast_amp_max, slow_amp_max;
00067 double fast_amp_min=0, slow_amp_min=0;
00068 double tdiff_min=-1e6, tdiff_max=1e6;
00069
00070
00071 for(SourceDetPulseMap::const_iterator i_source=gDetectorPulseMap.begin();
00072 i_source!= gDetectorPulseMap.end(); ++i_source){
00073
00074
00075
00076 if(passThruId.matches(i_source->first.Generator())){
00077 cout<<"Skipping TDP source, '"<<i_source->first<<"' as it was made with the pass-through generator"<<endl;
00078 continue;
00079 }
00080
00081
00082 name='h'+i_source->first.str();
00083 modules::parser::ToCppValid(name);
00084 title=" from ";
00085 title+=i_source->first.str();
00086 if(Debug()) {
00087 cout<<"Made histogram called "<<name<<" with title "<< title<<endl;
00088 }
00089
00090
00091 fast_id=i_source->first.Channel();
00092 fast_id.SlowFast(IDs::kFast);
00093 slow_id=fast_id.GetCorrespondingFastSlow();
00094 try{
00095 n_bits = setup->GetNBits(SetupNavigator::Instance()->GetBank(fast_id));
00096 fast_amp_max = std::pow(2, n_bits);
00097 n_bits = setup->GetNBits(SetupNavigator::Instance()->GetBank(slow_id));
00098 slow_amp_max = std::pow(2, n_bits);
00099 }catch(Except::InvalidDetector& e){
00100 cout<<"PlotTDPs::BeforeFirstEntry: Skipping channel '"<<i_source->first.Channel()
00101 <<"' due to an error querying the setup navigator. This is probably casued by only 1 of "
00102 "the fast/slow channels being connected for this run."<<endl;
00103 continue;
00104 }
00105
00106
00107 tmp.amplitudes=new TH2F((name+"_amp").c_str(),("Amplitudes of TDPs "+title).c_str(),
00108 150,fast_amp_min,fast_amp_max,150,slow_amp_min,slow_amp_max);
00109 tmp.amplitudes->SetXTitle("Amplitude in Fast channel");
00110 tmp.amplitudes->SetYTitle("Amplitude in Slow channel");
00111
00112
00113 tmp.fast_only_amps=new TH1F((name+"_fast_only_amp").c_str(),
00114 ("Amplitudes of hits with no corresponding slow pulse "+title).c_str(),
00115 200,fast_amp_min,fast_amp_max);
00116 tmp.fast_only_amps->SetXTitle("Amplitude in Fast channel");
00117
00118
00119 tmp.slow_only_amps=new TH1F((name+"_slow_only_amp").c_str(),
00120 ("Amplitudes of hits with no corresponding fast pulse "+title).c_str(),
00121 200,slow_amp_min,slow_amp_max);
00122 tmp.slow_only_amps->SetXTitle("Amplitude in Slow channel");
00123
00124
00125
00126 tmp.scale_factor=new TH1F((name+"_scale_factor").c_str(),
00127 ("Ratio of amplitudes "+title).c_str(),
00128 200,0,10);
00129 tmp.scale_factor->SetXTitle("Ratio of amplitudes, A_{Slow} / A_{Fast}");
00130
00131
00132 full_title.str("Time difference vs. fast amplitude for pulses ");
00133 full_title<<title;
00134 tmp.time_diff_fast=new TH2F((name+"_fast_amp_tdiff").c_str(),full_title.str().c_str(),
00135 200,tdiff_min,tdiff_max,200,fast_amp_min,fast_amp_max);
00136 tmp.time_diff_fast->SetXTitle("t_{Fast} - t_{Slow}");
00137 tmp.time_diff_fast->SetYTitle("Amplitude in Fast channel");
00138
00139 full_title.str("Time difference vs. slow amplitude for pulses ");
00140 full_title<<title;
00141 tmp.time_diff_slow=new TH2F((name+"_slow_amp_tdiff").c_str(),full_title.str().c_str(),
00142 200,tdiff_min,tdiff_max,200,slow_amp_min,slow_amp_max);
00143 tmp.time_diff_slow->SetXTitle("t_{Fast} - t_{Slow}");
00144 tmp.time_diff_slow->SetYTitle("Amplitude in Slow channel");
00145
00146 tmp.times=new TH2F((name+"_times").c_str(),("Times of pulses "+title).c_str(),
00147 200,0,-1,200,0,-1);
00148 tmp.times->SetXTitle("Time in Fast channel");
00149 tmp.times->SetYTitle("Time in Slow channel");
00150
00151 if(fCutHists){
00152
00153 full_title.str("");
00154 full_title<<"Amplitudes in slow channel if paired to a fast pulse with amplitude above ";
00155 full_title<<fFastCut<<title;
00156 tmp.slow_amps_fast_cut=new TH1F((name+"_slow_amp_fast_cut").c_str(),
00157 full_title.str().c_str(), 200,slow_amp_min,slow_amp_max);
00158 tmp.slow_amps_fast_cut->SetXTitle("Amplitude in Slow channel");
00159
00160
00161 full_title.str("");
00162 full_title<<"Amplitudes in fast channel if paired to a slow pulse with amplitude above ";
00163 full_title<<fSlowCut<<title;
00164 tmp.fast_amps_slow_cut=new TH1F((name+"_fast_amps_slow_cut").c_str(),
00165 full_title.str().c_str(), 200,fast_amp_min,fast_amp_max);
00166 tmp.fast_amps_slow_cut->SetXTitle("Amplitude in Fast channel");
00167 } else{
00168 tmp.fast_amps_slow_cut=NULL;
00169 tmp.slow_amps_fast_cut=NULL;
00170 }
00171
00172 tmp.amp_status=tmp.amp_inter=tmp.amp_grad=0;
00173
00174
00175 fPlotsList[i_source->first]=tmp;
00176 }
00177
00178
00179 GetDirectory()->cd("/");
00180 return 0;
00181 }
00182
00183 int PlotTDPs::ProcessEntry(TGlobalData* gData,const TSetupData *setup){
00184
00185
00186 const DetectorPulseList* pulseList;
00187 SourceDetPulseMap::const_iterator i_pulse_list;
00188 double fast_amp, slow_amp, fast_time, slow_time;
00189 Detector_t plots;
00190 for(PlotsList_t::iterator i_source=fPlotsList.begin();
00191 i_source!=fPlotsList.end(); ++i_source){
00192
00193 i_pulse_list=gDetectorPulseMap.find(i_source->first);
00194 if(i_pulse_list== gDetectorPulseMap.end()){
00195 cout<<"Unable to find TDP list for source, "<<i_source->first<<endl;
00196 return 1;
00197 }
00198 pulseList=&i_pulse_list->second;
00199 plots=i_source->second;
00200
00201
00202 for(DetectorPulseList::const_iterator i_pulse=pulseList->begin();
00203 i_pulse!=pulseList->end(); ++i_pulse){
00204
00205
00206 fast_amp=(*i_pulse)->GetAmplitude(TDetectorPulse::kFast);
00207 slow_amp=(*i_pulse)->GetAmplitude(TDetectorPulse::kSlow);
00208 fast_time=(*i_pulse)->GetTime(TDetectorPulse::kFast);
00209 slow_time=(*i_pulse)->GetTime(TDetectorPulse::kSlow);
00210
00211
00212
00213 if(fast_amp==definitions::DefaultValue){
00214 plots.slow_only_amps->Fill(slow_amp);
00215 }else if(slow_amp==definitions::DefaultValue){
00216 plots.fast_only_amps->Fill(fast_amp);
00217 }else {
00218
00219 plots.amplitudes->Fill(slow_amp,fast_amp);
00220 plots.time_diff_fast->Fill(slow_time - fast_time ,fast_amp);
00221 plots.time_diff_slow->Fill(slow_time - fast_time ,slow_amp);
00222 plots.times->Fill(slow_time, fast_time);
00223 plots.scale_factor->Fill(slow_amp/fast_amp);
00224 }
00225
00226 if(!fCutHists) continue;
00227
00228 if(fast_amp> fFastCut && slow_amp!=definitions::DefaultValue){
00229 plots.slow_amps_fast_cut->Fill(slow_amp);
00230 }
00231 if(slow_amp> fSlowCut && fast_amp!=definitions::DefaultValue){
00232 plots.fast_amps_slow_cut->Fill(fast_amp);
00233 }
00234
00235 }
00236 }
00237
00238 return 0;
00239 }
00240
00241 int PlotTDPs::AfterLastEntry(TGlobalData* gData,const TSetupData *setup){
00242
00243 for(PlotsList_t::iterator i_source=fPlotsList.begin();
00244 i_source!=fPlotsList.end(); ++i_source){
00245 Detector_t& plots=i_source->second;
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 TProfile* profile=plots.amplitudes->ProfileX(Form("%s_fit",plots.amplitudes->GetName()));
00256 TFitResultPtr fit=profile->Fit("pol1","QS");
00257 if(((int)fit)!=0){
00258 cout<<"PlotTDPs: Warning: fitting to 2D amplitude plots failed with status: "
00259 << (int)fit <<" for "<<i_source->first<<endl;
00260 } else {
00261 plots.amp_inter=fit->Parameter(0);
00262 plots.amp_grad=fit->Parameter(1);
00263 }
00264 plots.amp_status=fit;
00265 }
00266
00267 if(Debug()){
00268 cout<<"PlotTDPs: Fitted amplitude gradients:"<<endl;
00269 cout<<" intercept\t| gradient\t| source"<<endl;
00270 cout<<" ---------\t| --------\t| ------"<<endl;
00271 for(PlotsList_t::const_iterator i_source=fPlotsList.begin();
00272 i_source!=fPlotsList.end(); ++i_source){
00273 const Detector_t& plots=i_source->second;
00274 if(!plots.amp_status)
00275 cout<< " "<<plots.amp_inter<<"\t| "<< plots.amp_grad<<"\t| "<<i_source->first<<endl;
00276 }
00277 }
00278
00279
00280 return 0;
00281 }
00282
00283 ALCAP_REGISTER_MODULE(PlotTDPs,slow_ch_cut,fast_ch_cut);