00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string>
00015 #include <map>
00016 #include <utility>
00017 #include <sstream>
00018 #include <iostream>
00019
00020
00021 #include "midas.h"
00022
00023
00024 #include <TH2.h>
00025 #include <TDirectory.h>
00026
00027
00028 #include "TGlobalData.h"
00029 #include "TSetupData.h"
00030 #include "TPulseIsland.h"
00031
00032 using std::string;
00033 using std::map;
00034 using std::vector;
00035 using std::pair;
00036
00037
00038 INT MMuSCTimeDifferences_init(void);
00039 INT MMuSCTimeDifferences(EVENT_HEADER*, void*);
00040
00041 extern HNDLE hDB;
00042 extern TGlobalData* gData;
00043 extern TSetupData* gSetup;
00044
00045 ANA_MODULE MMuSCTimeDifferences_module =
00046 {
00047 "MMuSCTimeDifferences",
00048 "Peter Winter",
00049 MMuSCTimeDifferences,
00050 NULL,
00051 NULL,
00052 MMuSCTimeDifferences_init,
00053 NULL,
00054 NULL,
00055 0,
00056 NULL,
00057 };
00058
00059 static TH1 **hTime;
00060 static TH2 **hAmplitudeVsTime;
00061 static TH1 **hTime_PP;
00062 static TH2 **hAmplitudeVsTime_PP;
00063 static int *last;
00064 static std::vector<std::string> detectors;
00065 static int nr_detectors;
00066
00067 int FillCoincidenceTimeHistogram(int tMusc,const std::vector<TPulseIsland*>& pulses,
00068 int start, TH1 *h, TH2 *hAmp, double t_low, double t_high,
00069 bool PP, TH1 *h_PP, TH2 *hAmp_PP, const std::string& bank_name);
00070
00071 bool IsPileupProtected(double t,const std::vector<TPulseIsland*>& p, double window, int *lPP, const std::string& bank_name);
00072 double GetTime(const TPulseIsland* p, const TSetupData* setup, const std::string& bank_name);
00073
00076 INT MMuSCTimeDifferences_init()
00077 {
00078 gDirectory->mkdir("MuSC_TimingCorrelations")->cd();
00079
00080 for(std::map<std::string, std::string>::iterator it = gSetup->fBankToDetectorMap.begin();
00081 it != gSetup->fBankToDetectorMap.end(); ++it){
00082 if(it->first != std::string("") && it->first != std::string("blank")){
00083 if(it->second != std::string("muSc")) detectors.push_back(it->second);
00084 }
00085 }
00086 std::cout << "Number of detectors fo Time Correlations: " << detectors.size() << std::endl;
00087 std::cout << "Size of Bank to Detector map: " << gSetup->fBankToDetectorMap.size() << std::endl;
00088 std::cout << "Size of TPI map: " << gData->fPulseIslandToChannelMap.size() << std::endl;
00089
00090 nr_detectors = detectors.size();
00091 hTime = new TH1*[nr_detectors];
00092 hTime_PP = new TH1*[nr_detectors];
00093 hAmplitudeVsTime = new TH2*[nr_detectors];
00094 hAmplitudeVsTime_PP = new TH2*[nr_detectors];
00095 last = new int[nr_detectors];
00096
00097 for(int i = 0; i<nr_detectors; ++i){
00098 last[i] = 0;
00099
00100 std::string hName("hMuSC_");
00101 hName += detectors[i];
00102 hName += "_Timediff";
00103 std::string hTitle("Tdiff for ");
00104 hTitle += detectors[i];
00105 hTitle += " - muSC";
00106 hTime[i] = new TH1F(hName.c_str(), hTitle.c_str(), 500, -13000, 13000);
00107 hTime[i]->GetXaxis()->SetTitle("Time difference [ns]");
00108 hName += "_PP";
00109 hTitle += " with PP";
00110 hTime_PP[i] = new TH1F(hName.c_str(), hTitle.c_str(), 500, -13000, 13000);
00111 hTime_PP[i]->GetXaxis()->SetTitle("Time difference [ns]");
00112
00113 hName = "hMuSC_";
00114 hName += detectors[i];
00115 hName += "_AmplitudeVsTdiff";
00116 hTitle = "Amplitude versus tdiff for ";
00117 hTitle += detectors[i];
00118 hTitle += " - muSC";
00119 hAmplitudeVsTime[i] = new TH2F(hName.c_str(), hTitle.c_str(), 250, -5000, 5000,
00120 250, 0, 3000);
00121 hAmplitudeVsTime[i]->GetXaxis()->SetTitle("Time difference [ns]");
00122 hAmplitudeVsTime[i]->GetYaxis()->SetTitle("Amplitude");
00123 hName += "_PP";
00124 hTitle += " with PP";
00125 hAmplitudeVsTime_PP[i] = new TH2F(hName.c_str(), hTitle.c_str(), 250, -5000, 5000,
00126 250, 0, 3000);
00127 hAmplitudeVsTime_PP[i]->GetXaxis()->SetTitle("Time difference [ns]");
00128 hAmplitudeVsTime_PP[i]->GetYaxis()->SetTitle("Amplitude");
00129 }
00130 gDirectory->cd("/");
00131
00132 return SUCCESS;
00133 }
00134
00138 INT MMuSCTimeDifferences(EVENT_HEADER *pheader, void *pevent){
00139 double t_low = -10000;
00140 double t_high = 10000;
00141
00142
00143 typedef map<string, vector<TPulseIsland*> > TStringPulseIslandMap;
00144
00145
00146
00147 TStringPulseIslandMap& pulse_islands_map =
00148 gData->fPulseIslandToChannelMap;
00149
00150
00151 std::string str_MuSC = gSetup->GetBankName("muSc");
00152 int lPP = 0;
00153 for(int i=0; i<nr_detectors; ++i) last[i] = 0;
00154
00155 for(std::vector<TPulseIsland*>::iterator it_Musc = pulse_islands_map[str_MuSC].begin();
00156 it_Musc != pulse_islands_map[str_MuSC].end(); ++it_Musc){
00157
00158 double tMusc = GetTime(*it_Musc,gSetup,str_MuSC);
00159
00160 bool PP = IsPileupProtected(tMusc, pulse_islands_map[str_MuSC], 10000, &lPP,str_MuSC);
00161
00162 std::string bank_name;
00163 for(int i=0; i<nr_detectors; ++i){
00164 bank_name=gSetup->GetBankName(detectors[i]);
00165 last[i] = FillCoincidenceTimeHistogram(tMusc, pulse_islands_map[bank_name],
00166 last[i], hTime[i], hAmplitudeVsTime[i], t_low, t_high,
00167 PP, hTime_PP[i], hAmplitudeVsTime_PP[i], bank_name);
00168 }
00169 }
00170
00171 return SUCCESS;
00172 }
00173
00174 int FillCoincidenceTimeHistogram(int tMusc,const std::vector<TPulseIsland*>& pulses,
00175 int start, TH1 *h, TH2 *hAmp, double t_low, double t_high,
00176 bool PP, TH1 *h_PP, TH2 *hAmp_PP, const std::string& bank_name)
00177 {
00178 for(int p = start; p < (int)pulses.size(); ++p){
00179
00180 double tDiff = GetTime(pulses[p],gSetup, bank_name) - tMusc;
00181 if(tDiff < t_low){
00182 start = p;
00183 continue;
00184 }
00185 if(tDiff > t_high) break;
00186 h->Fill(tDiff);
00187 hAmp->Fill(tDiff, pulses[p]->GetPulseHeight());
00188 if(PP){
00189 h_PP->Fill(tDiff);
00190 hAmp_PP->Fill(tDiff, pulses[p]->GetPulseHeight());
00191 }
00192 }
00193
00194 return start;
00195 }
00196
00197 bool IsPileupProtected(double t,const std::vector<TPulseIsland*>& p, double window, int *lPP, const std::string& bank_name){
00198 if(t < window || t > 1.12E8-window) return false;
00199 for(unsigned i = *lPP; i < p.size(); ++i){
00200 double t2 = GetTime(p[i],gSetup,bank_name);
00201 if(t2 < t-window){
00202 *lPP = i;
00203 }
00204 if(t2 > t+window) break;
00205 if(t != t2 && t2 > t-window && t2 < t+window) return false;
00206 }
00207 return true;
00208 }
00209
00210 double GetTime(const TPulseIsland* p, const TSetupData* setup, const std::string& bank_name){
00211 return p->GetPulseTime() - setup->GetTimeShift(bank_name);
00212 }