CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 }
46 
47 // FWConfigurationManager::FWConfigurationManager(const FWConfigurationManager& rhs)
48 // {
49 // // do actual copying here;
50 // }
51 
53 {
54 }
55 
56 //
57 // assignment operators
58 //
59 // const FWConfigurationManager& FWConfigurationManager::operator=(const FWConfigurationManager& rhs)
60 // {
61 // //An exception safe implementation is
62 // FWConfigurationManager temp(rhs);
63 // swap(rhs);
64 //
65 // return *this;
66 // }
67 
68 //
69 // member functions
70 //
71 void
73 {
74  assert(0!=iConf);
75  m_configurables[iName]=iConf;
76 }
77 
78 //
79 // const member functions
80 //
81 void
83 {
84  assert(0!=iConfig.keyValues());
85  for(FWConfiguration::KeyValues::const_iterator it = iConfig.keyValues()->begin(),
86  itEnd = iConfig.keyValues()->end();
87  it != itEnd;
88  ++it) {
89  std::map<std::string,FWConfigurable*>::const_iterator itFound = m_configurables.find(it->first);
90  assert(itFound != m_configurables.end());
91  itFound->second->setFrom(it->second);
92  }
93 }
94 
95 void
97 {
99  for(std::map<std::string,FWConfigurable*>::const_iterator it = m_configurables.begin(),
100  itEnd = m_configurables.end();
101  it != itEnd;
102  ++it) {
103  it->second->addTo(config);
104  oConfig.addKeyValue(it->first, config, true);
105  }
106 }
107 
108 
109 void
111 {
112  try
113  {
114  std::ofstream file(iName.c_str());
115  if(not file) {
116  std::string message = "unable to open file " + iName;
117  message += iName;
118  throw std::runtime_error(message.c_str());
119  }
120  FWConfiguration top;
121  to(top);
122  fwLog(fwlog::kInfo) << "Writing to file "<< iName.c_str() << "...\n";
123  fflush(stdout);
124 
125  FWConfiguration::streamTo(file, top, "top");
126  }
127  catch (std::runtime_error &e)
128  {
129  fwLog(fwlog::kError) << "FWConfigurationManager::writeToFile() " << e.what() << std::endl;
130  }
131 }
132 
133 void
135 {
136  Int_t error=0;
137  // Int_t value =
138  gROOT->LoadMacro( iName.c_str(), &error );
139  if(0 != error) {
140  std::string message("unable to load macro file ");
141  message += iName;
142  throw std::runtime_error(message.c_str());
143  }
144 
145  const std::string command("(Long_t)(fwConfig() )");
146 
147  error = 0;
148  Long_t lConfig = gROOT->ProcessLineFast(command.c_str(),
149  &error);
150 
151  {
152  //need to unload this macro so that we can load a new configuration
153  // which uses the same function name in the macro
154  Int_t error = 0;
155  gROOT->ProcessLineSync((std::string(".U ")+iName).c_str(), &error);
156  }
157  if(0 != error) {
158  std::string message("unable to properly parse configuration file ");
159  message += iName;
160  throw std::runtime_error(message.c_str());
161  }
162  std::auto_ptr<FWConfiguration> config( reinterpret_cast<FWConfiguration*>(lConfig) );
163 
164  setFrom( *config);
165 }
166 
167 
174 void
176 {
177  std::ifstream f(iName.c_str());
178  if (f.peek() != (int) '<')
179  return readFromOldFile(iName);
180 
181  // Check that the syntax is correct.
182  SimpleSAXParser syntaxTest(f);
183  syntaxTest.parse();
184  f.close();
185 
186  // Read again, this time actually parse.
187  std::ifstream g(iName.c_str());
188  // Actually parse the results.
190  parser.parse();
191  setFrom(*parser.config());
192 }
193 
196 {
197  struct CMatch {
199  int cnt;
200  const FWConfiguration* cfg;
201 
202  CMatch(std::string f):file(f), cnt(0), cfg(0) {}
203  bool operator < (const CMatch& x) const { return cnt < x.cnt; }
204  };
205 
206  std::vector<CMatch> clist;
207  clist.push_back(CMatch("reco.fwc"));
208  clist.push_back(CMatch("miniaod.fwc"));
209  clist.push_back(CMatch("aod.fwc"));
210  std::vector<FWJobMetadataManager::Data> & sdata = dataMng->usableData();
211 
212  for (std::vector<CMatch>::iterator c = clist.begin(); c != clist.end(); ++c ) {
213  std::string iName = gSystem->Which(TROOT::GetMacroPath(), c->file.c_str(), kReadPermission);
214  std::ifstream f(iName.c_str());
215  if (f.peek() != (int) '<') {
216  fwLog(fwlog::kWarning) << "FWConfigurationManager::guessAndReadFromFile can't open "<< iName << std::endl ;
217  continue;
218  }
219 
220  // Read again, this time actually parse.
221  std::ifstream g(iName.c_str());
223  parser->parse();
224 
225  c->cfg = parser->config();
226  const FWConfiguration::KeyValues* keyValues = 0;
227  for(FWConfiguration::KeyValues::const_iterator it = c->cfg->keyValues()->begin(),
228  itEnd = c->cfg->keyValues()->end(); it != itEnd; ++it) {
229  if (it->first == "EventItems" ) {
230  keyValues = it->second.keyValues();
231  break;
232  }
233  }
234 
235  for (FWConfiguration::KeyValues::const_iterator it = keyValues->begin(); it != keyValues->end(); ++it)
236  {
237  const FWConfiguration& conf = it->second;
238  const FWConfiguration::KeyValues* keyValues = conf.keyValues();
239  const std::string& type = (*keyValues)[0].second.value();
240  for(std::vector<FWJobMetadataManager::Data>::iterator di = sdata.begin(); di != sdata.end(); ++di)
241  {
242  if (di->type_ == type) {
243  c->cnt++;
244  break;
245  }
246  }
247  }
248  // printf("%s file %d matches\n", iName.c_str(), c->cnt);
249  }
250  std::sort(clist.begin(), clist.end());
251  fwLog(fwlog::kInfo) << "Loading configuration file " << clist.back().file << std::endl;
252  setFrom(*(clist.back().cfg));
253 
254  return clist.back().file;
255 }
256 
257 //
258 // static member functions
259 //
type
Definition: HCALResponse.h:21
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
void to(FWConfiguration &) const
tuple cfg
Definition: looper.py:293
const KeyValues * keyValues() const
assert(m_qm.get())
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
bool operator<(const FedChannelConnection &, const FedChannelConnection &)
void setFrom(const FWConfiguration &) const
double f[11][100]
std::vector< Data > & usableData()
tuple conf
Definition: dbtoconf.py:185
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
std::string guessAndReadFromFile(FWJobMetadataManager *) const
#define fwLog(_level_)
Definition: fwLog.h:50
void add(const std::string &iName, FWConfigurable *)
does not take ownership
volatile std::atomic< bool > shutdown_flag false
static void streamTo(std::ostream &oTo, const FWConfiguration &iConfig, const std::string &name)
std::map< std::string, FWConfigurable * > m_configurables