CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ZmumuEvtSelEffCorrWeightProducer.cc
Go to the documentation of this file.
2 
4 
6 
8 
10 
11 #include <TFile.h>
12 
13 #include <vector>
14 #include <algorithm>
15 #include <math.h>
16 
18  : lutEfficiencyPt_(0),
19  lutEffCorrEta_(0)
20 {
21  srcSelectedMuons_ = cfg.getParameter<edm::InputTag>("selectedMuons");
22 
24  if ( inputFileName.location() == edm::FileInPath::Unknown)
25  throw cms::Exception("MuonRadiationCorrWeightProducer")
26  << " Failed to find File = " << inputFileName << " !!\n";
27  std::auto_ptr<TFile> inputFile(new TFile(inputFileName.fullPath().data()));
28 
29  std::string lutEfficiencyPtName = cfg.getParameter<std::string>("lutEfficiencyPt");
30  TH2* lutEfficiencyPt = dynamic_cast<TH2*>(inputFile->Get(lutEfficiencyPtName.data()));
31  if ( !lutEfficiencyPt )
32  throw cms::Exception("MuonRadiationCorrWeightProducer")
33  << " Failed to load LUT = " << lutEfficiencyPtName << " from file = " << inputFile->GetName() << " !!\n";
34  lutEfficiencyPt_ = (TH2*)lutEfficiencyPt->Clone(std::string(lutEfficiencyPt->GetName()).append("_cloned").data());
37 
38  std::string lutEffCorrEtaName = cfg.getParameter<std::string>("lutEffCorrEta");
39  TH2* lutEffCorrEta = dynamic_cast<TH2*>(inputFile->Get(lutEffCorrEtaName.data()));
40  if ( !lutEffCorrEta )
41  throw cms::Exception("MuonRadiationCorrWeightProducer")
42  << " Failed to load LUT = " << lutEffCorrEtaName << " from file = " << inputFile->GetName() << " !!\n";
43  lutEffCorrEta_ = (TH2*)lutEffCorrEta->Clone(std::string(lutEffCorrEta->GetName()).append("_cloned").data());
44  xAxisEffCorrEta_ = lutEffCorrEta_->GetXaxis();
45  yAxisEffCorrEta_ = lutEffCorrEta_->GetYaxis();
46 
47  minWeight_ = cfg.getParameter<double>("minWeight");
48  maxWeight_ = cfg.getParameter<double>("maxWeight");
49 
50  verbosity_ = ( cfg.exists("verbosity") ) ?
51  cfg.getParameter<int>("verbosity") : 0;
52 
53  produces<double>("weight");
54  produces<double>("weightUp");
55  produces<double>("weightDown");
56 }
57 
59 {
60  delete lutEfficiencyPt_;
61  delete lutEffCorrEta_;
62 }
63 
64 namespace
65 {
66  int findBin(TAxis* xAxis, double x)
67  {
68  int bin = xAxis->FindBin(x);
69  if ( bin < 1 ) bin = 1;
70  if ( bin > xAxis->GetNbins() ) bin = xAxis->GetNbins();
71  return bin;
72  }
73 
74  double square(double x)
75  {
76  return x*x;
77  }
78 }
79 
81 {
82  if ( verbosity_ ) {
83  std::cout << "<ZmumuEvtSelEffCorrWeightProducer::produce>:" << std::endl;
84  std::cout << " srcSelectedMuons = " << srcSelectedMuons_ << std::endl;
85  }
86 
87  double weight = 1.;
88  double weightUp = 2.*maxWeight_;
89  double weightDown = 0.;
90 
91  std::vector<reco::CandidateBaseRef> selMuons = getSelMuons(evt, srcSelectedMuons_);
92  const reco::CandidateBaseRef muPlus = getTheMuPlus(selMuons);
93  const reco::CandidateBaseRef muMinus = getTheMuMinus(selMuons);
94  if ( muPlus.isNonnull() && muMinus.isNonnull() ) {
95  if ( verbosity_ ) {
96  std::cout << "Mu+: Pt = " << muPlus->pt() << ", eta = " << muPlus->eta() << ", phi = " << muPlus->phi() << std::endl;
97  std::cout << "Mu-: Pt = " << muMinus->pt() << ", eta = " << muMinus->eta() << ", phi = " << muMinus->phi() << std::endl;
98  }
99 
100  int binX_efficiencyPt = findBin(xAxisEfficiencyPt_, muPlus->pt());
101  int binY_efficiencyPt = findBin(yAxisEfficiencyPt_, muMinus->pt());
102  double efficiencyPt = lutEfficiencyPt_->GetBinContent(binX_efficiencyPt, binY_efficiencyPt);
103  double efficiencyPtErr = lutEfficiencyPt_->GetBinError(binX_efficiencyPt, binY_efficiencyPt);
104 
105  int binX_effCorrEta = findBin(xAxisEffCorrEta_, muPlus->eta());
106  int binY_effCorrEta = findBin(yAxisEffCorrEta_, muMinus->eta());
107  double effCorrEta = lutEffCorrEta_->GetBinContent(binX_effCorrEta, binY_effCorrEta);
108  double effCorrEtaErr = lutEffCorrEta_->GetBinError(binX_effCorrEta, binY_effCorrEta);
109 
110  double efficiency = efficiencyPt*effCorrEta;
111  if ( efficiency > 0. ) {
112  weight = 1./efficiency;
113  if ( weight > maxWeight_ ) weight = maxWeight_;
114  if ( weight < minWeight_ ) weight = minWeight_;
115  weightUp = weight + TMath::Sqrt(square(efficiencyPtErr*effCorrEta) + square(efficiencyPt*effCorrEtaErr));
116  if ( weightUp > (2.*maxWeight_) ) weightUp = 2.*maxWeight_;
117  if ( weightUp < weight ) weightUp = weight;
118  weightDown = weight - TMath::Sqrt(square(efficiencyPtErr*effCorrEta) + square(efficiencyPt*effCorrEtaErr));
119  if ( weightDown > weight ) weightDown = weight;
120  if ( weightDown < 0. ) weightDown = 0.;
121  } else {
122  weight = maxWeight_;
123  weightUp = 2.*maxWeight_;
124  weightDown = 0.;
125  }
126  }
127 
128  if ( verbosity_ ) {
129  std::cout << "--> weight = " << weight << " + " << (weightUp - weight) << " - " << (weight - weightDown) << std::endl;
130  }
131 
132  std::auto_ptr<double> weightPtr(new double(weight));
133  evt.put(weightPtr, "weight");
134  std::auto_ptr<double> weightUpPtr(new double(weightUp));
135  evt.put(weightUpPtr, "weightUp");
136  std::auto_ptr<double> weightDownPtr(new double(weightDown));
137  evt.put(weightDownPtr, "weightDown");
138 }
139 
141 
143 
144 
T getParameter(std::string const &) const
reco::CandidateBaseRef getTheMuMinus(const std::vector< reco::CandidateBaseRef > &)
tuple cfg
Definition: looper.py:293
virtual double pt() const =0
transverse momentum
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:330
T x() const
Cartesian x coordinate.
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
void produce(edm::Event &, const edm::EventSetup &)
ZmumuEvtSelEffCorrWeightProducer(const edm::ParameterSet &)
double square(const double a)
tuple cout
Definition: gather_cfg.py:121
std::vector< reco::CandidateBaseRef > getSelMuons(const edm::Event &, const edm::InputTag &)
reco::CandidateBaseRef getTheMuPlus(const std::vector< reco::CandidateBaseRef > &)
virtual double phi() const =0
momentum azimuthal angle
virtual double eta() const =0
momentum pseudorapidity