ATLAS Offline Software
Loading...
Searching...
No Matches
refit.cxx
Go to the documentation of this file.
1
13
14
15#include <iostream>
16#include <vector>
17#include <string>
18#include <stdio.h>
19
20#include "TKey.h"
21#include "TH1D.h"
22#include "TH2D.h"
23#include "TFile.h"
24#include "TClass.h"
25
26#include "simpletimer.h"
27
28
29#include "dataset.h"
30
32#include "Resplot.h"
33
35std::vector<TFile*> fptr;
36
37
39struct timeval global_timer;
40
41
43std::string depth = "";
44
46int ir=0;
47
48
49
50
51
52
53bool contains( const std::string& s, const std::string& regex ) {
54 return s.find( regex )!=std::string::npos;
55}
56
57
58bool handleEfficiency( TDirectory* tnd ) {
59
60 TList* list = tnd->GetListOfKeys();
61 // std::cout << "list size " << list->GetSize() << std::endl;
62
63 for ( int i=0 ; i<list->GetSize() ; i++ ) {
64
65 std::string name( list->At(i)->GetName() );
66
67 TKey* tobj = (TKey*)list->At(i);
68
69 TH1F* eff = 0;
70
71 if ( contains( tobj->GetClassName(), "TH1" ) ) {
72
73 if ( contains( name, "_eff" ) &&
74 ( !contains( name, "_eff_n" ) && !contains( name, "_eff_d" ) ) ) {
75
76 eff = (TH1F*)tobj->ReadObj();
77
78 std::string dname = name+"_d";
79 std::string nname = name+"_n";
80
81 TH1F* num = 0;
82 TH1F* den = 0;
83
84 for ( int j=0 ; j<list->GetSize() ; j++ ) {
85
86 const std::string name( list->At(j)->GetName() );
87
88 if ( name==dname ) den = (TH1F*)((TKey*)list->At(j))->ReadObj();
89 if ( name==nname ) num = (TH1F*)((TKey*)list->At(j))->ReadObj();
90
91 if ( den && num ) {
93 std::cout << "Efficiency " << eff->GetName() << std::endl;
94
95 std::string title = eff->GetTitle();
96
97 delete eff;
98
99 Efficiency1D e( num, den, name );
100 eff = e.Hist();
101 eff->SetName( name.c_str() );
102 eff->SetTitle( title.c_str() );
103
104 eff->Write("",TObject::kOverwrite);
105
106 break;
107 }
108 }
109
110 }
111
112 }
113
114 }
115
116 return true;
117}
118
119
120
121
122bool isResplot( TDirectory* tnd ) {
123
124 if ( tnd==0 ) return false;
125
126 static const std::string token[6] = { "1d", "2d", "mean", "sigma", "chi2", "fractional uncertainty" };
127 bool found[6] = { false, false, false, false, false, false };
128 int count = 0;
129
130 // tnd->pwd();
131 // tnd->ls();
132 // std::cout << "\tnkeys " << tnd->GetNkeys() << std::endl;
133 TList* list = tnd->GetListOfKeys();
134 // std::cout << "list size " << list->GetSize() << std::endl;
135
136 for ( int i=0 ; i<list->GetSize() ; i++ ) {
137
138 std::string name( list->At( i )->GetName() );
139
140 // std::cout << "name " << name << std::endl;
141
142 for ( int j=0 ; j<6 ; j++ ) {
143 if ( !found[j] ) {
144 if ( name == token[j] ) {
145 count++;
146 found[j] = true;
147 break;
148 }
149 }
150 }
151
152 }
153
154 std::cout << "Resplot count " << count << " " << tnd->GetName() << std::endl;
155
156 if ( count == 6 ) return true;
157 else return false;
158}
159
160
161
162
163
165
166void search(TDirectory* td=0, const std::string& s="") {
167
168 ir++;
169
171
172 if ( ir>10 ) exit(0);
173
174
175
176 TDirectory* here = gDirectory;
177
178 // gDirectory->pwd();
179
180 std::string ns = s;
181
182 if ( ns!="" ) ns += "/";
183
184 if ( td ) {
185 gDirectory->cd(td->GetName());
186
187 ns += td->GetName();
188
189 std::cout << "\n" << depth << "Directory " << ns; // << std::endl;
190 }
191
192 std::string savedepth = depth;
193 depth += "\t";
194
195 // std::cout << ir << " " << ns << "TDirectory " << gDirectory->GetPath() << " " << gDirectory->GetName() << std::endl;
196
197 TList* tl = gDirectory->GetListOfKeys();
198
199 struct timeval tv = simpletimer_start();
200
201 // std::cout << "\ttl " << tl << std::endl;
202
203 handleEfficiency( gDirectory );
204
205 for ( int i=tl->GetSize() ; i-- ; ) {
206
207 TKey* tobj = (TKey*)tl->At(i);
208
209 if ( tobj==0 ) continue;
210
211 // std::cout << "tobj " << tobj;
212 // if ( tobj ) std::cout << " : \t" << tobj->GetName();
213 // std::cout << std::endl;
214
215 if ( std::string(tobj->GetClassName()).find("TDirectory")!=std::string::npos ) {
216 // std::cout << ns << "Directory " << tobj->GetName() << std::endl;
217
218 TDirectory* tnd = (TDirectory*)tobj->ReadObj();
219
220 if ( tnd ) {
221 bool res = isResplot( tnd );
222
223 if ( res ) {
224
225 tnd->ls();
226 tnd->pwd();
227
228 std::cout << "Resplot refitting " << tnd->GetName() << std::endl;
229 Resplot r( tnd->GetName() );
230
231 std::cout << "deleting ..." << std::endl;
232 gDirectory->Delete( (std::string(tnd->GetName())+";1").c_str() );
233
234 std::cout << "refitting ..." << std::endl;
235 r.Refit( Resplot::FitNull95 );
236
237
238 std::cout << "writing ..." << std::endl;
239
240 r.Write();
241 }
242 else search( tnd, ns );
243
244 }
245
246 }
247 // else {
248 // bool status = false;
249 //
250 // std::string objname = ns;
251 // if ( objname!="" ) objname += std::string("/") + tobj->GetName();
252 //
253 // if ( std::string(tobj->GetClassName()).find("TH1")!=std::string::npos ) std::cout << "objname " << objname.c_str() << std::endl;
254 //
255 // }
256
257 }
258
259 const double t = simpletimer_stop(tv);
260
261 const double global_time = simpletimer_stop(global_timer);
262
263
264 std::cout << "\tprocessed directory in " << t*0.001 << " s from " << global_time*0.001 << " s";
265
266
267 depth = std::move(savedepth);
268
269 ir--;
270
271 here->cd();
272
273}
274
275
276
277
278#include <cstdlib>
279
280
281
282bool file_exists(const std::string& s) {
283 if ( FILE* testfile=fopen(s.c_str(),"r") ) {
284 fclose(testfile);
285 return true;
286 }
287 return false;
288}
289
290
291
292
293void cost( const std::string& inputfile, const std::string& outputfile ) {
294
297
298 std::cout << "processing" << std::endl;
299
301 if ( file_exists(outputfile) ) {
302 std::cerr << "output file " << outputfile << " already exists" << std::endl;
303 return;
304 }
305
306 std::cout << "copying file to " << outputfile << std::endl;
307
309 system( ("cp "+inputfile+" "+outputfile).c_str() );
310
311
314
315 std::cout << "opening root files" << std::endl;
316
317
318 std::cout << "opening " << inputfile << std::endl;
319
320 fptr.resize(2);
321
323 fptr[0] = new TFile( outputfile.c_str(), "update" );
324
325 if ( fptr[0]==0 ) {
326 std::cerr << "cannot open " << outputfile.c_str() << std::endl;
327 return;
328 }
329
330 fptr[0]->cd();
331
332 TDirectory* here = gDirectory;
333
335
338 search();
339
340 here->cd();
341
342 std::cout << "closing the files" << std::endl;
343
345 // f0->Write();
346 fptr[0]->Close();
347
348}
349
350
351
352
353int usage(std::ostream& s, int argc, char** argv) {
354 if ( argc>0 ) {
355 s << "Usage: " << argv[0] << " [OPTIONS] -o output.root input.root \n\n";
356 s << " -o filename \toutput filename (required)\n";
357 s << " -h, --help \tdisplay this help\n";
358 s << std::endl;
359 } else {
360 s << "refit:usage() no arguments!\n";
361 }
362 return 0;
363}
364
365
366
367
368int main(int argc, char** argv) {
369
370 // if ( argc<3 ) usage( std::cerr << "not enough command options", argc, argv );
371 if ( argc<3 ) return usage( std::cerr, argc, argv );
372
373
374 for ( int i=1 ; i<argc ; i++ ) {
375 if ( std::string(argv[i])=="-h" || std::string(argv[i])=="--help" ) return usage( std::cout, argc, argv );
376 }
377
378 bool verbose = false;
379
380 std::string output_file = "";
381 std::string input_file = "";
382
383 for ( int i=1 ; i<argc ; i++ ) {
384 if ( std::string(argv[i])=="--verbose" ) verbose = true;
385 else if ( std::string(argv[i])=="-o" ) {
386 ++i;
387 if ( i<argc ) output_file = argv[i];
388 else return usage( std::cerr, argc, argv );
389 }
390 else {
391 if ( input_file=="" ) input_file = argv[i];
392 else return usage( std::cerr, argc, argv );
393 }
394 }
395
396
398 if ( output_file == "" ) return usage( std::cerr, argc, argv );
399 if ( input_file == "" ) return usage( std::cerr, argc, argv );
400
401 // dataset data("test_EF");
402 // files = data.datafiles();
403
405
406
407 // time the actual running of the cost() routine
408
409 if ( verbose ) std::cout << "timing" << std::endl;
410
411 struct timeval tv = simpletimer_start();
412
413 cost( input_file, output_file );
414
415 double t = simpletimer_stop(tv);
416
417 std::cout << "\ntotal time " << t*0.001 << " s (" << (t*0.001/60) << " min)" << std::endl;
418
419 return 0;
420}
ofstream outputfile
struct timeval simpletimer_start(void)
double simpletimer_stop(const struct timeval &start_time)
std::pair< std::vector< unsigned int >, bool > res
these functions have a precision of about 0.001 ms
static TF1 * FitNull95(TH1D *s, double a=0, double b=0)
Definition Resplot.cxx:1672
struct timeval global_timer
glabal timer - how long have I taken so far?
Definition fastadd.cxx:42
int ir
counter of the current depth
Definition fastadd.cxx:49
std::string depth
tag string for intendation
Definition fastadd.cxx:46
StatusCode usage()
int r
Definition globals.cxx:22
std::vector< TFile * > fptr
Definition hcg.cxx:51
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
bool verbose
Definition hcg.cxx:73
int main()
Definition hello.cxx:18
void search(TDirectory *td=0, const std::string &s="")
recursive directory search for TH1 and TH2
Definition refit.cxx:166
bool isResplot(TDirectory *tnd)
Definition refit.cxx:122
bool handleEfficiency(TDirectory *tnd)
Definition refit.cxx:58
bool contains(const std::string &s, const std::string &regex)
does a string contain the substring
Definition refit.cxx:53
bool file_exists(const std::string &s)
Definition refit.cxx:282
void cost(const std::string &inputfile, const std::string &outputfile)
Definition refit.cxx:293