CMS 3D CMS Logo

RawPCCProducer.cc
Go to the documentation of this file.
1 
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 #include <vector>
14 #include <mutex>
15 #include <cmath>
33 
34 class RawPCCProducer : public edm::global::EDProducer<edm::EndLuminosityBlockProducer> {
35 public:
36  explicit RawPCCProducer(const edm::ParameterSet&);
37  ~RawPCCProducer() override;
38 
39 private:
40  void globalEndLuminosityBlockProduce(edm::LuminosityBlock& lumiSeg, const edm::EventSetup& iSetup) const final;
41  void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const final;
42 
43  //input object labels
45  //background corrections from DB
47  //The list of modules to skip in the lumi calc.
48  const std::vector<int> modVeto_;
49  //background corrections
50  const bool applyCorr_;
51  //Output average values
53 
54  //output object labels
56 
57  //produce csv lumi file
58  const bool saveCSVFile_;
61 };
62 
63 //--------------------------------------------------------------------------------------------------
65  : pccToken_(consumes<reco::PixelClusterCounts, edm::InLumi>(edm::InputTag(
66  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters").getParameter<std::string>("inputPccLabel"),
67  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters").getParameter<std::string>("ProdInst")))),
68  lumiCorrectionsToken_(esConsumes<edm::Transition::EndLuminosityBlock>()),
69  modVeto_(
70  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters").getParameter<std::vector<int>>("modVeto")),
71  applyCorr_(iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
72  .getUntrackedParameter<bool>("ApplyCorrections", false)),
73  takeAverageValue_(iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
74  .getUntrackedParameter<std::string>("OutputValue", std::string("Average"))),
75  putToken_(produces<LumiInfo, edm::Transition::EndLuminosityBlock>(
76  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
77  .getUntrackedParameter<std::string>("outputProductName", "alcaLumi"))),
78  saveCSVFile_(iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
79  .getUntrackedParameter<bool>("saveCSVFile", false)),
80  csvOutLabel_(iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
81  .getUntrackedParameter<std::string>("label", std::string("rawPCC.csv"))) {}
82 
83 //--------------------------------------------------------------------------------------------------
85 
86 //--------------------------------------------------------------------------------------------------
88 
89 //--------------------------------------------------------------------------------------------------
91  const edm::EventSetup& iSetup) const {
92  //The total raw luminosity from the pixel clusters - not scaled
93  float totalLumi = 0.0;
94  //the statistical error on the lumi - large num ie sqrt(N)
95  float statErrOnLumi = 0.0;
96 
97  //new vector containing clusters per bxid
98  std::vector<int> clustersPerBXOutput(LumiConstants::numBX, 0);
99  //new vector containing clusters per bxid with afterglow corrections
100  std::vector<float> corrClustersPerBXOutput(LumiConstants::numBX, 0);
101 
105  const edm::Handle<reco::PixelClusterCounts> pccHandle = lumiSeg.getHandle(pccToken_);
106  const reco::PixelClusterCounts& inputPcc = *(pccHandle.product());
107  //vector with Module IDs 1-1 map to bunch x-ing in clusers
108  auto modID = inputPcc.readModID();
109  //vector with total events at each bxid.
110  auto events = inputPcc.readEvents();
111  //cluster counts per module per bx
112  auto clustersPerBXInput = inputPcc.readCounts();
113 
117  std::vector<int> goodMods;
118  for (unsigned int i = 0; i < modID.size(); i++) {
119  if (std::find(modVeto_.begin(), modVeto_.end(), modID.at(i)) == modVeto_.end()) {
120  goodMods.push_back(i);
121  }
122  }
123  for (int bx = 0; bx < int(LumiConstants::numBX); bx++) {
124  for (unsigned int i = 0; i < goodMods.size(); i++) {
125  clustersPerBXOutput.at(bx) += clustersPerBXInput.at(goodMods.at(i) * int(LumiConstants::numBX) + bx);
126  }
127  }
128 
132  std::vector<float> correctionScaleFactors;
133  if (applyCorr_) {
134  const auto pccCorrections = &iSetup.getData(lumiCorrectionsToken_);
135  correctionScaleFactors = pccCorrections->getCorrectionsBX();
136  } else {
137  correctionScaleFactors.resize(LumiConstants::numBX, 1.0);
138  }
139 
140  for (unsigned int i = 0; i < clustersPerBXOutput.size(); i++) {
141  if (events.at(i) != 0) {
142  corrClustersPerBXOutput[i] = clustersPerBXOutput[i] * correctionScaleFactors[i];
143  } else {
144  corrClustersPerBXOutput[i] = 0.0;
145  }
146  totalLumi += corrClustersPerBXOutput[i];
147  statErrOnLumi += float(events[i]);
148  }
149 
150  std::vector<float> errorPerBX; //Stat error (or number of events)
151  errorPerBX.assign(events.begin(), events.end());
152 
156  if (takeAverageValue_ == "Average") {
157  unsigned int NActiveBX = 0;
158  for (int bx = 0; bx < int(LumiConstants::numBX); bx++) {
159  if (events[bx] > 0) {
160  NActiveBX++;
161  corrClustersPerBXOutput[bx] /= float(events[bx]);
162  errorPerBX[bx] = 1 / sqrt(float(events[bx]));
163  }
164  }
165  if (statErrOnLumi != 0) {
166  totalLumi = totalLumi / statErrOnLumi * float(NActiveBX);
167  statErrOnLumi = 1 / sqrt(statErrOnLumi) * totalLumi;
168  }
169  }
170 
173  LumiInfo outputLumiInfo;
174  outputLumiInfo.setTotalInstLumi(totalLumi);
175  outputLumiInfo.setTotalInstStatError(statErrOnLumi);
176  outputLumiInfo.setErrorLumiAllBX(errorPerBX);
177  outputLumiInfo.setInstLumiAllBX(corrClustersPerBXOutput);
178  lumiSeg.emplace(putToken_, std::move(outputLumiInfo));
179 
180  //Lumi saved in the csv file
181  if (saveCSVFile_) {
182  std::lock_guard<std::mutex> lock(fileLock_);
183  std::ofstream csfile(csvOutLabel_, std::ios_base::app);
184  csfile << std::to_string(lumiSeg.run()) << ",";
185  csfile << std::to_string(lumiSeg.luminosityBlock()) << ",";
186  csfile << std::to_string(totalLumi);
187 
188  for (unsigned int bx = 0; bx < LumiConstants::numBX; bx++)
189  csfile << "," << std::to_string(corrClustersPerBXOutput[bx]);
190  csfile << std::endl;
191 
192  csfile.close();
193  }
194 }
195 
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const edm::ESGetToken< LumiCorrections, LumiCorrectionsRcd > lumiCorrectionsToken_
std::vector< int > const & readModID() const
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
const std::string csvOutLabel_
static std::mutex mutex
Definition: Proxy.cc:8
T const * product() const
Definition: Handle.h:70
static const unsigned int numBX
Definition: LumiConstants.h:8
std::string to_string(const V &value)
Definition: OMSAccess.h:71
void setInstLumiAllBX(std::vector< float > &instLumiByBX)
Definition: LumiInfo.cc:31
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
const std::string takeAverageValue_
std::vector< int > const & readEvents() const
int iEvent
Definition: GenABIO.cc:224
T sqrt(T t)
Definition: SSEVec.h:19
void setTotalInstLumi(float totalLumi)
Definition: LumiInfo.h:122
const bool applyCorr_
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< int > const & readCounts() const
const edm::EDPutTokenT< LumiInfo > putToken_
void globalEndLuminosityBlockProduce(edm::LuminosityBlock &lumiSeg, const edm::EventSetup &iSetup) const final
void emplace(EDPutTokenT< PROD > token, Args &&... args)
puts a new product
std::mutex fileLock_
~RawPCCProducer() override
void setTotalInstStatError(float statError)
Definition: LumiInfo.h:126
const std::vector< int > modVeto_
fixed size matrix
HLT enums.
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
void setErrorLumiAllBX(std::vector< float > &errLumiByBX)
Definition: LumiInfo.cc:35
const bool saveCSVFile_
RawPCCProducer(const edm::ParameterSet &)
LuminosityBlockNumber_t luminosityBlock() const
void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const final
int events
def move(src, dest)
Definition: eostools.py:511
edm::EDGetTokenT< reco::PixelClusterCounts > pccToken_