Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "CalibMuon/DTCalibration/interface/DTMeanTimerFitter.h"
00011
00012 #include "CalibMuon/DTCalibration/interface/vDriftHistos.h"
00013
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015
00016 #include <iostream>
00017
00018 #include "TFile.h"
00019 #include "TF1.h"
00020
00021 using namespace std;
00022
00023 DTMeanTimerFitter::DTMeanTimerFitter(TFile *file) : hInputFile(file), theVerbosityLevel(0) {
00024
00025 hDebugFile = new TFile("DTMeanTimerFitter.root", "RECREATE");
00026 }
00027
00028 DTMeanTimerFitter::~DTMeanTimerFitter() {
00029 hDebugFile->Close();
00030 }
00031
00032 vector<float> DTMeanTimerFitter::evaluateVDriftAndReso (const TString& N) {
00033
00034
00035 hTMaxCell * histos = new hTMaxCell(N, hInputFile);
00036 vector<float> vDriftAndReso;
00037
00038
00039 if(histos->hTmax123 != 0) {
00040 vector<TH1F*> hTMax;
00041 vector <TH1F*> hT0;
00042 hTMax.push_back(histos->hTmax123);
00043 hTMax.push_back(histos->hTmax124s72);
00044 hTMax.push_back(histos->hTmax124s78);
00045 hTMax.push_back(histos->hTmax134s72);
00046 hTMax.push_back(histos->hTmax134s78);
00047 hTMax.push_back(histos->hTmax234);
00048
00049 hT0.push_back(histos->hTmax_3t0);
00050 hT0.push_back(histos->hTmax_2t0);
00051 hT0.push_back(histos->hTmax_t0);
00052 hT0.push_back(histos->hTmax_0);
00053
00054 vector<Double_t> factor;
00055
00056 factor.push_back(sqrt(2./3.));
00057 factor.push_back(sqrt(2./7.));
00058 factor.push_back(sqrt(8./7.));
00059 factor.push_back(sqrt(2./7.));
00060 factor.push_back(sqrt(8./7.));
00061 factor.push_back(sqrt(2./3.));
00062
00063
00064
00065 vector<Double_t> mean;
00066 vector<Double_t> sigma;
00067 vector<Double_t> count;
00068
00069 for(vector<TH1F*>::const_iterator ith = hTMax.begin();
00070 ith != hTMax.end(); ith++) {
00071 TF1 *funct = fitTMax(*ith);
00072 if(!funct){
00073 edm::LogError("DTMeanTimerFitter") << "Error when fitting TMax..histogram name" << (*ith)->GetName();
00074
00075 vector<float> defvec(6,-1);
00076 return defvec;
00077 }
00078 hDebugFile->cd();
00079 (*ith)->Write();
00080
00081
00082 mean.push_back(funct->GetParameter(1));
00083 sigma.push_back(funct->GetParameter(2));
00084 count.push_back((*ith)->GetEntries());
00085 }
00086
00087 Double_t tMaxMean=0.;
00088 Double_t wTMaxSum=0.;
00089 Double_t sigmaT=0.;
00090 Double_t wSigmaSum = 0.;
00091
00092
00093 for(int i=0; i<=5; i++) {
00094 if(count[i]<200) continue;
00095 tMaxMean += mean[i]*(count[i]/(sigma[i]*sigma[i]));
00096 wTMaxSum += count[i]/(sigma[i]*sigma[i]);
00097 sigmaT += count[i]*factor[i]*sigma[i];
00098 wSigmaSum += count[i];
00099
00100
00101
00102 }
00103 if((!wTMaxSum)||(!wSigmaSum)){
00104 edm::LogError("DTMeanTimerFitter") << "Error zero sum of weights..returning default";
00105 vector<float> defvec(6,-1);
00106 return defvec;
00107 }
00108
00109 tMaxMean /= wTMaxSum;
00110 sigmaT /= wSigmaSum;
00111
00112
00113 Double_t vDrift = 2.1 / tMaxMean;
00114 Double_t reso = vDrift * sigmaT;
00115 vDriftAndReso.push_back(vDrift);
00116 vDriftAndReso.push_back(reso);
00117
00118 edm::LogVerbatim("DTMeanTimerFitter") << " final TMaxMean=" << tMaxMean << " sigma= " << sigmaT
00119 << " v_d and reso: " << vDrift << " " << reso << endl;
00120
00121
00122 map<Double_t,TH1F*> hEntries;
00123 for(vector<TH1F*>::const_iterator ith = hT0.begin();
00124 ith != hT0.end(); ith++) {
00125 hEntries[(*ith)->GetEntries()] = (*ith);
00126 }
00127
00128
00129 int counter = 0;
00130 for(map<Double_t,TH1F*>::reverse_iterator iter = hEntries.rbegin();
00131 iter != hEntries.rend(); iter++) {
00132 counter++;
00133 if (counter==1) hT0.push_back(iter->second);
00134 else if (counter==2) {hT0.push_back(iter->second); break;}
00135 }
00136
00137
00138 vector<Double_t> meanT0;
00139 vector<Double_t> sigmaT0;
00140 vector<Double_t> countT0;
00141
00142 for(vector<TH1F*>::const_iterator ith = hT0.begin();
00143 ith != hT0.end(); ith++) {
00144 try{
00145 (*ith)->Fit("gaus");
00146 } catch(std::exception){
00147 edm::LogError("DTMeanTimerFitter") << "Exception when fitting T0..histogram " << (*ith)->GetName();
00148
00149 vector<float> defvec(6,-1);
00150 return defvec;
00151 }
00152 TF1 *f1 = (*ith)->GetFunction("gaus");
00153
00154 meanT0.push_back(f1->GetParameter(1));
00155 sigmaT0.push_back(f1->GetParameter(2));
00156 countT0.push_back((*ith)->GetEntries());
00157 }
00158
00159 if(hT0.size() != 6) {
00160 edm::LogVerbatim("DTMeanTimerFitter") << "t0 histograms = " << hT0.size();
00161 for(int i=1; i<=4;i++) {
00162 vDriftAndReso.push_back(-1);
00163 }
00164 return vDriftAndReso;
00165 }
00166
00167 for(int it0=0; it0<=2; it0++) {
00168 if((countT0[it0] > 200) && (countT0[it0+1] > 200)) {
00169 Double_t deltaT0 = meanT0[it0] - meanT0[it0+1];
00170 vDriftAndReso.push_back(deltaT0);
00171 }
00172 else
00173 vDriftAndReso.push_back(999.);
00174 }
00175
00176 if((countT0[4] > 200) && (countT0[5] > 200)) {
00177 Double_t t0Diff = histos->GetT0Factor(hT0[4]) - histos->GetT0Factor(hT0[5]);
00178 Double_t deltaT0MaxEntries = (meanT0[4] - meanT0[5])/ t0Diff;
00179 vDriftAndReso.push_back(deltaT0MaxEntries);
00180 }
00181 else
00182 vDriftAndReso.push_back(999.);
00183 }
00184 else {
00185 for(int i=1; i<=6; i++) {
00186
00187
00188 vDriftAndReso.push_back(-1);
00189 }
00190 }
00191 return vDriftAndReso;
00192 }
00193
00194
00195
00196 TF1* DTMeanTimerFitter::fitTMax(TH1F* histo){
00197
00198 Double_t peak = (((((histo->GetXaxis())->GetXmax())-((histo->GetXaxis())->GetXmin()))/histo->GetNbinsX())*
00199 (histo->GetMaximumBin()))+((histo->GetXaxis())->GetXmin());
00200
00201 LogDebug("DTMeanTimerFitter") <<"Peak "<<peak<<" : "<<"xmax "<<((histo->GetXaxis())->GetXmax())
00202 <<" xmin "<<((histo->GetXaxis())->GetXmin())
00203 <<" nbin "<<histo->GetNbinsX()
00204 <<" bin with max "<<(histo->GetMaximumBin());
00205 Double_t range = 2.*histo->GetRMS();
00206
00207
00208 TF1 *rGaus = new TF1("rGaus","gaus",peak-range,peak+range);
00209 rGaus->SetMarkerSize();
00210 try{
00211 histo->Fit("rGaus","R");
00212 } catch(std::exception){
00213 edm::LogError("DTMeanTimerFitter") << "Exception when fitting TMax..histogram " << histo->GetName()
00214 << " setting return function pointer to zero";
00215 return 0;
00216 }
00217 TF1 *f1 = histo->GetFunction("rGaus");
00218 return f1;
00219 }