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