ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/PlotUtil.cxx
Revision: 1.1
Committed: Mon Mar 29 16:06:36 2010 UTC (15 years, 1 month ago) by joshmt
Content type: text/plain
Branch: MAIN
Log Message:
more mature plotting utilities

File Contents

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