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.
11 
12 #include "TH1F.h"
13 #include "TH2F.h"
14 #include "TH3F.h"
15 #include "RVersion.h"
16 
17 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,27,0)
18 #include "TEfficiency.h"
19 #else
20 #include "TGraphAsymmErrors.h"
21 #endif
22 using namespace edm;
23 using namespace std;
24 
25 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,27,0)
26 class HeavyFlavorHarvesting : public edm::EDAnalyzer { //, public TGraphAsymmErrors{
27 #else
28 class HeavyFlavorHarvesting : public edm::EDAnalyzer , public TGraphAsymmErrors{
29 #endif
30  public:
32  virtual ~HeavyFlavorHarvesting();
33  virtual void analyze(const edm::Event& event, const edm::EventSetup& eventSetup) override {};
34  virtual void endRun(const edm::Run &, const edm::EventSetup &) override;
35  private:
36  void calculateEfficiency(const ParameterSet& pset);
37  void calculateEfficiency1D( TH1* num, TH1* den, string name );
38  void calculateEfficiency2D( TH2F* num, TH2F* den, string name );
42 };
43 
45  myDQMrootFolder( pset.getUntrackedParameter<string>("MyDQMrootFolder") ),
46  efficiencies( pset.getUntrackedParameter<VParameterSet>("Efficiencies") )
47 {
48 }
49 
52  if( !dqmStore ){
53  LogError("HLTriggerOfflineHeavyFlavor") << "Could not find DQMStore service\n";
54  return;
55  }
56  for(VParameterSet::const_iterator pset = efficiencies.begin(); pset!=efficiencies.end(); pset++){
57  calculateEfficiency(*pset);
58  }
59 }
60 
62 //get hold of numerator and denominator histograms
63  vector<string> numDenEffMEnames = pset.getUntrackedParameter<vector<string> >("NumDenEffMEnames");
64  if(numDenEffMEnames.size()!=3){
65  LogDebug("HLTriggerOfflineHeavyFlavor") << "NumDenEffMEnames must have three names"<<endl;
66  return;
67  }
68  string denMEname = myDQMrootFolder+"/"+numDenEffMEnames[1];
69  string numMEname = myDQMrootFolder+"/"+numDenEffMEnames[0];
70  MonitorElement *denME = dqmStore->get(denMEname);
71  MonitorElement *numME = dqmStore->get(numMEname);
72  if(denME==0 || numME==0){
73  LogDebug("HLTriggerOfflineHeavyFlavor") << "Could not find MEs: "<<denMEname<<" or "<<numMEname<<endl;
74  return;
75  }
76  TH1 *den = denME->getTH1();
77  TH1 *num = numME->getTH1();
78  //check compatibility of the histograms
79  if( den->GetNbinsX() != num->GetNbinsX() || den->GetNbinsY() != num->GetNbinsY() || den->GetNbinsZ() != num->GetNbinsZ() ){
80  LogDebug("HLTriggerOfflineHeavyFlavor") << "Monitoring elements "<<numMEname<<" and "<<denMEname<<"are incompatible"<<endl;
81  return;
82  }
83  //figure out the directory and efficiency name
84  string effName = numDenEffMEnames[2];
85  string effDir = myDQMrootFolder;
86  string::size_type slashPos = effName.rfind('/');
87  if ( string::npos != slashPos ) {
88  effDir += "/"+effName.substr(0, slashPos);
89  effName.erase(0, slashPos+1);
90  }
91  dqmStore->setCurrentFolder(effDir);
92  //calculate the efficiencies
93  int dimensions = num->GetDimension();
94  if(dimensions==1){
95  calculateEfficiency1D( num, den, effName );
96  }else if(dimensions==2){
97  calculateEfficiency2D( (TH2F*)num, (TH2F*)den, effName );
98  TH1D* numX = ((TH2F*)num)->ProjectionX();
99  TH1D* denX = ((TH2F*)den)->ProjectionX();
100  calculateEfficiency1D( numX, denX, effName+"X" );
101  delete numX;
102  delete denX;
103  TH1D* numY = ((TH2F*)num)->ProjectionY();
104  TH1D* denY = ((TH2F*)den)->ProjectionY();
105  calculateEfficiency1D( numY, denY, effName+"Y" );
106  delete numY;
107  delete denY;
108  }else{
109  return;
110  }
111 }
112 
113 void HeavyFlavorHarvesting::calculateEfficiency1D( TH1* num, TH1* den, string effName ){
114  TProfile* eff;
115  if(num->GetXaxis()->GetXbins()->GetSize()==0){
116  eff = new TProfile(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXmin(),num->GetXaxis()->GetXmax());
117  }else{
118  eff = new TProfile(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXbins()->GetArray());
119  }
120  eff->SetTitle(effName.c_str());
121  eff->SetXTitle( num->GetXaxis()->GetTitle() );
122  eff->SetYTitle("Efficiency");
123  eff->SetOption("PE");
124  eff->SetLineColor(2);
125  eff->SetLineWidth(2);
126  eff->SetMarkerStyle(20);
127  eff->SetMarkerSize(0.8);
128  eff->GetYaxis()->SetRangeUser(-0.001,1.001);
129  eff->SetStats(kFALSE);
130  for(int i=1;i<=num->GetNbinsX();i++){
131  double e, low, high;
132 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,27,0)
133  if (int(den->GetBinContent(i))>0.) e= double(num->GetBinContent(i))/double(den->GetBinContent(i));
134  else e=0.;
135  low=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,false);
136  high=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,true);
137 #else
138  Efficiency( (double)num->GetBinContent(i), (double)den->GetBinContent(i), 0.683, e, low, high );
139 #endif
140  double err = e-low>high-e ? e-low : high-e;
141  //here is the trick to store info in TProfile:
142  eff->SetBinContent( i, e );
143  eff->SetBinEntries( i, 1 );
144  eff->SetBinError( i, sqrt(e*e+err*err) );
145  }
146  dqmStore->bookProfile(effName,eff);
147  delete eff;
148 }
149 
150 void HeavyFlavorHarvesting::calculateEfficiency2D( TH2F* num, TH2F* den, string effName ){
151  TProfile2D* eff;
152  if(num->GetXaxis()->GetXbins()->GetSize()==0 && num->GetYaxis()->GetXbins()->GetSize()==0){
153  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());
154  }else if(num->GetXaxis()->GetXbins()->GetSize()!=0 && num->GetYaxis()->GetXbins()->GetSize()==0){
155  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());
156  }else if(num->GetXaxis()->GetXbins()->GetSize()==0 && num->GetYaxis()->GetXbins()->GetSize()!=0){
157  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());
158  }else{
159  eff = new TProfile2D(effName.c_str(),effName.c_str(),num->GetXaxis()->GetNbins(),num->GetXaxis()->GetXbins()->GetArray(),num->GetYaxis()->GetNbins(),num->GetYaxis()->GetXbins()->GetArray());
160  }
161  eff->SetTitle(effName.c_str());
162  eff->SetXTitle( num->GetXaxis()->GetTitle() );
163  eff->SetYTitle( num->GetYaxis()->GetTitle() );
164  eff->SetZTitle("Efficiency");
165  eff->SetOption("colztexte");
166  eff->GetZaxis()->SetRangeUser(-0.001,1.001);
167  eff->SetStats(kFALSE);
168  for(int i=0;i<num->GetSize();i++){
169  double e, low, high;
170 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,27,0)
171  if (int(den->GetBinContent(i))>0.) e= double(num->GetBinContent(i))/double(den->GetBinContent(i));
172  else e=0.;
173  low=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,false);
174  high=TEfficiency::Wilson((double)den->GetBinContent(i),(double)num->GetBinContent(i),0.683,true);
175 #else
176  Efficiency( (double)num->GetBinContent(i), (double)den->GetBinContent(i), 0.683, e, low, high );
177 #endif
178  double err = e-low>high-e ? e-low : high-e;
179  //here is the trick to store info in TProfile:
180  eff->SetBinContent( i, e );
181  eff->SetBinEntries( i, 1 );
182  eff->SetBinError( i, sqrt(e*e+err*err) );
183  }
184  dqmStore->bookProfile2D(effName,eff);
185  delete eff;
186 }
187 
189 }
190 
191 //define this as a plug-in
#define LogDebug(id)
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
void calculateEfficiency2D(TH2F *num, TH2F *den, string name)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:33
virtual void endRun(const edm::Run &, const edm::EventSetup &) override
void calculateEfficiency1D(TH1 *num, TH1 *den, string name)
uint16_t size_type
void calculateEfficiency(const ParameterSet &pset)
const VParameterSet efficiencies
T sqrt(T t)
Definition: SSEVec.h:48
TH1 * getTH1(void) const
HeavyFlavorHarvesting(const edm::ParameterSet &pset)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
virtual void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
Definition: Run.h:41