ATLAS Offline Software
Loading...
Searching...
No Matches
computils.h File Reference
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include "label.h"
#include "utils.h"
#include "DrawLabel.h"
#include "TStyle.h"
#include "TPad.h"
#include "TH1D.h"
#include "TFile.h"
#include "TH1.h"
#include "TGraphAsymmErrors.h"
#include "TLegend.h"
#include <cmath>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  data_mismatch
class  true_mean
class  AxisInfo
 class to store information about axes, limits, whether it is log or linear scale etc More...
class  Legend
 slightly more convenient legend class More...
class  tPlotter< T >
 generic plotter class - better to have one of these - make sure it can be configured however you like, line styles, marker types, legends etc now a template so can be used for TH1D and TH2D etc More...
class  Plots
 set of generic plots More...
class  HistDetails
 details of the histogram axes etc More...
class  Panel

Typedefs

typedef tPlotter< TH1F > Plotter

Functions

void ATLASFORAPP_LABEL (double x, double y, int color, double size=0.06)
void myText (Double_t x, Double_t y, Color_t color, const std::string &text, Double_t tsize)
std::string stime ()
 return the current data and time
double integral (TH1 *h)
void Norm (TH1 *h, double scale=1)
double Entries (TH1 *h)
bool contains (const std::string &s, const std::string &p)
 does a string contain the substring
bool contains (const std::string &s, char p) noexcept
bool fcontains (const std::string &s, const std::string &p)
 does a string contain the substring at the beginning of the string
bool exists (const std::string &filename)
 does a file exist
std::string tail (std::string s, const std::string &pattern)
 tail of a string
std::string head (std::string s, const std::string &pattern)
 head of a string
std::string globbed (const std::string &s)
 match a file name
void contents (std::vector< std::string > &keys, TDirectory *td, const std::string &directory="", const std::string &pattern="", const std::string &path="")
void contents (std::vector< std::string > &keys, TDirectory *td, const std::string &directory="", const std::vector< std::string > &patterns=std::vector< std::string >(), const std::string &path="")
double realmax (TH1 *h, bool include_error=true, double lo=0, double hi=0)
double realmin (TH1 *h, bool include_error=true, double lo=0, double hi=0)
std::string findcell (std::string name, const std::string &regex, const std::string &splitex="/")
std::string findrun (TFile *f)
double plotable (TH1 *h)
template<typename T>
std::ostream & operator<< (std::ostream &s, std::vector< T > &v)
std::vector< int > findxrange (TH1 *h, bool symmetric=false)
 automatically set the xrange on a histogram
std::vector< double > findxrangeuser (TH1 *h, bool symmetric=false)
void trim_tgraph (TH1 *h, TGraphAsymmErrors *t)
void xrange (TH1 *h, bool symmetric=true)
void copyReleaseInfo (TFile *finput, TFile *foutdir)
 copy the TTree of release info from one directory to another
std::ostream & operator<< (std::ostream &s, const AxisInfo &a)
template<typename T>
void setParameters (T *h, TGraphAsymmErrors *tg)
template<typename T>
void zeroErrors (T *h)
bool empty (TH1 *h)
void hminus (TH1 *h)
std::ostream & operator<< (std::ostream &s, const HistDetails &h)
std::ostream & operator<< (std::ostream &s, const Panel &p)

Variables

bool LINEF
bool LINES
static std::string release
int colours [6]
int markers [6]
double msizes [6]
template<typename T>
bool tPlotter< T >::s_plotref = true
 use non c++17 format for improved external compatability
template<typename T>
bool tPlotter< T >::s_meanplotref = true

Detailed Description

Author
mark sutton
Date
Sat Aug 30 2014 14:38:03 CEST

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

Definition in file computils.h.

Typedef Documentation

◆ Plotter

typedef tPlotter<TH1F> Plotter

Definition at line 886 of file computils.h.

Function Documentation

◆ ATLASFORAPP_LABEL()

void ATLASFORAPP_LABEL ( double x,
double y,
int color,
double size = 0.06 )

Definition at line 188 of file computils.cxx.

