CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalPedestalValidator.cc
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <vector>
3 #include <map>
4 #include <string>
5 #include <sstream>
6 #include <fstream>
7 #include <iostream>
8 
14 
15 namespace {
16  bool defaultsFile (const std::string fParam) {
17  return fParam == "defaults";
18  }
19 
20  bool asciiFile (const std::string fParam) {
21  return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".txt";
22  }
23 
24  bool xmlFile (const std::string fParam) {
25  return fParam.find (':') == std::string::npos && std::string (fParam, fParam.length () - 4) == ".xml";
26  }
27 
28  bool dbFile (const std::string fParam) {
29  return fParam.find (':') != std::string::npos;
30  }
31 
32  bool masterDb (const std::string fParam) {
33  return fParam.find ('@') != std::string::npos;
34  }
35 
36  template <class T>
37  bool getObject (T* fObject, const std::string& fDb, const std::string& fTag, int fRun) {
38  if (!fObject) return false;
39  if (fDb.empty ()) return false;
40  if (asciiFile (fDb)) {
41  std::ifstream stream (fDb.c_str ());
42  HcalDbASCIIIO::getObject (stream, fObject);
43  return true;
44  }
45  else if (masterDb (fDb)) {
46  std::cout << "HcalPedestalValidator-> Use input: MasterDB " << fDb << std::endl;
47  HcalDbOnline masterDb (fDb);
48  return masterDb.getObject (fObject, fTag, fRun);
49  }
50  else {
51  return false;
52  }
53  }
54 
55  template <class T>
56  bool putObject (T** fObject, const std::string& fDb, const std::string& fTag, int fRun) {
57  if (!fObject || !*fObject) return false;
58  if (fDb.empty ()) return false;
59  if (asciiFile (fDb)) {
60  std::ofstream stream (fDb.c_str ());
61  HcalDbASCIIIO::dumpObject (stream, **fObject);
62  return true;
63  }
64  else {
65  return false;
66  }
67  }
68 }
69 
70 // Args is a copy-paste from Fedor's peds_txt2xml.cc
71 class Args {
72  public:
73  Args () {};
74  ~Args () {};
75  void defineOption (const std::string& fOption, const std::string& fComment = "");
76  void defineParameter (const std::string& fParameter, const std::string& fComment = "");
77  void parse (int nArgs, char* fArgs []);
78  void printOptionsHelp () const;
79  std::string command () const;
80  std::vector<std::string> arguments () const;
81  bool optionIsSet (const std::string& fOption) const;
82  std::string getParameter (const std::string& fKey);
83  private:
84  std::string mProgramName;
85  std::vector <std::string> mOptions;
86  std::vector <std::string> mParameters;
87  std::vector <std::string> mArgs;
88  std::map <std::string, std::string> mParsed;
89  std::map <std::string, std::string> mComments;
90 };
91 
92 int main (int argn, char* argv []) {
93 
94 // CORAL required variables to be set, even if not needed
95  const char* foo1 = "CORAL_AUTH_USER=blaaah";
96  const char* foo2 = "CORAL_AUTH_PASSWORD=blaaah";
97  if (!::getenv("CORAL_AUTH_USER")) ::putenv(const_cast<char*>(foo1));
98  if (!::getenv("CORAL_AUTH_PASSWORD")) ::putenv(const_cast<char*>(foo2));
99 
100  Args args;
101  args.defineParameter ("-p", "raw pedestals");
102  args.defineParameter ("-w", "raw widths");
103  args.defineParameter ("-run", "current run number <0>");
104  args.defineParameter ("-ptag", "raw pedestal tag <NULL>");
105  args.defineParameter ("-wtag", "raw width tag <ptag>");
106  args.defineParameter ("-pref", "reference pedestals");
107  args.defineParameter ("-wref", "reference widths");
108  args.defineParameter ("-ptagref", "reference pedestal tag <NULL>");
109  args.defineParameter ("-wtagref", "reference width tag <ptagref>");
110  args.defineParameter ("-pval", "validated pedestals");
111  args.defineParameter ("-wval", "validated widths");
112  args.defineOption ("-help", "this help");
113 
114  args.parse (argn, argv);
115  std::vector<std::string> arguments = args.arguments ();
116  if (args.optionIsSet ("-help")) {
117  args.printOptionsHelp ();
118  return -1;
119  }
120 
121 // read parameters from command line
122  std::string RawPedSource = args.getParameter("-p");
123  std::string RawPedWidSource = args.getParameter("-w");
124  std::string RawPedTag = args.getParameter("-ptag").empty() ? "" : args.getParameter("-ptag");
125  std::string RawPedWidTag = args.getParameter("-wtag").empty() ? RawPedTag : args.getParameter("-wtag");
126  int RawPedRun = args.getParameter("-run").empty() ? 0 : (int)strtoll (args.getParameter("-run").c_str(),0,0);
127  int RawPedWidRun = RawPedRun;
128  std::string RefPedSource = args.getParameter("-pref");
129  std::string RefPedWidSource = args.getParameter("-wref");
130  std::string RefPedTag = args.getParameter("-ptagref").empty() ? "" : args.getParameter("-ptagref");
131  std::string RefPedWidTag = args.getParameter("-wtagref").empty() ? RefPedTag : args.getParameter("-wtagref");
132  int RefPedRun = RawPedRun;
133  int RefPedWidRun = RefPedRun;
134  std::string outputPedDest = args.getParameter("-pval");
135  std::string outputPedWidDest = args.getParameter("-wval");
136  std::string outputPedTag = "";
137  std::string outputPedWidTag = "";
138  int outputPedRun = RawPedRun;
139  int outputPedWidRun = outputPedRun;
140 
141 // get reference objects
142  HcalPedestals* RefPeds = 0;
143  RefPeds = new HcalPedestals ();
144  if (!getObject (RefPeds, RefPedSource, RefPedTag, RefPedRun)) {
145  std::cerr << "HcalPedestalValidator-> Failed to get reference Pedestals" << std::endl;
146  return 1;
147  }
148  HcalPedestalWidths* RefPedWids = 0;
149  RefPedWids = new HcalPedestalWidths ();
150  if (!getObject (RefPedWids, RefPedWidSource, RefPedWidTag, RefPedWidRun)) {
151  std::cerr << "HcalPedestalValidator-> Failed to get reference PedestalWidths" << std::endl;
152  return 2;
153  }
154 
155 // get input raw objects
156  HcalPedestals* RawPeds = 0;
157  RawPeds = new HcalPedestals ();
158  if (!getObject (RawPeds, RawPedSource, RawPedTag, RawPedRun)) {
159  std::cerr << "HcalPedestalValidator-> Failed to get raw Pedestals" << std::endl;
160  return 3;
161  }
162  HcalPedestalWidths* RawPedWids = 0;
163  RawPedWids = new HcalPedestalWidths ();
164  if (!getObject (RawPedWids, RawPedWidSource, RawPedWidTag, RawPedWidRun)) {
165  std::cerr << "HcalPedestalValidator-> Failed to get raw PedestalWidths" << std::endl;
166  return 4;
167  }
168 
169 // make output objects
170  HcalPedestals* outputPeds = 0;
171  outputPeds = new HcalPedestals ();
172  HcalPedestalWidths* outputPedWids = 0;
173  outputPedWids = new HcalPedestalWidths ();
174 
175 // run algorithm
176  int nstat[4]={2500,2500,2500,2500};
177  int Flag=HcalPedestalAnalysis::HcalPedVal(nstat,RefPeds,RefPedWids,RawPeds,RawPedWids,outputPeds,outputPedWids);
178 
179  delete RefPeds;
180  delete RefPedWids;
181  delete RawPeds;
182  delete RawPedWids;
183 
184 
185 // store new objects if necessary
186  if (Flag%100000>0) {
187  if (outputPeds) {
188  if (!putObject (&outputPeds, outputPedDest, outputPedTag, outputPedRun)) {
189  std::cerr << "HcalPedestalAnalyzer-> Failed to put output Pedestals" << std::endl;
190  return 5;
191  }
192  }
193  if (outputPedWids) {
194  if (!putObject (&outputPedWids, outputPedWidDest, outputPedWidTag, outputPedWidRun)) {
195  std::cerr << "HcalPedestalAnalyzer-> Failed to put output PedestalWidths" << std::endl;
196  return 6;
197  }
198  }
199  }
200  delete outputPeds;
201  delete outputPedWids;
202 
203 return 0;
204 }
205 
206 //==================== Args ===== BEGIN ==============================
207 void Args::defineOption (const std::string& fOption, const std::string& fComment) {
208  mOptions.push_back (fOption);
209  mComments [fOption] = fComment;
210 }
211 
212 void Args::defineParameter (const std::string& fParameter, const std::string& fComment) { mParameters.push_back (fParameter);
213  mComments [fParameter] = fComment;
214 }
215 
216 void Args::parse (int nArgs, char* fArgs []) {
217  if (nArgs <= 0) return;
218  mProgramName = std::string (fArgs [0]);
219  int iarg = 0;
220  while (++iarg < nArgs) {
221  std::string arg (fArgs [iarg]);
222  if (arg [0] != '-') mArgs.push_back (arg);
223  else {
224  if (std::find (mOptions.begin(), mOptions.end (), arg) != mOptions.end ()) {
225  mParsed [arg] = "";
226  }
227  if (std::find (mParameters.begin(), mParameters.end (), arg) != mParameters.end ()) {
228  if (iarg >= nArgs) {
229  std::cerr << "ERROR: Parameter " << arg << " has no value specified. Ignore parameter." << std::endl;
230  }
231  else {
232  mParsed [arg] = std::string (fArgs [++iarg]);
233  }
234  }
235  }
236  }
237 }
238 
239 void Args::printOptionsHelp () const {
240  char buffer [1024];
241  std::cout << "Parameters:" << std::endl;
242  for (unsigned i = 0; i < mParameters.size (); i++) {
243  std::map<std::string, std::string>::const_iterator it = mComments.find (mParameters [i]);
244  std::string comment = it != mComments.end () ? it->second : "uncommented";
245  sprintf (buffer, " %-8s <value> : %s", (mParameters [i]).c_str(), comment.c_str());
246  std::cout << buffer << std::endl;
247  }
248  std::cout << "Options:" << std::endl;
249  for (unsigned i = 0; i < mOptions.size (); i++) {
250  std::map<std::string, std::string>::const_iterator it = mComments.find (mOptions [i]);
251  std::string comment = it != mComments.end () ? it->second : "uncommented";
252  sprintf (buffer, " %-8s : %s", (mOptions [i]).c_str(), comment.c_str());
253  std::cout << buffer << std::endl;
254  }
255 }
256 
257 std::string Args::command () const {
258  int ipos = mProgramName.rfind ('/');
259  return std::string (mProgramName, ipos+1);
260 }
261 
262 std::vector<std::string> Args::arguments () const {return mArgs;}
263 
264 bool Args::optionIsSet (const std::string& fOption) const {
265  return mParsed.find (fOption) != mParsed.end ();
266 }
267 
268 std::string Args::getParameter (const std::string& fKey) {
269  if (optionIsSet (fKey)) return mParsed [fKey];
270  return "";
271 }
272 
int i
Definition: DBlmapReader.cc:9
std::string mProgramName
void defineOption(const std::string &fOption, const std::string &fComment="")
TObject * getObject(TDirectory *fDir, const std::vector< std::string > &fObjectName)
Definition: compareHists.cc:44
std::map< std::string, std::string > mComments
bool defaultsFile(const std::string fParam)
std::vector< std::string > arguments() const
std::vector< std::string > mOptions
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
int main(int argc, char **argv)
std::vector< std::string > mParameters
std::vector< std::string > mArgs
void printOptionsHelp() const
void parse(int nArgs, char *fArgs[])
Gather conditions data from online DB.
Definition: HcalDbOnline.h:34
bool dbFile(const std::string fParam)
std::string command() const
bool xmlFile(const std::string fParam)
bool getObject(std::istream &fInput, HcalPedestals *fObject)
dictionary args
bool dumpObject(std::ostream &fOutput, const HcalPedestals &fObject)
bool asciiFile(const std::string fParam)
tuple cout
Definition: gather_cfg.py:121
std::map< std::string, std::string > mParsed
static int HcalPedVal(int nstat[4], const HcalPedestals *fRefPedestals, const HcalPedestalWidths *fRefPedestalWidths, HcalPedestals *fRawPedestals, HcalPedestalWidths *fRawPedestalWidths, HcalPedestals *fValPedestals, HcalPedestalWidths *fValPedestalWidths)
void defineParameter(const std::string &fParameter, const std::string &fComment="")
long double T
bool optionIsSet(const std::string &fOption) const
std::string getParameter(const std::string &fKey)
#define comment(par)
Definition: vmac.h:162