CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
46  const std::string takeAverageValue_; //Output average values
47 
48  const std::vector<int> modVeto_; //The list of modules to skip in the lumi calc.
49 
52  const bool saveCSVFile_;
53 
54  const bool applyCorr_;
55 };
56 
57 //--------------------------------------------------------------------------------------------------
59  : lumiCorrectionsToken_(esConsumes<edm::Transition::EndLuminosityBlock>()),
60  putToken_{produces<LumiInfo, edm::Transition::EndLuminosityBlock>(
61  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
62  .getUntrackedParameter<std::string>("outputProductName", "alcaLumi"))},
63  takeAverageValue_{iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
64  .getUntrackedParameter<std::string>("OutputValue", std::string("Totals"))},
65  modVeto_{
66  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters").getParameter<std::vector<int>>("modVeto")},
67  csvOutLabel_{iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
68  .getUntrackedParameter<std::string>("label", std::string("rawPCC.csv"))},
69  saveCSVFile_{iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
70  .getUntrackedParameter<bool>("saveCSVFile", false)},
71  applyCorr_{iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters")
72  .getUntrackedParameter<bool>("ApplyCorrections", false)} {
73  auto pccSource =
74  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters").getParameter<std::string>("inputPccLabel");
75  auto prodInst =
76  iConfig.getParameter<edm::ParameterSet>("RawPCCProducerParameters").getParameter<std::string>("ProdInst");
77 
78  pccToken_ = consumes<reco::PixelClusterCounts, edm::InLumi>(edm::InputTag(pccSource, prodInst));
79 }
80 
81 //--------------------------------------------------------------------------------------------------
83 
84 //--------------------------------------------------------------------------------------------------
86 
87 //--------------------------------------------------------------------------------------------------
89  const edm::EventSetup& iSetup) const {
90  float totalLumi = 0.0; //The total raw luminosity from the pixel clusters - not scaled
91  float statErrOnLumi = 0.0; //the statistical error on the lumi - large num ie sqrt(N)
92 
93  //new vector containing clusters per bxid
94  std::vector<int> clustersPerBXOutput(LumiConstants::numBX, 0);
95  //new vector containing clusters per bxid with afterglow corrections
96  std::vector<float> corrClustersPerBXOutput(LumiConstants::numBX, 0);
97 
98  //The indicies of all the good modules - not vetoed
99  std::vector<int> goodMods;
100 
102  lumiSeg.getByToken(pccToken_, pccHandle);
103 
104  const reco::PixelClusterCounts& inputPcc = *(pccHandle.product());
105 
106  //vector with Module IDs 1-1 map to bunch x-ing in clusers
107  auto modID = inputPcc.readModID();
108  //vector with total events at each bxid.
109  auto events = inputPcc.readEvents();
110  auto clustersPerBXInput = inputPcc.readCounts();
111 
112  //making list of modules to sum over
113  for (unsigned int i = 0; i < modID.size(); i++) {
114  if (std::find(modVeto_.begin(), modVeto_.end(), modID.at(i)) == modVeto_.end()) {
115  goodMods.push_back(i);
116  }
117  }
118 
119  //summing over good modules only
120  for (int bx = 0; bx < int(LumiConstants::numBX); bx++) {
121  for (unsigned int i = 0; i < goodMods.size(); i++) {
122  clustersPerBXOutput.at(bx) += clustersPerBXInput.at(goodMods.at(i) * int(LumiConstants::numBX) + bx);
123  }
124  }
125 
126  std::vector<float> correctionScaleFactors;
127  if (applyCorr_) {
128  //Get PCC corrections from the event setup through a token
129  const auto pccCorrections = &iSetup.getData(lumiCorrectionsToken_);
130  correctionScaleFactors = pccCorrections->getCorrectionsBX();
131  } else {
132  correctionScaleFactors.resize(LumiConstants::numBX, 1.0);
133  }
134 
135  for (unsigned int i = 0; i < clustersPerBXOutput.size(); i++) {
136  if (events.at(i) != 0) {
137  corrClustersPerBXOutput[i] = clustersPerBXOutput[i] * correctionScaleFactors[i];
138  } else {
139  corrClustersPerBXOutput[i] = 0.0;
140  }
141  totalLumi += corrClustersPerBXOutput[i];
142  statErrOnLumi += float(events[i]);
143  }
144 
145  std::vector<float> errorPerBX; //Stat error (or number of events)
146  errorPerBX.assign(events.begin(), events.end());
147 
148  if (takeAverageValue_ == "Average") {
149  unsigned int NActiveBX = 0;
150  for (int bx = 0; bx < int(LumiConstants::numBX); bx++) {
151  if (events[bx] > 0) {
152  NActiveBX++;
153  // Counting where events are will only work
154  // for ZeroBias or AlwaysTrue triggers.
155  // Random triggers will get all BXs.
156  corrClustersPerBXOutput[bx] /= float(events[bx]);
157  errorPerBX[bx] = 1 / sqrt(float(events[bx]));
158  }
159  }
160  if (statErrOnLumi != 0) {
161  totalLumi = totalLumi / statErrOnLumi * float(NActiveBX);
162  statErrOnLumi = 1 / sqrt(statErrOnLumi) * totalLumi;
163  }
164  }
165 
166  LumiInfo outputLumiInfo;
167 
168  outputLumiInfo.setTotalInstLumi(totalLumi);
169  outputLumiInfo.setTotalInstStatError(statErrOnLumi);
170 
171  outputLumiInfo.setErrorLumiAllBX(errorPerBX);
172  outputLumiInfo.setInstLumiAllBX(corrClustersPerBXOutput);
173 
174  if (saveCSVFile_) {
175  std::lock_guard<std::mutex> lock(fileLock_);
176  std::ofstream csfile(csvOutLabel_, std::ios_base::app);
177  csfile << std::to_string(lumiSeg.run()) << ",";
178  csfile << std::to_string(lumiSeg.luminosityBlock()) << ",";
179  csfile << std::to_string(totalLumi);
180 
181  if (totalLumi > 0) {
182  for (unsigned int bx = 0; bx < LumiConstants::numBX; bx++) {
183  csfile << "," << std::to_string(corrClustersPerBXOutput[bx]);
184  }
185  csfile << std::endl;
186  } else if (totalLumi < 0) {
187  edm::LogInfo("WARNING") << "WHY IS LUMI NEGATIVE?!?!?!? " << totalLumi;
188  }
189 
190  csfile.close();
191  }
192  lumiSeg.emplace(putToken_, std::move(outputLumiInfo));
193 }
194 
const edm::ESGetToken< LumiCorrections, LumiCorrectionsRcd > lumiCorrectionsToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
const std::string csvOutLabel_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static std::mutex mutex
Definition: Proxy.cc:8
static const unsigned int numBX
Definition: LumiConstants.h:8
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
void emplace(EDPutTokenT< PROD > token, Args &&...args)
puts a new product
const std::string takeAverageValue_
LuminosityBlockNumber_t luminosityBlock() const
bool getData(T &iHolder) const
Definition: EventSetup.h:128
int iEvent
Definition: GenABIO.cc:224
std::vector< int > const & readCounts() const
T sqrt(T t)
Definition: SSEVec.h:19
void setTotalInstLumi(float totalLumi)
Definition: LumiInfo.h:122
std::vector< int > const & readModID() const
def move
Definition: eostools.py:511
const bool applyCorr_
Transition
Definition: Transition.h:12
RunNumber_t run() const
const edm::EDPutTokenT< LumiInfo > putToken_
Log< level::Info, false > LogInfo
void globalEndLuminosityBlockProduce(edm::LuminosityBlock &lumiSeg, const edm::EventSetup &iSetup) const final
T const * product() const
Definition: Handle.h:70
std::mutex fileLock_
~RawPCCProducer() override
void setTotalInstStatError(float statError)
Definition: LumiInfo.h:126
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
const std::vector< int > modVeto_
tuple events
Definition: patZpeak.py:20
void setErrorLumiAllBX(std::vector< float > &errLumiByBX)
Definition: LumiInfo.cc:35
const bool saveCSVFile_
RawPCCProducer(const edm::ParameterSet &)
void produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const final
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::vector< int > const & readEvents() const
edm::EDGetTokenT< reco::PixelClusterCounts > pccToken_