test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
hcalIovTool.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 "CondCore/IOVService/interface/IOV.h"
11 #include "CondTools/Hcal/interface/HcalDbTool.h"
12 #include "CondTools/Hcal/interface/HcalDbOnline.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 bool dbFile (const std::string fParam) {
31  return fParam.find (':') != std::string::npos;
32 }
33 
34 bool onlineFile (const std::string fParam) {
35  return fParam.find ('@') != std::string::npos &&
36  fParam.find ("cms_val_lb") == std::string::npos;
37 }
38 
39 }
40 
41 class Args {
42  public:
43  Args () {};
44  ~Args () {};
45  void defineOption (const std::string& fOption, const std::string& fComment = "");
46  void defineParameter (const std::string& fParameter, const std::string& fComment = "");
47  void parse (int nArgs, char* fArgs []);
48  void printOptionsHelp () const;
49  std::string command () const;
50  std::vector<std::string> arguments () const;
51  bool optionIsSet (const std::string& fOption) const;
52  std::string getParameter (const std::string& fKey);
53  private:
55  std::vector <std::string> mOptions;
56  std::vector <std::string> mParameters;
57  std::vector <std::string> mArgs;
58  std::map <std::string, std::string> mParsed;
59  std::map <std::string, std::string> mComments;
60 };
61 
62 void printHelp (const Args& args) {
63  char buffer [1024];
64  std::cout << "Tool to manipulate by IOV of Hcal Calibrations" << std::endl;
65  std::cout << " feedback -> ratnikov@fnal.gov" << std::endl;
66  std::cout << "Use:" << std::endl;
67  sprintf (buffer, " %s <what> <options> <parameters>\n", args.command ().c_str());
68  std::cout << buffer;
69  std::cout << " where <what> is: \n tag\n iov" << std::endl;
70  args.printOptionsHelp ();
71 }
72 
73 void printTags (const std::string& fDb, bool fVerbose) {
74  std::vector<std::string> allTags;
75  if (dbFile (fDb)) {
76  HcalDbTool poolDb (fDb, fVerbose);
77  allTags = poolDb.metadataAllTags ();
78  std::cout << "Tags available in CMS POOL DB instance: " << fDb << std::endl;
79  }
80  if (onlineFile (fDb)) {
81  HcalDbOnline onlineDb (fDb, fVerbose);
82  allTags = onlineDb.metadataAllTags ();
83  std::cout << "Tags available in HCAL master DB instance: " << fDb << std::endl;
84  }
85  for (unsigned i = 0; i < allTags.size(); i++) {
86  std::cout << allTags[i] << std::endl;
87  }
88 }
89 
90 void printRuns (const std::string& fDb, const std::string fTag, bool fVerbose) {
91  std::vector <IntervalOV> allIOVs;
92  if (dbFile (fDb)) {
93  HcalDbTool poolDb (fDb, fVerbose);
94  cond::IOV iov;
95  if (poolDb.getObject (&iov, fTag)) {
96  allIOVs = allIOV (iov);
97  std::cout << "IOVs available for tag " << fTag << " in CMS POOL DB instance: " << fDb << std::endl;
98  }
99  else {
100  std::cerr << "printRuns-> can not find IOV for tag " << fTag << std::endl;
101  }
102  }
103  if (onlineFile (fDb)) {
104  HcalDbOnline onlineDb (fDb, fVerbose);
105  allIOVs = onlineDb.getIOVs (fTag);
106  }
107  for (unsigned i = 0; i < allIOVs.size(); i++) {
108  std::cout << "[ " << allIOVs[i].first << " ... " << allIOVs[i].second << " )" << std::endl;
109  }
110 }
111 
112 int main (int argn, char* argv []) {
113 
114  Args args;
115  args.defineParameter ("-db", "DB connection string, POOL format, or online format");
116  args.defineParameter ("-tag", "tag specifyer");
117  args.defineOption ("-help", "this help");
118  args.defineOption ("-verbose", "this help");
119 
120  args.parse (argn, argv);
121 
122  std::vector<std::string> arguments = args.arguments ();
123 
124  if (arguments.size () < 1 || args.optionIsSet ("-help")) {
125  printHelp (args);
126  return -1;
127  }
128 
129  std::string db = args.getParameter ("-db");
130  std::string tag = args.getParameter ("-tag");
131  bool verbose = args.optionIsSet ("-verbose");
132 
133  std::string what = arguments [0];
134 
135  if (what == "tag") {
136  printTags (db, verbose);
137  }
138  else if (what == "iov") {
139  printRuns (db, tag, verbose);
140  }
141  std::cout << "Program has completed successfully" << std::endl;
142  return 0;
143 }
144 
145 
146 //==================== Args ===== BEGIN ==============================
147 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
148  mOptions.push_back (fOption);
149  mComments [fOption] = fComment;
150 }
151 
152 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) {
153  mParameters.push_back (fParameter);
154  mComments [fParameter] = fComment;
155 }
156 
157 void Args::parse (int nArgs, char* fArgs []) {
158  if (nArgs <= 0) return;
159  mProgramName = std::string (fArgs [0]);
160  int iarg = 0;
161  while (++iarg < nArgs) {
162  std::string arg (fArgs [iarg]);
163  if (arg [0] != '-') mArgs.push_back (arg);
164  else {
165  if (std::find (mOptions.begin(), mOptions.end (), arg) != mOptions.end ()) {
166  mParsed [arg] = "";
167  }
168  if (std::find (mParameters.begin(), mParameters.end (), arg) != mParameters.end ()) {
169  if (iarg >= nArgs) {
170  std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
171  }
172  else {
173  mParsed [arg] = std::string (fArgs [++iarg]);
174  }
175  }
176  }
177  }
178 }
179 
180 void Args::printOptionsHelp () const {
181  char buffer [1024];
182  std::cout << "Parameters:" << std::endl;
183  for (unsigned i = 0; i < mParameters.size (); i++) {
184  std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
185  std::string comment = it != mComments.end () ? it->second : "uncommented";
186  sprintf (buffer, " %-8s <value> : %s", (mParameters [i]).c_str(), comment.c_str());
187  std::cout << buffer << std::endl;
188  }
189  std::cout << "Options:" << std::endl;
190  for (unsigned i = 0; i < mOptions.size (); i++) {
191  std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
192  std::string comment = it != mComments.end () ? it->second : "uncommented";
193  sprintf (buffer, " %-8s : %s", (mOptions [i]).c_str(), comment.c_str());
194  std::cout << buffer << std::endl;
195  }
196 }
197 
198 std::string Args::command () const {
199  int ipos = mProgramName.rfind ('/');
200  return std::string (mProgramName, ipos+1);
201 }
202 
203 std::vector<std::string> Args::arguments () const {return mArgs;}
204 
205 bool Args::optionIsSet (const std::string& fOption) const {
206  return mParsed.find (fOption) != mParsed.end ();
207 }
208 
210  if (optionIsSet (fKey)) return mParsed [fKey];
211  return "";
212 }
213 //==================== Args ===== END ==============================
~Args()
Definition: hcalIovTool.cc:44
int i
Definition: DBlmapReader.cc:9
std::string mProgramName
void defineOption(const std::string &fOption, const std::string &fComment="")
std::map< std::string, std::string > mComments
void printTags(const std::string &fDb, bool fVerbose)
Definition: hcalIovTool.cc:73
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
tuple result
Definition: mps_fire.py:83
std::vector< std::string > mParameters
void printRuns(const std::string &fDb, const std::string fTag, bool fVerbose)
Definition: hcalIovTool.cc:90
tuple iov
Definition: o2o.py:307
std::vector< IntervalOV > getIOVs(const std::string &fTag)
std::vector< std::string > mArgs
void printOptionsHelp() const
void parse(int nArgs, char *fArgs[])
Gather conditions data from online DB.
Definition: HcalDbOnline.h:34
Args()
Definition: hcalIovTool.cc:43
bool dbFile(const std::string fParam)
std::vector< std::string > metadataAllTags()
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
void defineParameter(const std::string &fParameter, const std::string &fComment="")
bool onlineFile(const std::string fParam)
bool optionIsSet(const std::string &fOption) const
std::string getParameter(const std::string &fKey)
#define comment(par)
Definition: vmac.h:161