CMS 3D CMS Logo

FWConfigurationManager.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWConfigurationManager
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Sun Feb 24 14:42:32 EST 2008
11 //
12 
13 // system include files
14 #include <fstream>
15 #include <iostream>
16 #include <memory>
17 #include <stdexcept>
18 #include "TROOT.h"
19 #include "TSystem.h"
20 #include "TStopwatch.h"
21 
22 // user include files
31 
32 //
33 // constants, enums and typedefs
34 //
35 
36 //
37 // static data member definitions
38 //
39 
40 //
41 // constructors and destructor
42 //
44 
45 // FWConfigurationManager::FWConfigurationManager(const FWConfigurationManager& rhs)
46 // {
47 // // do actual copying here;
48 // }
49 
51 
52 //
53 // assignment operators
54 //
55 // const FWConfigurationManager& FWConfigurationManager::operator=(const FWConfigurationManager& rhs)
56 // {
57 // //An exception safe implementation is
58 // FWConfigurationManager temp(rhs);
59 // swap(rhs);
60 //
61 // return *this;
62 // }
63 
64 //
65 // member functions
66 //
68  assert(nullptr != iConf);
69  m_configurables[iName] = iConf;
70 }
71 
72 //
73 // const member functions
74 //
76  assert(nullptr != iConfig.keyValues());
77  for (FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(), itEnd = iConfig.keyValues()->end();
78  it != itEnd;
79  ++it) {
80  std::map<std::string, FWConfigurable*>::const_iterator itFound = m_configurables.find(it->first);
81  assert(itFound != m_configurables.end());
82  itFound->second->setFrom(it->second);
83  }
84 }
85 
88  for (std::map<std::string, FWConfigurable*>::const_iterator it = m_configurables.begin(),
89  itEnd = m_configurables.end();
90  it != itEnd;
91  ++it) {
92  it->second->addTo(config);
93  oConfig.addKeyValue(it->first, config, true);
94  }
95 }
96 
98  try {
99  std::ofstream file(iName.c_str());
100  if (not file) {
101  std::string message = "unable to open file " + iName;
102  message += iName;
103  throw std::runtime_error(message.c_str());
104  }
105  FWConfiguration top;
106  to(top);
107  fwLog(fwlog::kInfo) << "Writing to file " << iName.c_str() << "...\n";
108  fflush(stdout);
109 
110  FWConfiguration::streamTo(file, top, "top");
111  } catch (std::runtime_error& e) {
112  fwLog(fwlog::kError) << "FWConfigurationManager::writeToFile() " << e.what() << std::endl;
113  }
114 }
115 
117  Int_t error = 0;
118  // Int_t value =
119  gROOT->LoadMacro(iName.c_str(), &error);
120  if (0 != error) {
121  std::string message("unable to load macro file ");
122  message += iName;
123  throw std::runtime_error(message.c_str());
124  }
125 
126  const std::string command("(Long_t)(fwConfig() )");
127 
128  error = 0;
129  Long_t lConfig = gROOT->ProcessLineFast(command.c_str(), &error);
130 
131  {
132  //need to unload this macro so that we can load a new configuration
133  // which uses the same function name in the macro
134  Int_t error = 0;
135  gROOT->ProcessLineSync((std::string(".U ") + iName).c_str(), &error);
136  }
137  if (0 != error) {
138  std::string message("unable to properly parse configuration file ");
139  message += iName;
140  throw std::runtime_error(message.c_str());
141  }
142  std::unique_ptr<FWConfiguration> config(reinterpret_cast<FWConfiguration*>(lConfig));
143 
144  setFrom(*config);
145 }
146 
154  std::ifstream f(iName.c_str());
155  if (f.peek() != (int)'<')
156  return readFromOldFile(iName);
157 
158  // Check that the syntax is correct.
159  SimpleSAXParser syntaxTest(f);
160  syntaxTest.parse();
161  f.close();
162 
163  // Read again, this time actually parse.
164  std::ifstream g(iName.c_str());
165  // Actually parse the results.
167  parser.parse();
168  setFrom(*parser.config());
169 }
170 
172  struct CMatch {
174  int cnt;
175  const FWConfiguration* cfg;
176 
177  CMatch(std::string f) : file(f), cnt(0), cfg(nullptr) {}
178  bool operator<(const CMatch& x) const { return cnt < x.cnt; }
179  };
180 
181  std::vector<CMatch> clist;
182  clist.push_back(CMatch("reco.fwc"));
183  clist.push_back(CMatch("miniaod.fwc"));
184  clist.push_back(CMatch("aod.fwc"));
185  std::vector<FWJobMetadataManager::Data>& sdata = dataMng->usableData();
186 
187  for (std::vector<CMatch>::iterator c = clist.begin(); c != clist.end(); ++c) {
188  std::string iName = gSystem->Which(TROOT::GetMacroPath(), c->file.c_str(), kReadPermission);
189  std::ifstream f(iName.c_str());
190  if (f.peek() != (int)'<') {
191  fwLog(fwlog::kWarning) << "FWConfigurationManager::guessAndReadFromFile can't open " << iName << std::endl;
192  continue;
193  }
194 
195  // Read again, this time actually parse.
196  std::ifstream g(iName.c_str());
198  parser->parse();
199 
200  c->cfg = parser->config();
201  const FWConfiguration::KeyValues* keyValues = nullptr;
202  for (FWConfiguration::KeyValues::const_iterator it = c->cfg->keyValues()->begin(),
203  itEnd = c->cfg->keyValues()->end();
204  it != itEnd;
205  ++it) {
206  if (it->first == "EventItems") {
207  keyValues = it->second.keyValues();
208  break;
209  }
210  }
211 
212  for (FWConfiguration::KeyValues::const_iterator it = keyValues->begin(); it != keyValues->end(); ++it) {
213  const FWConfiguration& conf = it->second;
214  const FWConfiguration::KeyValues* keyValues = conf.keyValues();
215  const std::string& type = (*keyValues)[0].second.value();
216  for (std::vector<FWJobMetadataManager::Data>::iterator di = sdata.begin(); di != sdata.end(); ++di) {
217  if (di->type_ == type) {
218  c->cnt++;
219  break;
220  }
221  }
222  }
223  // printf("%s file %d matches\n", iName.c_str(), c->cnt);
224  }
225  std::sort(clist.begin(), clist.end());
226  fwLog(fwlog::kInfo) << "Loading configuration file " << clist.back().file << std::endl;
227  setFrom(*(clist.back().cfg));
228 
229  return clist.back().file;
230 }
231 
232 //
233 // static member functions
234 //
type
Definition: HCALResponse.h:21
void to(FWConfiguration &) const
const KeyValues * keyValues() const
void writeToFile(const std::string &) const
void readFromOldFile(const std::string &) const
void readFromFile(const std::string &) const
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
config
Definition: looper.py:291
void setFrom(const FWConfiguration &) const
double f[11][100]
std::vector< Data > & usableData()
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
std::string guessAndReadFromFile(FWJobMetadataManager *) const
#define fwLog(_level_)
Definition: fwLog.h:45
void add(const std::string &iName, FWConfigurable *)
does not take ownership
list command
Definition: mps_check.py:25
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:69
static void streamTo(std::ostream &oTo, const FWConfiguration &iConfig, const std::string &name)
std::map< std::string, FWConfigurable * > m_configurables