CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuScleFitBase.cc
Go to the documentation of this file.
1 #ifndef MUSCLEFITBASE_C
2 #define MUSCLEFITBASE_C
3 
4 #include "MuScleFitBase.h"
6 
7 void MuScleFitBase::fillHistoMap(TFile* outputFile, unsigned int iLoop) {
8  //Reconstructed muon kinematics
9  //-----------------------------
10  outputFile->cd();
11  // If no Z is required, use a smaller mass range.
12  double minMass = 0.;
13  double maxMass = 200.;
14  double maxPt = 100.;
15  double yMaxEta = 4.;
16  double yMaxPt = 2.;
17  if( MuScleFitUtils::resfind[0] != 1 ) {
18  maxMass = 20.;
19  maxPt = 20.;
20  yMaxEta = 0.2;
21  yMaxPt = 0.2;
22  // If running on standalone muons we need to expand the window range
23  if( theMuonType_ == 2 ) {
24  yMaxEta = 20.;
25  }
26  }
27 
28  LogDebug("MuScleFitBase") << "Creating new histograms" << std::endl;
29 
30  mapHisto_["hRecBestMu"] = new HParticle ("hRecBestMu", minMass, maxMass, maxPt);
31  mapHisto_["hRecBestMuVSEta"] = new HPartVSEta ("hRecBestMuVSEta", minMass, maxMass, maxPt);
32  //mapHisto_["hRecBestMuVSPhi"] = new HPartVSPhi ("hRecBestMuVSPhi");
33  //mapHisto_["hRecBestMu_Acc"] = new HParticle ("hRecBestMu_Acc", minMass, maxMass);
34  mapHisto_["hDeltaRecBestMu"] = new HDelta ("hDeltaRecBestMu");
35 
36  mapHisto_["hRecBestRes"] = new HParticle ("hRecBestRes", minMass, maxMass, maxPt);
37  mapHisto_["hRecBestResAllEvents"] = new HParticle ("hRecBestResAllEvents", minMass, maxMass, maxPt);
38  //mapHisto_["hRecBestRes_Acc"] = new HParticle ("hRecBestRes_Acc", minMass, maxMass);
39  // If not finding Z, use a smaller mass window
40  mapHisto_["hRecBestResVSMu"] = new HMassVSPart ("hRecBestResVSMu", minMass, maxMass, maxPt);
41  mapHisto_["hRecBestResVSRes"] = new HMassVSPart ("hRecBestResVSRes", minMass, maxMass, maxPt);
42  //Generated Mass versus pt
43  mapHisto_["hGenResVSMu"] = new HMassVSPart ("hGenResVSMu", minMass, maxMass, maxPt);
44  // Likelihood values VS muon variables
45  // -------------------------------------
46  mapHisto_["hLikeVSMu"] = new HLikelihoodVSPart ("hLikeVSMu");
47  mapHisto_["hLikeVSMuMinus"] = new HLikelihoodVSPart ("hLikeVSMuMinus");
48  mapHisto_["hLikeVSMuPlus"] = new HLikelihoodVSPart ("hLikeVSMuPlus");
49 
50  //Resolution VS muon kinematic
51  //----------------------------
52  mapHisto_["hResolMassVSMu"] = new HResolutionVSPart( outputFile, "hResolMassVSMu", maxPt, 0., yMaxEta, 0., yMaxPt, true );
53  mapHisto_["hFunctionResolMassVSMu"] = new HResolutionVSPart( outputFile, "hFunctionResolMassVSMu", maxPt, 0, 0.1, 0, 0.1, true );
54  mapHisto_["hResolPtGenVSMu"] = new HResolutionVSPart( outputFile, "hResolPtGenVSMu", maxPt, -0.1, 0.1, -0.1, 0.1 );
55  mapHisto_["hResolPtSimVSMu"] = new HResolutionVSPart( outputFile, "hResolPtSimVSMu", maxPt, -0.1, 0.1, -0.1, 0.1 );
56  mapHisto_["hResolEtaGenVSMu"] = new HResolutionVSPart( outputFile, "hResolEtaGenVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
57  mapHisto_["hResolEtaSimVSMu"] = new HResolutionVSPart( outputFile, "hResolEtaSimVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
58  mapHisto_["hResolThetaGenVSMu"] = new HResolutionVSPart( outputFile, "hResolThetaGenVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
59  mapHisto_["hResolThetaSimVSMu"] = new HResolutionVSPart( outputFile, "hResolThetaSimVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
60  mapHisto_["hResolCotgThetaGenVSMu"] = new HResolutionVSPart( outputFile, "hResolCotgThetaGenVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
61  mapHisto_["hResolCotgThetaSimVSMu"] = new HResolutionVSPart( outputFile, "hResolCotgThetaSimVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
62  mapHisto_["hResolPhiGenVSMu"] = new HResolutionVSPart( outputFile, "hResolPhiGenVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
63  mapHisto_["hResolPhiSimVSMu"] = new HResolutionVSPart( outputFile, "hResolPhiSimVSMu", maxPt, -0.02, 0.02, -0.02, 0.02 );
64 
66  mapHisto_["hdMdPt1"] = new HResolutionVSPart( outputFile, "hdMdPt1", maxPt, 0, 100, -3.2, 3.2, true );
67  mapHisto_["hdMdPt2"] = new HResolutionVSPart( outputFile, "hdMdPt2", maxPt, 0, 100, -3.2, 3.2, true );
68  mapHisto_["hdMdPhi1"] = new HResolutionVSPart( outputFile, "hdMdPhi1", maxPt, 0, 100, -3.2, 3.2, true );
69  mapHisto_["hdMdPhi2"] = new HResolutionVSPart( outputFile, "hdMdPhi2", maxPt, 0, 100, -3.2, 3.2, true );
70  mapHisto_["hdMdCotgTh1"] = new HResolutionVSPart( outputFile, "hdMdCotgTh1", maxPt, 0, 100, -3.2, 3.2, true );
71  mapHisto_["hdMdCotgTh2"] = new HResolutionVSPart( outputFile, "hdMdCotgTh2", maxPt, 0, 100, -3.2, 3.2, true );
72  }
73 
74  HTH2D * recoGenHisto = new HTH2D(outputFile, "hPtRecoVsPtGen", "Pt reco vs Pt gen", "hPtRecoVsPtGen", 120, 0., 120., 120, 0, 120.);
75  (*recoGenHisto)->SetXTitle("Pt gen (GeV)");
76  (*recoGenHisto)->SetYTitle("Pt reco (GeV)");
77  mapHisto_["hPtRecoVsPtGen"] = recoGenHisto;
78  HTH2D * recoSimHisto = new HTH2D(outputFile, "hPtRecoVsPtSim", "Pt reco vs Pt sim", "hPtRecoVsPtSim", 120, 0., 120., 120, 0, 120.);
79  (*recoSimHisto)->SetXTitle("Pt sim (GeV)");
80  (*recoSimHisto)->SetYTitle("Pt reco (GeV)");
81  mapHisto_["hPtRecoVsPtSim"] = recoSimHisto;
82  // Resolutions from resolution functions
83  // -------------------------------------
84  mapHisto_["hFunctionResolPt"] = new HFunctionResolution( outputFile, "hFunctionResolPt", maxPt );
85  mapHisto_["hFunctionResolCotgTheta"] = new HFunctionResolution( outputFile, "hFunctionResolCotgTheta", maxPt );
86  mapHisto_["hFunctionResolPhi"] = new HFunctionResolution( outputFile, "hFunctionResolPhi", maxPt );
87 
88  // Mass probability histograms
89  // ---------------------------
90  // The word "profile" is added to the title automatically
91  mapHisto_["hMass_P"] = new HTProfile( outputFile, "Mass_P", "Mass probability", 4000, 0., 200., 0., 50. );
92  mapHisto_["hMass_fine_P"] = new HTProfile( outputFile, "Mass_fine_P", "Mass probability", 4000, 0., 20., 0., 50. );
93  mapHisto_["hMass_Probability"] = new HTH1D( outputFile, "Mass_Probability", "Mass probability", 4000, 0., 200.);
94  mapHisto_["hMass_fine_Probability"] = new HTH1D( outputFile, "Mass_fine_Probability", "Mass probability", 4000, 0., 20.);
95  mapHisto_["hMassProbVsMu"] = new HMassVSPartProfile( "hMassProbVsMu", minMass, maxMass, maxPt );
96  mapHisto_["hMassProbVsRes"] = new HMassVSPartProfile( "hMassProbVsRes", minMass, maxMass, maxPt );
97  mapHisto_["hMassProbVsMu_fine"] = new HMassVSPartProfile( "hMassProbVsMu_fine", minMass, maxMass, maxPt );
98  mapHisto_["hMassProbVsRes_fine"] = new HMassVSPartProfile( "hMassProbVsRes_fine", minMass, maxMass, maxPt );
99 
100  // (M_reco-M_gen)/M_gen vs (pt, eta) of the muons from MC
101  mapHisto_["hDeltaMassOverGenMassVsPt"] = new HTH2D( outputFile, "DeltaMassOverGenMassVsPt", "DeltaMassOverGenMassVsPt", "DeltaMassOverGenMass", 200, 0, maxPt, 200, -0.2, 0.2 );
102  mapHisto_["hDeltaMassOverGenMassVsEta"] = new HTH2D( outputFile, "DeltaMassOverGenMassVsEta", "DeltaMassOverGenMassVsEta", "DeltaMassOverGenMass", 200, -3., 3., 200, -0.2, 0.2 );
103 
104  // Square of mass resolution vs (pt, eta) of the muons from MC
105  // EM 2012.12.19 mapHisto_["hMassResolutionVsPtEta"] = new HCovarianceVSxy( "Mass", "Mass", 100, 0., maxPt, 60, -3, 3, outputFile->mkdir("MassCovariance") );
106  // Mass resolution vs (pt, eta) from resolution function
107  mapHisto_["hFunctionResolMass"] = new HFunctionResolution( outputFile, "hFunctionResolMass", maxPt );
108 }
109 
111  for (std::map<std::string, Histograms*>::const_iterator histo=mapHisto_.begin();
112  histo!=mapHisto_.end(); histo++) {
113  delete (*histo).second;
114  }
115 }
116 
117 void MuScleFitBase::writeHistoMap( const unsigned int iLoop ) {
118  for (std::map<std::string, Histograms*>::const_iterator histo=mapHisto_.begin();
119  histo!=mapHisto_.end(); histo++) {
120  // This is to avoid writing into subdirs. Need a workaround.
121  theFiles_[iLoop]->cd();
122  (*histo).second->Write();
123  }
124 }
125 
127 {
128  TH2D * GLZ[24];
129  TH2D * GL[6];
130  TFile * ProbsFile;
131  if( probabilitiesFile_ != "" ) {
132  ProbsFile = new TFile (probabilitiesFile_.c_str());
133  std::cout << "[MuScleFit-Constructor]: Reading TH2D probabilities from " << probabilitiesFile_ << std::endl;
134  }
135  else {
136  // edm::FileInPath file("MuonAnalysis/MomentumScaleCalibration/test/Probs_new_1000_CTEQ.root");
137  // edm::FileInPath file("MuonAnalysis/MomentumScaleCalibration/test/Probs_new_Horace_CTEQ_1000.root");
138  // edm::FileInPath file("MuonAnalysis/MomentumScaleCalibration/test/Probs_merge.root");
140  ProbsFile = new TFile (file.fullPath().c_str());
141  std::cout << "[MuScleFit-Constructor]: Reading TH2D probabilities from " << probabilitiesFileInPath_ << std::endl;
142  }
143 
144 
145  ProbsFile->cd();
146  if( theMuonType_!=2 && MuScleFitUtils::resfind[0]) {
147  for ( int i=0; i<24; i++ ) {
148  char nameh[6];
149  sprintf (nameh,"GLZ%d",i);
150  GLZ[i] = dynamic_cast<TH2D*>(ProbsFile->Get(nameh));
151  }
152  }
154  GL[0] = dynamic_cast<TH2D*> (ProbsFile->Get("GL0"));
156  GL[1] = dynamic_cast<TH2D*> (ProbsFile->Get("GL1"));
158  GL[2] = dynamic_cast<TH2D*> (ProbsFile->Get("GL2"));
160  GL[3] = dynamic_cast<TH2D*> (ProbsFile->Get("GL3"));
162  GL[4] = dynamic_cast<TH2D*> (ProbsFile->Get("GL4"));
164  GL[5] = dynamic_cast<TH2D*> (ProbsFile->Get("GL5"));
165 
166  // Read the limits for M and Sigma axis for each pdf
167  // Note: we assume all the Z histograms to have the same limits
168  // x is mass, y is sigma
170  MuScleFitUtils::ResHalfWidth[0] = (GLZ[0]->GetXaxis()->GetXmax() - GLZ[0]->GetXaxis()->GetXmin())/2.;
171  MuScleFitUtils::ResMaxSigma[0] = (GLZ[0]->GetYaxis()->GetXmax() - GLZ[0]->GetYaxis()->GetXmin());
172  MuScleFitUtils::ResMinMass[0] = (GLZ[0]->GetXaxis()->GetXmin());
173  }
175  MuScleFitUtils::ResHalfWidth[0] = (GL[0]->GetXaxis()->GetXmax() - GL[0]->GetXaxis()->GetXmin())/2.;
176  MuScleFitUtils::ResMaxSigma[0] = (GL[0]->GetYaxis()->GetXmax() - GL[0]->GetYaxis()->GetXmin());
177  MuScleFitUtils::ResMinMass[0] = (GL[0]->GetXaxis()->GetXmin());
178  }
179  for( int i=1; i<6; ++i ) {
181  MuScleFitUtils::ResHalfWidth[i] = (GL[i]->GetXaxis()->GetXmax() - GL[i]->GetXaxis()->GetXmin())/2.;
182  MuScleFitUtils::ResMaxSigma[i] = (GL[i]->GetYaxis()->GetXmax() - GL[i]->GetYaxis()->GetXmin());
183  MuScleFitUtils::ResMinMass[i] = (GL[i]->GetXaxis()->GetXmin());
184  // if( debug_>2 ) {
185  std::cout << "MuScleFitUtils::ResHalfWidth["<<i<<"] = " << MuScleFitUtils::ResHalfWidth[i] << std::endl;
186  std::cout << "MuScleFitUtils::ResMaxSigma["<<i<<"] = " << MuScleFitUtils::ResMaxSigma[i] << std::endl;
187  // }
188  }
189  }
190 
191  // Extract normalization for mass slice in Y bins of Z
192  // ---------------------------------------------------
194  for (int iY=0; iY<24; iY++) {
195  int nBinsX = GLZ[iY]->GetNbinsX();
196  int nBinsY = GLZ[iY]->GetNbinsY();
197  if( nBinsX != MuScleFitUtils::nbins+1 || nBinsY != MuScleFitUtils::nbins+1 ) {
198  std::cout << "Error: for histogram \"" << GLZ[iY]->GetName() << "\" bins are not " << MuScleFitUtils::nbins << std::endl;
199  std::cout<< "nBinsX = " << nBinsX << ", nBinsY = " << nBinsY << std::endl;
200  exit(1);
201  }
202  for (int iy=0; iy<=MuScleFitUtils::nbins; iy++) {
203  MuScleFitUtils::GLZNorm[iY][iy] = 0.;
204  for (int ix=0; ix<=MuScleFitUtils::nbins; ix++) {
205  MuScleFitUtils::GLZValue[iY][ix][iy] = GLZ[iY]->GetBinContent(ix+1, iy+1);
207  }
208  }
209  }
210  }
211 
213  int nBinsX = GL[0]->GetNbinsX();
214  int nBinsY = GL[0]->GetNbinsY();
215  if( nBinsX != MuScleFitUtils::nbins+1 || nBinsY != MuScleFitUtils::nbins+1 ) {
216  std::cout << "Error: for histogram \"" << GL[0]->GetName() << "\" bins are not " << MuScleFitUtils::nbins << std::endl;
217  std::cout<< "nBinsX = " << nBinsX << ", nBinsY = " << nBinsY << std::endl;
218  exit(1);
219  }
220 
221  for (int iy=0; iy<=MuScleFitUtils::nbins; iy++) {
222  MuScleFitUtils::GLNorm[0][iy] = 0.;
223  for (int ix=0; ix<=MuScleFitUtils::nbins; ix++) {
224  MuScleFitUtils::GLValue[0][ix][iy] = GL[0]->GetBinContent(ix+1, iy+1);
225  // N.B. approximation: we should compute the integral of the function used to compute the probability (linear
226  // interpolation of the mass points). This computation could be troublesome because the points have a steep
227  // variation near the mass peak and the normal integral is not precise in these conditions.
228  // Furthermore it is slow.
230  }
231  }
232  }
233  // Extract normalization for each mass slice
234  // -----------------------------------------
235  for (int ires=1; ires<6; ires++) {
237  int nBinsX = GL[ires]->GetNbinsX();
238  int nBinsY = GL[ires]->GetNbinsY();
239  if( nBinsX != MuScleFitUtils::nbins+1 || nBinsY != MuScleFitUtils::nbins+1 ) {
240  std::cout << "Error: for histogram \"" << GL[ires]->GetName() << "\" bins are not " << MuScleFitUtils::nbins << std::endl;
241  std::cout<< "nBinsX = " << nBinsX << ", nBinsY = " << nBinsY << std::endl;
242  exit(1);
243  }
244 
245  for (int iy=0; iy<=MuScleFitUtils::nbins; iy++) {
246  MuScleFitUtils::GLNorm[ires][iy] = 0.;
247  for (int ix=0; ix<=MuScleFitUtils::nbins; ix++) {
248  MuScleFitUtils::GLValue[ires][ix][iy] = GL[ires]->GetBinContent(ix+1, iy+1);
249  // N.B. approximation: we should compute the integral of the function used to compute the probability (linear
250  // interpolation of the mass points). This computation could be troublesome because the points have a steep
251  // variation near the mass peak and the normal integral is not precise in these conditions.
252  // Furthermore it is slow.
254  }
255  }
256  }
257  }
258  // Free all the memory for the probability histograms.
260  for ( int i=0; i<24; i++ ) {
261  delete GLZ[i];
262  }
263  }
265  delete GL[0];
266  for (int ires=1; ires<6; ires++) {
268  delete GL[ires];
269  }
270  delete ProbsFile;
271 }
272 
273 #endif
#define LogDebug(id)
static double GLValue[6][1001][1001]
int i
Definition: DBlmapReader.cc:9
static int nbins
static bool debugMassResol_
int ires[2]
static double ResMinMass[6]
TF1 * GL
A wrapper for the TH1D histogram to allow it to be put inside the same map as all the other classes i...
Definition: Histograms.h:171
std::map< std::string, Histograms * > mapHisto_
The map of histograms.
Definition: MuScleFitBase.h:77
A wrapper for the TProfile histogram to allow it to be put inside the same map as all the other class...
Definition: Histograms.h:202
static double GLZNorm[40][1001]
void clearHistoMap()
Clean the histograms map.
static double ResMaxSigma[6]
static double GLZValue[40][1001][1001]
void writeHistoMap(const unsigned int iLoop)
Save the histograms map to file.
void fillHistoMap(TFile *outputFile, unsigned int iLoop)
Create the histograms map.
Definition: MuScleFitBase.cc:7
std::vector< TFile * > theFiles_
The files were the histograms are saved.
Definition: MuScleFitBase.h:74
static double GLNorm[6][1001]
A wrapper for the TH2D histogram to allow it to be put inside the same map as all the other classes i...
Definition: Histograms.h:130
tuple cout
Definition: gather_cfg.py:121
static std::vector< int > resfind
static double ResHalfWidth[6]
void readProbabilityDistributionsFromFile()
Read probability distributions from a local root file.
std::string probabilitiesFileInPath_
Definition: MuScleFitBase.h:42
A set of histograms for resolution.
Definition: Histograms.h:1138
std::string probabilitiesFile_
Definition: MuScleFitBase.h:43