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