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