CMS 3D CMS Logo

modGains.cc
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <cstring>
6 #include <string>
7 #include <memory>
8 
21 
22 class modGains : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
23 
24 public:
25  explicit modGains(const edm::ParameterSet&);
26  ~modGains() override;
27 
28 private:
29  void beginRun(edm::Run const& iEvent, edm::EventSetup const&) override {}
30  void analyze(edm::Event const&, edm::EventSetup const&) override;
31  void endRun(edm::Run const& iEvent, edm::EventSetup const&) override {}
34  double val;
35  bool vectorop;
36 };
37 
39  s_operation = iConfig.getUntrackedParameter<std::string>("Operation");
40  fileIn = iConfig.getUntrackedParameter<std::string>("FileIn");
41  fileOut = iConfig.getUntrackedParameter<std::string>("FileOut");
42  fileCorr = iConfig.getUntrackedParameter<std::string>("FileCorr");
43  val = iConfig.getUntrackedParameter<double>("ScalarFactor");
44 
45  if ( (std::strcmp(s_operation.c_str(),"add")==0) ||
46  (std::strcmp(s_operation.c_str(),"sub")==0) ||
47  (std::strcmp(s_operation.c_str(),"mult")==0) ||
48  (std::strcmp(s_operation.c_str(),"div")==0) ) { // vector operation
49  vectorop = true;
50  } else if ((std::strcmp(s_operation.c_str(),"sadd")==0) ||
51  (std::strcmp(s_operation.c_str(),"ssub")==0) ||
52  (std::strcmp(s_operation.c_str(),"smult")==0) ||
53  (std::strcmp(s_operation.c_str(),"sdiv")==0)) {// scalar operation
54  std::cerr << "Scalar operation: using val=" << val << std::endl;
55  } else {
56  throw cms::Exception("Unknown", "modGains") << "Unknown operator. "
57  << s_operation <<" Stopping.\n";
58  }
59 }
60 
62 
63 void modGains::analyze(edm::Event const&, edm::EventSetup const& iSetup) {
64 
66  iSetup.get<HcalRecNumberingRecord>().get(htopo);
67  HcalTopology topo = (*htopo);
68 
69  // get base conditions
70  std::cerr << fileIn << std::endl;
71  std::ifstream inStream (fileIn.c_str());
72  HcalGains gainsIn(&topo);;
73  HcalDbASCIIIO::getObject (inStream, &gainsIn);
74  inStream.close();
75 
76  HcalRespCorrs corrsIn(&topo);;
77  if (vectorop) {
78  std::ifstream inCorr (fileCorr.c_str());
79  HcalDbASCIIIO::getObject (inCorr, &corrsIn);
80  inCorr.close();
81  }
82 
83  HcalGains gainsOut(&topo);;
84  std::vector<DetId> channels = gainsIn.getAllChannels ();
85  std::cerr << "size = " << channels.size() << std::endl;
86  for (unsigned i = 0; i < channels.size(); i++) {
87  DetId id = channels[i];
88 
89  if (vectorop) { // vector operation
90  if ((std::strcmp(s_operation.c_str(),"mult")==0)||(std::strcmp(s_operation.c_str(),"div")==0)) val = 1.0; // mult,div
91  if ((std::strcmp(s_operation.c_str(),"add")==0)||(std::strcmp(s_operation.c_str(),"sub")==0)) val = 0.0; // add,sub
92  if (corrsIn.exists(id)) {
93  val = corrsIn.getValues(id)->getValue();
94  }
95  if (i%100 == 0)
96  std::cerr << "Vector operation, " << i << "th channel: using val=" << val << std::endl;
97  }
98 
99  std::unique_ptr<HcalGain> p_item;
100  if ((std::strcmp(s_operation.c_str(),"add")==0) || (std::strcmp(s_operation.c_str(),"sadd")==0))
101  p_item = std::make_unique<HcalGain>(id, gainsIn.getValues(id)->getValue(0) + val, gainsIn.getValues(id)->getValue(1) + val,
102  gainsIn.getValues(id)->getValue(2) + val, gainsIn.getValues(id)->getValue(3) + val);
103 
104  if ((std::strcmp(s_operation.c_str(),"sub")==0) || (std::strcmp(s_operation.c_str(),"ssub")==0))
105  p_item = std::make_unique<HcalGain>(id, gainsIn.getValues(id)->getValue(0) - val, gainsIn.getValues(id)->getValue(1) - val,
106  gainsIn.getValues(id)->getValue(2) - val, gainsIn.getValues(id)->getValue(3) - val);
107 
108  if ((std::strcmp(s_operation.c_str(),"mult")==0) || (std::strcmp(s_operation.c_str(),"smult")==0))
109  p_item = std::make_unique<HcalGain>(id, gainsIn.getValues(id)->getValue(0) * val, gainsIn.getValues(id)->getValue(1) * val,
110  gainsIn.getValues(id)->getValue(2) * val, gainsIn.getValues(id)->getValue(3) * val);
111 
112  if ((std::strcmp(s_operation.c_str(),"div")==0) || (std::strcmp(s_operation.c_str(),"sdiv")==0))
113  p_item = std::make_unique<HcalGain>(id, gainsIn.getValues(id)->getValue(0) / val, gainsIn.getValues(id)->getValue(1) / val,
114  gainsIn.getValues(id)->getValue(2) / val, gainsIn.getValues(id)->getValue(3) / val);
115 
116 
117  // for all
118  if (p_item) gainsOut.addValues(*p_item);
119  // std::cerr << i << std::endl;
120  }
121  // write out
122  std::ofstream outStream (fileOut.c_str());
123  HcalDbASCIIIO::dumpObject (outStream, gainsOut);
124  outStream.close();
125 }
126 
127 //define this as a plug-in
std::string fileOut
Definition: modGains.cc:33
T getUntrackedParameter(std::string const &, T const &) const
double val
Definition: modGains.cc:34
void beginRun(edm::Run const &iEvent, edm::EventSetup const &) override
Definition: modGains.cc:29
void analyze(edm::Event const &, edm::EventSetup const &) override
Definition: modGains.cc:63
const Item * getValues(DetId fId, bool throwOnFail=true) const
void endRun(edm::Run const &iEvent, edm::EventSetup const &) override
Definition: modGains.cc:31
std::string fileIn
Definition: modGains.cc:33
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const bool exists(DetId fId) const
Definition: DetId.h:18
std::string s_operation
Definition: modGains.cc:32
~modGains() override
Definition: modGains.cc:61
modGains(const edm::ParameterSet &)
Definition: modGains.cc:38
bool getObject(std::istream &fInput, HcalPedestals *fObject)
bool dumpObject(std::ostream &fOutput, const HcalPedestals &fObject)
float getValue() const
Definition: HcalRespCorr.h:20
T get() const
Definition: EventSetup.h:71
bool vectorop
Definition: modGains.cc:35
bool addValues(const Item &myItem)
std::string fileCorr
Definition: modGains.cc:33
Definition: Run.h:45