CMS 3D CMS Logo

SiStripGainsPCLHarvester.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // CMSSW includes
27 
28 // user include files
32 #include <iostream>
33 #include <sstream>
34 
35 //********************************************************************************//
37  doStoreOnDB(false),
38  GOOD(0),
39  BAD(0),
40  MASKED(0),
41  NStripAPVs(0),
42  NPixelDets(0),
43  bareTkGeomPtr_(nullptr)
44 {
45 
46  m_Record = ps.getUntrackedParameter<std::string> ("Record" , "SiStripApvGainRcd");
47  CalibrationLevel = ps.getUntrackedParameter<int> ("CalibrationLevel" , 0);
48  MinNrEntries = ps.getUntrackedParameter<double> ("minNrEntries" , 20);
49  m_DQMdir = ps.getUntrackedParameter<std::string> ("DQMdir" , "AlCaReco/SiStripGains");
50  m_calibrationMode = ps.getUntrackedParameter<std::string> ("calibrationMode" , "StdBunch");
51  tagCondition_NClusters = ps.getUntrackedParameter<double> ("NClustersForTagProd", 2E8);
52  tagCondition_GoodFrac = ps.getUntrackedParameter<double> ("GoodFracForTagProd" , 0.95);
53 
54  //Set the monitoring element tag and store
55  dqm_tag_.reserve(7);
56  dqm_tag_.clear();
57  dqm_tag_.push_back( "StdBunch" ); // statistic collection from Standard Collision Bunch @ 3.8 T
58  dqm_tag_.push_back( "StdBunch0T" ); // statistic collection from Standard Collision Bunch @ 0 T
59  dqm_tag_.push_back( "AagBunch" ); // statistic collection from First Collision After Abort Gap @ 3.8 T
60  dqm_tag_.push_back( "AagBunch0T" ); // statistic collection from First Collision After Abort Gap @ 0 T
61  dqm_tag_.push_back( "IsoMuon" ); // statistic collection from Isolated Muon @ 3.8 T
62  dqm_tag_.push_back( "IsoMuon0T" ); // statistic collection from Isolated Muon @ 0 T
63  dqm_tag_.push_back( "Harvest" ); // statistic collection: Harvest
64 
65 }
66 
67 //********************************************************************************//
68 // ------------ method called for each event ------------
69 void
71 {
72  using namespace edm;
73 
74  this->checkBookAPVColls(iSetup); // check whether APV colls are booked and do so if not yet done
75 
76  edm::ESHandle<SiStripGain> gainHandle;
77  iSetup.get<SiStripGainRcd>().get(gainHandle);
78  if(!gainHandle.isValid()){edm::LogError("SiStripGainPCLHarvester")<< "gainHandle is not valid\n"; exit(0);}
79 
80  edm::ESHandle<SiStripQuality> SiStripQuality_;
81  iSetup.get<SiStripQualityRcd>().get(SiStripQuality_);
82 
83  for(unsigned int a=0;a<APVsCollOrdered.size();a++){
84 
85  std::shared_ptr<stAPVGain> APV = APVsCollOrdered[a];
86 
87  if(APV->SubDet==PixelSubdetector::PixelBarrel || APV->SubDet==PixelSubdetector::PixelEndcap) continue;
88 
89  APV->isMasked = SiStripQuality_->IsApvBad(APV->DetId,APV->APVId);
90 
91  if(gainHandle->getNumberOfTags()!=2){edm::LogError("SiStripGainPCLHarvester")<< "NUMBER OF GAIN TAG IS EXPECTED TO BE 2\n";fflush(stdout);exit(0);};
92  float newPreviousGain = gainHandle->getApvGain(APV->APVId,gainHandle->getRange(APV->DetId, 1),1);
93  if(APV->PreviousGain!=1 and newPreviousGain!=APV->PreviousGain)edm::LogWarning("SiStripGainPCLHarvester")<< "WARNING: ParticleGain in the global tag changed\n";
94  APV->PreviousGain = newPreviousGain;
95 
96  float newPreviousGainTick = gainHandle->getApvGain(APV->APVId,gainHandle->getRange(APV->DetId, 0),0);
97  if(APV->PreviousGainTick!=1 and newPreviousGainTick!=APV->PreviousGainTick){
98  edm::LogWarning("SiStripGainPCLHarvester")<< "WARNING: TickMarkGain in the global tag changed\n"<< std::endl
99  <<" APV->SubDet: "<< APV->SubDet << " APV->APVId:" << APV->APVId << std::endl
100  <<" APV->PreviousGainTick: "<<APV->PreviousGainTick<<" newPreviousGainTick: "<<newPreviousGainTick<<std::endl;
101  }
102  APV->PreviousGainTick = newPreviousGainTick;
103  }
104 }
105 
106 //********************************************************************************//
108 
109  edm::LogInfo("SiStripGainsPCLHarvester") << "Starting harvesting statistics" << std::endl;
110 
111  std::string DQM_dir = m_DQMdir;
112 
113  std::string stag = *(std::find(dqm_tag_.begin(), dqm_tag_.end(),m_calibrationMode));
114  if(stag.size()!=0 && stag[0]!='_') stag.insert(0,1,'_');
115 
116  std::string cvi = DQM_dir + std::string("/Charge_Vs_Index") + stag;
117 
118  MonitorElement* Charge_Vs_Index = igetter_.get(cvi.c_str());
119 
120  if (Charge_Vs_Index==0) {
121  edm::LogError("SiStripGainsPCLHarvester") << "Harvesting: could not retrieve " << cvi.c_str()
122  << ", statistics will not be summed!" << std::endl;
123  } else {
124  edm::LogInfo("SiStripGainsPCLHarvester") << "Harvesting "
125  << (Charge_Vs_Index)->getTH2S()->GetEntries() << " more clusters" << std::endl;
126  }
127 
128  algoComputeMPVandGain(Charge_Vs_Index);
129  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(Charge_Vs_Index);
130 
131  // write out the APVGains record
133 
134  if( doStoreOnDB ){
135  if( poolDbService.isAvailable() )
136  poolDbService->writeOne(theAPVGains.get(), poolDbService->currentTime(),m_Record);
137  else
138  throw std::runtime_error("PoolDBService required.");
139  } else {
140  edm::LogInfo("SiStripGainsPCLHarvester") << "Will not produce payload!" << std::endl;
141  }
142 }
143 
144 //********************************************************************************//
145 void
147 
148  unsigned int I=0;
149  TH1F* Proj = NULL;
150  double FitResults[6];
151  double MPVmean = 300;
152 
153  if ( Charge_Vs_Index==0 ) {
154  edm::LogError("SiStripGainsPCLHarvester") << "Harvesting: could not execute algoComputeMPVandGain method because "
155  << m_calibrationMode <<" statistics cannot be retrieved.\n"
156  << "Please check if input contains "
157  << m_calibrationMode <<" data." << std::endl;
158  return;
159  }
160 
161  TH2S *chvsidx = (Charge_Vs_Index)->getTH2S();
162 
163  printf("Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
164  printf("Fitting Charge Distribution :");
165  int TreeStep = APVsColl.size()/50;
166 
167  for(auto it = APVsColl.begin();it!=APVsColl.end();it++,I++){
168 
169  if(I%TreeStep==0){printf(".");fflush(stdout);}
170  std::shared_ptr<stAPVGain> APV = it->second;
171  if(APV->Bin<0) APV->Bin = chvsidx->GetXaxis()->FindBin(APV->Index);
172 
173  if(APV->isMasked){APV->Gain=APV->PreviousGain; MASKED++; continue;}
174 
175  Proj = (TH1F*)(chvsidx->ProjectionY("",chvsidx->GetXaxis()->FindBin(APV->Index),chvsidx->GetXaxis()->FindBin(APV->Index),"e"));
176  if(!Proj)continue;
177 
178  if(CalibrationLevel==0){
179  }else if(CalibrationLevel==1){
180  int SecondAPVId = APV->APVId;
181  if(SecondAPVId%2==0){ SecondAPVId = SecondAPVId+1; }else{ SecondAPVId = SecondAPVId-1; }
182  std::shared_ptr<stAPVGain> APV2 = APVsColl[(APV->DetId<<4) | SecondAPVId];
183  if(APV2->Bin<0) APV2->Bin = chvsidx->GetXaxis()->FindBin(APV2->Index);
184  TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY("",APV2->Bin,APV2->Bin,"e"));
185  if(Proj2){Proj->Add(Proj2,1);delete Proj2;}
186  }else if(CalibrationLevel==2){
187  for(unsigned int i=0;i<16;i++){ //loop up to 6APV for Strip and up to 16 for Pixels
188  auto tmpit = APVsColl.find((APV->DetId<<4) | i);
189  if(tmpit==APVsColl.end())continue;
190  std::shared_ptr<stAPVGain> APV2 = tmpit->second;
191  if(APV2->DetId != APV->DetId || APV2->APVId == APV->APVId)continue;
192  if(APV2->Bin<0) APV2->Bin = chvsidx->GetXaxis()->FindBin(APV2->Index);
193  TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY("",APV2->Bin,APV2->Bin,"e"));
194  if(Proj2){Proj->Add(Proj2,1);delete Proj2;}
195  }
196  } else {
197  CalibrationLevel = 0;
198  printf("Unknown Calibration Level, will assume %i\n",CalibrationLevel);
199  }
200 
201  getPeakOfLandau(Proj,FitResults);
202  APV->FitMPV = FitResults[0];
203  APV->FitMPVErr = FitResults[1];
204  APV->FitWidth = FitResults[2];
205  APV->FitWidthErr = FitResults[3];
206  APV->FitChi2 = FitResults[4];
207  APV->FitNorm = FitResults[5];
208  APV->NEntries = Proj->GetEntries();
209 
210  if(IsGoodLandauFit(FitResults)){
211  APV->Gain = APV->FitMPV / MPVmean;
212  if(APV->SubDet>2)GOOD++;
213  }else{
214  APV->Gain = APV->PreviousGain;
215  if(APV->SubDet>2)BAD++;
216  }
217  if(APV->Gain<=0) APV->Gain = 1;
218 
219  delete Proj;
220  }printf("\n");
221 }
222 
223 //********************************************************************************//
224 void
225 SiStripGainsPCLHarvester::getPeakOfLandau(TH1* InputHisto, double* FitResults, double LowRange, double HighRange)
226 {
227  FitResults[0] = -0.5; //MPV
228  FitResults[1] = 0; //MPV error
229  FitResults[2] = -0.5; //Width
230  FitResults[3] = 0; //Width error
231  FitResults[4] = -0.5; //Fit Chi2/NDF
232  FitResults[5] = 0; //Normalization
233 
234  if( InputHisto->GetEntries() < MinNrEntries)return;
235 
236  // perform fit with standard landau
237  TF1 MyLandau("MyLandau","landau",LowRange, HighRange);
238  MyLandau.SetParameter(1,300);
239  InputHisto->Fit(&MyLandau,"0QR WW");
240 
241  // MPV is parameter 1 (0=constant, 1=MPV, 2=Sigma)
242  FitResults[0] = MyLandau.GetParameter(1); //MPV
243  FitResults[1] = MyLandau.GetParError(1); //MPV error
244  FitResults[2] = MyLandau.GetParameter(2); //Width
245  FitResults[3] = MyLandau.GetParError(2); //Width error
246  FitResults[4] = MyLandau.GetChisquare() / MyLandau.GetNDF(); //Fit Chi2/NDF
247  FitResults[5] = MyLandau.GetParameter(0);
248 
249 }
250 
251 //********************************************************************************//
252 bool
254  if(FitResults[0] <= 0 )return false;
255  // if(FitResults[1] > MaxMPVError )return false;
256  // if(FitResults[4] > MaxChi2OverNDF)return false;
257  return true;
258 }
259 
260 
261 //********************************************************************************//
262 // ------------ method called once each job just before starting event loop ------------
263 void
265 
267  const TrackerGeometry *newBareTkGeomPtr = &(*tkGeom_);
268  if (newBareTkGeomPtr == bareTkGeomPtr_) return; // already filled APVColls, nothing changed
269 
270  if (!bareTkGeomPtr_) { // pointer not yet set: called the first time => fill the APVColls
271  auto const & Det = newBareTkGeomPtr->dets();
272 
273  unsigned int Index=0;
274 
275  for(unsigned int i=0;i<Det.size();i++){
276 
277  DetId Detid = Det[i]->geographicalId();
278  int SubDet = Detid.subdetId();
279 
280  if( SubDet == StripSubdetector::TIB || SubDet == StripSubdetector::TID ||
281  SubDet == StripSubdetector::TOB || SubDet == StripSubdetector::TEC ){
282 
283  auto DetUnit = dynamic_cast<const StripGeomDetUnit*> (Det[i]);
284  if(!DetUnit)continue;
285 
286  const StripTopology& Topo = DetUnit->specificTopology();
287  unsigned int NAPV = Topo.nstrips()/128;
288 
289  for(unsigned int j=0;j<NAPV;j++){
290  auto APV = std::make_shared<stAPVGain>();
291  APV->Index = Index;
292  APV->Bin = -1;
293  APV->DetId = Detid.rawId();
294  APV->APVId = j;
295  APV->SubDet = SubDet;
296  APV->FitMPV = -1;
297  APV->FitMPVErr = -1;
298  APV->FitWidth = -1;
299  APV->FitWidthErr = -1;
300  APV->FitChi2 = -1;
301  APV->FitNorm = -1;
302  APV->Gain = -1;
303  APV->PreviousGain = 1;
304  APV->PreviousGainTick = 1;
305  APV->x = DetUnit->position().basicVector().x();
306  APV->y = DetUnit->position().basicVector().y();
307  APV->z = DetUnit->position().basicVector().z();
308  APV->Eta = DetUnit->position().basicVector().eta();
309  APV->Phi = DetUnit->position().basicVector().phi();
310  APV->R = DetUnit->position().basicVector().transverse();
311  APV->Thickness = DetUnit->surface().bounds().thickness();
312  APV->NEntries = 0;
313  APV->isMasked = false;
314 
315  APVsCollOrdered.push_back(APV);
316  APVsColl[(APV->DetId<<4) | APV->APVId] = APV;
317  Index++;
318  NStripAPVs++;
319  } // loop on APVs
320  } // if is Strips
321  } // loop on dets
322 
323  for(unsigned int i=0;i<Det.size();i++){ //Make two loop such that the Pixel information is added at the end --> make transition simpler
324  DetId Detid = Det[i]->geographicalId();
325  int SubDet = Detid.subdetId();
327  auto DetUnit = dynamic_cast<const PixelGeomDetUnit*> (Det[i]);
328  if(!DetUnit) continue;
329 
330  const PixelTopology& Topo = DetUnit->specificTopology();
331  unsigned int NROCRow = Topo.nrows()/(80.);
332  unsigned int NROCCol = Topo.ncolumns()/(52.);
333 
334  for(unsigned int j=0;j<NROCRow;j++){
335  for(unsigned int i=0;i<NROCCol;i++){
336  auto APV = std::make_shared<stAPVGain>();
337  APV->Index = Index;
338  APV->Bin = -1;
339  APV->DetId = Detid.rawId();
340  APV->APVId = (j<<3 | i);
341  APV->SubDet = SubDet;
342  APV->FitMPV = -1;
343  APV->FitMPVErr = -1;
344  APV->FitWidth = -1;
345  APV->FitWidthErr = -1;
346  APV->FitChi2 = -1;
347  APV->Gain = -1;
348  APV->PreviousGain = 1;
349  APV->PreviousGainTick = 1;
350  APV->x = DetUnit->position().basicVector().x();
351  APV->y = DetUnit->position().basicVector().y();
352  APV->z = DetUnit->position().basicVector().z();
353  APV->Eta = DetUnit->position().basicVector().eta();
354  APV->Phi = DetUnit->position().basicVector().phi();
355  APV->R = DetUnit->position().basicVector().transverse();
356  APV->Thickness = DetUnit->surface().bounds().thickness();
357  APV->isMasked = false; //SiPixelQuality_->IsModuleBad(Detid.rawId());
358  APV->NEntries = 0;
359 
360  APVsCollOrdered.push_back(APV);
361  APVsColl[(APV->DetId<<4) | APV->APVId] = APV;
362  Index++;
363  NPixelDets++;
364 
365  } // loop on ROC cols
366  } // loop on ROC rows
367  } // if Pixel
368  } // loop on Dets
369  } //if (!bareTkGeomPtr_) ...
370  bareTkGeomPtr_ = newBareTkGeomPtr;
371 }
372 
373 //********************************************************************************//
375 
376  // The goal of this function is to check wether or not there is enough statistics
377  // to produce a meaningful tag for the DB
378 
379  if( Charge_Vs_Index==0 ) {
380  edm::LogError("SiStripGainsPCLHarvester") << "produceTagFilter -> Return false: could not retrieve the "
381  << m_calibrationMode <<" statistics.\n"
382  << "Please check if input contains "
383  << m_calibrationMode << " data." << std::endl;
384  return false;
385  }
386 
387 
388  float integral = (Charge_Vs_Index)->getTH2S()->Integral();
389  if( (Charge_Vs_Index)->getTH2S()->Integral(0,NStripAPVs+1, 0, 99999 ) < tagCondition_NClusters) {
390  edm::LogWarning("SiStripGainsPCLHarvester")
391  << "calibrationMode -> " << m_calibrationMode << "\n"
392  << "produceTagFilter -> Return false: Statistics is too low : " << integral << std::endl;
393  return false;
394  }
395  if((1.0 * GOOD) / (GOOD+BAD) < tagCondition_GoodFrac) {
396  edm::LogWarning("SiStripGainsPCLHarvester")
397  << "calibrationMode -> " << m_calibrationMode << "\n"
398  << "produceTagFilter -> Return false: ratio of GOOD/TOTAL is too low: " << (1.0 * GOOD) / (GOOD+BAD) << std::endl;
399  return false;
400  }
401  return true;
402 }
403 
404 //********************************************************************************//
405 std::unique_ptr<SiStripApvGain>
407 {
408  std::unique_ptr<SiStripApvGain> obj = std::unique_ptr<SiStripApvGain>(new SiStripApvGain());
409 
410  if(!produceTagFilter(Charge_Vs_Index)){
411  edm::LogWarning("SiStripGainsPCLHarvester")<< "getNewObject -> will not produce a paylaod because produceTagFilter returned false " << std::endl;
412  return obj;
413  } else {
414  doStoreOnDB=true;
415  }
416 
417  std::vector<float> theSiStripVector;
418  unsigned int PreviousDetId = 0;
419  for(unsigned int a=0;a<APVsCollOrdered.size();a++){
420  std::shared_ptr<stAPVGain> APV = APVsCollOrdered[a];
421  if(APV==NULL){ printf("Bug\n"); continue; }
422  if(APV->SubDet<=2)continue;
423  if(APV->DetId != PreviousDetId){
424  if(!theSiStripVector.empty()){
425  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
426  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
427  }
428  theSiStripVector.clear();
429  PreviousDetId = APV->DetId;
430  }
431  theSiStripVector.push_back(APV->Gain);
432 
433  LogDebug("SiStripGainsPCLHarvester")<<" DetId: "<<APV->DetId
434  <<" APV: "<<APV->APVId
435  <<" Gain: "<<APV->Gain
436  <<std::endl;
437 
438  }
439  if(!theSiStripVector.empty()){
440  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
441  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
442  }
443 
444  return obj;
445 }
446 
447 
448 //********************************************************************************//
449 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
450 void
452 
454  desc.setUnknown();
455  descriptions.addDefault(desc);
456 
457 }
458 
459 //********************************************************************************//
461 }
462 
#define LogDebug(id)
std::unique_ptr< SiStripApvGain > getNewObject(const MonitorElement *Charge_Vs_Index)
bool IsApvBad(const uint32_t &detid, const short &apvNb) const
T getUntrackedParameter(std::string const &, T const &) const
virtual int nrows() const =0
virtual void dqmEndJob(DQMStore::IBooker &ibooker_, DQMStore::IGetter &igetter_)
edm::ESHandle< TrackerGeometry > tkGeom_
const DetContainer & dets() const
Returm a vector of all GeomDet (including all GeomDetUnits)
size_t getNumberOfTags() const
Definition: SiStripGain.h:87
MonitorElement * get(const std::string &path)
Definition: DQMStore.cc:305
virtual void beginRun(edm::Run const &run, edm::EventSetup const &isetup)
std::vector< std::string > dqm_tag_
#define NULL
Definition: scimark2.h:8
bool produceTagFilter(const MonitorElement *Charge_Vs_Index)
#define nullptr
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
void getPeakOfLandau(TH1 *InputHisto, double *FitResults, double LowRange=50, double HighRange=5400)
void algoComputeMPVandGain(const MonitorElement *Charge_Vs_Index)
static float getApvGain(const uint16_t &apv, const SiStripApvGain::Range &range)
Definition: SiStripGain.h:69
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
SiStripGainsPCLHarvester(const edm::ParameterSet &ps)
Data available (reporting)
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool isAvailable() const
Definition: Service.h:46
std::pair< ContainerIterator, ContainerIterator > Range
const std::complex< double > I
Definition: I.h:8
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
double const GOOD
Definition: Constants.h:15
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
virtual void endRun(edm::Run const &run, edm::EventSetup const &isetup)
Integral< F, X >::type integral(const F &f)
Definition: Integral.h:69
Definition: DetId.h:18
bool IsGoodLandauFit(double *FitResults)
double const BAD
Definition: Constants.h:17
virtual int nstrips() const =0
const T & get() const
Definition: EventSetup.h:56
const TrackerGeometry * bareTkGeomPtr_
virtual void checkBookAPVColls(const edm::EventSetup &setup)
std::vector< std::shared_ptr< stAPVGain > > APVsCollOrdered
HLT enums.
virtual int ncolumns() const =0
double a
Definition: hdecay.h:121
bool isValid() const
Definition: ESHandle.h:47
Definition: Run.h:42
const SiStripApvGain::Range getRange(uint32_t detID) const
Definition: SiStripGain.h:66
std::unordered_map< unsigned int, std::shared_ptr< stAPVGain > > APVsColl