CMS 3D CMS Logo

DTTTrigResidualCorrection.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author A. Vilela Pereira
5  */
6 
12 
18 
20 
21 #include "TFile.h"
22 #include "TH1F.h"
23 #include "TF1.h"
24 #include "TCanvas.h"
25 
26 #include "RooPlot.h"
27 #include "RooRealVar.h"
28 #include "RooDataHist.h"
29 #include "RooGaussian.h"
30 #include "RooAddPdf.h"
31 #include "RooFitResult.h"
32 #include "RooGlobalFunc.h"
33 
34 #include <string>
35 #include <sstream>
36 #include <fstream>
37 
38 using namespace std;
39 using namespace edm;
40 
41 namespace dtCalibration {
42 
43 DTTTrigResidualCorrection::DTTTrigResidualCorrection(const ParameterSet& pset) {
44  string residualsRootFile = pset.getParameter<string>("residualsRootFile");
45  rootFile_ = new TFile(residualsRootFile.c_str(),"READ");
46  rootBaseDir_ = pset.getUntrackedParameter<string>("rootBaseDir","/DQMData/DT/DTCalibValidation");
47  useFit_ = pset.getParameter<bool>("useFitToResiduals");
48  //useConstantvDrift_ = pset.getParameter<bool>("useConstantDriftVelocity");
49  dbLabel_ = pset.getUntrackedParameter<string>("dbLabel", "");
50  useSlopesCalib_ = pset.getUntrackedParameter<bool>("useSlopesCalib",false);
51 
52  // Load external slopes
53  if(useSlopesCalib_){
54  ifstream fileSlopes;
55  fileSlopes.open( pset.getParameter<FileInPath>("slopesFileName").fullPath().c_str() );
56 
57  int tmp_wheel = 0, tmp_sector = 0, tmp_station = 0, tmp_SL = 0;
58  double tmp_ttrig = 0., tmp_t0 = 0., tmp_kfact = 0.;
59  int tmp_a = 0, tmp_b = 0, tmp_c = 0, tmp_d = 0;
60  double tmp_v_eff = 0.;
61  while(!fileSlopes.eof()){
62  fileSlopes >> tmp_wheel >> tmp_sector >> tmp_station >> tmp_SL >> tmp_a >> tmp_b >>
63  tmp_ttrig >> tmp_t0 >> tmp_kfact >> tmp_c >> tmp_d >> tmp_v_eff;
64  vDriftEff_[tmp_wheel+2][tmp_sector-1][tmp_station-1][tmp_SL-1] = -tmp_v_eff;
65  }
66  fileSlopes.close();
67  }
68 
69  bool debug = pset.getUntrackedParameter<bool>("debug",false);
70  fitter_ = new DTResidualFitter(debug);
71 }
72 
73 DTTTrigResidualCorrection::~DTTTrigResidualCorrection() {
74  delete rootFile_;
75  delete fitter_;
76 }
77 
78 void DTTTrigResidualCorrection::setES(const EventSetup& setup) {
79  // Get tTrig record from DB
80  ESHandle<DTTtrig> tTrig;
81  //setup.get<DTTtrigRcd>().get(tTrig);
82  setup.get<DTTtrigRcd>().get(dbLabel_,tTrig);
83  tTrigMap_ = &*tTrig;
84 
85  // Get vDrift record
86  ESHandle<DTMtime> mTimeHandle;
87  setup.get<DTMtimeRcd>().get(mTimeHandle);
88  mTimeMap_ = &*mTimeHandle;
89 }
90 
91 DTTTrigData DTTTrigResidualCorrection::correction(const DTSuperLayerId& slId) {
92 
93  float tTrigMean,tTrigSigma,kFactor;
94  int status = tTrigMap_->get(slId,tTrigMean,tTrigSigma,kFactor,DTTimeUnits::ns);
95  if(status != 0) throw cms::Exception("[DTTTrigResidualCorrection]") << "Could not find tTrig entry in DB for"
96  << slId << endl;
97 
98  float vDrift,hitResolution;
99  status = mTimeMap_->get(slId,vDrift,hitResolution,DTVelocityUnits::cm_per_ns);
100  if(status != 0) throw cms::Exception("[DTTTrigResidualCorrection]") << "Could not find vDrift entry in DB for"
101  << slId << endl;
102  TH1F residualHisto = *(getHisto(slId));
103  LogTrace("Calibration") << "[DTTTrigResidualCorrection]: \n"
104  << " Mean, RMS = " << residualHisto.GetMean() << ", " << residualHisto.GetRMS();
105 
106  double fitMean = -1.;
107  double fitSigma = -1.;
108  if(useFit_){
109  LogTrace("Calibration") << "[DTTTrigResidualCorrection]: Fitting histogram " << residualHisto.GetName();
110  int nSigmas = 2;
111 
112  DTResidualFitResult fitResult = fitter_->fitResiduals(residualHisto,nSigmas);
113  fitMean = fitResult.fitMean;
114  fitSigma = fitResult.fitSigma;
115 
116  LogTrace("Calibration") << "[DTTTrigResidualCorrection]: \n"
117  << " Fit Mean = " << fitMean << "\n"
118  << " Fit Sigma = " << fitSigma;
119  }
120  double resMean = (useFit_) ? fitMean : residualHisto.GetMean();
121 
122  int wheel = slId.wheel();
123  int sector = slId.sector();
124  int station = slId.station();
125  int superLayer = slId.superLayer();
126  double resTime = 0.;
127  if(useSlopesCalib_){
128  double vdrift_eff = vDriftEff_[wheel+2][sector-1][station-1][superLayer-1];
129  if(vdrift_eff == 0) vdrift_eff = vDrift;
130 
131  if(vdrift_eff) resTime = resMean/vdrift_eff;
132 
133  LogTrace("Calibration") << "[DTTTrigResidualCorrection]: Effective vDrift, correction to tTrig = "
134  << vdrift_eff << ", " << resTime;
135  } else{
136  if(vDrift) resTime = resMean/vDrift;
137 
138  LogTrace("Calibration") << "[DTTTrigResidualCorrection]: vDrift from DB, correction to tTrig = "
139  << vDrift << ", " << resTime;
140  }
141 
142  double corrMean = tTrigMean;
143  double corrSigma = (tTrigSigma != 0.) ? tTrigSigma : 1.;
144  double corrKFact = (tTrigSigma != 0.) ? (kFactor + resTime/tTrigSigma) : resTime;
145 
146  return DTTTrigData(corrMean,corrSigma,corrKFact);
147 }
148 
149 const TH1F* DTTTrigResidualCorrection::getHisto(const DTSuperLayerId& slId) {
150  string histoName = getHistoName(slId);
151  LogTrace("Calibration") << "[DTTTrigResidualCorrection]: Accessing histogram " << histoName.c_str();
152  TH1F* histo = static_cast<TH1F*>(rootFile_->Get(histoName.c_str()));
153  if(!histo) throw cms::Exception("[DTTTrigResidualCorrection]") << "residual histogram not found:"
154  << histoName << endl;
155  return histo;
156 }
157 
159 
160  int step = 3;
161 
162  std::string wheel = std::to_string(slId.wheel());
163  std::string station = std::to_string(slId.station());
164  std::string sector = std::to_string(slId.sector());
165  std::string superLayer = std::to_string(slId.superlayer());
166  std::string Step = std::to_string(step);
167 
168  string histoName =
169  rootBaseDir_ + "/Wheel" + wheel + "/Station" + station + "/Sector" + sector +
170  "/hResDist_STEP" + Step + "_W" + wheel + "_St" + station + "_Sec" + sector + "_SL" + superLayer;
171 
172  return histoName;
173 }
174 
175 } // namespace
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
def getHistoName(wheel, station, sector)
int superLayer() const
Return the superlayer number.
#define LogTrace(id)
int superlayer() const
Return the superlayer number (deprecated method name)
#define debug
Definition: HDRShower.cc:19
HLT enums.
int sector() const
Definition: DTChamberId.h:61
T get() const
Definition: EventSetup.h:71
std::string fullPath() const
Definition: FileInPath.cc:163
step
Definition: StallMonitor.cc:94
int station() const
Return the station number.
Definition: DTChamberId.h:51
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45