ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Common/CPlot.hh
Revision: 1.1
Committed: Sat Jun 4 14:09:52 2011 UTC (13 years, 11 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Log Message:
new

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     #include <TH2F.h>
27     #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     TH2F* hist2D;
46     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     void AddHist1D(TH1F *h, TString drawopt="", int color=kBlack, int linesty=1, int fillsty=0);
68     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     void AddToStack(TH1F *h, TString label, int color);
75     void AddToStack(TFile *f, TString histName, int color);
76     void AddToStack(TFile *f, TString histName, TString label, int color);
77    
78     // Adding a 2D histogram to the plot
79     void AddHist2D(TH2F *h, TString drawopt="", int fillcolor=kWhite, int linecolor=kBlack);
80     void AddHist2D(TFile *f, TString histName, TString drawopt="", int fillcolor=kWhite, int linecolor=kBlack);
81    
82     // Adding a graph (with error bars) to the plot
83     void AddGraph(TGraph *gr, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
84     void AddGraph(TGraph *gr, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
85     void AddGraph(TFile *f, TString grName, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
86     void AddGraph(TFile *f, TString grName, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
87    
88     // Adding a profile histogram to the plot
89     void AddProfile(TProfile *gr, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
90     void AddProfile(TProfile *gr, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
91     void AddProfile(TFile *f, TString prName, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
92     void AddProfile(TFile *f, TString prName, TString label, TString drawopt, int color=kBlack, int marksty=kFullDotLarge, int linesty=1);
93    
94     // Adding a text box to the plot
95     void AddTextBox(TString text, double x1, double y1, double x2, double y2,
96     int bordersize=1, int textcolor=kBlack, int fillcolor=kWhite);
97     void AddTextBox(double x1, double y1, double x2, double y2,
98     int bordersize, int textcolor, int fillcolor, int nlines, ...);
99    
100     // Add a line between two points (x1,y1) and (x2,y2)
101     void AddLine(double x1, double y1, double x2, double y2, int color=kBlack, int style=1);
102     void AddLine(double x1, double y1, double x2, double y2, int color, int style, TString label);
103    
104     // Add a box with coordinates: bottom left (x1,y1), top right (x2,y2)
105     void AddBox(double x1, double y1, double x2, double y2, int linecolor=kBlack, int linesty=1, int fillcolor=kWhite);
106     void AddBox(double x1, double y1, double x2, double y2, int linecolor, int linesty, int fillcolor, TString label);
107    
108     // Add a 1D function
109     void AddFcn(TF1* fcn, int color=kBlack, int linesty=1);
110     void AddFcn(TF1* fcn, TString label, int color=kBlack, int linesty=1);
111    
112     // Set legend position
113     void SetLegend(double x1, double y1, double x2, double y2) {
114     assert(fLeg);
115     fLeg->SetX1(x1); fLeg->SetY1(y1); fLeg->SetX2(x2); fLeg->SetY2(y2);
116     }
117     // Translate legend box
118     void TransLegend(double dx, double dy) {
119     assert(fLeg);
120     fLeg->SetX1(fLeg->GetX1()+dx); fLeg->SetY1(fLeg->GetY1()+dy);
121     fLeg->SetX2(fLeg->GetX2()+dx); fLeg->SetY2(fLeg->GetY2()+dy);
122     }
123    
124     // Set stats box position
125     void SetStats(double x, double y) { fStatsX = x; fStatsY = y; }
126    
127     // Translate stats box
128     void TransStats(double dx, double dy) { fStatsX += dx; fStatsY += dy; }
129    
130     //
131     // Set general properties of the plot
132     //
133     void SetName(TString str) { fName = str; } // plot name (for output)
134     void SetTitle(TString str) { fTitle = str; } // plot title
135     void SetXTitle(TString str) { fXTitle = str; } // x-axis title
136     void SetYTitle(TString str) { fYTitle = str; } // y-axis title
137     void SetXRange(double xmin, double xmax) { fXmin = xmin; fXmax = xmax; } // x-axis range
138     void SetYRange(double ymin, double ymax) { fYmin = ymin; fYmax = ymax; } // y-axis range
139     void SetLogx(int value=1) { fLogx = value; } // toggle logscale x-axis
140     void SetLogy(int value=1) { fLogy = value; } // toggle logscale y-axis
141     void SetGridx(bool value=1) { fGridx = value; } // toggle grid lines from x-axis ticks
142     void SetGridy(bool value=1) { fGridy = value; } // toggle grid lines from y-axis ticks
143     void Rebin(int ngroup) { fRebin = ngroup; } // 1D histogram re-bin
144     void ShowStats(int show=111) { fShowStats = show; } // display statistics
145    
146     //
147     // Accessors
148     //
149     TLegend* GetLegend() { return fLeg; }
150     THStack* GetStack() { return fStack; }
151    
152     protected:
153     vector<CPlotItem> fItems; // list of items to be plotted
154     vector<TPaveText*> fTextBoxes; // list of text boxes
155     vector<TLine*> fLines; // list of lines
156     vector<TBox*> fBoxes; // list of boxes
157     vector<TF1*> fFcns; // list of 1D functions
158     THStack *fStack; // histogram stack
159     TString fName; // plot name
160     TString fTitle; // plot title
161     TString fXTitle; // x-axis title
162     TString fYTitle; // y-axis title
163     double fXmin, fXmax; // x-axis range
164     double fYmin, fYmax; // y-axis range
165     int fLogx, fLogy; // logscale axes
166     int fGridx, fGridy; // grid lines
167     int fRebin; // grouping for histogram re-bin
168     TLegend *fLeg; // legend object
169     int fShowStats; // whether to display statistics
170     double fStatsX, fStatsY; // x,y coordinates of top left corner of stats box
171    
172     vector<TLegendEntry*> fStackEntries; // pointer to legend entry objects for histograms in a stack
173    
174     RooPlot *fRooPlot;
175    
176     static int sCount; // number of CPlot instances
177     };
178    
179     int CPlot::sCount = 0;
180     TString CPlot::sOutDir = ".";
181    
182     #endif