-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhistio.c
More file actions
146 lines (126 loc) · 3.73 KB
/
Copy pathhistio.c
File metadata and controls
146 lines (126 loc) · 3.73 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef histio_c
#define histio_c
#include "TClass.h"
#include "TList.h"
#include "TFile.h"
#include "TIterator.h"
#include "TRegexp.h"
#include "TDirectory.h"
#include "TObject.h"
#include "TSystem.h"
#include "TKey.h"
#include "TH1.h"
//#include "histio.h"
#include <iostream>
#include <iomanip>
void histio()
{
}
void saveHist(const char* filename, const char* pat, bool delete_hists = false )
{
printf(" Saving histograms in %s\n", filename ) ; fflush( stdout ) ;
TList* list = gDirectory->GetList() ;
TIterator* iter = list->MakeIterator();
TRegexp re(pat,kTRUE) ;
TFile outf(filename,"RECREATE") ;
TObject* obj ;
while((obj=iter->Next())) {
if (TString(obj->GetName()).Index(re)>=0) {
obj->Write() ;
std::cout << "." ;
// cout << setw(9) << counter++ << " : " << obj->GetName() << std::endl ;
}
}
std::cout << std::endl ;
outf.Close() ;
delete iter ;
if ( delete_hists ) {
printf("\n\n Deleting histograms.\n\n") ;
gDirectory->DeleteAll();//This line will remove histrograms from memory. Without this line, when we create a histogram, I will be saved everytime we call saveHist function. Important when we use "run_all.C"
} else {
printf("\n\n Not deleting histograms.\n\n") ;
}
}
//void loadHist(const char* filename, const char* pfx, const char* pat, Bool_t doAdd, Double_t scaleFactor)
void loadHist(const char* filename="in.root", const char* pfx=0, const char* pat="*", Bool_t doAdd=kFALSE, Double_t scaleFactor=-1.0)
{
cout << " Reading histograms from file: " << filename << endl ;
TFile inf(filename) ;
//inf.ReadAll() ;
if ( ! inf.IsOpen() ) { gSystem -> Exit(-1) ; }
TList* list = inf.GetListOfKeys() ;
TIterator* iter = list->MakeIterator();
TRegexp re(pat,kTRUE) ;
std::cout << "pat = " << pat << std::endl ;
gDirectory->cd("Rint:") ;
TObject* obj ;
TKey* key ;
std::cout << "doAdd = " << (doAdd?"T":"F") << std::endl ;
std::cout << "loadHist: reading." ;
while((key=(TKey*)iter->Next())) {
Int_t ridx = TString(key->GetName()).Index(re) ;
if (ridx==-1) {
continue ;
}
obj = inf.Get(key->GetName()) ;
TObject* clone ;
if (pfx) {
// Find existing TH1-derived objects
TObject* oldObj = 0 ;
if (doAdd){
//////oldObj = gDirectory->Get(Form("%s_%s",pfx,obj->GetName())) ;
oldObj = gDirectory->Get(Form("%s_%s",obj->GetName(),pfx)) ;
if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
oldObj = 0 ;
}
}
if (oldObj) {
clone = oldObj ;
//// ((TH1*)clone)->Add((TH1*)obj) ;
if ( scaleFactor > 0 ) {
((TH1*)clone)->Sumw2() ;
((TH1*)clone)->Add((TH1*)obj, scaleFactor) ;
} else {
((TH1*)clone)->Add((TH1*)obj) ;
}
} else {
/////clone = obj->Clone(Form("%s_%s",pfx,obj->GetName())) ;
clone = obj->Clone(Form("%s_%s",obj->GetName(),pfx)) ;
}
} else {
// Find existing TH1-derived objects
TObject* oldObj = 0 ;
if (doAdd){
oldObj = gDirectory->Get(key->GetName()) ;
if (oldObj && !oldObj->IsA()->InheritsFrom(TH1::Class())) {
oldObj = 0 ;
}
}
if (oldObj) {
clone = oldObj ;
///// ((TH1*)clone)->Add((TH1*)obj) ;
if ( scaleFactor > 0 ) {
((TH1*)clone)->Sumw2() ;
((TH1*)clone)->Add((TH1*)obj, scaleFactor) ;
} else {
((TH1*)clone)->Add((TH1*)obj) ;
}
} else {
clone = obj->Clone() ;
}
}
if ( scaleFactor > 0 && !doAdd ) {
((TH1*) clone)->Sumw2() ;
((TH1*) clone)->Scale(scaleFactor) ;
}
if (!gDirectory->GetList()->FindObject(clone)) {
gDirectory->Append(clone) ;
}
std::cout << "." ;
std::cout.flush() ;
}
std::cout << std::endl;
inf.Close() ;
delete iter ;
}
#endif