ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/PlotUtil.cxx
Revision: 1.4
Committed: Thu Sep 23 10:11:20 2010 UTC (14 years, 7 months ago) by joshmt
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +0 -11 lines
Log Message:
move fortranize into its own file

File Contents

# Content
1 #include <iostream>
2
3 #include "TString.h"
4 #include "TH1.h"
5 #include "TTree.h"
6
7 #include "PlotUtil.h"
8
9 using namespace std;
10
11 PlotUtil::PlotUtil(HistHolder* hh) :
12 hh_(hh),
13 debug_(false),
14 lastVarname_("MET")
15 {
16 }
17
18 PlotUtil::~PlotUtil() {}
19
20 Double_t PlotUtil::ErrorOnIntegral(const TH1D* h, const Int_t lowbin, Int_t highbin) {
21
22 if ( highbin == 0) highbin = h->GetNbinsX();
23
24 Double_t err=0;
25
26 Double_t thisbin=0;
27 for ( int i = lowbin; i<=highbin ; i++) {
28 thisbin = h->GetBinError(i);
29 err += thisbin*thisbin;
30 }
31
32 return sqrt( err );
33
34 }
35
36 void PlotUtil::createHistos(TString varname,int nbins, float hmin, float hmax) {
37 if (varname=="") varname = lastVarname_;
38
39 for ( map<TString,TTree*>::const_iterator isamp=samples_.begin() ; isamp!=samples_.end() ; ++isamp) {
40 TString fullname;
41 fullname.Form("H%s_%s",varname.Data(),isamp->first.Data());
42 hh_->make(fullname.Data(),varname.Data(),nbins,hmin,hmax);
43 }
44 lastVarname_=varname;
45 }
46
47 void PlotUtil::fillHistos(TString varname,TString cut, TString drawcommand) {
48 if (varname=="") varname = lastVarname_;
49
50 if (drawcommand=="") drawcommand=varname;
51
52 for ( map<TString,TTree*>::const_iterator i=samples_.begin() ; i!=samples_.end() ; ++i) {
53 i->second->Project(getFullName(varname,i->first),drawcommand,cut);
54 }
55 lastVarname_=varname;
56 }
57
58 TString PlotUtil::getFullName(TString varname,TString sampleid) {
59
60 TString fullname;
61 fullname.Form("H%s_%s",varname.Data(),sampleid.Data());
62 return fullname;
63 }
64
65 void PlotUtil::addQCD(TString varname) {
66 if (varname=="") varname = lastVarname_;
67
68 const TString qcdhist="H"+varname+"_qcd";
69
70 TH1D* hbase=hh_->find(qcdhist);
71 if (hbase==0) {
72 cout<<"Could not find the _qcd histogram!"<<endl;
73 return;
74 }
75
76 for ( map<TString,TTree*>::const_iterator i=samples_.begin() ; i!=samples_.end() ; ++i) {
77 TString fullname=getFullName(varname,i->first);
78 if ( fullname!= qcdhist && fullname.Contains("_qcd") ) {
79 if (debug_) cout<<"Adding to base qcd histo: "<<fullname<<endl;
80 TH1D* htoadd=hh_->find(fullname);
81 hbase->Add(htoadd);
82 }
83 }
84 lastVarname_=varname;
85 }
86
87 void PlotUtil::setLineColors(TString varname) {
88 if (varname=="") varname = lastVarname_;
89
90 for ( map<TString,UInt_t>::const_iterator i=sampleColors_.begin() ; i!=sampleColors_.end() ; ++i) {
91 TString fullname=getFullName(varname,i->first);
92 if (debug_) {
93 cout<<fullname<<endl;
94 cout<<hh_->find(fullname)<<endl;
95 }
96 hh_->find(fullname)->SetLineColor(i->second);
97 }
98
99 lastVarname_=varname;
100 }
101
102 void PlotUtil::drawPlots(TString varname) {
103 if (varname=="") varname = lastVarname_;
104
105 //want to exclude the secondary qcd histos
106 const TString qcdhist="H"+varname+"_qcd";
107
108 //find the "tallest" histogram
109 TH1D* firsttodraw=0;
110 double max=0;
111 for ( map<TString,TTree*>::const_iterator i=samples_.begin() ; i!=samples_.end() ; ++i) {
112 TString fullname=getFullName(varname,i->first);
113
114 if ( (!fullname.Contains("_qcd")) || (fullname ==qcdhist) ) {
115 double mymax= hh_->find(fullname)->GetMaximum();
116 if (mymax>max) {
117 max=mymax;
118 firsttodraw = hh_->find(fullname);
119 }
120 }
121 }
122
123 firsttodraw->Draw();
124
125 //draw the rest
126 for ( map<TString,TTree*>::const_iterator i=samples_.begin() ; i!=samples_.end() ; ++i) {
127 TString fullname=getFullName(varname,i->first);
128 if ( (!fullname.Contains("_qcd")) || (fullname ==qcdhist) ) {
129 TH1D* hist= hh_->find(fullname);
130 if (hist!= firsttodraw) hist->Draw("SAME");
131 }
132 }
133
134 lastVarname_=varname;
135 }
136
137 void PlotUtil::fillLegend(TLegend * leg, TString varname) {
138 if (varname=="") varname = lastVarname_;
139
140 if (debug_) cout<<"[fillLegend]"<<endl;
141 setLineColors(varname);
142 if (debug_) cout<<"[done with setLineColors()]"<<endl;
143
144 //want to exclude the secondary qcd histos
145 const TString qcdhist="H"+varname+"_qcd";
146
147 for ( map<TString,TTree*>::const_iterator i=samples_.begin() ; i!=samples_.end() ; ++i) {
148 TString fullname=getFullName(varname,i->first);
149 if ( (!fullname.Contains("_qcd")) || (fullname ==qcdhist) ) {
150 leg->AddEntry(hh_->find(fullname), sampleNames_[i->first]);
151 }
152 }
153 lastVarname_=varname;
154 }
155
156 void PlotUtil::addSample(TString sampleid,TTree* sampletree,TString sampleName,UInt_t kcolor) {
157
158 samples_[sampleid]=sampletree;
159
160 if (sampleName=="") sampleName=sampleid;
161 sampleNames_[sampleid]=sampleName;
162
163 sampleColors_[sampleid]=kcolor;
164
165 }
166