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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// -*- C++ -*-
//
// Package: Core
// Class : FWConfigurationManager
//
// Implementation:
// <Notes on implementation>
//
// Original Author: Chris Jones
// Created: Sun Feb 24 14:42:32 EST 2008
//
// system include files
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
#include "TROOT.h"
#include "TSystem.h"
#include "TStopwatch.h"
// user include files
#include "Fireworks/Core/interface/FWConfigurationManager.h"
#include "Fireworks/Core/interface/FWConfiguration.h"
#include "Fireworks/Core/interface/FWConfigurable.h"
#include "Fireworks/Core/interface/fwLog.h"
#include "Fireworks/Core/interface/SimpleSAXParser.h"
#include "Fireworks/Core/interface/FWJobMetadataManager.h"
#include "Fireworks/Core/interface/fwLog.h"
#include "Fireworks/Core/interface/FWXMLConfigParser.h"
//
// constants, enums and typedefs
//
//
// static data member definitions
//
//
// constructors and destructor
//
FWConfigurationManager::FWConfigurationManager() : m_ignore(false) {}
// FWConfigurationManager::FWConfigurationManager(const FWConfigurationManager& rhs)
// {
// // do actual copying here;
// }
FWConfigurationManager::~FWConfigurationManager() {}
//
// assignment operators
//
// const FWConfigurationManager& FWConfigurationManager::operator=(const FWConfigurationManager& rhs)
// {
// //An exception safe implementation is
// FWConfigurationManager temp(rhs);
// swap(rhs);
//
// return *this;
// }
//
// member functions
//
void FWConfigurationManager::add(const std::string& iName, FWConfigurable* iConf) {
assert(nullptr != iConf);
m_configurables[iName] = iConf;
}
//
// const member functions
//
void FWConfigurationManager::setFrom(const FWConfiguration& iConfig) const {
assert(nullptr != iConfig.keyValues());
for (FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(), itEnd = iConfig.keyValues()->end();
it != itEnd;
++it) {
std::map<std::string, FWConfigurable*>::const_iterator itFound = m_configurables.find(it->first);
assert(itFound != m_configurables.end());
itFound->second->setFrom(it->second);
}
}
void FWConfigurationManager::to(FWConfiguration& oConfig) const {
FWConfiguration config;
for (std::map<std::string, FWConfigurable*>::const_iterator it = m_configurables.begin(),
itEnd = m_configurables.end();
it != itEnd;
++it) {
it->second->addTo(config);
oConfig.addKeyValue(it->first, config, true);
}
}
void FWConfigurationManager::writeToFile(const std::string& iName) const {
try {
std::ofstream file(iName.c_str());
if (not file) {
std::string message = "unable to open file " + iName;
message += iName;
throw std::runtime_error(message.c_str());
}
FWConfiguration top;
to(top);
fwLog(fwlog::kInfo) << "Writing to file " << iName.c_str() << "...\n";
fflush(stdout);
FWConfiguration::streamTo(file, top, "top");
} catch (std::runtime_error& e) {
fwLog(fwlog::kError) << "FWConfigurationManager::writeToFile() " << e.what() << std::endl;
}
}
void FWConfigurationManager::readFromOldFile(const std::string& iName) const {
Int_t error = 0;
// Int_t value =
gROOT->LoadMacro(iName.c_str(), &error);
if (0 != error) {
std::string message("unable to load macro file ");
message += iName;
throw std::runtime_error(message.c_str());
}
const std::string command("(Long_t)(fwConfig() )");
error = 0;
Long_t lConfig = gROOT->ProcessLineFast(command.c_str(), &error);
{
//need to unload this macro so that we can load a new configuration
// which uses the same function name in the macro
Int_t error = 0;
gROOT->ProcessLineSync((std::string(".U ") + iName).c_str(), &error);
}
if (0 != error) {
std::string message("unable to properly parse configuration file ");
message += iName;
throw std::runtime_error(message.c_str());
}
std::unique_ptr<FWConfiguration> config(reinterpret_cast<FWConfiguration*>(lConfig));
setFrom(*config);
}
/** Reads the configuration specified in @a iName and creates the internal
representation in terms of FWConfigutation objects.
Notice that if the file does not start with '<' the old CINT macro based
system is used.
*/
void FWConfigurationManager::readFromFile(const std::string& iName) const {
std::ifstream f(iName.c_str());
if (f.peek() != (int)'<')
return readFromOldFile(iName);
// Check that the syntax is correct.
SimpleSAXParser syntaxTest(f);
syntaxTest.parse();
f.close();
// Read again, this time actually parse.
std::ifstream g(iName.c_str());
// Actually parse the results.
FWXMLConfigParser parser(g);
parser.parse();
setFrom(*parser.config());
}
std::string FWConfigurationManager::guessAndReadFromFile(FWJobMetadataManager* dataMng) const {
struct CMatch {
std::string file;
int cnt;
const FWConfiguration* cfg;
CMatch(std::string f) : file(f), cnt(0), cfg(nullptr) {}
bool operator<(const CMatch& x) const { return cnt < x.cnt; }
};
std::vector<CMatch> clist;
clist.push_back(CMatch("reco.fwc"));
clist.push_back(CMatch("miniaod.fwc"));
clist.push_back(CMatch("aod.fwc"));
std::vector<FWJobMetadataManager::Data>& sdata = dataMng->usableData();
for (std::vector<CMatch>::iterator c = clist.begin(); c != clist.end(); ++c) {
std::string iName = gSystem->Which(TROOT::GetMacroPath(), c->file.c_str(), kReadPermission);
std::ifstream f(iName.c_str());
if (f.peek() != (int)'<') {
fwLog(fwlog::kWarning) << "FWConfigurationManager::guessAndReadFromFile can't open " << iName << std::endl;
continue;
}
// Read again, this time actually parse.
std::ifstream g(iName.c_str());
FWXMLConfigParser* parser = new FWXMLConfigParser(g);
parser->parse();
c->cfg = parser->config();
const FWConfiguration::KeyValues* keyValues = nullptr;
for (FWConfiguration::KeyValues::const_iterator it = c->cfg->keyValues()->begin(),
itEnd = c->cfg->keyValues()->end();
it != itEnd;
++it) {
if (it->first == "EventItems") {
keyValues = it->second.keyValues();
break;
}
}
for (FWConfiguration::KeyValues::const_iterator it = keyValues->begin(); it != keyValues->end(); ++it) {
const FWConfiguration& conf = it->second;
const FWConfiguration::KeyValues* keyValues = conf.keyValues();
const std::string& type = (*keyValues)[0].second.value();
for (std::vector<FWJobMetadataManager::Data>::iterator di = sdata.begin(); di != sdata.end(); ++di) {
if (di->type_ == type) {
c->cnt++;
break;
}
}
}
// printf("%s file %d matches\n", iName.c_str(), c->cnt);
}
std::sort(clist.begin(), clist.end());
fwLog(fwlog::kInfo) << "Loading configuration file " << clist.back().file << std::endl;
setFrom(*(clist.back().cfg));
return clist.back().file;
}
//
// static member functions
//
|