CMS 3D CMS Logo

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[6];
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  ProbsFile->cd();
146  for ( int i=0; i<6; i++ ) {
147  char nameh[6];
148  sprintf (nameh,"GLZ%d",i);
149  GLZ[i] = dynamic_cast<TH2D*>(ProbsFile->Get(nameh));
150  }
151  }
152  else if(MuScleFitUtils::resfind[0]) {
153  GL[0] = dynamic_cast<TH2D*> (ProbsFile->Get("GL0"));
154  }
155  else if(MuScleFitUtils::resfind[1])
156  GL[1] = dynamic_cast<TH2D*> (ProbsFile->Get("GL1"));
157  else if(MuScleFitUtils::resfind[2])
158  GL[2] = dynamic_cast<TH2D*> (ProbsFile->Get("GL2"));
159  else if(MuScleFitUtils::resfind[3])
160  GL[3] = dynamic_cast<TH2D*> (ProbsFile->Get("GL3"));
161  else if(MuScleFitUtils::resfind[4])
162  GL[4] = dynamic_cast<TH2D*> (ProbsFile->Get("GL4"));
163  else if(MuScleFitUtils::resfind[5])
164  GL[5] = dynamic_cast<TH2D*> (ProbsFile->Get("GL5"));
165  else {
166  std::cout<<"[MuScleFit-Constructor]: No resonance selected, please fill the resfind array"<<std::endl;
167  exit(1);
168  }
169 
170  // Read the limits for M and Sigma axis for each pdf
171  // Note: we assume all the Z histograms to have the same limits
172  // x is mass, y is sigma
174  MuScleFitUtils::ResHalfWidth[0] = (GLZ[0]->GetXaxis()->GetXmax() - GLZ[0]->GetXaxis()->GetXmin())/2.;
175  MuScleFitUtils::ResMaxSigma[0] = (GLZ[0]->GetYaxis()->GetXmax() - GLZ[0]->GetYaxis()->GetXmin());
176  MuScleFitUtils::ResMinMass[0] = (GLZ[0]->GetXaxis()->GetXmin());
177  }
179  MuScleFitUtils::ResHalfWidth[0] = (GL[0]->GetXaxis()->GetXmax() - GL[0]->GetXaxis()->GetXmin())/2.;
180  MuScleFitUtils::ResMaxSigma[0] = (GL[0]->GetYaxis()->GetXmax() - GL[0]->GetYaxis()->GetXmin());
181  MuScleFitUtils::ResMinMass[0] = (GL[0]->GetXaxis()->GetXmin());
182  }
183  for( int i=1; i<6; ++i ) {
185  MuScleFitUtils::ResHalfWidth[i] = (GL[i]->GetXaxis()->GetXmax() - GL[i]->GetXaxis()->GetXmin())/2.;
186  MuScleFitUtils::ResMaxSigma[i] = (GL[i]->GetYaxis()->GetXmax() - GL[i]->GetYaxis()->GetXmin());
187  MuScleFitUtils::ResMinMass[i] = (GL[i]->GetXaxis()->GetXmin());
188  // if( debug_>2 ) {
189  std::cout << "MuScleFitUtils::ResHalfWidth["<<i<<"] = " << MuScleFitUtils::ResHalfWidth[i] << std::endl;
190  std::cout << "MuScleFitUtils::ResMaxSigma["<<i<<"] = " << MuScleFitUtils::ResMaxSigma[i] << std::endl;
191  // }
192  }
193  }
194 
195  // Extract normalization for mass slice in Y bins of Z
196  // ---------------------------------------------------
198  for (int iY=0; iY<6; iY++) {
199  int nBinsX = GLZ[iY]->GetNbinsX();
200  int nBinsY = GLZ[iY]->GetNbinsY();
201  if( nBinsX != MuScleFitUtils::nbins+1 || nBinsY != MuScleFitUtils::nbins+1 ) {
202  std::cout << "Error: for histogram \"" << GLZ[iY]->GetName() << "\" bins are not " << MuScleFitUtils::nbins << std::endl;
203  std::cout<< "nBinsX = " << nBinsX << ", nBinsY = " << nBinsY << std::endl;
204  exit(1);
205  }
206  for (int iy=0; iy<=MuScleFitUtils::nbins; iy++) {
207  MuScleFitUtils::GLZNorm[iY][iy] = 0.;
208  for (int ix=0; ix<=MuScleFitUtils::nbins; ix++) {
209  MuScleFitUtils::GLZValue[iY][ix][iy] = GLZ[iY]->GetBinContent(ix+1, iy+1);
211  }
212  }
213  }
214  }
215 
217  int nBinsX = GL[0]->GetNbinsX();
218  int nBinsY = GL[0]->GetNbinsY();
219  if( nBinsX != MuScleFitUtils::nbins+1 || nBinsY != MuScleFitUtils::nbins+1 ) {
220  std::cout << "Error: for histogram \"" << GL[0]->GetName() << "\" bins are not " << MuScleFitUtils::nbins << std::endl;
221  std::cout<< "nBinsX = " << nBinsX << ", nBinsY = " << nBinsY << std::endl;
222  exit(1);
223  }
224 
225  for (int iy=0; iy<=MuScleFitUtils::nbins; iy++) {
226  MuScleFitUtils::GLNorm[0][iy] = 0.;
227  for (int ix=0; ix<=MuScleFitUtils::nbins; ix++) {
228  MuScleFitUtils::GLValue[0][ix][iy] = GL[0]->GetBinContent(ix+1, iy+1);
229  // N.B. approximation: we should compute the integral of the function used to compute the probability (linear
230  // interpolation of the mass points). This computation could be troublesome because the points have a steep
231  // variation near the mass peak and the normal integral is not precise in these conditions.
232  // Furthermore it is slow.
234  }
235  }
236  }
237  // Extract normalization for each mass slice
238  // -----------------------------------------
239  for (int ires=1; ires<6; ires++) {
241  int nBinsX = GL[ires]->GetNbinsX();
242  int nBinsY = GL[ires]->GetNbinsY();
243  if( nBinsX != MuScleFitUtils::nbins+1 || nBinsY != MuScleFitUtils::nbins+1 ) {
244  std::cout << "Error: for histogram \"" << GL[ires]->GetName() << "\" bins are not " << MuScleFitUtils::nbins << std::endl;
245  std::cout<< "nBinsX = " << nBinsX << ", nBinsY = " << nBinsY << std::endl;
246  exit(1);
247  }
248 
249  for (int iy=0; iy<=MuScleFitUtils::nbins; iy++) {
250  MuScleFitUtils::GLNorm[ires][iy] = 0.;
251  for (int ix=0; ix<=MuScleFitUtils::nbins; ix++) {
252  MuScleFitUtils::GLValue[ires][ix][iy] = GL[ires]->GetBinContent(ix+1, iy+1);
253  // N.B. approximation: we should compute the integral of the function used to compute the probability (linear
254  // interpolation of the mass points). This computation could be troublesome because the points have a steep
255  // variation near the mass peak and the normal integral is not precise in these conditions.
256  // Furthermore it is slow.
258  }
259  }
260  }
261  }
262  // Free all the memory for the probability histograms.
264  for ( int i=0; i<6; i++ ) {
265  delete GLZ[i];
266  }
267  }
269  delete GL[0];
270  for (int ires=1; ires<6; ires++) {
272  delete GL[ires];
273  }
274  delete ProbsFile;
275 }
276 
277 #endif
#define LogDebug(id)
static double GLValue[6][1001][1001]
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:172
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:203
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.
static bool rapidityBinsForZ_
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:131
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:1139
std::string probabilitiesFile_
Definition: MuScleFitBase.h:43