189{
190 TLatex* lat = new TLatex(); //lat.SetTextAlign(12); lat.SetTextSize(tsize);
191 lat->SetNDC();
192 lat->SetTextFont(72);
193 lat->SetTextColor(color);
194 lat->SetTextSize(size);
195 lat->DrawLatex(x,y,"ATLAS");
196
197 TLatex* lat2 = new TLatex(); //lat.SetTextAlign(12); lat.SetTextSize(tsize);
198 lat2->SetNDC();
199 lat2->SetTextFont(52);
200 lat2->SetTextColor(color);
201 lat2->SetTextSize(size); // this 0.13 should really be calculated as a ratio of the width of the canvas
202 lat2->DrawLatex(x+0.13,y,"For Approval");
203}
#define y
#define x

◆ contains() [1/2]

bool contains ( const std::string & s,
char p )
noexcept

Definition at line 39 of file chainparser.cxx.

39 {
40 return s.find( c )!=std::string::npos;
41}

◆ contains() [2/2]

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

does a string contain the substring

does a string contain the substring

Definition at line 114 of file hcg.cxx.

114{ return s.find(regx)!=std::string::npos; }

◆ contents() [1/2]

void contents ( std::vector< std::string > & keys,
TDirectory * td,
const std::string & directory = "",
const std::string & pattern = "",
const std::string & path = "" )

Definition at line 321 of file computils.cxx.

322 {
323 std::vector<std::string> patterns;
324 patterns.push_back(pattern);
325 contents( keys, td, directory, patterns, path );
326}
void contents(std::vector< std::string > &keys, TDirectory *td, const std::string &directory, const std::string &pattern, const std::string &path)
std::vector< std::string > patterns
Definition listroot.cxx:187

◆ contents() [2/2]

void contents ( std::vector< std::string > & keys,
TDirectory * td,
const std::string & directory = "",
const std::vector< std::string > & patterns = std::vector< std::string >(),
const std::string & path = "" )

directory, cd to it ...

test to see whether we are searhing for a specific directory name not a directory so include this ...

Definition at line 331 of file computils.cxx.

332 {
333
334 bool print = false;
335
336 TList* tl = td->GetListOfKeys();
337
338 for ( int i=tl->GetSize() ; i-- ; ) {
339
340 TKey* tobj = (TKey*)tl->At(i);
341
342 if ( tobj==0 ) continue;
343
344 if ( std::string(tobj->GetClassName()).find("TDirectory")!=std::string::npos ) {
345
346 TDirectory* tnd = (TDirectory*)tobj->ReadObj();
347
349 std::string dname = tnd->GetName();
350
351 std::string newpath = path+dname+"/";
352 contents( keys, tnd, directory, patterns, newpath );
353
354 }
355 else {
358 if ( directory == "" || contains( path, directory ) ) {
359
360
361 bool matched = true;
362 for ( size_t i=patterns.size() ; i-- ; ) {
363 const std::string& pattern = patterns[i];
364 if ( contains(std::string(tobj->GetName()), pattern ) ) matched &=true;
365 else matched = false;
366 }
367 if ( matched ) {
368 //coverity[DEADCODE]
369 if ( print ) std::cout << "will process " << td->GetName() << " \t:: " << tobj->GetName() << "\tpatterns: " << patterns.size() << std::endl;
370 print = false;
371 keys.push_back( path+tobj->GetName() );
372 }
373 }
374 }
375 }
376}
void print(char *figname, TCanvas *c1)
bool contains(const std::string &s, const std::string &p)
contains a string
path
python interpreter configuration --------------------------------------—
Definition athena.py:126

◆ copyReleaseInfo()

void copyReleaseInfo ( TFile * finput,
TFile * foutdir )

copy the TTree of release info from one directory to another

copy the TTree of release info from one directory to another

Definition at line 621 of file computils.cxx.

621 {
622
623 std::vector<std::string> release_data;
624
625 if ( finput && foutdir ) {
626
627 TTree* tree = (TTree*)finput->Get("dataTree");
628 TTree* clone = tree->CloneTree();
629
630 foutdir->cd();
631 clone->Write("", TObject::kOverwrite);
632
633 delete clone;
634
635 }
636
637}
TChain * tree

◆ empty()

bool empty ( TH1 * h)

Definition at line 296 of file computils.cxx.

