CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
hcalPublishConditions.cc
Go to the documentation of this file.
1 #include <stdlib.h>
2 
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include <map>
7 #include <algorithm>
8 #include <string>
9 
10 #include "DataFormats/Common/interface/Timestamp.h"
11 #include "CondCore/IOVService/interface/IOV.h"
12 #include "CondTools/Hcal/interface/HcalDbTool.h"
13 
14 namespace {
15  typedef HcalDbTool::IOVRun IOVRun;
16  typedef std::map<IOVRun,std::string> IOVCollection;
17  typedef std::pair<IOVRun,IOVRun> IntervalOV;
18 
19  std::vector <IntervalOV> allIOV (const cond::IOV& fIOV) {
20  std::vector <IntervalOV> result;
21  IOVRun iovMin = 0;
22  for (IOVCollection::const_iterator iovi = fIOV.iov.begin (); iovi != fIOV.iov.end (); iovi++) {
23  IOVRun iovMax = iovi->first;
24  result.push_back (std::make_pair (iovMin, iovMax));
25  iovMin = iovMax;
26  }
27  return result;
28  }
29 }
30 
31 class Args {
32  public:
33  Args () {};
34  ~Args () {};
35  void defineOption (const std::string& fOption, const std::string& fComment = "");
36  void defineParameter (const std::string& fParameter, const std::string& fComment = "");
37  void parse (int nArgs, char* fArgs []);
38  void printOptionsHelp () const;
39  std::string command () const;
40  std::vector<std::string> arguments () const;
41  bool optionIsSet (const std::string& fOption) const;
42  std::string getParameter (const std::string& fKey);
43  private:
45  std::vector <std::string> mOptions;
46  std::vector <std::string> mParameters;
47  std::vector <std::string> mArgs;
48  std::map <std::string, std::string> mParsed;
49  std::map <std::string, std::string> mComments;
50 };
51 
52 void printHelp (const Args& args) {
53  char buffer [1024];
54  std::cout << "Tool to convert RAW HCAL conditions into standard offline accessible ones" << std::endl;
55  std::cout << " feedback -> ratnikov@fnal.gov" << std::endl;
56  std::cout << "Use:" << std::endl;
57  sprintf (buffer, " %s <what> <options> <parameters>\n", args.command ().c_str());
58  std::cout << buffer;
59  std::cout << " where <what> is: \n pedestals\n gains\n pwidths\n gwidths\n emap\n qie\n calibqie" << std::endl;
60  args.printOptionsHelp ();
61 }
62 
63 
64 bool publishObjects (const std::string& fInputDb, const std::string& fInputTag,
65  const std::string& fOutputDb, const std::string& fOutputTag,
66  bool fVerbose) {
67  HcalDbTool db (fInputDb, fVerbose);
68  cond::IOV inputIov;
69  cond::IOV outputIov;
70  std::vector<std::string> allTags = db.metadataAllTags ();
71  for (unsigned i = 0; i < allTags.size(); i++) {
72  if (allTags[i] == fInputTag) {
73  if (!db.getObject (&inputIov, fInputTag)) {
74  std::cerr << "copyObject-> Can not get IOV for input tags " << fInputTag << std::endl;
75  return false;
76  }
77  }
78  if (allTags[i] == fOutputTag) {
79  if (!db.getObject (&outputIov, fOutputTag)) {
80  std::cerr << "copyObject-> Can not get IOV for output tags " << fOutputTag << std::endl;
81  return false;
82  }
83  }
84  }
85  if (fVerbose) {
86  std::vector <IntervalOV> allIOVs = allIOV (inputIov);
87  std::cout << " all IOVs available for input tag " << fInputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
88  for (unsigned i = 0; i < allIOVs.size(); i++) {
89  std::cout << "[ " << allIOVs[i].first << " .. " << allIOVs[i].second << " ) " << inputIov.iov [allIOVs[i].second] << std::endl;
90  }
91  allIOVs = allIOV (outputIov);
92  std::cout << "\n all IOVs available for output tag " << fOutputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
93  for (unsigned i = 0; i < allIOVs.size(); i++) {
94  std::cout << "[ " << allIOVs[i].first << " .. " << allIOVs[i].second << " ) " << outputIov.iov [allIOVs[i].second] << std::endl;
95  }
96  }
97 
98  // first check that all objects of output are contained in input
99  IOVCollection::const_iterator iovIn = inputIov.iov.begin ();
100  if (iovIn == inputIov.iov.end ()) {
101  std::cerr << "Input IOV is empty - nothing to do" << std::endl;
102  return true;
103  }
104  std::string inputToken = iovIn->second;
105  iovIn++;
106  IOVCollection::const_iterator iovOut = outputIov.iov.begin ();
107  for (; ; iovOut++, iovIn++) {
108  if (iovIn == inputIov.iov.end ()) {
109  if (++iovOut == outputIov.iov.end ()) {
110  std::cerr << "Nothing to update" << std::endl;
111  return true;
112  }
113  else {
114  std::cerr << "List of input IOV is too short" << std::endl;
115  return false;
116  }
117  }
118  if (iovOut == outputIov.iov.end ()) { // empty output
119  outputIov.iov [iovIn->first] = inputToken;
120  inputToken = iovIn->second;
121  break;
122  }
123  if (inputToken != iovOut->second) {
124  std::cerr << "Mismatched tokens: \n in: " << iovIn->second << "\n out: " << iovOut->second << std::endl;
125  return false;
126  }
127 
128  // is it the open end?
129  IOVCollection::const_iterator iovOut2 = iovOut;
130  if (++iovOut2 == outputIov.iov.end ()) {
131  outputIov.iov.erase (iovOut->first);
132  outputIov.iov [iovIn->first] = inputToken;
133  inputToken = iovIn->second;
134  break;
135  }
136  if (iovIn->first != iovOut->first) {
137  std::cerr << "Mismatched runs: in: " << iovIn->first << ", out: " << iovOut->first << std::endl;
138  return false;
139  }
140 
141  inputToken = iovIn->second;
142  }
143  std::cout << "Good! Input and output does match" << std::endl;
144 
145  for (iovIn++; iovIn != inputIov.iov.end (); iovIn++) {
146  IOVRun run = iovIn->first;
147  outputIov.iov [run] = inputToken;
148  inputToken = iovIn->second;
149  }
150  // last open token
151  outputIov.iov [edm::Timestamp::endOfTime().value()] = inputToken;
152 
153  if (fVerbose) {
154  std::vector <IntervalOV> allIOVs = allIOV (outputIov);
155  std::cout << "\n Done! All IOVs available for output tag " << fOutputTag << " in CMS POOL DB instance: " << fInputDb << std::endl;
156  for (unsigned i = 0; i < allIOVs.size(); i++) {
157  std::cout << "[ " << allIOVs[i].first << " ... " << allIOVs[i].second << " ) " << outputIov.iov [allIOVs[i].second] << std::endl;
158  }
159  }
160  return db.putObject (&outputIov, fOutputTag);
161 }
162 
163 
164 int main (int argn, char* argv []) {
165 
166  Args args;
167  args.defineParameter ("-input", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
168  args.defineParameter ("-inputtag", "tag for raw conditions");
169  args.defineParameter ("-output", "DB connection string, POOL format, i.e. oracle://devdb10/CMS_COND_HCAL");
170  args.defineParameter ("-outputtag", "tag for production conditions");
171  args.defineOption ("-help", "this help");
172  args.defineOption ("-verbose", "verbose");
173 
174  args.parse (argn, argv);
175 
176  std::vector<std::string> arguments = args.arguments ();
177 
178  if (arguments.size () < 1 || args.optionIsSet ("-help")) {
179  printHelp (args);
180  return -1;
181  }
182 
183  std::string inputdb = args.getParameter ("-input");
184  std::string inputtag = args.getParameter ("-inputtag");
185  std::string outputdb = args.getParameter ("-output");
186  std::string outputtag = args.getParameter ("-outputtag");
187  bool verbose = args.optionIsSet ("-verbose");
188 
189  publishObjects (inputdb, inputtag, outputdb, outputtag, verbose);
190 }
191 
192 
193 //==================== Args ===== BEGIN ==============================
194 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
195  mOptions.push_back (fOption);
196  mComments [fOption] = fComment;
197 }
198 
199 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
200  mParameters.push_back (fParameter);
201  mComments [fParameter] = fComment;
202 }
203 
204 void Args::parse (int nArgs, char* fArgs []) {
205  if (nArgs <= 0) return;
206  mProgramName = std::string (fArgs [0]);
207  int iarg = 0;
208  while (++iarg < nArgs) {
209  std::string arg (fArgs [iarg]);
210  if (arg [0] != '-') mArgs.push_back (arg);
211  else {
212  if (std::find (mOptions.begin(), mOptions.end (), arg) != mOptions.end ()) {
213  mParsed [arg] = "";
214  }
215  if (std::find (mParameters.begin(), mParameters.end (), arg) != mParameters.end ()) {
216  if (iarg >= nArgs) {
217  std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
218  }
219  else {
220  mParsed [arg] = std::string (fArgs [++iarg]);
221  }
222  }
223  }
224  }
225 }
226 
227 void Args::printOptionsHelp () const {
228  char buffer [1024];
229  std::cout << "Parameters:" << std::endl;
230  for (unsigned i = 0; i < mParameters.size (); i++) {
231  std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
232  std::string comment = it != mComments.end () ? it->second : "uncommented";
233  sprintf (buffer, " %-8s <value> : %s", (mParameters [i]).c_str(), comment.c_str());
234  std::cout << buffer << std::endl;
235  }
236  std::cout << "Options:" << std::endl;
237  for (unsigned i = 0; i < mOptions.size (); i++) {
238  std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
239  std::string comment = it != mComments.end () ? it->second : "uncommented";
240  sprintf (buffer, " %-8s : %s", (mOptions [i]).c_str(), comment.c_str());
241  std::cout << buffer << std::endl;
242  }
243 }
244 
245 std::string Args::command () const {
246  int ipos = mProgramName.rfind ('/');
247  return std::string (mProgramName, ipos+1);
248 }
249 
250 std::vector<std::string> Args::arguments () const {return mArgs;}
251 
252 bool Args::optionIsSet (const std::string& fOption) const {
253  return mParsed.find (fOption) != mParsed.end ();
254 }
255 
257  if (optionIsSet (fKey)) return mParsed [fKey];
258  return "";
259 }
260 //==================== Args ===== END ==============================
int i
Definition: DBlmapReader.cc:9
std::string mProgramName
static Timestamp endOfTime()
Definition: Timestamp.h:102
bool publishObjects(const std::string &fInputDb, const std::string &fInputTag, const std::string &fOutputDb, const std::string &fOutputTag, bool fVerbose)
void defineOption(const std::string &fOption, const std::string &fComment="")
std::map< std::string, std::string > mComments
std::vector< std::string > arguments() const
std::vector< std::string > mOptions
tuple db
Definition: EcalCondDB.py:151
std::map< IOVRun, std::string > IOVCollection
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
A arg
Definition: Factorize.h:36
std::vector< std::string > mParameters
std::vector< std::string > mArgs
tuple result
Definition: query.py:137
void printOptionsHelp() const
void parse(int nArgs, char *fArgs[])
void printHelp(const Args &args)
std::string command() const
HcalDbTool::IOVRun IOVRun
tuple cout
Definition: gather_cfg.py:145
std::map< std::string, std::string > mParsed
list inputdb
Definition: align_cfg.py:8
void defineParameter(const std::string &fParameter, const std::string &fComment="")
bool optionIsSet(const std::string &fOption) const
TimeValue_t value() const
Definition: Timestamp.h:56
std::string getParameter(const std::string &fKey)
#define comment(par)
Definition: vmac.h:161