File indexing completed on 2024-04-06 12:26:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <memory>
0021 #include <sstream>
0022 #include <fstream>
0023
0024
0025 #include "FWCore/Framework/interface/Frameworkfwd.h"
0026 #include "FWCore/Framework/interface/global/EDProducer.h"
0027
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030
0031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0032 #include "FWCore/Utilities/interface/StreamID.h"
0033
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035
0036 #include "DataFormats/Luminosity/interface/LumiInfo.h"
0037
0038
0039
0040
0041
0042 class LumiProducerFromBrilcalc : public edm::global::EDProducer<> {
0043 public:
0044 explicit LumiProducerFromBrilcalc(const edm::ParameterSet&);
0045 ~LumiProducerFromBrilcalc() override = default;
0046
0047 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0048
0049 private:
0050 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0051
0052
0053 const std::string lumiFile_;
0054 const bool throwIfNotFound_;
0055 const bool doBunchByBunch_;
0056 std::map<std::pair<int, int>, std::pair<float, float> > lumiData_;
0057 };
0058
0059
0060
0061
0062
0063 LumiProducerFromBrilcalc::LumiProducerFromBrilcalc(const edm::ParameterSet& iConfig)
0064 : lumiFile_(iConfig.getParameter<std::string>("lumiFile")),
0065 throwIfNotFound_(iConfig.getParameter<bool>("throwIfNotFound")),
0066 doBunchByBunch_(iConfig.getParameter<bool>("doBunchByBunch")) {
0067
0068 produces<LumiInfo>("brilcalc");
0069
0070
0071 if (doBunchByBunch_) {
0072 throw cms::Exception("LumiProducerFromBrilcalc")
0073 << "Sorry, bunch-by-bunch luminosity is not yet supported. Please bug your friendly lumi expert!";
0074 }
0075
0076
0077 edm::LogInfo("LumiProducerFromBrilcalc") << "Reading luminosity data from " << lumiFile_ << "...one moment...";
0078 std::ifstream lumiFile(lumiFile_);
0079 if (!lumiFile.is_open()) {
0080 throw cms::Exception("LumiProducerFromBrilcalc") << "Failed to open input luminosity file " << lumiFile_;
0081 }
0082
0083 int nLS = 0;
0084 std::string line;
0085 while (true) {
0086 std::getline(lumiFile, line);
0087 if (lumiFile.eof() || lumiFile.fail())
0088 break;
0089 if (line.empty())
0090 continue;
0091 if (line.at(0) == '#')
0092 continue;
0093
0094
0095
0096 std::stringstream ss(line);
0097 std::string field;
0098 std::vector<std::string> fields;
0099
0100 while (std::getline(ss, field, ','))
0101 fields.push_back(field);
0102
0103 if (fields.size() != 9) {
0104 edm::LogWarning("LumiProducerFromBrilcalc") << "Malformed line in csv file: " << line;
0105 continue;
0106 }
0107
0108
0109
0110 int run, ls;
0111 std::stringstream runfill(fields[0]);
0112 runfill >> run;
0113 std::stringstream lsls(fields[1]);
0114 lsls >> ls;
0115
0116
0117 float lumiDel, lumiRec, dtFrac;
0118 std::stringstream lumiDelString(fields[5]);
0119 lumiDelString >> lumiDel;
0120 std::stringstream lumiRecString(fields[6]);
0121 lumiRecString >> lumiRec;
0122
0123
0124 dtFrac = 1.0 - lumiRec / lumiDel;
0125
0126
0127 lumiData_[std::make_pair(run, ls)] = std::make_pair(lumiDel, dtFrac);
0128 nLS++;
0129 }
0130 edm::LogInfo("LumiProducerFromBrilcalc") << "Read " << nLS << " lumisections from " << lumiFile_;
0131 lumiFile.close();
0132 }
0133
0134
0135
0136
0137
0138
0139 void LumiProducerFromBrilcalc::produce(edm::StreamID iStreamID,
0140 edm::Event& iEvent,
0141 const edm::EventSetup& iSetup) const {
0142 std::vector<float> bxlumi(3564, 0);
0143 std::pair<int, int> runls = std::make_pair(iEvent.run(), iEvent.luminosityBlock());
0144 if (lumiData_.count(runls) == 1) {
0145
0146 LogDebug("LumiProducerFromBrilcalc") << "Filling for run " << runls.first << " ls " << runls.second
0147 << " with delivered " << lumiData_.at(runls).first << " dt "
0148 << lumiData_.at(runls).second;
0149 iEvent.put(std::make_unique<LumiInfo>(lumiData_.at(runls).second, bxlumi, lumiData_.at(runls).first), "brilcalc");
0150 } else {
0151 if (throwIfNotFound_) {
0152 throw cms::Exception("LumiProducerFromBrilcalc")
0153 << "Failed to find luminosity for run " << runls.first << " LS " << runls.second;
0154 } else {
0155
0156 edm::LogWarning("LumiProducerFromBrilcalc")
0157 << "Failed to find luminosity for run " << runls.first << " ls " << runls.second;
0158 iEvent.put(std::make_unique<LumiInfo>(0, bxlumi, 0), "brilcalc");
0159 }
0160 }
0161 }
0162
0163
0164 void LumiProducerFromBrilcalc::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0165
0166 edm::ParameterSetDescription desc;
0167 desc.add<std::string>("lumiFile");
0168 desc.add<bool>("throwIfNotFound", false);
0169 desc.add<bool>("doBunchByBunch", false);
0170 descriptions.addDefault(desc);
0171 }
0172
0173
0174 DEFINE_FWK_MODULE(LumiProducerFromBrilcalc);