ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RootMacros/plotHistogramsTogether.C
(Generate patch)

Comparing UserCode/RootMacros/plotHistogramsTogether.C (file contents):
Revision 1.2 by anderson, Wed Oct 22 03:43:43 2008 UTC vs.
Revision 1.6 by anderson, Thu Jul 2 19:44:38 2009 UTC

# Line 7 | Line 7
7    
8    Can be run from a bash prompt as well:
9      root -b -l -q "plotHistogramsTogether.C(\"fileA.root\",\"fileB.root\")"
10 +    root -b -l -q "plotHistogramsTogether.C(\"fileA.root\",\"fileB.root\",\"Signal\",\"Background\",10,2,1)"
11  
12    Michael B. Anderson
13    Sept 5, 2008
# Line 24 | Line 25 | TFile   *sourceFile1, *sourceFile2;
25   TString label1, label2;
26   TString outputFolder,outputFilename;
27   TCanvas *canvasDefault;
28 <
28 > Float_t scale1, scale2;
29 > bool showStatsBoxes;
30  
31   // *******************************************
32   // Variables
33   TString imageType = "gif";
34   int outputWidth   = 480;
35   int outputHeight  = 360;
36 + bool yAxisLogScale = false;
37   // End of Variables
38   // *******************************************
39  
# Line 38 | Line 41 | int outputHeight  = 360;
41   void plotHistogramsTogether(TString fileName1,
42                              TString fileName2,
43                              TString fileLabel1 = "",
44 <                            TString fileLabel2 = "") {
44 >                            TString fileLabel2 = "",
45 >                            Float_t fileScale1 = 1.0,
46 >                            Float_t fileScale2 = 1.0,
47 >                            bool showStats = false) {
48  
49 +  // If file labels were not given as argument,
50 +  // use the filename as a label
51    if (fileLabel1 == "") {
52      fileLabel1 = fileName1;
53      fileLabel2 = fileName2;
54      fileLabel1.ReplaceAll(".root","");
55      fileLabel1.ReplaceAll(".root","");
56    }
57 <  label1 = fileLabel1;
57 >  label1 = fileLabel1;
58    label2 = fileLabel2;
59  
60 +  // Set the scale of the histograms.
61 +  // If they are < 0.0, they will be area normalized
62 +  scale1 = fileScale1;
63 +  scale2 = fileScale2;
64 +  showStatsBoxes = showStats;
65 +
66    sourceFile1 = TFile::Open( fileName1 );
67    sourceFile2 = TFile::Open( fileName2 );
68  
# Line 60 | Line 74 | void plotHistogramsTogether(TString file
74  
75    canvasDefault = new TCanvas("canvasDefault","testCanvas",outputWidth,outputHeight);
76  
77 <  // Plot all histograms against eachother
77 >  // This function will plot all histograms from
78 >  // file1 against matching histogram from file2
79    recurseOverKeys(sourceFile1);
80  
81    sourceFile1->Close();
# Line 123 | Line 138 | void recurseOverKeys( TDirectory *target
138  
139   void plot2Histograms(TH1* htemp1, TH1* htemp2, TString filename) {
140  
141 <  TString title = htemp1->GetName();
141 >  //TString title = htemp1->GetName();
142 >  TString title = htemp1->GetTitle();
143 >
144 >  // Make sure histograms exist
145 >  if ( !htemp2 ) {
146 >    cout << "Histogram missing from 2nd file: " << htemp1->GetName() << endl;
147 >    return;
148 >  }
149 >
150 >  // Scale by given factor.
151 >  // If given factor is negative, area normalize
152 >  if (scale1 > 0.0) {
153 >    htemp1->Scale(scale1);
154 >  } else {
155 >    Double_t integral = htemp1->Integral();
156 >    if (integral>0.0) htemp1->Scale(1/integral);
157 >  }
158 >  if (scale2 > 0.0) {
159 >    htemp2->Scale(scale2);
160 >  } else {
161 >    Double_t integral = htemp2->Integral();
162 >    if (integral>0.0) htemp2->Scale(1/integral);
163 >  }
164  
165    // Set the histogram colors & lines
166    htemp1->SetLineColor(kRed);
# Line 131 | Line 168 | void plot2Histograms(TH1* htemp1, TH1* h
168    htemp1->SetLineWidth(1);
169    htemp2->SetLineWidth(2);
170  
171 <  // Turn off Stats Box
172 <  gStyle->SetOptStat(0);
171 >  // Turn off stats
172 >  if (!showStatsBoxes) {
173 >    gStyle->SetOptStat(0);
174 >  }
175  
176    // Create TStack but we will draw without stacking
177    THStack *tempStack = new THStack();
178 <  tempStack->Add(htemp1);
179 <  tempStack->Add(htemp2);
178 >  tempStack->Add(htemp1,"sames");
179 >  tempStack->Add(htemp2,"sames");
180  
181    // Draw the histogram and titles
182 <  tempStack->Draw();
182 >  tempStack->Draw("hist nostack");
183    tempStack->SetTitle(title);
184 <  //tempStack->GetXaxis()->SetTitle(titleX);
146 <  tempStack->Draw("nostack");
184 >  tempStack->GetXaxis()->SetTitle(htemp1->GetXaxis()->GetTitle());
185  
186    // Draw the legend
187 <  TLegend *infoBox = new TLegend(0.74, 0.74, 0.98, 0.98, "");
187 >  TLegend *infoBox = new TLegend(0.75, 0.83, 0.99, 0.99, "");
188    infoBox->AddEntry(htemp1,label1,"L");
189    infoBox->AddEntry(htemp2,label2,"L");
190 +  infoBox->SetShadowColor(0);  // 0 = transparent
191 +  infoBox->SetFillColor(kWhite);
192    infoBox->Draw();
193  
194 +  // Place the stats boxes to be non-overlapping
195 +  if (showStatsBoxes) {
196 +    canvasDefault->SetRightMargin(0.2);
197 +    canvasDefault->Update();
198 +    TPaveStats *st1 = (TPaveStats*)htemp1->GetListOfFunctions()->FindObject("stats");
199 +    TPaveStats *st2 = (TPaveStats*)htemp2->GetListOfFunctions()->FindObject("stats");
200 +    st1->SetX1NDC(.79); st1->SetX2NDC(.99);
201 +    st1->SetY1NDC(.6);  st1->SetY2NDC(.8);
202 +    st2->SetX1NDC(.79); st2->SetX2NDC(.99);
203 +    st2->SetY1NDC(.38); st2->SetY2NDC(.58);
204 +    canvasDefault->Modified();
205 +  }
206 +
207 +  // Set log y axis
208 +  if (yAxisLogScale) canvasDefault->SetLogy(1);
209    // Save the canvas
210    canvasDefault->SaveAs(filename);
211  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines