1 |
rkogler |
1.1 |
#include <iostream>
|
2 |
|
|
#include <stdio.h>
|
3 |
|
|
#include <sys/stat.h>
|
4 |
|
|
#include <TSystem.h>
|
5 |
|
|
#include <TObjString.h>
|
6 |
|
|
#include <TKey.h>
|
7 |
|
|
#include <TDirectory.h>
|
8 |
|
|
#include <TClass.h>
|
9 |
|
|
|
10 |
|
|
#include "FileParser.h"
|
11 |
|
|
#include "SHist.h"
|
12 |
|
|
|
13 |
|
|
using namespace std;
|
14 |
|
|
|
15 |
|
|
FileParser::FileParser()
|
16 |
|
|
{
|
17 |
|
|
m_file = NULL;
|
18 |
|
|
m_hists = NULL;
|
19 |
|
|
debug = false;
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
FileParser::~FileParser()
|
23 |
|
|
{
|
24 |
|
|
if (m_file){
|
25 |
|
|
CloseFile();
|
26 |
|
|
}
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
void FileParser::CloseFile()
|
30 |
|
|
{
|
31 |
|
|
if (m_file){
|
32 |
|
|
m_file->Close();
|
33 |
|
|
delete m_file;
|
34 |
|
|
m_file = NULL;
|
35 |
|
|
}
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
bool FileParser::FileExists(TString filename)
|
39 |
|
|
{
|
40 |
|
|
struct stat buf;
|
41 |
|
|
if (stat(filename.Data(), &buf) != -1)
|
42 |
|
|
{
|
43 |
|
|
return true;
|
44 |
|
|
}
|
45 |
|
|
return false;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
void FileParser::OpenFile(TString fname, TString cyclename)
|
50 |
|
|
{
|
51 |
|
|
// open the root files with names given in the TObjArray
|
52 |
|
|
|
53 |
|
|
if (m_file != NULL){
|
54 |
|
|
cerr << "FileParser::OpenFile: Can not open new file, since file "
|
55 |
|
|
<< m_file->GetName() << " is still in memory. Abort." << endl;
|
56 |
|
|
exit(EXIT_FAILURE);
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
if (cyclename.Sizeof()!=0){
|
60 |
|
|
TString Prefix(cyclename);
|
61 |
|
|
Prefix.Append(".");
|
62 |
|
|
fname.Prepend(Prefix);
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
// check if name consists of a wildcard, if so use hadd to combine histograms
|
66 |
|
|
if (fname.Contains("*")){
|
67 |
|
|
TString target(fname);
|
68 |
|
|
target.ReplaceAll("*","");
|
69 |
|
|
|
70 |
|
|
// check if target exists, delete if yes
|
71 |
|
|
if (FileExists(target)){
|
72 |
|
|
if (debug) cout << "Target exists, removing file " << target << endl;
|
73 |
|
|
remove(target);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
TString command = "hadd " + target + " " + fname;
|
77 |
|
|
gSystem->Exec(command);
|
78 |
|
|
fname = target;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
if (debug) cout << "Opening file with name " << fname << "..." << endl;
|
82 |
|
|
m_file = new TFile(fname, "READ");
|
83 |
|
|
if (debug){
|
84 |
|
|
cout << "... success! pointer = " << m_file << endl;
|
85 |
|
|
cout << "name = " << m_file << endl;
|
86 |
|
|
cout << " is open? " << m_file->IsOpen() << endl;
|
87 |
|
|
m_file->ls();
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
if (!m_file->IsOpen()) {
|
91 |
|
|
cout << endl << "FileParser: File " << fname << " does not exist!!!" << endl;
|
92 |
|
|
exit(EXIT_FAILURE);
|
93 |
|
|
} else { // success!
|
94 |
|
|
cout << "FileParser: Successfully opened file " << fname << endl;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
StoreProcessName(fname);
|
98 |
|
|
|
99 |
|
|
// create a new TObjArray to store all histograms
|
100 |
|
|
m_hists = new TObjArray();
|
101 |
|
|
|
102 |
|
|
return;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
void FileParser::StoreProcessName(TString name)
|
106 |
|
|
{
|
107 |
|
|
|
108 |
|
|
TObjArray* pieces = name.Tokenize(".");
|
109 |
|
|
for (int i=0; i<pieces->GetEntries(); ++i){
|
110 |
|
|
TString piece = ((TObjString*)pieces->At(i))->GetString();
|
111 |
|
|
if (piece.CompareTo("root")==0){
|
112 |
|
|
m_process = ((TObjString*)pieces->At(i-1))->GetString();
|
113 |
|
|
if (debug) cout << "Process in file = " << m_process << endl;
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
TObjArray* FileParser::FindSubdirs()
|
119 |
|
|
{
|
120 |
|
|
// find all subdirectories (former histogram collections) in the open file
|
121 |
|
|
// returns a TObjArray with the names of the subdirectories
|
122 |
|
|
|
123 |
|
|
m_file->cd();
|
124 |
|
|
TObjArray* dirnames = new TObjArray();
|
125 |
|
|
TKey *key;
|
126 |
|
|
TIter nextkey( gDirectory->GetListOfKeys() );
|
127 |
|
|
while ( (key = (TKey*)nextkey())) {
|
128 |
|
|
TObject *obj = key->ReadObj();
|
129 |
|
|
if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) { // found a subdirectory!
|
130 |
|
|
TString dirname(((TDirectory*) obj)->GetName());
|
131 |
|
|
dirnames->Add(new TObjString(dirname));
|
132 |
|
|
if (debug) cout << "Found directory " << dirname << endl;
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
return dirnames;
|
136 |
|
|
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
void FileParser::BrowseFile()
|
140 |
|
|
{
|
141 |
|
|
|
142 |
|
|
if (!m_file){
|
143 |
|
|
cerr << "FileParser::BrowseFile: No file open. Abort." << endl;
|
144 |
|
|
exit(EXIT_FAILURE);
|
145 |
|
|
}
|
146 |
|
|
TObjArray* dirs = FindSubdirs();
|
147 |
|
|
|
148 |
|
|
// loop over all directories and get the histograms
|
149 |
|
|
for (Int_t i=0; i<dirs->GetEntries(); ++i){
|
150 |
|
|
|
151 |
|
|
TString dirname = ((TObjString*)dirs->At(i))->GetString();
|
152 |
|
|
if (debug) cout << "Getting all histograms from directory " << dirname << endl;
|
153 |
|
|
|
154 |
|
|
m_file->cd();
|
155 |
|
|
gDirectory->Cd(dirname);
|
156 |
|
|
|
157 |
|
|
// loop over all histograms in the directory and store them
|
158 |
|
|
TKey *key;
|
159 |
|
|
TIter nextkey( gDirectory->GetListOfKeys() );
|
160 |
|
|
while ( (key = (TKey*)nextkey())) {
|
161 |
|
|
|
162 |
|
|
TObject *obj = key->ReadObj();
|
163 |
|
|
|
164 |
|
|
if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
|
165 |
|
|
|
166 |
|
|
// histogram found
|
167 |
|
|
SHist* shist = new SHist((TH1*) obj);
|
168 |
|
|
shist->SetProcessName(m_process);
|
169 |
|
|
shist->SetDir(dirname);
|
170 |
|
|
if (debug) cout << "Adding hist " << shist->GetHist()->GetName()
|
171 |
|
|
<< " (process = " << m_process << ")" << endl;
|
172 |
|
|
m_hists->Add(shist);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
delete obj;
|
176 |
|
|
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
return;
|
182 |
|
|
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
void FileParser::SetInfo(TString legname, double weight, int colour, int marker)
|
186 |
|
|
{
|
187 |
|
|
|
188 |
|
|
for (int i=0; i<m_hists->GetEntries(); ++i){
|
189 |
|
|
SHist* sh = (SHist*)m_hists->At(i);
|
190 |
|
|
sh->SetLegName(legname);
|
191 |
|
|
sh->SetWeight(weight);
|
192 |
|
|
if (weight>0) sh->GetHist()->Scale(weight);
|
193 |
|
|
sh->GetHist()->SetMarkerColor(colour);
|
194 |
|
|
sh->GetHist()->SetLineColor(colour);
|
195 |
|
|
|
196 |
|
|
if (marker > 1 ){
|
197 |
|
|
sh->SetDrawMarker(true);
|
198 |
|
|
sh->GetHist()->SetMarkerStyle(marker);
|
199 |
|
|
} else {
|
200 |
|
|
sh->SetDrawMarker(false);
|
201 |
|
|
sh->GetHist()->SetMarkerStyle(0);
|
202 |
|
|
sh->GetHist()->SetMarkerSize(0);
|
203 |
|
|
sh->GetHist()->SetLineWidth(2);
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
// histogram is transparent if marker < 0
|
207 |
|
|
if (marker < 0 ){
|
208 |
|
|
// change line style
|
209 |
|
|
if (marker==-1) sh->GetHist()->SetLineStyle(kDashed);
|
210 |
|
|
if (marker==-2) sh->GetHist()->SetLineStyle(kDotted);
|
211 |
|
|
if (marker==-3) sh->GetHist()->SetLineStyle(kDashDotted);
|
212 |
|
|
if (marker==-4) sh->GetHist()->SetLineStyle(kDashDotted);
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
|
|
}
|