00001 #include "FastSlowCompare.h"
00002 #include "RegisterModule.inc"
00003 #include "TPulseIsland.h"
00004 #include "TGlobalData.h"
00005 #include "TSetupData.h"
00006 #include "ModulesOptions.h"
00007 #include "definitions.h"
00008
00009 #include "TH1I.h"
00010 #include "TH2I.h"
00011 #include "TDirectory.h"
00012
00013 #include <stdexcept>
00014 #include <iostream>
00015 #include <algorithm>
00016 #include <numeric>
00017 #include <cmath>
00018 #include <climits>
00019 #include <map>
00020 #include <string>
00021
00022 static std::vector<double> TPI2Times(const std::vector<TPulseIsland*>&);
00023 static double GetTime(const TPulseIsland*, const int);
00024 static bool isValid(const TPulseIsland*);
00025
00026 FastSlowCompare::FastSlowCompare(modules::options* opts): BaseModule("FastSlowCompare",opts) {
00027 const static std::string title_time("hTime");
00028 const static std::string title_moretime("hMoreTime");
00029 const static std::string title_meantime("hMeanTime");
00030 const static std::string title_nperwide("hNPerSlowWide");
00031 const static std::string title_npertight("hNPerSlowTight");
00032 fPairs[std::string("SiR2-S")] = std::string("SiR2-F");
00033 fPairs[std::string("SiL2-S")] = std::string("SiL2-F");
00034 fPairs[std::string("SiR1-1-S")] = std::string("SiR1-1-F");
00035 fPairs[std::string("SiR1-2-S")] = std::string("SiR1-2-F");
00036 fPairs[std::string("SiR1-3-S")] = std::string("SiR1-3-F");
00037 fPairs[std::string("SiR1-4-S")] = std::string("SiR1-4-F");
00038 fPairs[std::string("SiL1-1-S")] = std::string("SiL1-1-F");
00039 fPairs[std::string("SiL1-2-S")] = std::string("SiL1-2-F");
00040 fPairs[std::string("SiL1-3-S")] = std::string("SiL1-3-F");
00041 fPairs[std::string("SiL1-4-S")] = std::string("SiL1-4-F");
00042 fPairs[std::string("Ge-S")] = std::string("Ge-F");
00043
00044 TDirectory* par_dir = TDirectory::CurrentDirectory();
00045 dir->cd();
00046 std::map<std::string, std::string>::iterator iPair;
00047 for (iPair = fPairs.begin(); iPair != fPairs.end(); ++iPair) {
00048 fHist_Time[iPair->first] = new TH1I((title_time + iPair->first).c_str(), "Relative Time of Fast", 400, -200., 200.);
00049 fHist_MoreTime[iPair->first] = new TH1I((title_moretime + iPair->first).c_str(), "Relative Time of Fast", 1000, -100000., 100000.);
00050 fHist_NPerSlowWide[iPair->first] = new TH1I((title_nperwide + iPair->first).c_str(), "Number of Fast Pulses within 100us", 10, -0.5, 9.5);
00051 fHist_NPerSlowTight[iPair->first] = new TH1I((title_npertight + iPair->first).c_str(), "Number of Fast Pulses within 400ns", 10, -0.5, 9.5);
00052 }
00053 par_dir->cd();
00054 }
00055
00056 FastSlowCompare::~FastSlowCompare(){
00057 }
00058
00059
00060
00061
00062 int FastSlowCompare::BeforeFirstEntry(TGlobalData* gData, const TSetupData *setup){
00063 return 0;
00064 }
00065
00066
00067
00068 int FastSlowCompare::ProcessEntry(TGlobalData* gData, const TSetupData *setup){
00069 const std::map< std::string, std::vector<TPulseIsland*> >& TPIMap = gData->fPulseIslandToChannelMap;
00070
00071 std::map<std::string, std::string>::iterator iPair;
00072 for (iPair = fPairs.begin(); iPair != fPairs.end(); ++iPair) {
00073 const std::vector<double> slow_times = TPI2Times(TPIMap.at(setup->GetBankName(iPair->first)));
00074 const std::vector<double> fast_times = TPI2Times(TPIMap.at(setup->GetBankName(iPair->second)));
00075 const std::vector<double>::const_iterator beg_slow = slow_times.begin();
00076 const std::vector<double>::const_iterator beg_fast = fast_times.begin();
00077 const std::vector<double>::const_iterator end_slow = slow_times.end();
00078 const std::vector<double>::const_iterator end_fast = fast_times.end();
00079 std::vector<double>::const_iterator slow;
00080 std::vector<double>::const_iterator fast[2];
00081
00082 for (slow = beg_slow; slow < end_slow; ++slow) {
00083 static std::vector<double>::const_iterator iTime;
00084 fast[0] = std::upper_bound(beg_fast, end_fast, *slow + 100000.);
00085 fast[1] = std::upper_bound(beg_fast, end_fast, *slow - 100000.);
00086 fHist_NPerSlowWide[iPair->first]->Fill(fast[0] - fast[1]);
00087 for(iTime = fast[1]; iTime < fast[0]; ++iTime) {
00088 fHist_Time.at(iPair->first)->Fill(*iTime - *slow);
00089 fHist_MoreTime.at(iPair->first)->Fill(*iTime - *slow);
00090 }
00091 fast[0] = std::lower_bound(beg_fast, end_fast, *slow + 400.);
00092 fast[1] = std::upper_bound(beg_fast, end_fast, *slow - 400.);
00093 fHist_NPerSlowTight[iPair->first]->Fill(fast[0] - fast[1]);
00094 }
00095 }
00096
00097
00098 return 0;
00099 }
00100
00101
00102
00103
00104
00105 int FastSlowCompare::AfterLastEntry(TGlobalData* gData, const TSetupData *setup) {
00106 return 0;
00107 }
00108
00109
00110
00111 double GetTime(const TPulseIsland* tpi, const int pol) {
00112 const static double cf_frac = 0.2;
00113 const std::vector<int>& samps = tpi->GetSamples();
00114 const std::vector<int>::const_iterator b = samps.begin(), e = samps.end();
00115 const unsigned int ped = std::accumulate(b, b + 5, 0)/5.;
00116 std::vector<int>::const_iterator m = pol > 0 ? std::max_element(b, e) : std::min_element(b, e);
00117 const unsigned int amp = *m;
00118 const unsigned int cf = pol > 0 ? (unsigned int)(cf_frac*(double)(amp-ped)) + ped : (unsigned int)((double)(ped-amp)*(1.-cf_frac) + amp);
00119 while (m != b && (pol > 0 ? *--m > (int)cf : *--m < (int)cf));
00120
00121 double dx = (double)(m-b);
00122 if (*(m+1) != *m)
00123 dx += (double)((int)cf - *m)/(double)(*(m+1) - *m);
00124 return (dx + (double)tpi->GetTimeStamp()) * tpi->GetClockTickInNs() - TSetupData::Instance()->GetTimeShift(tpi->GetBankName());
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 std::vector<double> TPI2Times(const std::vector<TPulseIsland*>& vec) {
00146 static std::vector<TPulseIsland*>::const_iterator tpi;
00147 std::vector<double> times;
00148 for (tpi = vec.begin(); tpi < vec.end(); ++tpi)
00149
00150 times.push_back(GetTime(*tpi, (*tpi)->GetTriggerPolarity()));
00151 return times;
00152 }
00153
00154 bool isValid (const TPulseIsland* tpi) {
00155 const std::vector<int>& samps = tpi->GetSamples();
00156 const int min = 0;
00157 const int max = (int)std::pow(2., (double)TSetupData::Instance()->GetNBits(tpi->GetBankName())) - 1;
00158 if (*std::min_element(samps.begin(), samps.end()) <= min ||
00159 *std::max_element(samps.begin(), samps.end()) >= max)
00160 return false;
00161 return true;
00162 }
00163
00164
00165
00166
00167
00168 ALCAP_REGISTER_MODULE(FastSlowCompare);