test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HeavyFlavorHarvesting.cc
Go to the documentation of this file.
12 
13 #include "TH1F.h"
14 #include "TH2F.h"
15 #include "TH3F.h"
16 #include "RVersion.h"
17 
18 #include "TEfficiency.h"
19 
20 using namespace edm;
21 using namespace std;
22 
24 
25  public:
27  virtual ~HeavyFlavorHarvesting();
28  // virtual void endRun(const edm::Run &, const edm::EventSetup &) override;
29  private:
30  void calculateEfficiency(const ParameterSet& pset, DQMStore::IBooker &, DQMStore::IGetter &);
31  void calculateEfficiency1D( TH1* num, TH1* den, string name, DQMStore::IBooker &, DQMStore::IGetter &);
32  void calculateEfficiency2D( TH2F* num, TH2F* den, string name, DQMStore::IBooker &, DQMStore::IGetter &);
33 
36  protected:
37  void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &) override; //performed in the endJob
38 };
39 
41  myDQMrootFolder( pset.getUntrackedParameter<string>("MyDQMrootFolder") ),
42  efficiencies( pset.getUntrackedParameter<VParameterSet>("Efficiencies") )
43 {
44 }
45 
47  for(VParameterSet::const_iterator pset = efficiencies.begin(); pset!=efficiencies.end(); pset++){
48  calculateEfficiency(*pset, ibooker_, igetter_);
49  }
50 }
51 
53 //get hold of numerator and denominator histograms
54  vector<string> numDenEffMEnames = pset.getUntrackedParameter<vector<string> >("NumDenEffMEnames");
55  if(numDenEffMEnames.size()!=3){
56  LogDebug("HLTriggerOfflineHeavyFlavor") << "NumDenEffMEnames must have three names"<<endl;
57  return;
58  }
59  string denMEname = myDQMrootFolder+"/"+numDenEffMEnames[1];
60  string numMEname = myDQMrootFolder+"/"+numDenEffMEnames[0];
61  MonitorElement *denME = igetter_.get(denMEname);
62  MonitorElement *numME = igetter_.get(numMEname);
63  if(denME==0 || numME==0){
64  LogDebug("HLTriggerOfflineHeavyFlavor") << "Could not find MEs: "<<denMEname<<" or "<<numMEname<<endl;
65  return;
66  }
67  TH1 *den = denME->getTH1();
68  TH1 *num = numME->getTH1();
69  //check compatibility of the histograms
70  if( den->GetNbinsX() != num->GetNbinsX() || den->GetNbinsY() != num->GetNbinsY() || den->GetNbinsZ() != num->GetNbinsZ() ){
71  LogDebug("HLTriggerOfflineHeavyFlavor") << "Monitoring elements "<<numMEname<<" and "<<denMEname<<"are incompatible"<<endl;
72  return;
73  }
74  //figure out the directory and efficiency name
75  string effName = numDenEffMEnames[2];
76  string effDir = myDQMrootFolder;
77  string::size_type slashPos = effName.rfind('/');
78  if ( string::npos != slashPos ) {
79  effDir += "/"+effName.substr(0, slashPos);
80  effName.erase(0, slashPos+1);
81  }
82  ibooker_.setCurrentFolder(effDir);
83  //calculate the efficiencies
84  int dimensions = num->GetDimension();
85  if(dimensions==1){
86  calculateEfficiency1D( num, den, effName, ibooker_, igetter_ );
87  }else if(dimensions==2){
88  calculateEfficiency2D( (TH2F*)num, (TH2F*)den, effName, ibooker_, igetter_ );
89  TH1D* numX = ((TH2F*)num)->ProjectionX();
90  TH1D* denX = ((TH2F*)den)->ProjectionX();
91  calculateEfficiency1D( numX, denX, effName+"X", ibooker_, igetter_ );
92  delete numX;
93  delete denX;
94  TH1D* numY = ((TH2F*)num)->ProjectionY();
95  TH1D* denY = ((TH2F*)den)->ProjectionY();
96  calculateEfficiency1D( numY, denY, effName+"Y", ibooker_, igetter_ );
97  delete numY;
98  delete denY;
99  }else{
100  return;
101  }
102 }
103 
104 void HeavyFlavorHarvesting::calculateEfficiency1D( TH1* num, TH1* den, string effName, DQMStore::IBooker & ibooker_, DQMStore::IGetter & igetter_){
105  TProfile* eff;
106  if(num->GetXaxis()->GetXbins()->GetSize()==0){
107  eff = new TProfile(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXmin(),num->GetXaxis()->GetXmax());
108  }else{
109  eff = new TProfile(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXbins()->GetArray());
110  }
111  eff->SetTitle(effName.c_str());
112  eff->SetXTitle( num->GetXaxis()->GetTitle() );
113  eff->SetYTitle("Efficiency");
114  eff->SetOption("PE");
115  eff->SetLineColor(2);
116  eff->SetLineWidth(2);
117  eff->SetMarkerStyle(20);
118  eff->SetMarkerSize(0.8);
119  eff->GetYaxis()->SetRangeUser(-0.001,1.001);
120  eff->SetStats(kFALSE);
121  for(int i=1;i<=num->GetNbinsX();i++){
122  double e, low, high;
123  if (int(den->GetBinContent(i))>0.) e= double(num->GetBinContent(i))/double(den->GetBinContent(i));
124  else e=0.;
125  low=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,false);
126  high=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,true);
127 
128  double err = e-low>high-e ? e-low : high-e;
129  //here is the trick to store info in TProfile:
130  eff->SetBinContent( i, e );
131  eff->SetBinEntries( i, 1 );
132  eff->SetBinError( i, sqrt(e*e+err*err) );
133  }
134  ibooker_.bookProfile(effName,eff);
135  delete eff;
136 }
137 
138 void HeavyFlavorHarvesting::calculateEfficiency2D( TH2F* num, TH2F* den, string effName, DQMStore::IBooker & ibooker_, DQMStore::IGetter & igetter_){
139  TProfile2D* eff;
140  if(num->GetXaxis()->GetXbins()->GetSize()==0 && num->GetYaxis()->GetXbins()->GetSize()==0){
141  eff = new TProfile2D(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXmin(),num->GetXaxis()->GetXmax(),num->GetYaxis()->GetNbins(),num->GetYaxis()->GetXmin(),num->GetYaxis()->GetXmax());
142  }else if(num->GetXaxis()->GetXbins()->GetSize()!=0 && num->GetYaxis()->GetXbins()->GetSize()==0){
143  eff = new TProfile2D(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXbins()->GetArray(),num->GetYaxis()->GetNbins(),num->GetYaxis()->GetXmin(),num->GetYaxis()->GetXmax());
144  }else if(num->GetXaxis()->GetXbins()->GetSize()==0 && num->GetYaxis()->GetXbins()->GetSize()!=0){
145  eff = new TProfile2D(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXmin(),num->GetXaxis()->GetXmax(),num->GetYaxis()->GetNbins(),num->GetYaxis()->GetXbins()->GetArray());
146  }else{
147  eff = new TProfile2D(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXbins()->GetArray(),num->GetYaxis()->GetNbins(),num->GetYaxis()->GetXbins()->GetArray());
148  }
149  eff->SetTitle(effName.c_str());
150  eff->SetXTitle( num->GetXaxis()->GetTitle() );
151  eff->SetYTitle( num->GetYaxis()->GetTitle() );
152  eff->SetZTitle("Efficiency");
153  eff->SetOption("colztexte");
154  eff->GetZaxis()->SetRangeUser(-0.001,1.001);
155  eff->SetStats(kFALSE);
156  for(int i=0;i<num->GetSize();i++){
157  double e, low, high;
158  if (int(den->GetBinContent(i))>0.) e= double(num->GetBinContent(i))/double(den->GetBinContent(i));
159  else e=0.;
160  low=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,false);
161  high=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,true);
162 
163  double err = e-low>high-e ? e-low : high-e;
164  //here is the trick to store info in TProfile:
165  eff->SetBinContent( i, e );
166  eff->SetBinEntries( i, 1 );
167  eff->SetBinError( i, sqrt(e*e+err*err) );
168  }
169  ibooker_.bookProfile2D(effName,eff);
170  delete eff;
171 }
172 
174 }
175 
176 //define this as a plug-in
#define LogDebug(id)
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
MonitorElement * bookProfile(Args &&...args)
Definition: DQMStore.h:157
MonitorElement * get(const std::string &path)
Definition: DQMStore.cc:304
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
void calculateEfficiency1D(TH1 *num, TH1 *den, string name, DQMStore::IBooker &, DQMStore::IGetter &)
uint16_t size_type
const VParameterSet efficiencies
MonitorElement * bookProfile2D(Args &&...args)
Definition: DQMStore.h:163
T sqrt(T t)
Definition: SSEVec.h:18
TH1 * getTH1(void) const
HeavyFlavorHarvesting(const edm::ParameterSet &pset)
void calculateEfficiency2D(TH2F *num, TH2F *den, string name, DQMStore::IBooker &, DQMStore::IGetter &)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:276
void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &) override
void calculateEfficiency(const ParameterSet &pset, DQMStore::IBooker &, DQMStore::IGetter &)