-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileMergeObjects.cxx
164 lines (140 loc) · 5.09 KB
/
FileMergeObjects.cxx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "FileMergeObjects.h"
#include <stdexcept>
#include <iostream>
#include <memory>
#include "TDirectory.h"
#include "TFile.h"
#include "TList.h"
#include "TKey.h"
#include "TH1.h"
#include "AGMergeHelpers.h"
//================================================================
FileMergeObjects::FileMergeObjects(const std::string& filename, double weight)
: Named("")
{
TFile infile(filename.c_str(), "READ");
if(!infile.IsOpen()) {
throw std::runtime_error("FileMergeObjects(): can't open input file \""+filename+"\"");
}
std::cout<<"FileMergeObjects(): initializing from file "<<filename
<<", weight="<<weight
<<std::endl;
init(infile, weight);
infile.Close();
}
//================================================================
FileMergeObjects::FileMergeObjects(TDirectory& dir, double weight)
: Named(dir.GetName(), dir.GetTitle())
{
//std::cout<<"FileMergeObjects ctr from dir "<<dir.GetName()<<std::endl;
init(dir, weight);
}
//================================================================
void FileMergeObjects::init(TDirectory& dir, double weight) {
TList *keyList = dir.GetListOfKeys();
if(keyList) {
//std::cerr<<"Printing key list of length "<<keyList->GetSize()<<std::endl;
//keyList->Print();
//----------------------------------------------------------------
TIter it(keyList);
TKey *key(0);
while( (key = dynamic_cast<TKey*>(it())) ) {
// std::cout<<"init(): key name = "<<key->GetName()<<", title = "<<key->GetTitle()<<std::endl;
std::auto_ptr<TObject> obj(dir.Get(key->GetName()));
TDirectory *subdir = dynamic_cast<TDirectory*>(obj.get());
if(subdir) {
// std::cout<<"init(): adding subdir for key name = "<<key->GetName()<<", title = "<<key->GetTitle()<<std::endl;
m_subDirs.push_back(FileMergeObjects(*subdir,weight));
} // subdir
else {
TH1 *hist = dynamic_cast<TH1*>(obj.get());
if(hist) {
hist->SetDirectory(0);
//tmp tmp tmp tmp
// FIXME:
//hist->Sumw2();
agmerge::applyWeight(hist, weight);
m_hists.push_back(hist);
obj.release();
} // hist
else {
std::cout<<"WARNING: ignoring non-TH1 object for key name = "
<<key->GetName()<<", title = "<<key->GetTitle()
<<std::endl;
}
} // not subdir
} // while(key)
}
else {
throw std::runtime_error("FileMergeObjects(): NULL key list from TDirectory::GetListOfKeys()");
}
}
//================================================================
void FileMergeObjects::addFile(const std::string& filename, double weight) {
std::cout<<"FileMergeObjects: adding file "<<filename
<<", weight = "<<weight
<<std::endl;
TFile f(filename.c_str(), "READ");
addDirectory(f, weight);
f.Close();
}
//================================================================
void FileMergeObjects::addDirectory(TDirectory& dir, double weight) {
ChdirTemp wd(&dir);
//std::cout<<"FileMergeObjects: adding directory "<<dir.GetName()<<std::endl;
//----------------------------------------------------------------
for(DirList::iterator i = m_subDirs.begin(); i!=m_subDirs.end(); ++i) {
const std::string subdirname(i->getDirName());
//std::cout<<"Getting subdir "<<subdirname<<std::endl;
TDirectory *subdir = dynamic_cast<TDirectory*>(dir.Get(subdirname.c_str()));
if(subdir) {
i->addDirectory(*subdir, weight);
}
else {
throw std::runtime_error("FileMergeObjects::addDirectory(): NULL for subdir \""+subdirname+"\"");
}
}
//----------------------------------------------------------------
for(HistList::iterator i = m_hists.begin(); i!=m_hists.end(); ++i) {
const std::string histname((*i)->GetName());
//std::cout<<"Getting histo "<<histname<<std::endl;
std::auto_ptr<TH1> hh(dynamic_cast<TH1*>(dir.Get(histname.c_str())));
if(hh.get()) {
// was: (*i)->Add(*i, hh.get(), 1., weight);
agmerge::addTo(*i, hh.get(), weight);
}
else {
throw std::runtime_error("FileMergeObjects::addDirectory(): NULL for histo \""+histname+"\"");
}
}
//----------------
wd.cd_back();
}
//================================================================
void FileMergeObjects::write(TDirectory *dir) const {
ChdirTemp wd(dir);
TDirectory *subdir(dir);
//----------------------------------------------------------------
if(getDirName().length()) {
subdir = dir->mkdir(getDirName().c_str(), getDirTitle().c_str());
if(!subdir) {
throw std::runtime_error("FileMergeObjects::write(): Could not do TDirectory::mkdir()");
}
if(subdir->cd() != kTRUE) {
throw std::runtime_error("FileMergeObjects::write(): Could not cd() to subdir");
}
}
//----------------------------------------------------------------
for(DirList::const_iterator i=m_subDirs.begin(); i!=m_subDirs.end(); ++i) {
if(subdir->cd() != kTRUE) {
throw std::runtime_error("FileMergeObjects::write(): Could not cd() to subdir");
}
i->write(subdir);
}
for(HistList::const_iterator i=m_hists.begin(); i!=m_hists.end(); ++i) {
(*i)->Write();
}
wd.cd_back();
}
//================================================================
//EOF