ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Common/CPlot.hh
Revision: 1.2
Committed: Tue Jul 26 07:18:45 2011 UTC (13 years, 9 months ago) by dkralph
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +6 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 dkralph 1.1 //==================================================================================================
2     // CPlot class:
3     // ------------
4     // Class to manage a plot. Member methods are provided to facilitate properties of the plot
5     // (e.g. styles, colors, combining histograms or graphs, etc.)
6     //
7     // USAGE: Need to be compiled on ROOT startup. Add the following line to rootlogon.C,
8     //
9     // gROOT->Macro("CPlot.cc+");
10     //
11     // (Modify path argument if rootlogon.C and CPlot.cc are in different directories)
12     //==================================================================================================
13    
14     #ifndef CPLOT_HH
15     #define CPLOT_HH
16    
17     #include <TROOT.h>
18     #include <TSystem.h>
19     #include <TCanvas.h>
20     #include <TFile.h>
21     #include <TLegend.h>
22     #include <TLegendEntry.h>
23     #include <TPaveStats.h>
24     #include <TF1.h>
25     #include <TH1F.h>
26 dkralph 1.2 #include <TH2D.h>
27 dkralph 1.1 #include <TGraph.h>
28     #include <TProfile.h>
29     #include <TLine.h>
30     #include <THStack.h>
31     #include <TBox.h>
32     #include <vector>
33    
34     #include "RooGlobalFunc.h"
35     #include "RooPlot.h"
36    
37     using namespace RooFit;
38    
39     class CPlotItem {
40     public:
41     CPlotItem():hist1D(0),hist2D(0),graph(0),prof(0),drawopt(""){}
42     ~CPlotItem(){}
43    
44     TH1F* hist1D;
45 dkralph 1.2 TH2D* hist2D;
46 dkralph 1.1 TGraph* graph;
47     TProfile* prof;
48     TString drawopt;
49     };
50    
51     class CPlot {
52     public:
53     CPlot();
54     CPlot(TString name, TString title, TString xtitle, TString ytitle);
55     CPlot(TString name, RooPlot* frame, TString title, TString xtitle, TString ytitle);
56     ~CPlot(){}
57    
58     static TString sOutDir; // output directory
59    
60     // // Clear the plot by resetting all object properties
61     // void Clear(){}
62    
63     // Draw the plot to a given canvas
64     void Draw(TCanvas *c, bool doSave=false, TString format="png");
65    
66     // Adding a 1D histogram to the plot
67 dkralph 1.2 void AddHist1D(TH1F *h, TString drawopt="", int color=kBlack, int linesty=1, int fillsty=0, int linecolor=0);
68 dkralph 1.1 void AddHist1D(TH1F *h, TString label, TString drawopt, int color=kBlack, int linesty=1, int fillsty=0);
69     void AddHist1D(TFile *f, TString histName, TString drawopt="", int color=kBlack, int linesty=1, int fillsty=0);
70     void AddHist1D(TFile *f, TString histName, TString label, TString drawopt, int color=kBlack, int linesty=1, int fillsty=0);
71    
72     // Adding a 1D histogram to a histogram stack
73     void AddToStack(TH1F *h, int color);
74 dkralph 1.2 void AddToStack(TH1F *h, TString label, int color, int linecolor=1,
75     int linestyle=1, int linewidth=0, int fillstyle=1001);
76 dkralph 1.1 void AddToStack(TFile *f, TString histName, int color);
77     void AddToStack(TFile *f, TString histName, TString label, int color);
78    
79     // Adding a 2D histogram to the plot
80 dkralph 1.2 void AddHist2D(TH2D *h, TString drawopt="", int fillcolor=kWhite, int linecolor=kBlack);
81 dkralph 1.1 void AddHist2D(TFile *f, TString histName, TString drawopt="", int fillcolor=kWhite, int linecolor=kBlack);
82    
83     // Adding a graph (with error bars) to the plot
84     void AddGraph(TGraph *gr, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
85     void AddGraph(TGraph *gr, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
86     void AddGraph(TFile *f, TString grName, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
87     void AddGraph(TFile *f, TString grName, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
88    
89     // Adding a profile histogram to the plot
90     void AddProfile(TProfile *gr, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
91     void AddProfile(TProfile *gr, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
92     void AddProfile(TFile *f, TString prName, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
93     void AddProfile(TFile *f, TString prName, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
94    
95     // Adding a text box to the plot
96     void AddTextBox(TString text, double x1, double y1, double x2, double y2,
97     int bordersize=1, int textcolor=kBlack, int fillcolor=kWhite);
98     void AddTextBox(double x1, double y1, double x2, double y2,
99     int bordersize, int textcolor, int fillcolor, int nlines, ...);
100    
101     // Add a line between two points (x1,y1) and (x2,y2)
102     void AddLine(double x1, double y1, double x2, double y2, int color=kBlack, int style=1);
103     void AddLine(double x1, double y1, double x2, double y2, int color, int style, TString label);
104    
105     // Add a box with coordinates: bottom left (x1,y1), top right (x2,y2)
106     void AddBox(double x1, double y1, double x2, double y2, int linecolor=kBlack, int linesty=1, int fillcolor=kWhite);
107     void AddBox(double x1, double y1, double x2, double y2, int linecolor, int linesty, int fillcolor, TString label);
108    
109     // Add a 1D function
110     void AddFcn(TF1* fcn, int color=kBlack, int linesty=1);
111     void AddFcn(TF1* fcn, TString label, int color=kBlack, int linesty=1);
112    
113     // Set legend position
114     void SetLegend(double x1, double y1, double x2, double y2) {
115     assert(fLeg);
116     fLeg->SetX1(x1); fLeg->SetY1(y1); fLeg->SetX2(x2); fLeg->SetY2(y2);
117     }
118     // Translate legend box
119     void TransLegend(double dx, double dy) {
120     assert(fLeg);
121     fLeg->SetX1(fLeg->GetX1()+dx); fLeg->SetY1(fLeg->GetY1()+dy);
122     fLeg->SetX2(fLeg->GetX2()+dx); fLeg->SetY2(fLeg->GetY2()+dy);
123     }
124    
125     // Set stats box position
126     void SetStats(double x, double y) { fStatsX = x; fStatsY = y; }
127    
128     // Translate stats box
129     void TransStats(double dx, double dy) { fStatsX += dx; fStatsY += dy; }
130    
131     //
132     // Set general properties of the plot
133     //
134     void SetName(TString str) { fName = str; } // plot name (for output)
135     void SetTitle(TString str) { fTitle = str; } // plot title
136     void SetXTitle(TString str) { fXTitle = str; } // x-axis title
137     void SetYTitle(TString str) { fYTitle = str; } // y-axis title
138     void SetXRange(double xmin, double xmax) { fXmin = xmin; fXmax = xmax; } // x-axis range
139     void SetYRange(double ymin, double ymax) { fYmin = ymin; fYmax = ymax; } // y-axis range
140     void SetLogx(int value=1) { fLogx = value; } // toggle logscale x-axis
141     void SetLogy(int value=1) { fLogy = value; } // toggle logscale y-axis
142     void SetGridx(bool value=1) { fGridx = value; } // toggle grid lines from x-axis ticks
143     void SetGridy(bool value=1) { fGridy = value; } // toggle grid lines from y-axis ticks
144     void Rebin(int ngroup) { fRebin = ngroup; } // 1D histogram re-bin
145     void ShowStats(int show=111) { fShowStats = show; } // display statistics
146    
147     //
148     // Accessors
149     //
150     TLegend* GetLegend() { return fLeg; }
151     THStack* GetStack() { return fStack; }
152    
153     protected:
154     vector<CPlotItem> fItems; // list of items to be plotted
155     vector<TPaveText*> fTextBoxes; // list of text boxes
156     vector<TLine*> fLines; // list of lines
157     vector<TBox*> fBoxes; // list of boxes
158     vector<TF1*> fFcns; // list of 1D functions
159     THStack *fStack; // histogram stack
160     TString fName; // plot name
161     TString fTitle; // plot title
162     TString fXTitle; // x-axis title
163     TString fYTitle; // y-axis title
164     double fXmin, fXmax; // x-axis range
165     double fYmin, fYmax; // y-axis range
166     int fLogx, fLogy; // logscale axes
167     int fGridx, fGridy; // grid lines
168     int fRebin; // grouping for histogram re-bin
169     TLegend *fLeg; // legend object
170     int fShowStats; // whether to display statistics
171     double fStatsX, fStatsY; // x,y coordinates of top left corner of stats box
172    
173     vector<TLegendEntry*> fStackEntries; // pointer to legend entry objects for histograms in a stack
174    
175     RooPlot *fRooPlot;
176    
177     static int sCount; // number of CPlot instances
178     };
179    
180     int CPlot::sCount = 0;
181     TString CPlot::sOutDir = ".";
182    
183     #endif