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