test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EmbeddingKineReweightProducer.h
Go to the documentation of this file.
1 #ifndef TauAnalysis_MCEmbeddingTools_EmbeddingKineReweightProducer_h
2 #define TauAnalysis_MCEmbeddingTools_EmbeddingKineReweightProducer_h
3 
21 
24 
27 
28 #include <TFile.h>
29 #include <TH1.h>
30 #include <TAxis.h>
31 
32 #include <string>
33 #include <map>
34 
36 {
37  public:
40 
41  void produce(edm::Event&, const edm::EventSetup&);
42 
43  private:
45 
46  struct lutEntryType
47  {
48  lutEntryType(TFile& inputFile, const std::string& variable, const std::string& lutName)
49  : variableName_(variable),
50  numDimensions_(0),
51  lut_(0),
52  xAxis_(0),
53  numBinsX_(0),
54  yAxis_(0),
55  numBinsY_(0)
56  {
57  if ( variable == "genDiTauPt" ) {
59  numDimensions_ = 1;
60  } else if ( variable == "genDiTauMass" ) {
62  numDimensions_ = 1;
63  } else if ( variable == "genDiTauMassVsGenDiTauPt" ) {
65  numDimensions_ = 2;
66  } else if ( variable == "genTau2PtVsGenTau1Pt" ) {
68  numDimensions_ = 2;
69  } else if ( variable == "genTau2EtaVsGenTau1Eta" ) {
71  numDimensions_ = 2;
72  } else throw cms::Exception("EmbeddingKineReweightProducer")
73  << " Invalid Configuration Parameter 'variable' = " << variable << " !!\n";
74  assert(numDimensions_ >= 1 && numDimensions_ <= 2);
75  TH1* lut = dynamic_cast<TH1*>(inputFile.Get(lutName.data()));
76  if ( !lut )
77  throw cms::Exception("EmbeddingReweightProducer")
78  << " Failed to load LUT = " << lutName << " from file = " << inputFile.GetName() << " !!\n";
79  lut_ = (TH1*)lut->Clone(std::string(lut->GetName()).append("_cloned").data());
80  if ( !lut_->GetSumw2N() ) lut_->Sumw2();
81  xAxis_ = lut_->GetXaxis();
82  numBinsX_ = xAxis_->GetNbins();
83  if ( numDimensions_ >= 2 ) {
84  yAxis_ = lut_->GetYaxis();
85  numBinsY_ = yAxis_->GetNbins();
86  }
87  }
89  {
90  delete lut_;
91  }
92  double operator()(const reco::Candidate& genDiTau) const
93  {
94  double x = 0.;
95  double y = 0.;
96  if ( variable_ == kGenDiTauPt ) {
97  x = genDiTau.pt();
98  } else if ( variable_ == kGenDiTauMass ) {
99  x = genDiTau.mass();
100  } else if ( variable_ == kGenDiTauMass_vs_genDiTauPt ) {
101  x = genDiTau.pt();
102  y = genDiTau.mass();
104  bool genTauFound = false;
105  const reco::CompositeCandidate* genDiTau_composite = dynamic_cast<const reco::CompositeCandidate*>(&genDiTau);
106  if ( genDiTau_composite && genDiTau_composite->numberOfDaughters() == 2 ) {
107  const reco::Candidate* genTau1 = genDiTau_composite->daughter(0);
108  const reco::Candidate* genTau2 = genDiTau_composite->daughter(1);
109  if ( !(genTau1 && genTau2) ) {
110  std::cerr << "Failed to find daughters of genDiTau --> skipping !!" << std::endl;
111  return 0.;
112  }
113  const reco::Candidate* genTauPlus = 0;
114  const reco::Candidate* genTauMinus = 0;
115  if ( genTau1->charge() > +0.5 && genTau2->charge() < -0.5 ) {
116  genTauPlus = genTau1;
117  genTauMinus = genTau2;
118  } else if ( genTau1->charge() < -0.5 && genTau2->charge() > +0.5 ) {
119  genTauPlus = genTau2;
120  genTauMinus = genTau1;
121  } else {
122  std::cerr << "Failed to find daughters of genDiTau --> skipping !!" << std::endl;
123  return 0.;
124  }
125  if ( genTauPlus && genTauMinus ) {
127  x = genTauPlus->pt();
128  y = genTauMinus->pt();
129  } else if ( variable_ == kGenTau2Eta_vs_genTau1Eta ) {
130  x = genTauPlus->eta();
131  y = genTauMinus->eta();
132  } else assert(0);
133  genTauFound = true;
134  }
135  }
136  if ( !genTauFound ) {
137  edm::LogWarning ("<EmbeddingKineReweightProducer>")
138  << "Failed to find gen. Tau decay products --> returning weight = 1.0 !!" << std::endl;
139  return 1.;
140  }
141  } else assert(0);
142  int idxBinX = xAxis_->FindBin(x);
143  if ( idxBinX <= 1 ) idxBinX = 1;
144  if ( idxBinX >= numBinsX_ ) idxBinX = numBinsX_;
145  if ( numDimensions_ == 1 ) return lut_->GetBinContent(idxBinX);
146  int idxBinY = yAxis_->FindBin(y);
147  if ( idxBinY <= 1 ) idxBinY = 1;
148  if ( idxBinY >= numBinsY_ ) idxBinY = numBinsY_;
149  if ( numDimensions_ == 2 ) return lut_->GetBinContent(idxBinX, idxBinY);
150  assert(0);
151  }
156  TH1* lut_;
157  TAxis* xAxis_;
159  TAxis* yAxis_;
161  };
162 
163  std::vector<lutEntryType*> lutEntries_;
164 
165  double minWeight_;
166  double maxWeight_;
167 
169 };
170 
171 #endif
172 
virtual float mass() const =0
mass
lutEntryType(TFile &inputFile, const std::string &variable, const std::string &lutName)
virtual float eta() const =0
momentum pseudorapidity
virtual size_type numberOfDaughters() const
number of daughters
virtual float pt() const =0
transverse momentum
std::vector< lutEntryType * > lutEntries_
virtual int charge() const =0
electric charge
tuple lut
Definition: lumiPlot.py:244
double operator()(const reco::Candidate &genDiTau) const
virtual const Candidate * daughter(size_type) const
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
void produce(edm::Event &, const edm::EventSetup &)
Definition: DDAxes.h:10
EmbeddingKineReweightProducer(const edm::ParameterSet &)