1 |
/*************************************************
|
2 |
Automatically plots histograms from two files
|
3 |
onto the same plot and saves them.
|
4 |
It looksfor ALL histograms in the first file
|
5 |
and plots the corresponding histogram in the 2nd
|
6 |
file onto the sample plot.
|
7 |
|
8 |
Can be run from a bash prompt as well:
|
9 |
root -b -l -q "plotHistogramsTogether.C(\"fileA.root\",\"fileB.root\")"
|
10 |
|
11 |
Michael B. Anderson
|
12 |
Sept 5, 2008
|
13 |
*************************************************/
|
14 |
|
15 |
#include <string.h>
|
16 |
#include "TFile.h"
|
17 |
#include "TH1.h"
|
18 |
#include "TKey.h"
|
19 |
#include "Riostream.h"
|
20 |
|
21 |
// Accesable everywhere
|
22 |
TObject *obj;
|
23 |
TFile *sourceFile1, *sourceFile2;
|
24 |
TString label1, label2;
|
25 |
TString outputFolder,outputFilename;
|
26 |
TCanvas *canvasDefault;
|
27 |
|
28 |
|
29 |
// *******************************************
|
30 |
// Variables
|
31 |
TString imageType = "gif";
|
32 |
int outputWidth = 480;
|
33 |
int outputHeight = 360;
|
34 |
// End of Variables
|
35 |
// *******************************************
|
36 |
|
37 |
|
38 |
void plotHistogramsTogether(TString fileName1,
|
39 |
TString fileName2,
|
40 |
TString fileLabel1 = "",
|
41 |
TString fileLabel2 = "") {
|
42 |
|
43 |
if (fileLabel1 == "") {
|
44 |
fileLabel1 = fileName1;
|
45 |
fileLabel2 = fileName2;
|
46 |
fileLabel1.ReplaceAll(".root","");
|
47 |
fileLabel1.ReplaceAll(".root","");
|
48 |
}
|
49 |
label1 = fileLabel1;
|
50 |
label2 = fileLabel2;
|
51 |
|
52 |
sourceFile1 = TFile::Open( fileName1 );
|
53 |
sourceFile2 = TFile::Open( fileName2 );
|
54 |
|
55 |
outputFolder = "HistogramsTogether/"; // Blank to use current directory,
|
56 |
// or, for a specific dir type
|
57 |
// something like "images/"
|
58 |
|
59 |
gSystem->MakeDirectory(outputFolder);
|
60 |
|
61 |
canvasDefault = new TCanvas("canvasDefault","testCanvas",outputWidth,outputHeight);
|
62 |
|
63 |
// Plot all histograms against eachother
|
64 |
recurseOverKeys(sourceFile1);
|
65 |
|
66 |
sourceFile1->Close();
|
67 |
sourceFile2->Close();
|
68 |
|
69 |
TString currentDir = gSystem->pwd();
|
70 |
cout << "Done. See images in:" << endl << currentDir << "/" << outputFolder << endl;
|
71 |
}
|
72 |
|
73 |
void recurseOverKeys( TDirectory *target1 ) {
|
74 |
|
75 |
// Figure out where we are
|
76 |
TString path( (char*)strstr( target1->GetPath(), ":" ) );
|
77 |
path.Remove( 0, 2 );
|
78 |
|
79 |
sourceFile1->cd( path );
|
80 |
|
81 |
TDirectory *current_sourcedir = gDirectory;
|
82 |
|
83 |
TKey *key;
|
84 |
TIter nextkey(current_sourcedir->GetListOfKeys());
|
85 |
|
86 |
while (key = (TKey*)nextkey()) {
|
87 |
|
88 |
obj = key->ReadObj();
|
89 |
|
90 |
// Check if this is a 1D histogram or a directory
|
91 |
if (obj->IsA()->InheritsFrom("TH1F")) {
|
92 |
|
93 |
// **************************
|
94 |
// Plot & Save this Histogram
|
95 |
TH1F *htemp1, *htemp2;
|
96 |
|
97 |
htemp1 = (TH1F*)obj;
|
98 |
TString histName = htemp1->GetName();
|
99 |
|
100 |
if (path != "") {
|
101 |
sourceFile2->GetObject(path+"/"+histName, htemp2);
|
102 |
} else {
|
103 |
sourceFile2->GetObject(histName, htemp2);
|
104 |
}
|
105 |
|
106 |
outputFilename=histName;
|
107 |
plot2Histograms(htemp1, htemp2, outputFolder+path+"/"+outputFilename+"."+imageType);
|
108 |
|
109 |
} else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
|
110 |
// it's a subdirectory
|
111 |
|
112 |
cout << "Found subdirectory " << obj->GetName() << endl;
|
113 |
gSystem->MakeDirectory(outputFolder+path+"/"+obj->GetName());
|
114 |
|
115 |
// obj is now the starting point of another round of merging
|
116 |
// obj still knows its depth within the target file via
|
117 |
// GetPath(), so we can still figure out where we are in the recursion
|
118 |
recurseOverKeys( (TDirectory*)obj );
|
119 |
|
120 |
} // end of IF a TDriectory
|
121 |
}
|
122 |
}
|
123 |
|
124 |
void plot2Histograms(TH1* htemp1, TH1* htemp2, TString filename) {
|
125 |
|
126 |
TString title = htemp1->GetName();
|
127 |
|
128 |
// Set the histogram colors & lines
|
129 |
htemp1->SetLineColor(kRed);
|
130 |
htemp2->SetLineColor(kBlue);
|
131 |
htemp1->SetLineWidth(1);
|
132 |
htemp2->SetLineWidth(2);
|
133 |
|
134 |
// Turn off Stats Box
|
135 |
gStyle->SetOptStat(0);
|
136 |
|
137 |
// Create TStack but we will draw without stacking
|
138 |
THStack *tempStack = new THStack();
|
139 |
tempStack->Add(htemp1);
|
140 |
tempStack->Add(htemp2);
|
141 |
|
142 |
// Draw the histogram and titles
|
143 |
tempStack->Draw();
|
144 |
tempStack->SetTitle(title);
|
145 |
//tempStack->GetXaxis()->SetTitle(titleX);
|
146 |
tempStack->Draw("nostack");
|
147 |
|
148 |
// Draw the legend
|
149 |
TLegend *infoBox = new TLegend(0.74, 0.74, 0.98, 0.98, "");
|
150 |
infoBox->AddEntry(htemp1,label1,"L");
|
151 |
infoBox->AddEntry(htemp2,label2,"L");
|
152 |
infoBox->Draw();
|
153 |
|
154 |
// Save the canvas
|
155 |
canvasDefault->SaveAs(filename);
|
156 |
|
157 |
}
|
158 |
|