296 {
297 for ( int i=h->GetNbinsX() ; i>0 ; i-- ) if ( h->GetBinContent(i)!=0 ) return false;
298 return true;
299}
Header file for AthHistogramAlgorithm.

◆ Entries()

double Entries ( TH1 * h)

Definition at line 52 of file computils.cxx.

52 {
53 double n = 0;
54 for ( int i=h->GetNbinsX()+1 ; --i ; ) n += h->GetBinContent(i);
55 return n;
56}

◆ exists()

bool exists ( const std::string & filename)

does a file exist

Definition at line 256 of file computils.cxx.

256 {
257 struct stat sb;
258 if ( stat( filename.c_str(), &sb)==0 ) return true; // && S_ISREG(sb.st_mode ))
259 else return false;
260}

◆ fcontains()

bool fcontains ( const std::string & s,
const std::string & p )

does a string contain the substring at the beginning of the string

does a string contain the substring at the beginning of the string

Definition at line 234 of file computils.cxx.

234 {
235 return (s.find(p)==0);
236}

◆ findcell()

std::string findcell ( std::string name,
const std::string & regex,
const std::string & splitex = "/" )

Definition at line 548 of file computils.cxx.

548 {
549
550 size_t posex = name.find( regex );
551
552 if ( posex==std::string::npos ) return "";
553
554 size_t pos = name.find_last_of( splitex );
555
556 std::string duff = name;
557
558 while ( pos!=std::string::npos && pos>posex+regex.size() ) {
559 name.resize(pos); //pos must be <=string length
560 pos = name.find_last_of( splitex );
561 }
562
563 pos = name.find( regex );
564
565 name = name.substr( pos, name.size() );
566
567 pos = name.find( splitex );
568
569 if ( pos!=std::string::npos ) return name.substr( 0, pos );
570
571 return name;
572}

◆ findrun()

std::string findrun ( TFile * f)

go through sub directories

Definition at line 576 of file computils.cxx.

576 {
577
578 TDirectory* here = gDirectory;
579
580 f->cd();
581
582 std::cout << "gDirectory::GetName() " << gDirectory->GetName() << std::endl;
583
584 // gDirectory->pwd();
585
586 // gDirectory->ls();
587
588 TList* tl = gDirectory->GetListOfKeys();
589
591
592 for ( int i=0 ; i<tl->GetSize() ; i++ ) {
593
594 TKey* tobj = (TKey*)tl->At(i);
595
596 if ( tobj==0 ) continue;
597
598 if ( std::string(tobj->GetClassName()).find("TDirectory")!=std::string::npos ) {
599
600 TDirectory* tnd = (TDirectory*)tobj->ReadObj();
601
602 std::string name = tnd->GetName();
603
604 if ( name.find( "run_" )==0 ) {
605 here->cd();
606 return name;
607 }
608 }
609 }
610
611 here->cd();
612
613 return "";
614}

◆ findxrange()

std::vector< int > findxrange ( TH1 * h,
bool symmetric = false )

automatically set the xrange on a histogram

zoom on non-empty bins

Definition at line 449 of file computils.cxx.

449 {
450
451 int ilo = 1;
452 int ihi = h->GetNbinsX();
453
454 h->GetXaxis()->SetRange( ilo, ihi );
455
456 std::vector<int> limits(2,0);
457 limits[0] = ilo;
458 limits[1] = ihi;
459
460 if ( empty(h) ) return limits;
461
462#if 1
463
465 for ( ; ilo<=ihi ; ilo++ ) if ( h->GetBinContent(ilo)!=0 ) break;
466 for ( ; ihi>=ilo ; ihi-- ) if ( h->GetBinContent(ihi)!=0 ) break;
467
468#else
469
471
473
474 double icont = 1/content;
475
476 double flo = 0;
477 double fhi = 0;
478 for ( ; ilo<=ihi ; ilo++ ) {
479 flo += h->GetBinContent(ilo);
480 if ( (flo*icont)>0.0005 ) break;
481 }
482
483 for ( ; ihi>=ilo ; ihi-- ) {
484 fhi += h->GetBinContent(ihi);
485 if ( (fhi*icont)>0.0005 ) break;
486 }
487
488#endif
489
490 int delta_lo = ilo-1;
491 int delta_hi = h->GetNbinsX()-ihi;
492
493 if ( symmetric ) {
494 if ( delta_hi<delta_lo ) {
495 limits[0] = 1+delta_hi;
496 limits[1] = ihi;
497 }
498 else {
499 limits[0] = 1+delta_lo;
500 limits[1] = h->GetNbinsX()-delta_lo;
501 }
502 }
503 else {
504
505 if ( ilo>1 ) ilo--;
506 if ( ihi<h->GetNbinsX() ) ihi++;
507
508 limits[0] = ilo;
509 limits[1] = ihi;
510 }
511
512 return limits;
513
514}
static const Attributes_t empty
str content
Definition grepfile.py:56

