-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormatGraphs.cxx
116 lines (103 loc) · 3.01 KB
/
FormatGraphs.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "GeneratorStudy.h"
#include "FormatGraphs.h"
/**
* main function
* Arguments are <root file> <config file (optional)>
*/
int main(int argc, char **argv)
{
gStyle->SetOptStat(0);
string graphfilename="";
if (argc < 2)
{
cout<<"Usage: "<<argv[0]<<" <ROOT file containing graphs> <summary title>"<<endl;
return -1;
}
graphfilename=argv[1];
if (! (graphfilename.find(".root") + 5 == graphfilename.length() && graphfilename.length() > 4))
{
cout<<"Not a root file "<<graphfilename<<endl;
return -1;
}
string title=graphfilename;
if (argc > 2)
title=argv[2];
TFile *graphFile = new TFile(graphfilename.c_str());
if (graphFile->IsZombie())
{
cout<<"No valid ROOT file given. Filename was "<<graphfilename<<endl;
return -1;
}
string plotfilename = graphfilename;
plotfilename.replace(graphfilename.length()-5,5,".png");
cout<<"Processing plots from "<<graphfilename<<" ("<<title<<") and writing to "<<plotfilename<<endl;
std::vector<TGraph*> graphs;
TList* list = graphFile->GetListOfKeys() ;
TIter next(list) ;
TKey* key ;
TObject* obj ;
while (( key = (TKey*)next() )) {
obj = key->ReadObj() ;
if ( obj->InheritsFrom("TGraph")) // It's a graph
{
graphs.push_back((TGraph*)obj);
}
}
double max=0;
int biggestGraph=0;
std::vector<int> colors = LoadColors();
TLegend *legend=new TLegend(0.1,0.5,0.48,0.9);
for (int i=0;i<graphs.size();i++)
{
TGraph *graph=graphs.at(i);
cout<<graph->GetName()<<": "<<graph->GetHistogram()->GetMaximum()<<endl;
if (graph->GetHistogram()->GetMaximum() > max)
{
max=graph->GetHistogram()->GetMaximum();
biggestGraph=i;
}
graph->SetLineColor(colors.at(i));
graph->SetLineWidth(2);
graph->GetYaxis()->SetRangeUser(0,max);
graph->GetYaxis()->SetTitleOffset(1.5);
graph->GetXaxis()->SetTitle("Percent resolution (sigma) at 1 MeV");
string legendName = graph->GetName(); // Strip isotope prefix
int underscorepos = legendName.find("_");
legendName = legendName.substr(underscorepos + 1);
legend->AddEntry(graph,legendName.c_str(),"l");
graph->SetTitle(title.c_str());
}
cout<<graphs.at(biggestGraph)->GetName()<<" has the biggest max: "<<max<<endl;
TCanvas *c = new TCanvas("c",title.c_str(),900,600);
graphs.at(biggestGraph)->Draw();
legend->Draw();
for (int i=0;i<graphs.size();i++)
{
if (i!=biggestGraph)
{
graphs.at(i)->Draw("SAME");
}
}
c->SetTitle(title.c_str());
c->SaveAs(plotfilename.c_str());
return 0;
}
std::vector<int>LoadColors()
{
std::vector<int> colors;
colors.push_back(kRed);
colors.push_back(kBlue+2);
colors.push_back(kGreen+3);
colors.push_back(kMagenta+3);
colors.push_back(kOrange+1);
colors.push_back(kCyan-2);
colors.push_back(kPink+10);
colors.push_back(kViolet-8);
colors.push_back(kOrange+3);
colors.push_back(kRed+3);
colors.push_back(kBlue-7);
colors.push_back(kSpring-1);
colors.push_back(kGray+2);
colors.push_back(kPink-9);
return colors;
}