ATLAS Offline Software
Loading...
Searching...
No Matches
refit.cxx File Reference

navigates through the directory structure of a file copying the histograms or recalculating efficiencies, or refitting Resplots if it finds any More...

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include "TKey.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TFile.h"
#include "TClass.h"
#include "simpletimer.h"
#include "dataset.h"
#include "TrigInDetAnalysis/Efficiency1D.h"
#include "Resplot.h"
#include <cstdlib>

Go to the source code of this file.

Functions

bool contains (const std::string &s, const std::string &regex)
 does a string contain the substring
bool handleEfficiency (TDirectory *tnd)
bool isResplot (TDirectory *tnd)
void search (TDirectory *td=0, const std::string &s="")
 recursive directory search for TH1 and TH2
bool file_exists (const std::string &s)
void cost (const std::string &inputfile, const std::string &outputfile)
int usage (std::ostream &s, int argc, char **argv)
int main (int argc, char **argv)

Variables

std::vector< TFile * > fptr
 file names and file pointers
struct timeval global_timer
 glabal timer - how long have I taken so far?
std::string depth = ""
 tag string for intendation
int ir =0
 counter of the current depth

Detailed Description

navigates through the directory structure of a file copying the histograms or recalculating efficiencies, or refitting Resplots if it finds any

Author
mark sutton
Date
Thu 5 Sep 2013 20:32:47 CEST

Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration

Definition in file refit.cxx.

Function Documentation

◆ contains()

bool contains ( const std::string & s,
const std::string & regex )

does a string contain the substring

does a string contain the substring

Definition at line 53 of file refit.cxx.

53 {
54 return s.find( regex )!=std::string::npos;
55}

◆ cost()

void cost ( const std::string & inputfile,
const std::string & outputfile )

no point doing anything if there is only 1 input file because there would be nothing to add to it

don't over write esisting files

copy the first file so we add to this copy

don't bother to open the first file - we've copied it to the output and will update the contents

open the output file

recursively look through the directory structure

don't need to write the histogram again - writing them as we go along

Definition at line 293 of file refit.cxx.

293 {
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}
ofstream outputfile
struct timeval simpletimer_start(void)
struct timeval global_timer
glabal timer - how long have I taken so far?
Definition fastadd.cxx:42
std::vector< TFile * > fptr
Definition hcg.cxx:51
void search(TDirectory *td=0, const std::string &s="")
recursive directory search for TH1 and TH2
Definition refit.cxx:166
bool file_exists(const std::string &s)
Definition refit.cxx:282

◆ file_exists()

bool file_exists ( const std::string & s)

Definition at line 282 of file refit.cxx.

282 {
283 if ( FILE* testfile=fopen(s.c_str(),"r") ) {
284 fclose(testfile);
285 return true;
286 }
287 return false;
288}

◆ handleEfficiency()

bool handleEfficiency ( TDirectory * tnd)

hooray !!!

Definition at line 58 of file refit.cxx.

58 {
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}
list(name, path='/')
Definition histSizes.py:38
TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
bool contains(const std::string &s, const std::string &regex)
does a string contain the substring
Definition refit.cxx:53

◆ isResplot()

bool isResplot ( TDirectory * tnd)

Definition at line 122 of file refit.cxx.

122 {
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}
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146

◆ main()

int main ( int argc,
char ** argv )

if output file is not defined

check som input files

Definition at line 368 of file refit.cxx.

368 {
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}
double simpletimer_stop(const struct timeval &start_time)
StatusCode usage()
bool verbose
Definition hcg.cxx:73
void cost(const std::string &inputfile, const std::string &outputfile)
Definition refit.cxx:293

◆ search()

void search ( TDirectory * td = 0,
const std::string & s = "" )

recursive directory search for TH1 and TH2

don;t go more than 10 directories deep

Definition at line 166 of file refit.cxx.

166 {
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}
std::pair< std::vector< unsigned int >, bool > res
static TF1 * FitNull95(TH1D *s, double a=0, double b=0)
Definition Resplot.cxx:1673
int ir
counter of the current depth
Definition fastadd.cxx:49
std::string depth
tag string for intendation
Definition fastadd.cxx:46
int r
Definition globals.cxx:22
bool isResplot(TDirectory *tnd)
Definition refit.cxx:122
bool handleEfficiency(TDirectory *tnd)
Definition refit.cxx:58

◆ usage()

int usage ( std::ostream & s,
int argc,
char ** argv )

Definition at line 353 of file refit.cxx.

353 {
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}

Variable Documentation

◆ depth

std::string depth = ""

tag string for intendation

Definition at line 43 of file refit.cxx.

◆ fptr

std::vector<TFile*> fptr

file names and file pointers

Definition at line 35 of file refit.cxx.

◆ global_timer

struct timeval global_timer

glabal timer - how long have I taken so far?

Definition at line 39 of file refit.cxx.

◆ ir

int ir =0

counter of the current depth

Definition at line 46 of file refit.cxx.