◆ findxrangeuser()

std::vector< double > findxrangeuser ( TH1 * h,
bool symmetric = false )

Definition at line 526 of file computils.cxx.

526 {
527
528 std::vector<int> limits = findxrange( h, symmetric );
529
530 std::vector<double> dlimits(2,0);
531
532 double dx = h->GetBinLowEdge(limits[1]+1)-h->GetBinLowEdge(limits[1]);
533
534 dlimits[0] = h->GetBinLowEdge(limits[0]);
535 dlimits[1] = h->GetBinLowEdge(limits[1]+1)-dx*1e-11;
536
537 return dlimits;
538}
std::vector< int > findxrange(TH1 *h, bool symmetric)
automatically set the xrange on a histogram

◆ globbed()

std::string globbed ( const std::string & s)

match a file name

glob for a file based on the pattern, then return the name of the first matching file

Definition at line 264 of file computils.cxx.

264 {
267
268 glob_t glob_result;
269 glob( s.c_str(), GLOB_TILDE, 0, &glob_result );
270
271 std::vector<std::string> ret;
272 for( unsigned i=0 ; i<glob_result.gl_pathc ; i++ ){
273 ret.push_back( std::string(glob_result.gl_pathv[i]) );
274 }
275 globfree(&glob_result);
276
277
278 if ( ret.empty() ) {
279 std::cerr << "no matching file: " << s << std::endl;
280 return "";
281 }
282
283 if ( ret.size()>1 ) {
284 for ( unsigned i=0 ; i<ret.size() ; i++ ) {
285 std::cout << "matched " << ret[i] << std::endl;
286 }
287 }
288
289 return ret[0];
290}

◆ head()

std::string head ( std::string s,
const std::string & pattern )

head of a string

Definition at line 312 of file computils.cxx.

312 {
313 size_t pos = s.find_last_of(pattern);
314 if ( pos != std::string::npos ) {
315 s.erase( pos, s.size() );
316 }
317 return s;
318}

◆ hminus()

void hminus ( TH1 * h)
inline

Definition at line 903 of file computils.h.

903 {
904 std::cout << __FUNCTION__ << std::endl;
905 for ( int i=1 ; i<=h->GetNbinsX() ; i++ ) {
906 double duff = h->GetBinContent(i);
907 if ( duff<0 ) {
908 std::cout<< "\t\t" << __FUNCTION__ << " " << h->GetName() << " " << i << " " << h->GetBinContent(i) << " " << (duff*1e6) << std::endl;
909 }
910 }
911 h->DrawCopy();
912 gPad->Print( (std::string("duff-")+h->GetName()+".pdf").c_str() );
913}

◆ integral()

double integral ( TH1 * h)

Definition at line 59 of file computils.cxx.

59 {
60 double n=0;
61 for ( int i=h->GetNbinsX() ; i>0 ; i-- ) n += h->GetBinContent(i);
62 return n;
63}

◆ myText()

void myText ( Double_t x,
Double_t y,
Color_t color,
const std::string & text,
Double_t tsize )

Definition at line 205 of file computils.cxx.

205 {
206
207 //Double_t tsize=0.05;
208 TLatex lat; lat.SetTextAlign(12); lat.SetTextSize(tsize);
209 lat.SetNDC();
210 lat.SetTextColor(color);
211 lat.DrawLatex(x,y,text.c_str());
212}

◆ Norm()

void Norm ( TH1 * h,
double scale = 1 )

Definition at line 67 of file computils.cxx.

