CMS 3D CMS Logo

DTMeanTimerFitter.cc
Go to the documentation of this file.
1 
2 /*
3  * See header file for a description of this class.
4  *
5  * \author S. Bolognesi - INFN Torino
6  */
7 
9 //#include "CalibMuon/DTCalibration/plugins/vDriftHistos.h"
11 
13 
14 #include <iostream>
15 
16 #include "TFile.h"
17 #include "TF1.h"
18 
19 using namespace std;
20 
21 DTMeanTimerFitter::DTMeanTimerFitter(TFile *file) : hInputFile(file), theVerbosityLevel(0) {
22  //hInputFile = new TFile("DTTMaxHistos.root", "READ");
23  hDebugFile = new TFile("DTMeanTimerFitter.root", "RECREATE");
24 }
25 
27  hDebugFile->Close();
28 }
29 
30 vector<float> DTMeanTimerFitter::evaluateVDriftAndReso (const TString& N) {
31 
32  // Retrieve histogram sets
34  vector<float> vDriftAndReso;
35 
36  // Check that the histo for this cell exists
37  if(histos->hTmax123 != 0) {
38  vector<TH1F*> hTMax; // histograms for <T_max> calculation
39  vector <TH1F*> hT0; // histograms for T0 evaluation
40  hTMax.push_back(histos->hTmax123);
41  hTMax.push_back(histos->hTmax124s72);
42  hTMax.push_back(histos->hTmax124s78);
43  hTMax.push_back(histos->hTmax134s72);
44  hTMax.push_back(histos->hTmax134s78);
45  hTMax.push_back(histos->hTmax234);
46 
47  hT0.push_back(histos->hTmax_3t0);
48  hT0.push_back(histos->hTmax_2t0);
49  hT0.push_back(histos->hTmax_t0);
50  hT0.push_back(histos->hTmax_0);
51 
52  vector<Double_t> factor; // factor relating the width of the Tmax distribution
53  // and the cell resolution
54  factor.push_back(sqrt(2./3.)); // hTmax123
55  factor.push_back(sqrt(2./7.)); // hTmax124s72
56  factor.push_back(sqrt(8./7.)); // hTmax124s78
57  factor.push_back(sqrt(2./7.)); // hTmax134s72
58  factor.push_back(sqrt(8./7.)); // hTmax134s78
59  factor.push_back(sqrt(2./3.)); // hTmax234
60 
61 
62  // Retrieve the gaussian mean and sigma for each TMax histogram
63  vector<Double_t> mean;
64  vector<Double_t> sigma;
65  vector<Double_t> count; //number of entries
66 
67  for(vector<TH1F*>::const_iterator ith = hTMax.begin();
68  ith != hTMax.end(); ++ith) {
69  TF1 *funct = fitTMax(*ith);
70  if(!funct){
71  edm::LogError("DTMeanTimerFitter") << "Error when fitting TMax..histogram name" << (*ith)->GetName();
72  // return empty or -1 filled vector?
73  vector<float> defvec(6,-1);
74  return defvec;
75  }
76  hDebugFile->cd();
77  (*ith)->Write();
78 
79  // Get mean, sigma and number of entries of each histogram
80  mean.push_back(funct->GetParameter(1));
81  sigma.push_back(funct->GetParameter(2));
82  count.push_back((*ith)->GetEntries());
83  }
84 
85  Double_t tMaxMean=0.;
86  Double_t wTMaxSum=0.;
87  Double_t sigmaT=0.;
88  Double_t wSigmaSum = 0.;
89 
90  //calculate total mean and sigma
91  for(int i=0; i<=5; i++) {
92  if(count[i]<200) continue;
93  tMaxMean += mean[i]*(count[i]/(sigma[i]*sigma[i]));
94  wTMaxSum += count[i]/(sigma[i]*sigma[i]);
95  sigmaT += count[i]*factor[i]*sigma[i];
96  wSigmaSum += count[i];
97  // cout << "TMaxMean "<<i<<": "<< mean[i] << " entries: " << count[i]
98  // << " sigma: " << sigma[i]
99  // << " weight: " << (count[i]/(sigma[i]*sigma[i])) << endl;
100  }
101  if((!wTMaxSum)||(!wSigmaSum)){
102  edm::LogError("DTMeanTimerFitter") << "Error zero sum of weights..returning default";
103  vector<float> defvec(6,-1);
104  return defvec;
105  }
106 
107  tMaxMean /= wTMaxSum;
108  sigmaT /= wSigmaSum;
109 
110  //calculate v_drift and resolution
111  Double_t vDrift = 2.1 / tMaxMean; //2.1 is the half cell length in cm
112  Double_t reso = vDrift * sigmaT;
113  vDriftAndReso.push_back(vDrift);
114  vDriftAndReso.push_back(reso);
115  //if(theVerbosityLevel >= 1)
116  edm::LogVerbatim("DTMeanTimerFitter") << " final TMaxMean=" << tMaxMean << " sigma= " << sigmaT
117  << " v_d and reso: " << vDrift << " " << reso << endl;
118 
119  // Order t0 histogram by number of entries (choose histograms with higher nr. of entries)
120  map<Double_t,TH1F*> hEntries;
121  for(vector<TH1F*>::const_iterator ith = hT0.begin();
122  ith != hT0.end(); ++ith) {
123  hEntries[(*ith)->GetEntries()] = (*ith);
124  }
125 
126  // add at the end of hT0 the two hists with the higher number of entries
127  int counter = 0;
128  for(map<Double_t,TH1F*>::reverse_iterator iter = hEntries.rbegin();
129  iter != hEntries.rend(); ++iter) {
130  counter++;
131  if (counter==1) hT0.push_back(iter->second);
132  else if (counter==2) {hT0.push_back(iter->second); break;}
133  }
134 
135  // Retrieve the gaussian mean and sigma of histograms for Delta(t0) evaluation
136  vector<Double_t> meanT0;
137  vector<Double_t> sigmaT0;
138  vector<Double_t> countT0;
139 
140  for(vector<TH1F*>::const_iterator ith = hT0.begin();
141  ith != hT0.end(); ++ith) {
142  try{
143  (*ith)->Fit("gaus");
144  } catch(std::exception){
145  edm::LogError("DTMeanTimerFitter") << "Exception when fitting T0..histogram " << (*ith)->GetName();
146  // return empty or -1 filled vector?
147  vector<float> defvec(6,-1);
148  return defvec;
149  }
150  TF1 *f1 = (*ith)->GetFunction("gaus");
151  // Get mean, sigma and number of entries of the histograms
152  meanT0.push_back(f1->GetParameter(1));
153  sigmaT0.push_back(f1->GetParameter(2));
154  countT0.push_back((*ith)->GetEntries());
155  }
156  //calculate Delta(t0)
157  if(hT0.size() != 6) { // check if you have all the t0 hists
158  edm::LogVerbatim("DTMeanTimerFitter") << "t0 histograms = " << hT0.size();
159  for(int i=1; i<=4;i++) {
160  vDriftAndReso.push_back(-1);
161  }
162  return vDriftAndReso;
163  }
164 
165  for(int it0=0; it0<=2; it0++) {
166  if((countT0[it0] > 200) && (countT0[it0+1] > 200)) {
167  Double_t deltaT0 = meanT0[it0] - meanT0[it0+1];
168  vDriftAndReso.push_back(deltaT0);
169  }
170  else
171  vDriftAndReso.push_back(999.);
172  }
173  //deltat0 using hists with max nr. of entries
174  if((countT0[4] > 200) && (countT0[5] > 200)) {
175  Double_t t0Diff = histos->GetT0Factor(hT0[4]) - histos->GetT0Factor(hT0[5]);
176  Double_t deltaT0MaxEntries = (meanT0[4] - meanT0[5])/ t0Diff;
177  vDriftAndReso.push_back(deltaT0MaxEntries);
178  }
179  else
180  vDriftAndReso.push_back(999.);
181  }
182  else {
183  for(int i=1; i<=6; i++) {
184  // 0=vdrift, 1=reso, 2=(3deltat0-2deltat0), 3=(2deltat0-1deltat0),
185  // 4=(1deltat0-0deltat0), 5=deltat0 from hists with max entries,
186  vDriftAndReso.push_back(-1);
187  }
188  }
189  return vDriftAndReso;
190 }
191 
192 
193 
195  // Find distribution peak and fit range
196  Double_t peak = (((((histo->GetXaxis())->GetXmax())-((histo->GetXaxis())->GetXmin()))/histo->GetNbinsX())*
197  (histo->GetMaximumBin()))+((histo->GetXaxis())->GetXmin());
198  //if(theVerbosityLevel >= 1)
199  LogDebug("DTMeanTimerFitter") <<"Peak "<<peak<<" : "<<"xmax "<<((histo->GetXaxis())->GetXmax())
200  <<" xmin "<<((histo->GetXaxis())->GetXmin())
201  <<" nbin "<<histo->GetNbinsX()
202  <<" bin with max "<<(histo->GetMaximumBin());
203  Double_t range = 2.*histo->GetRMS();
204 
205  // Fit each Tmax histogram with a Gaussian in a restricted interval
206  TF1 *rGaus = new TF1("rGaus","gaus",peak-range,peak+range);
207  rGaus->SetMarkerSize();// just silence gcc complainining about unused var
208  try{
209  histo->Fit("rGaus","R");
210  } catch(std::exception){
211  edm::LogError("DTMeanTimerFitter") << "Exception when fitting TMax..histogram " << histo->GetName()
212  << " setting return function pointer to zero";
213  return 0;
214  }
215  TF1 *f1 = histo->GetFunction("rGaus");
216  return f1;
217  }
#define LogDebug(id)
TH1F * hTmax134s78
Definition: vDriftHistos.h:410
TH1F * hTmax_2t0
Definition: vDriftHistos.h:419
TH1F * hTmax_3t0
Definition: vDriftHistos.h:412
Definition: Abs.h:5
virtual ~DTMeanTimerFitter()
Destructor.
DTMeanTimerFitter(TFile *file)
Constructor.
int GetT0Factor(TH1F *hist)
Definition: vDriftHistos.h:391
TH1F * hTmax234
Definition: vDriftHistos.h:411
TH1F * hTmax_t0
Definition: vDriftHistos.h:426
TH1F * hTmax134s72
Definition: vDriftHistos.h:409
TH1F * hTmax124s72
Definition: vDriftHistos.h:407
TF1 * fitTMax(TH1F *histo)
Really do the fit.
TH1F * hTmax_0
Definition: vDriftHistos.h:433
T sqrt(T t)
Definition: SSEVec.h:18
TH1F * hTmax124s78
Definition: vDriftHistos.h:408
#define N
Definition: blowfish.cc:9
TH1F * hTmax123
Definition: vDriftHistos.h:406
std::vector< float > evaluateVDriftAndReso(const TString &N)
Fit the TMax histos and evaluate VDrift and resolution.