ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/joshmt/PlotUtil.cxx
Revision: 1.2
Committed: Fri Apr 23 16:41:12 2010 UTC (15 years ago) by joshmt
Content type: text/plain
Branch: MAIN
Changes since 1.1: +4 -2 lines
Log Message:
add histo reset etc

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 joshmt 1.2 Double_t PlotUtil::ErrorOnIntegral(const TH1F* h, const Int_t lowbin, Int_t highbin) {
21 joshmt 1.1
22 joshmt 1.2 if ( highbin == 0) highbin = h->GetNbinsX();
23    
24 joshmt 1.1 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     TH1F* 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 joshmt 1.2 if (debug_) cout<<"Adding to base qcd histo: "<<fullname<<endl;
80 joshmt 1.1 TH1F* 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     TH1F* 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     TH1F* 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    
167     TString PlotUtil::fortranize(TString cut) {
168    
169     cut.ReplaceAll("==","eq");
170     cut.ReplaceAll(">=","gte");
171     cut.ReplaceAll("<=","lte");
172    
173     cut.ReplaceAll(">","gt");
174     cut.ReplaceAll("<","lt");
175    
176     return cut;
177     }