67 {
68 double n = 0;
69 for ( int i=h->GetNbinsX()+2 ; --i ; ) n += h->GetBinContent(i);
70 if ( n!=0 ) {
71 double in=scale/n;
72 for ( int i=h->GetNbinsX()+2 ; --i ; ) {
73 h->SetBinContent(i, h->GetBinContent(i)*in );
74 h->SetBinError(i, h->GetBinError(i)*in );
75 }
76 }
77
78}

◆ operator<<() [1/4]

std::ostream & operator<< ( std::ostream & s,
const AxisInfo & a )
inline

Definition at line 322 of file computils.h.

322 {
323 s << "[ " << a.tag() << ( a.log() ? " : log" : "" ) << " ";
324 if ( a.autoset() ) s << " : auto";
325 else if ( a.rangeset() ) s << " : range " << a.lo() << " - " << a.hi();
326 s << " ]";
327 return s;
328}
static Double_t a

◆ operator<<() [2/4]

std::ostream & operator<< ( std::ostream & s,
const HistDetails & h )
inline

Definition at line 1316 of file computils.h.

1316 {
1317 return s << "[ " << h.name() << " \tx: \"" << h.xtitle() << "\" " << h.xaxis() << "\t\ty: \"" << h.ytitle() << "\" " << h.yaxis() << " ]";
1318}

◆ operator<<() [3/4]

std::ostream & operator<< ( std::ostream & s,
const Panel & p )
inline

Definition at line 1375 of file computils.h.

1375 {
1376 s << "Panel: " << p.name();
1377 if ( p.size() == 1 ) s << "\t" << p[0];
1378 else for ( size_t i=0 ; i<p.size() ; i++ ) s << "\n\t" << p[i];
1379 return s;
1380}

◆ operator<<() [4/4]

template<typename T>
std::ostream & operator<< ( std::ostream & s,
std::vector< T > & v )

Definition at line 109 of file computils.h.

109 {
110 for ( unsigned i=0 ; i<v.size() ; i++ ) s << "\t" << v[i];
111 return s;
112}

◆ plotable()

double plotable ( TH1 * h)

Definition at line 239 of file computils.cxx.

239 { // , double xlo, double xhi ) {
240 double n = 0;
241
242 double xlo = h->GetBinLowEdge(1);
243 double xhi = h->GetBinLowEdge(h->GetNbinsX()+1);
244
245 // if ( xlo!=-999 ) _xlo = xlo;
246 // if ( xhi!=-999 ) _xhi = xhi;
247
248 for ( int i=h->GetNbinsX()+1 ; --i ; ) {
249 if ( h->GetBinCenter(i)>xlo && h->GetBinCenter(i)<xhi ) n += h->GetBinContent(i);
250 }
251 return n;
252}

◆ realmax()

double realmax ( TH1 * h,
bool include_error = true,
double lo = 0,
double hi = 0 )

Definition at line 382 of file computils.cxx.

382 {
383 double rm = 0;
384 if ( h->GetNbinsX()==0 ) return 0;
385
386 bool first = true;
387 for ( int i=1 ; i<=h->GetNbinsX() ; i++ ) {
388
389 if ( lo!=hi ) {
390 double c = h->GetBinCenter(i);
391 if ( lo>c || hi<c ) continue;
392 }
393
394 double re = h->GetBinContent(i);
395 if ( include_error ) re += h->GetBinError(i);
396 if ( re!=0 ) {
397 if ( first || rm<re ) {
398 rm = re;
399 first = false;
400 }
401 }
402 }
403
404 return rm;
405}
const boost::regex re(r_e)
bool first
Definition DeMoScan.py:534

◆ realmin()

double realmin ( TH1 * h,
bool include_error = true,
double lo = 0,
double hi = 0 )

Definition at line 409 of file computils.cxx.

409 {
410
411 if ( h->GetNbinsX()==0 ) return 0;
412
413 double rm = 0;
414
415 bool first = true;
416 for ( int i=1 ; i<=h->GetNbinsX() ; i++ ) {
417
418 if ( lo!=hi ) {
419 double c = h->GetBinCenter(i);
420 if ( lo>c || hi<c ) continue;
421 }
422
423 double re = h->GetBinContent(i);
424
425 if ( re!=0 ) {
426 // if ( include_error ) re -= h->GetBinError(i);
427 if ( first || rm>re ) rm = re;
428 first = false;
429 }
430 }
431
432 return rm;
433}

