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 //
fwLog
#define fwLog(_level_)
Definition: fwLog.h:45
funct::false
false
Definition: Factorize.h:29
FWConfiguration::streamTo
static void streamTo(std::ostream &oTo, const FWConfiguration &iConfig, const std::string &name)
Definition: FWConfiguration.cc:196
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
FWXMLConfigParser.h
cms::cuda::assert
assert(be >=bs)
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
SimpleSAXParser::parse
void parse(void)
Definition: SimpleSAXParser.cc:46
DDAxes::x
mps_check.command
list command
Definition: mps_check.py:25
FWConfigurationManager::readFromFile
void readFromFile(const std::string &) const
Definition: FWConfigurationManager.cc:153
relativeConstraints.error
error
Definition: relativeConstraints.py:53
FWConfiguration::KeyValues
std::vector< std::pair< std::string, FWConfiguration > > KeyValues
Definition: FWConfiguration.h:40
FWConfigurationManager::guessAndReadFromFile
std::string guessAndReadFromFile(FWJobMetadataManager *) const
Definition: FWConfigurationManager.cc:171
config
Definition: config.py:1
FWJobMetadataManager.h
FWConfigurationManager::FWConfigurationManager
FWConfigurationManager()
Definition: FWConfigurationManager.cc:43
FWJobMetadataManager
Definition: FWJobMetadataManager.h:18
FWXMLConfigParser
Definition: FWXMLConfigParser.h:32
submitPVResolutionJobs.config
config
parse the configuration file
Definition: submitPVResolutionJobs.py:281
FWConfigurationManager::to
void to(FWConfiguration &) const
Definition: FWConfigurationManager.cc:86
FWConfigurable.h
FWConfigurationManager::~FWConfigurationManager
virtual ~FWConfigurationManager()
Definition: FWConfigurationManager.cc:50
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FWConfigurationManager::add
void add(const std::string &iName, FWConfigurable *)
does not take ownership
Definition: FWConfigurationManager.cc:67
mps_setup.stdout
stdout
Definition: mps_setup.py:250
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
createfilelist.int
int
Definition: createfilelist.py:10
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
FWConfigurationManager::m_configurables
std::map< std::string, FWConfigurable * > m_configurables
Definition: FWConfigurationManager.h:60
fwlog::kWarning
Definition: fwLog.h:35
fwLog.h
FWConfigurationManager::writeToFile
void writeToFile(const std::string &) const
Definition: FWConfigurationManager.cc:97
looper.cfg
cfg
Definition: looper.py:297
SimpleSAXParser
Definition: SimpleSAXParser.h:69
FWConfigurationManager::setFrom
void setFrom(const FWConfiguration &) const
Definition: FWConfigurationManager.cc:75
FWConfiguration::addKeyValue
FWConfiguration & addKeyValue(const std::string &, const FWConfiguration &)
Definition: FWConfiguration.cc:58
fwlog::kInfo
Definition: fwLog.h:35
FWConfigurationManager::readFromOldFile
void readFromOldFile(const std::string &) const
Definition: FWConfigurationManager.cc:116
FWConfigurable
Definition: FWConfigurable.h:28
FWConfiguration::keyValues
const KeyValues * keyValues() const
Definition: FWConfiguration.h:51
FWJobMetadataManager::usableData
std::vector< Data > & usableData()
Definition: FWJobMetadataManager.h:31
fwlog::kError
Definition: fwLog.h:35
FWConfiguration
Definition: FWConfiguration.h:31
operator<
bool operator<(DTCELinkId const &lhs, DTCELinkId const &rhs)
Definition: DTCELinkId.h:70
FWConfigurationManager.h
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:46
SimpleSAXParser.h
g
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
FWConfiguration.h
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37