CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
modGains.cc
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <cstring>
6 #include <string>
7 
9 //#include "CondFormats/HcalObjects/interface/AllObjects.h"
12 
13 
14 
15 
16 
17 int main (int argn, char* argv []) {
18  if (argn < 4) {
19  std::cerr << "Use: " << argv[0] << " operation <gains(.txt)> <operand> <result(.txt)>" << std::endl;
20  std::cerr << " where operation: sadd,ssub,smult,sdiv = +-*/ of a scalar value \n";
21  std::cerr << " add,sub,mult,div = +-*/ of vector values (in RespCorr-format)\n";
22  return 1;
23  }
24 
25  // get base conditions
26  std::cerr << argv[2] << std::endl;
27  std::ifstream inStream (argv[2]);
28  HcalGains gainsIn;
29  HcalDbASCIIIO::getObject (inStream, &gainsIn);
30 
31  // where to write the result
32  std::ofstream outStream (argv[4]);
33 
34  // operation and operand
35  float val = 1.0;
36  std::string s_operation;
37  s_operation = argv[1];
38  bool vectorop = false;
39 
40  HcalRespCorrs corrsIn;
41 
42  if ( (std::strcmp(s_operation.c_str(),"add")==0) ||
43  (std::strcmp(s_operation.c_str(),"sub")==0) ||
44  (std::strcmp(s_operation.c_str(),"mult")==0) ||
45  (std::strcmp(s_operation.c_str(),"div")==0) ) // vector operation
46  {
47  vectorop = true;
48  std::ifstream inCorr (argv[3]);
49  HcalDbASCIIIO::getObject (inCorr, &corrsIn);
50  }
51  else if ((std::strcmp(s_operation.c_str(),"sadd")==0) ||
52  (std::strcmp(s_operation.c_str(),"ssub")==0) ||
53  (std::strcmp(s_operation.c_str(),"smult")==0) ||
54  (std::strcmp(s_operation.c_str(),"sdiv")==0)) // scalar operation
55  {
56  val = atof (argv[3]);
57  std::cerr << "Scalar operation: using val=" << val << std::endl;
58  }
59  else
60  {
61  std::cerr << "Unknown operator. Stopping. \n";
62  return 1;
63  }
64 
65  HcalGains gainsOut;
66  std::vector<DetId> channels = gainsIn.getAllChannels ();
67  std::cerr << "size = " << channels.size() << std::endl;
68  for (unsigned i = 0; i < channels.size(); i++) {
69  DetId id = channels[i];
70 
71  if (vectorop) // vector operation
72  {
73  if ((std::strcmp(s_operation.c_str(),"mult")==0)||(std::strcmp(s_operation.c_str(),"div")==0)) val = 1.0; // mult,div
74  if ((std::strcmp(s_operation.c_str(),"add")==0)||(std::strcmp(s_operation.c_str(),"sub")==0)) val = 0.0; // add,sub
75  if (corrsIn.exists(id))
76  {
77  val = corrsIn.getValues(id)->getValue();
78  }
79  if (i%100 == 0)
80  std::cerr << "Vector operation, " << i << "th channel: using val=" << val << std::endl;
81  }
82 
83  // std::cerr << "val=" << val << std::endl;
84  HcalGain* p_item = 0;
85  if ((std::strcmp(s_operation.c_str(),"add")==0) || (std::strcmp(s_operation.c_str(),"sadd")==0))
86  p_item = new HcalGain(id, gainsIn.getValues(id)->getValue(0) + val, gainsIn.getValues(id)->getValue(1) + val,
87  gainsIn.getValues(id)->getValue(2) + val, gainsIn.getValues(id)->getValue(3) + val);
88 
89  if ((std::strcmp(s_operation.c_str(),"sub")==0) || (std::strcmp(s_operation.c_str(),"ssub")==0))
90  p_item = new HcalGain(id, gainsIn.getValues(id)->getValue(0) - val, gainsIn.getValues(id)->getValue(1) - val,
91  gainsIn.getValues(id)->getValue(2) - val, gainsIn.getValues(id)->getValue(3) - val);
92 
93  if ((std::strcmp(s_operation.c_str(),"mult")==0) || (std::strcmp(s_operation.c_str(),"smult")==0))
94  p_item = new HcalGain(id, gainsIn.getValues(id)->getValue(0) * val, gainsIn.getValues(id)->getValue(1) * val,
95  gainsIn.getValues(id)->getValue(2) * val, gainsIn.getValues(id)->getValue(3) * val);
96 
97  if ((std::strcmp(s_operation.c_str(),"div")==0) || (std::strcmp(s_operation.c_str(),"sdiv")==0))
98  p_item = new HcalGain(id, gainsIn.getValues(id)->getValue(0) / val, gainsIn.getValues(id)->getValue(1) / val,
99  gainsIn.getValues(id)->getValue(2) / val, gainsIn.getValues(id)->getValue(3) / val);
100 
101 
102  // for all
103  if (p_item)
104  gainsOut.addValues(*p_item);
105  // std::cerr << i << std::endl;
106  }
107  // write out
108  HcalDbASCIIIO::dumpObject (outStream, gainsOut);
109  return 0;
110 }
111 
int i
Definition: DBlmapReader.cc:9
bool addValues(const Item &myItem, bool h2mode_=false)
float getValue(int fCapId) const
get value for capId = 0..3
Definition: HcalGain.h:20
int main(int argc, char **argv)
const bool exists(DetId fId) const
std::vector< DetId > getAllChannels() const
Definition: DetId.h:20
bool getObject(std::istream &fInput, HcalPedestals *fObject)
bool dumpObject(std::ostream &fOutput, const HcalPedestals &fObject)
float getValue() const
Definition: HcalRespCorr.h:18
const Item * getValues(DetId fId) const