◆ setParameters()

template<typename T>
void setParameters ( T * h,
TGraphAsymmErrors * tg )

Definition at line 436 of file computils.h.

436 {
437 tg->SetLineColor( h->GetLineColor() );
438 tg->SetMarkerColor( h->GetMarkerColor() );
439 tg->SetMarkerStyle( h->GetMarkerStyle() );
440 tg->SetMarkerSize( h->GetMarkerSize() );
441 tg->SetLineWidth( h->GetLineWidth() );
442 tg->SetLineStyle( h->GetLineStyle() );
443}

◆ stime()

std::string stime ( )

return the current data and time

Definition at line 215 of file computils.cxx.

215 {
216 time_t t;
217 time(&t);
218 return label("%s", ctime(&t) );
219}
std::string label(const std::string &format, int i)
Definition label.h:19
time(flags, cells_name, *args, **kw)

◆ tail()

std::string tail ( std::string s,
const std::string & pattern )

tail of a string

Definition at line 302 of file computils.cxx.

302 {
303 size_t pos = s.find(pattern);
304 while ( pos != std::string::npos ) {
305 s.erase( 0, pos+1 );
306 pos = s.find(pattern);
307 }
308 return s;
309}

◆ trim_tgraph()

void trim_tgraph ( TH1 * h,
TGraphAsymmErrors * t )

Definition at line 153 of file computils.cxx.

153 {
154
155 double ylo = h->GetMinimum();
156
157 int ih=1;
158
159 for ( int i=0 ; i<t->GetN() && ih<=h->GetNbinsX() ; i++, ih++ ) {
160
161 double yt = 0;
162 double xt = 0;
163 double ye = 0;
164
165 t->GetPoint( i, xt, yt );
166 ye = t->GetErrorYlow( i );
167
168 double yh = h->GetBinContent(ih);
169 double xh = h->GetBinCenter(ih);
170
171 while( !almost_equal( xh, xt, 5 ) && ih<=h->GetNbinsX() ) {
172 ih++;
173 yh = h->GetBinContent(ih);
174 xh = h->GetBinCenter(ih);
175 }
176
177 if ( !almost_equal( yh, yt, 5 ) ) throw data_mismatch(std::string("for histogram ")+h->GetName());
178
179 if ( (yt-ye) < ylo ) {
180 h->SetBinContent(ih, ylo-100 );
181 t->SetPoint( i, xt, ylo-100 );
182 }
183
184 }
185}
#define xt
#define yt
bool almost_equal(floaty_t a, floaty_t b, int max_diff)

◆ xrange()

void xrange ( TH1 * h,
bool symmetric = true )

Definition at line 518 of file computils.cxx.

518 {
519 std::vector<int> limits = findxrange( h, symmetric );
520 h->GetXaxis()->SetRange( limits[0], limits[1] );
521}

◆ zeroErrors()

template<typename T>
void zeroErrors ( T * h)

Definition at line 447 of file computils.h.

447 {
448 for ( int i=1 ; i<=h->GetNbinsX() ; i++ ) h->SetBinError( i, 1e-100 );
449}

Variable Documentation

◆ colours

int colours[6]
extern

Definition at line 46 of file computils.cxx.

46{ 1, 2, kBlue-4, 6, kCyan-2, kMagenta+2 };

◆ LINEF

bool LINEF
extern

Definition at line 40 of file computils.cxx.

◆ LINES

bool LINES
extern

Definition at line 41 of file computils.cxx.

◆ markers

int markers[6]
extern

Definition at line 47 of file computils.cxx.

47{ 20, 24, 25, 26, 25, 22 };

◆ msizes

double msizes[6]
extern

Definition at line 48 of file computils.cxx.

48{ 0.85, 1, 1, 1, 1, 1 };

◆ release

std::string release
static

Definition at line 50 of file computils.h.

◆ tPlotter< T >::s_meanplotref

template<typename T>
bool tPlotter< T >::s_meanplotref = true

Definition at line 895 of file computils.h.

◆ tPlotter< T >::s_plotref

template<typename T>
bool tPlotter< T >::s_plotref = true

use non c++17 format for improved external compatability

Definition at line 891 of file computils.h.