CMS 3D CMS Logo

SiStripGainsPCLHarvester.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // CMSSW includes
28 
29 // user include files
34 #include <iostream>
35 #include <sstream>
36 
37 //********************************************************************************//
39  doStoreOnDB(false),
40  GOOD(0),
41  BAD(0),
42  MASKED(0),
43  NStripAPVs(0),
44  NPixelDets(0),
45  bareTkGeomPtr_(nullptr),
46  tTopo_(nullptr)
47 {
48 
49  m_Record = ps.getUntrackedParameter<std::string> ("Record" , "SiStripApvGainRcd");
50  CalibrationLevel = ps.getUntrackedParameter<int> ("CalibrationLevel" , 0);
51  MinNrEntries = ps.getUntrackedParameter<double> ("minNrEntries" , 20);
52  m_DQMdir = ps.getUntrackedParameter<std::string> ("DQMdir" , "AlCaReco/SiStripGains");
53  m_calibrationMode = ps.getUntrackedParameter<std::string> ("calibrationMode" , "StdBunch");
54  tagCondition_NClusters = ps.getUntrackedParameter<double> ("NClustersForTagProd", 2E8);
55  tagCondition_GoodFrac = ps.getUntrackedParameter<double> ("GoodFracForTagProd" , 0.95);
56  doChargeMonitorPerPlane = ps.getUntrackedParameter<bool> ("doChargeMonitorPerPlane" , false);
57  VChargeHisto = ps.getUntrackedParameter<std::vector<std::string> > ("ChargeHisto");
58 
59  //Set the monitoring element tag and store
60  dqm_tag_.reserve(7);
61  dqm_tag_.clear();
62  dqm_tag_.push_back( "StdBunch" ); // statistic collection from Standard Collision Bunch @ 3.8 T
63  dqm_tag_.push_back( "StdBunch0T" ); // statistic collection from Standard Collision Bunch @ 0 T
64  dqm_tag_.push_back( "AagBunch" ); // statistic collection from First Collision After Abort Gap @ 3.8 T
65  dqm_tag_.push_back( "AagBunch0T" ); // statistic collection from First Collision After Abort Gap @ 0 T
66  dqm_tag_.push_back( "IsoMuon" ); // statistic collection from Isolated Muon @ 3.8 T
67  dqm_tag_.push_back( "IsoMuon0T" ); // statistic collection from Isolated Muon @ 0 T
68  dqm_tag_.push_back( "Harvest" ); // statistic collection: Harvest
69 
70 }
71 
72 //********************************************************************************//
73 // ------------ method called for each event ------------
74 void
76 {
77  using namespace edm;
78 
79  this->checkBookAPVColls(iSetup); // check whether APV colls are booked and do so if not yet done
80  this->checkAndRetrieveTopology(iSetup);
81 
82  edm::ESHandle<SiStripGain> gainHandle;
83  iSetup.get<SiStripGainRcd>().get(gainHandle);
84  if(!gainHandle.isValid()){edm::LogError("SiStripGainPCLHarvester")<< "gainHandle is not valid\n"; exit(0);}
85 
86  edm::ESHandle<SiStripQuality> SiStripQuality_;
87  iSetup.get<SiStripQualityRcd>().get(SiStripQuality_);
88 
89  for(unsigned int a=0;a<APVsCollOrdered.size();a++){
90 
91  std::shared_ptr<stAPVGain> APV = APVsCollOrdered[a];
92 
93  if(APV->SubDet==PixelSubdetector::PixelBarrel || APV->SubDet==PixelSubdetector::PixelEndcap) continue;
94 
95  APV->isMasked = SiStripQuality_->IsApvBad(APV->DetId,APV->APVId);
96 
97  if(gainHandle->getNumberOfTags()!=2){edm::LogError("SiStripGainPCLHarvester")<< "NUMBER OF GAIN TAG IS EXPECTED TO BE 2\n";fflush(stdout);exit(0);};
98  float newPreviousGain = gainHandle->getApvGain(APV->APVId,gainHandle->getRange(APV->DetId, 1),1);
99  if(APV->PreviousGain!=1 and newPreviousGain!=APV->PreviousGain)edm::LogWarning("SiStripGainPCLHarvester")<< "WARNING: ParticleGain in the global tag changed\n";
100  APV->PreviousGain = newPreviousGain;
101 
102  float newPreviousGainTick = gainHandle->getApvGain(APV->APVId,gainHandle->getRange(APV->DetId, 0),0);
103  if(APV->PreviousGainTick!=1 and newPreviousGainTick!=APV->PreviousGainTick){
104  edm::LogWarning("SiStripGainPCLHarvester")<< "WARNING: TickMarkGain in the global tag changed\n"<< std::endl
105  <<" APV->SubDet: "<< APV->SubDet << " APV->APVId:" << APV->APVId << std::endl
106  <<" APV->PreviousGainTick: "<<APV->PreviousGainTick<<" newPreviousGainTick: "<<newPreviousGainTick<<std::endl;
107  }
108  APV->PreviousGainTick = newPreviousGainTick;
109  }
110 }
111 
112 //********************************************************************************//
114 
115  edm::LogInfo("SiStripGainsPCLHarvester") << "Starting harvesting statistics" << std::endl;
116 
117  std::string DQM_dir = m_DQMdir;
118 
119  std::string stag = *(std::find(dqm_tag_.begin(), dqm_tag_.end(),m_calibrationMode));
120  if(stag.size()!=0 && stag[0]!='_') stag.insert(0,1,'_');
121 
122  std::string cvi = DQM_dir + std::string("/Charge_Vs_Index") + stag;
123 
124  MonitorElement* Charge_Vs_Index = igetter_.get(cvi.c_str());
125 
126  if (Charge_Vs_Index==0) {
127  edm::LogError("SiStripGainsPCLHarvester") << "Harvesting: could not retrieve " << cvi.c_str()
128  << ", statistics will not be summed!" << std::endl;
129  } else {
130  edm::LogInfo("SiStripGainsPCLHarvester") << "Harvesting "
131  << (Charge_Vs_Index)->getTH2S()->GetEntries() << " more clusters" << std::endl;
132  }
133 
134  algoComputeMPVandGain(Charge_Vs_Index);
135  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(Charge_Vs_Index);
136 
137  // write out the APVGains record
139 
140  if( doStoreOnDB ){
141  if( poolDbService.isAvailable() )
142  poolDbService->writeOne(theAPVGains.get(), poolDbService->currentTime(),m_Record);
143  else
144  throw std::runtime_error("PoolDBService required.");
145  } else {
146  edm::LogInfo("SiStripGainsPCLHarvester") << "Will not produce payload!" << std::endl;
147  }
148 
149 
150  //Collect the statistics for monitoring and validation
151  gainQualityMonitor(ibooker_,Charge_Vs_Index);
152 
153 }
154 
155 //********************************************************************************//
156 void
158  ibooker_.setCurrentFolder("AlCaReco/SiStripGainsHarvesting/");
159 
160 
161  std::vector<APVGain::APVmon> new_charge_histos;
162  std::vector<std::pair<std::string,std::string>> cnames = APVGain::monHnames(VChargeHisto,doChargeMonitorPerPlane,"newG2");
163  for (unsigned int i=0;i<cnames.size();i++){
164  MonitorElement* monitor = ibooker_.book1DD( (cnames[i]).first, (cnames[i]).second.c_str(), 100 , 0. , 1000. );
165  int id = APVGain::subdetectorId((cnames[i]).first);
166  int side = APVGain::subdetectorSide((cnames[i]).first);
167  int plane = APVGain::subdetectorPlane((cnames[i]).first);
168  new_charge_histos.push_back( APVGain::APVmon(id,side,plane,monitor) );
169  }
170 
171  int MPVbin = 300;
172  float MPVmin = 0.;
173  float MPVmax = 600.;
174 
175  MonitorElement* MPV_Vs_EtaTIB = ibooker_.book2DD("MPVvsEtaTIB" ,"MPV vs Eta TIB" ,50,-3.0,3.0,MPVbin,MPVmin,MPVmax);
176  MonitorElement* MPV_Vs_EtaTID = ibooker_.book2DD("MPVvsEtaTID" ,"MPV vs Eta TID" ,50,-3.0,3.0,MPVbin,MPVmin,MPVmax);
177  MonitorElement* MPV_Vs_EtaTOB = ibooker_.book2DD("MPVvsEtaTOB" ,"MPV vs Eta TOB" ,50,-3.0,3.0,MPVbin,MPVmin,MPVmax);
178  MonitorElement* MPV_Vs_EtaTEC = ibooker_.book2DD("MPVvsEtaTEC" ,"MPV vs Eta TEC" ,50,-3.0,3.0,MPVbin,MPVmin,MPVmax);
179  MonitorElement* MPV_Vs_EtaTECthin = ibooker_.book2DD("MPVvsEtaTEC1","MPV vs Eta TEC-thin" ,50,-3.0,3.0,MPVbin,MPVmin,MPVmax);
180  MonitorElement* MPV_Vs_EtaTECthick = ibooker_.book2DD("MPVvsEtaTEC2","MPV vs Eta TEC-thick",50,-3.0,3.0,MPVbin,MPVmin,MPVmax);
181 
182  MonitorElement* MPV_Vs_PhiTIB = ibooker_.book2DD("MPVvsPhiTIB" ,"MPV vs Phi TIB" ,50,-3.4,3.4,MPVbin,MPVmin,MPVmax);
183  MonitorElement* MPV_Vs_PhiTID = ibooker_.book2DD("MPVvsPhiTID" ,"MPV vs Phi TID" ,50,-3.4,3.4,MPVbin,MPVmin,MPVmax);
184  MonitorElement* MPV_Vs_PhiTOB = ibooker_.book2DD("MPVvsPhiTOB" ,"MPV vs Phi TOB" ,50,-3.4,3.4,MPVbin,MPVmin,MPVmax);
185  MonitorElement* MPV_Vs_PhiTEC = ibooker_.book2DD("MPVvsPhiTEC" ,"MPV vs Phi TEC" ,50,-3.4,3.4,MPVbin,MPVmin,MPVmax);
186  MonitorElement* MPV_Vs_PhiTECthin = ibooker_.book2DD("MPVvsPhiTEC1","MPV vs Phi TEC-thin ",50,-3.4,3.4,MPVbin,MPVmin,MPVmax);
187  MonitorElement* MPV_Vs_PhiTECthick = ibooker_.book2DD("MPVvsPhiTEC2","MPV vs Phi TEC-thick",50,-3.4,3.4,MPVbin,MPVmin,MPVmax);
188 
189  MonitorElement* NoMPV = ibooker_.book2DD("NoMPV" ,"NoMPV" ,350, -350, 350, 240, 0, 120);
190 
191  MonitorElement* Gains = ibooker_.book1DD("Gains" ,"Gains" , 300, 0, 2);
192  MonitorElement* MPVs = ibooker_.book1DD("MPVs" ,"MPVs" , MPVbin,MPVmin,MPVmax);
193  MonitorElement* MPVs320 = ibooker_.book1DD("MPV_320" ,"MPV 320 thickness", MPVbin,MPVmin,MPVmax);
194  MonitorElement* MPVs500 = ibooker_.book1DD("MPV_500" ,"MPV 500 thickness", MPVbin,MPVmin,MPVmax);
195  MonitorElement* MPVsTIB = ibooker_.book1DD("MPV_TIB" ,"MPV TIB" , MPVbin,MPVmin,MPVmax);
196  MonitorElement* MPVsTID = ibooker_.book1DD("MPV_TID" ,"MPV TID" , MPVbin,MPVmin,MPVmax);
197  MonitorElement* MPVsTIDP = ibooker_.book1DD("MPV_TIDP" ,"MPV TIDP" , MPVbin,MPVmin,MPVmax);
198  MonitorElement* MPVsTIDM = ibooker_.book1DD("MPV_TIDM" ,"MPV TIDM" , MPVbin,MPVmin,MPVmax);
199  MonitorElement* MPVsTOB = ibooker_.book1DD("MPV_TOB" ,"MPV TOB" , MPVbin,MPVmin,MPVmax);
200  MonitorElement* MPVsTEC = ibooker_.book1DD("MPV_TEC" ,"MPV TEC" , MPVbin,MPVmin,MPVmax);
201  MonitorElement* MPVsTECP = ibooker_.book1DD("MPV_TECP" ,"MPV TECP" , MPVbin,MPVmin,MPVmax);
202  MonitorElement* MPVsTECM = ibooker_.book1DD("MPV_TECM" ,"MPV TECM" , MPVbin,MPVmin,MPVmax);
203  MonitorElement* MPVsTECthin = ibooker_.book1DD("MPV_TEC1" ,"MPV TEC thin" , MPVbin,MPVmin,MPVmax);
204  MonitorElement* MPVsTECthick = ibooker_.book1DD("MPV_TEC2" ,"MPV TEC thick" , MPVbin,MPVmin,MPVmax);
205  MonitorElement* MPVsTECP1 = ibooker_.book1DD("MPV_TECP1" ,"MPV TECP thin " , MPVbin,MPVmin,MPVmax);
206  MonitorElement* MPVsTECP2 = ibooker_.book1DD("MPV_TECP2" ,"MPV TECP thick" , MPVbin,MPVmin,MPVmax);
207  MonitorElement* MPVsTECM1 = ibooker_.book1DD("MPV_TECM1" ,"MPV TECM thin" , MPVbin,MPVmin,MPVmax);
208  MonitorElement* MPVsTECM2 = ibooker_.book1DD("MPV_TECM2" ,"MPV TECM thick" , MPVbin,MPVmin,MPVmax);
209 
210  MonitorElement* MPVError = ibooker_.book1DD("MPVError" ,"MPV Error" , 150, 0, 150);
211  MonitorElement* MPVErrorVsMPV = ibooker_.book2DD("MPVErrorVsMPV" ,"MPV Error vs MPV" , 300, 0, 600, 150, 0, 150);
212  MonitorElement* MPVErrorVsEta = ibooker_.book2DD("MPVErrorVsEta" ,"MPV Error vs Eta" , 50, -3.0, 3.0, 150, 0, 150);
213  MonitorElement* MPVErrorVsPhi = ibooker_.book2DD("MPVErrorVsPhi" ,"MPV Error vs Phi" , 50, -3.4, 3.4, 150, 0, 150);
214  MonitorElement* MPVErrorVsN = ibooker_.book2DD("MPVErrorVsN" ,"MPV Error vs N" , 500, 0, 1000, 150, 0, 150);
215 
216  MonitorElement* DiffWRTPrevGainTIB = ibooker_.book1DD("DiffWRTPrevGainTIB" ,"Diff w.r.t. PrevGain TIB" , 250, 0,2);
217  MonitorElement* DiffWRTPrevGainTID = ibooker_.book1DD("DiffWRTPrevGainTID" ,"Diff w.r.t. PrevGain TID" , 250, 0,2);
218  MonitorElement* DiffWRTPrevGainTOB = ibooker_.book1DD("DiffWRTPrevGainTOB" ,"Diff w.r.t. PrevGain TOB" , 250, 0,2);
219  MonitorElement* DiffWRTPrevGainTEC = ibooker_.book1DD("DiffWRTPrevGainTEC" ,"Diff w.r.t. PrevGain TEC" , 250, 0,2);
220 
221  MonitorElement* GainVsPrevGainTIB = ibooker_.book2DD("GainVsPrevGainTIB" ,"Gain vs PrevGain TIB" , 100, 0,2, 100, 0,2);
222  MonitorElement* GainVsPrevGainTID = ibooker_.book2DD("GainVsPrevGainTID" ,"Gain vs PrevGain TID" , 100, 0,2, 100, 0,2);
223  MonitorElement* GainVsPrevGainTOB = ibooker_.book2DD("GainVsPrevGainTOB" ,"Gain vs PrevGain TOB" , 100, 0,2, 100, 0,2);
224  MonitorElement* GainVsPrevGainTEC = ibooker_.book2DD("GainVsPrevGainTEC" ,"Gain vs PrevGain TEC" , 100, 0,2, 100, 0,2);
225 
226 
227  for(unsigned int a=0;a<APVsCollOrdered.size();a++){
228 
229  std::shared_ptr<stAPVGain> APV = APVsCollOrdered[a];
230  if(APV==NULL)continue;
231 
232  unsigned int Index = APV->Index;
233  unsigned int SubDet = APV->SubDet;
234  unsigned int DetId = APV->DetId;
235  float z = APV->z;
236  float Eta = APV->Eta;
237  float R = APV->R;
238  float Phi = APV->Phi;
239  float Thickness = APV->Thickness;
240  double FitMPV = APV->FitMPV;
241  double FitMPVErr = APV->FitMPVErr;
242  double Gain = APV->Gain;
243  double NEntries = APV->NEntries;
244  double PreviousGain = APV->PreviousGain;
245 
246  if (SubDet<3) continue; // avoid to loop over Pixel det id
247 
248  if (Gain!=1.) {
249  std::vector<MonitorElement*> charge_histos = APVGain::FetchMonitor(new_charge_histos, DetId, tTopo_);
250  TH2S *chvsidx = (Charge_Vs_Index)->getTH2S();
251  int bin = chvsidx->GetXaxis()->FindBin(Index);
252  TH1D* Proj = chvsidx->ProjectionY("proj",bin,bin);
253  for (int binId=0; binId<Proj->GetXaxis()->GetNbins();binId++) {
254  double new_charge = Proj->GetXaxis()->GetBinCenter(binId) / Gain;
255  if (Proj->GetBinContent(binId)!=0.) {
256  for (unsigned int h=0;h<charge_histos.size();h++) {
257  TH1D* chisto = (charge_histos[h])->getTH1D();
258  for (int e=0;e<Proj->GetBinContent(binId);e++) chisto->Fill(new_charge);
259  }
260  }
261  }
262  }
263 
264 
265  if (FitMPV<0.) { // No fit of MPV
266  NoMPV->Fill(z,R);
267 
268  } else { // Fit of MPV
269  if(FitMPV>0.) Gains->Fill(Gain);
270 
271  MPVs->Fill(FitMPV);
272  if(Thickness<0.04) MPVs320->Fill(Phi,FitMPV);
273  if(Thickness>0.04) MPVs500->Fill(Phi,FitMPV);
274 
275  MPVError->Fill(FitMPVErr);
276  MPVErrorVsMPV->Fill(FitMPV,FitMPVErr);
277  MPVErrorVsEta->Fill(Eta,FitMPVErr);
278  MPVErrorVsPhi->Fill(Phi,FitMPVErr);
279  MPVErrorVsN->Fill(NEntries,FitMPVErr);
280 
281  if(SubDet==3) {
282  MPV_Vs_EtaTIB->Fill(Eta,FitMPV);
283  MPV_Vs_PhiTIB->Fill(Phi,FitMPV);
284  MPVsTIB->Fill(FitMPV);
285 
286  } else if(SubDet==4) {
287  MPV_Vs_EtaTID->Fill(Eta,FitMPV);
288  MPV_Vs_PhiTID->Fill(Phi,FitMPV);
289  MPVsTID->Fill(FitMPV);
290  if(Eta<0.) MPVsTIDM->Fill(FitMPV);
291  if(Eta>0.) MPVsTIDP->Fill(FitMPV);
292 
293  } else if (SubDet==5) {
294  MPV_Vs_EtaTOB->Fill(Eta,FitMPV);
295  MPV_Vs_PhiTOB->Fill(Phi,FitMPV);
296  MPVsTOB->Fill(FitMPV);
297 
298  } else if (SubDet==6) {
299  MPV_Vs_EtaTEC->Fill(Eta,FitMPV);
300  MPV_Vs_PhiTEC->Fill(Phi,FitMPV);
301  MPVsTEC->Fill(FitMPV);
302  if(Eta<0.) MPVsTECM->Fill(FitMPV);
303  if(Eta>0.) MPVsTECP->Fill(FitMPV);
304  if(Thickness<0.04) {
305  MPV_Vs_EtaTECthin->Fill(Eta,FitMPV);
306  MPV_Vs_PhiTECthin->Fill(Phi,FitMPV);
307  MPVsTECthin->Fill(FitMPV);
308  if(Eta>0.) MPVsTECP1->Fill(FitMPV);
309  if(Eta<0.) MPVsTECM1->Fill(FitMPV);
310  }
311  if(Thickness>0.04) {
312  MPV_Vs_EtaTECthick->Fill(Eta,FitMPV);
313  MPV_Vs_PhiTECthick->Fill(Phi,FitMPV);
314  MPVsTECthick->Fill(FitMPV);
315  if(Eta>0.) MPVsTECP2->Fill(FitMPV);
316  if(Eta<0.) MPVsTECM2->Fill(FitMPV);
317  }
318  }
319  }
320 
321  if(SubDet==3 && PreviousGain!=0. ) DiffWRTPrevGainTIB->Fill(Gain/PreviousGain);
322  else if(SubDet==4 && PreviousGain!=0. ) DiffWRTPrevGainTID->Fill(Gain/PreviousGain);
323  else if(SubDet==5 && PreviousGain!=0. ) DiffWRTPrevGainTOB->Fill(Gain/PreviousGain);
324  else if(SubDet==6 && PreviousGain!=0. ) DiffWRTPrevGainTEC->Fill(Gain/PreviousGain);
325 
326  if(SubDet==3 ) GainVsPrevGainTIB->Fill(PreviousGain,Gain);
327  else if(SubDet==4 ) GainVsPrevGainTID->Fill(PreviousGain,Gain);
328  else if(SubDet==5 ) GainVsPrevGainTOB->Fill(PreviousGain,Gain);
329  else if(SubDet==6 ) GainVsPrevGainTEC->Fill(PreviousGain,Gain);
330 
331  }
332 }
333 
334 
335 //********************************************************************************//
336 void
338 
339  unsigned int I=0;
340  TH1F* Proj = NULL;
341  double FitResults[6];
342  double MPVmean = 300;
343 
344  if ( Charge_Vs_Index==0 ) {
345  edm::LogError("SiStripGainsPCLHarvester") << "Harvesting: could not execute algoComputeMPVandGain method because "
346  << m_calibrationMode <<" statistics cannot be retrieved.\n"
347  << "Please check if input contains "
348  << m_calibrationMode <<" data." << std::endl;
349  return;
350  }
351 
352  TH2S *chvsidx = (Charge_Vs_Index)->getTH2S();
353 
354  printf("Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
355  printf("Fitting Charge Distribution :");
356  int TreeStep = APVsColl.size()/50;
357 
358  for(auto it = APVsColl.begin();it!=APVsColl.end();it++,I++){
359 
360  if(I%TreeStep==0){printf(".");fflush(stdout);}
361  std::shared_ptr<stAPVGain> APV = it->second;
362  if(APV->Bin<0) APV->Bin = chvsidx->GetXaxis()->FindBin(APV->Index);
363 
364  if(APV->isMasked){APV->Gain=APV->PreviousGain; MASKED++; continue;}
365 
366  Proj = (TH1F*)(chvsidx->ProjectionY("",chvsidx->GetXaxis()->FindBin(APV->Index),chvsidx->GetXaxis()->FindBin(APV->Index),"e"));
367  if(!Proj)continue;
368 
369  if(CalibrationLevel==0){
370  }else if(CalibrationLevel==1){
371  int SecondAPVId = APV->APVId;
372  if(SecondAPVId%2==0){ SecondAPVId = SecondAPVId+1; }else{ SecondAPVId = SecondAPVId-1; }
373  std::shared_ptr<stAPVGain> APV2 = APVsColl[(APV->DetId<<4) | SecondAPVId];
374  if(APV2->Bin<0) APV2->Bin = chvsidx->GetXaxis()->FindBin(APV2->Index);
375  TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY("",APV2->Bin,APV2->Bin,"e"));
376  if(Proj2){Proj->Add(Proj2,1);delete Proj2;}
377  }else if(CalibrationLevel==2){
378  for(unsigned int i=0;i<16;i++){ //loop up to 6APV for Strip and up to 16 for Pixels
379  auto tmpit = APVsColl.find((APV->DetId<<4) | i);
380  if(tmpit==APVsColl.end())continue;
381  std::shared_ptr<stAPVGain> APV2 = tmpit->second;
382  if(APV2->DetId != APV->DetId || APV2->APVId == APV->APVId)continue;
383  if(APV2->Bin<0) APV2->Bin = chvsidx->GetXaxis()->FindBin(APV2->Index);
384  TH1F* Proj2 = (TH1F*)(chvsidx->ProjectionY("",APV2->Bin,APV2->Bin,"e"));
385  if(Proj2){Proj->Add(Proj2,1);delete Proj2;}
386  }
387  } else {
388  CalibrationLevel = 0;
389  printf("Unknown Calibration Level, will assume %i\n",CalibrationLevel);
390  }
391 
392  getPeakOfLandau(Proj,FitResults);
393  APV->FitMPV = FitResults[0];
394  APV->FitMPVErr = FitResults[1];
395  APV->FitWidth = FitResults[2];
396  APV->FitWidthErr = FitResults[3];
397  APV->FitChi2 = FitResults[4];
398  APV->FitNorm = FitResults[5];
399  APV->NEntries = Proj->GetEntries();
400 
401  if(IsGoodLandauFit(FitResults)){
402  APV->Gain = APV->FitMPV / MPVmean;
403  if(APV->SubDet>2)GOOD++;
404  }else{
405  APV->Gain = APV->PreviousGain;
406  if(APV->SubDet>2)BAD++;
407  }
408  if(APV->Gain<=0) APV->Gain = 1;
409 
410  delete Proj;
411  }printf("\n");
412 }
413 
414 //********************************************************************************//
415 void
416 SiStripGainsPCLHarvester::getPeakOfLandau(TH1* InputHisto, double* FitResults, double LowRange, double HighRange)
417 {
418  FitResults[0] = -0.5; //MPV
419  FitResults[1] = 0; //MPV error
420  FitResults[2] = -0.5; //Width
421  FitResults[3] = 0; //Width error
422  FitResults[4] = -0.5; //Fit Chi2/NDF
423  FitResults[5] = 0; //Normalization
424 
425  if( InputHisto->GetEntries() < MinNrEntries)return;
426 
427  // perform fit with standard landau
428  TF1 MyLandau("MyLandau","landau",LowRange, HighRange);
429  MyLandau.SetParameter(1,300);
430  InputHisto->Fit(&MyLandau,"0QR WW");
431 
432  // MPV is parameter 1 (0=constant, 1=MPV, 2=Sigma)
433  FitResults[0] = MyLandau.GetParameter(1); //MPV
434  FitResults[1] = MyLandau.GetParError(1); //MPV error
435  FitResults[2] = MyLandau.GetParameter(2); //Width
436  FitResults[3] = MyLandau.GetParError(2); //Width error
437  FitResults[4] = MyLandau.GetChisquare() / MyLandau.GetNDF(); //Fit Chi2/NDF
438  FitResults[5] = MyLandau.GetParameter(0);
439 
440 }
441 
442 //********************************************************************************//
443 bool
445  if(FitResults[0] <= 0 )return false;
446  // if(FitResults[1] > MaxMPVError )return false;
447  // if(FitResults[4] > MaxChi2OverNDF)return false;
448  return true;
449 }
450 
451 
452 //********************************************************************************//
453 // ------------ method called once each job just before starting event loop ------------
454 void
456 
458  const TrackerGeometry *newBareTkGeomPtr = &(*tkGeom_);
459  if (newBareTkGeomPtr == bareTkGeomPtr_) return; // already filled APVColls, nothing changed
460 
461  if (!bareTkGeomPtr_) { // pointer not yet set: called the first time => fill the APVColls
462  auto const & Det = newBareTkGeomPtr->dets();
463 
464  unsigned int Index=0;
465 
466  for(unsigned int i=0;i<Det.size();i++){
467 
468  DetId Detid = Det[i]->geographicalId();
469  int SubDet = Detid.subdetId();
470 
471  if( SubDet == StripSubdetector::TIB || SubDet == StripSubdetector::TID ||
472  SubDet == StripSubdetector::TOB || SubDet == StripSubdetector::TEC ){
473 
474  auto DetUnit = dynamic_cast<const StripGeomDetUnit*> (Det[i]);
475  if(!DetUnit)continue;
476 
477  const StripTopology& Topo = DetUnit->specificTopology();
478  unsigned int NAPV = Topo.nstrips()/128;
479 
480  for(unsigned int j=0;j<NAPV;j++){
481  auto APV = std::make_shared<stAPVGain>();
482  APV->Index = Index;
483  APV->Bin = -1;
484  APV->DetId = Detid.rawId();
485  APV->APVId = j;
486  APV->SubDet = SubDet;
487  APV->FitMPV = -1;
488  APV->FitMPVErr = -1;
489  APV->FitWidth = -1;
490  APV->FitWidthErr = -1;
491  APV->FitChi2 = -1;
492  APV->FitNorm = -1;
493  APV->Gain = -1;
494  APV->PreviousGain = 1;
495  APV->PreviousGainTick = 1;
496  APV->x = DetUnit->position().basicVector().x();
497  APV->y = DetUnit->position().basicVector().y();
498  APV->z = DetUnit->position().basicVector().z();
499  APV->Eta = DetUnit->position().basicVector().eta();
500  APV->Phi = DetUnit->position().basicVector().phi();
501  APV->R = DetUnit->position().basicVector().transverse();
502  APV->Thickness = DetUnit->surface().bounds().thickness();
503  APV->NEntries = 0;
504  APV->isMasked = false;
505 
506  APVsCollOrdered.push_back(APV);
507  APVsColl[(APV->DetId<<4) | APV->APVId] = APV;
508  Index++;
509  NStripAPVs++;
510  } // loop on APVs
511  } // if is Strips
512  } // loop on dets
513 
514  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
515  DetId Detid = Det[i]->geographicalId();
516  int SubDet = Detid.subdetId();
518  auto DetUnit = dynamic_cast<const PixelGeomDetUnit*> (Det[i]);
519  if(!DetUnit) continue;
520 
521  const PixelTopology& Topo = DetUnit->specificTopology();
522  unsigned int NROCRow = Topo.nrows()/(80.);
523  unsigned int NROCCol = Topo.ncolumns()/(52.);
524 
525  for(unsigned int j=0;j<NROCRow;j++){
526  for(unsigned int i=0;i<NROCCol;i++){
527  auto APV = std::make_shared<stAPVGain>();
528  APV->Index = Index;
529  APV->Bin = -1;
530  APV->DetId = Detid.rawId();
531  APV->APVId = (j<<3 | i);
532  APV->SubDet = SubDet;
533  APV->FitMPV = -1;
534  APV->FitMPVErr = -1;
535  APV->FitWidth = -1;
536  APV->FitWidthErr = -1;
537  APV->FitChi2 = -1;
538  APV->Gain = -1;
539  APV->PreviousGain = 1;
540  APV->PreviousGainTick = 1;
541  APV->x = DetUnit->position().basicVector().x();
542  APV->y = DetUnit->position().basicVector().y();
543  APV->z = DetUnit->position().basicVector().z();
544  APV->Eta = DetUnit->position().basicVector().eta();
545  APV->Phi = DetUnit->position().basicVector().phi();
546  APV->R = DetUnit->position().basicVector().transverse();
547  APV->Thickness = DetUnit->surface().bounds().thickness();
548  APV->isMasked = false; //SiPixelQuality_->IsModuleBad(Detid.rawId());
549  APV->NEntries = 0;
550 
551  APVsCollOrdered.push_back(APV);
552  APVsColl[(APV->DetId<<4) | APV->APVId] = APV;
553  Index++;
554  NPixelDets++;
555 
556  } // loop on ROC cols
557  } // loop on ROC rows
558  } // if Pixel
559  } // loop on Dets
560  } //if (!bareTkGeomPtr_) ...
561  bareTkGeomPtr_ = newBareTkGeomPtr;
562 }
563 
564 void
566  if( !tTopo_ ) {
568  setup.get<TrackerTopologyRcd>().get( TopoHandle );
569  tTopo_ = TopoHandle.product();
570  }
571 }
572 
573 //********************************************************************************//
575 
576  // The goal of this function is to check wether or not there is enough statistics
577  // to produce a meaningful tag for the DB
578 
579  if( Charge_Vs_Index==0 ) {
580  edm::LogError("SiStripGainsPCLHarvester") << "produceTagFilter -> Return false: could not retrieve the "
581  << m_calibrationMode <<" statistics.\n"
582  << "Please check if input contains "
583  << m_calibrationMode << " data." << std::endl;
584  return false;
585  }
586 
587 
588  float integral = (Charge_Vs_Index)->getTH2S()->Integral();
589  if( (Charge_Vs_Index)->getTH2S()->Integral(0,NStripAPVs+1, 0, 99999 ) < tagCondition_NClusters) {
590  edm::LogWarning("SiStripGainsPCLHarvester")
591  << "calibrationMode -> " << m_calibrationMode << "\n"
592  << "produceTagFilter -> Return false: Statistics is too low : " << integral << std::endl;
593  return false;
594  }
595  if((1.0 * GOOD) / (GOOD+BAD) < tagCondition_GoodFrac) {
596  edm::LogWarning("SiStripGainsPCLHarvester")
597  << "calibrationMode -> " << m_calibrationMode << "\n"
598  << "produceTagFilter -> Return false: ratio of GOOD/TOTAL is too low: " << (1.0 * GOOD) / (GOOD+BAD) << std::endl;
599  return false;
600  }
601  return true;
602 }
603 
604 //********************************************************************************//
605 std::unique_ptr<SiStripApvGain>
607 {
608  std::unique_ptr<SiStripApvGain> obj = std::unique_ptr<SiStripApvGain>(new SiStripApvGain());
609 
610  if(!produceTagFilter(Charge_Vs_Index)){
611  edm::LogWarning("SiStripGainsPCLHarvester")<< "getNewObject -> will not produce a paylaod because produceTagFilter returned false " << std::endl;
612  return obj;
613  } else {
614  doStoreOnDB=true;
615  }
616 
617  std::vector<float> theSiStripVector;
618  unsigned int PreviousDetId = 0;
619  for(unsigned int a=0;a<APVsCollOrdered.size();a++){
620  std::shared_ptr<stAPVGain> APV = APVsCollOrdered[a];
621  if(APV==NULL){ printf("Bug\n"); continue; }
622  if(APV->SubDet<=2)continue;
623  if(APV->DetId != PreviousDetId){
624  if(!theSiStripVector.empty()){
625  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
626  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
627  }
628  theSiStripVector.clear();
629  PreviousDetId = APV->DetId;
630  }
631  theSiStripVector.push_back(APV->Gain);
632 
633  LogDebug("SiStripGainsPCLHarvester")<<" DetId: "<<APV->DetId
634  <<" APV: "<<APV->APVId
635  <<" Gain: "<<APV->Gain
636  <<std::endl;
637 
638  }
639  if(!theSiStripVector.empty()){
640  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
641  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
642  }
643 
644  return obj;
645 }
646 
647 
648 //********************************************************************************//
649 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
650 void
652 
654  desc.setUnknown();
655  descriptions.addDefault(desc);
656 
657 }
658 
659 //********************************************************************************//
661 }
662 
#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_
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
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)
const TrackerTopology * tTopo_
int subdetectorPlane(uint32_t, const TrackerTopology *)
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:1
std::vector< std::string > dqm_tag_
#define NULL
Definition: scimark2.h:8
DetId()
Create an empty or null id (also for persistence)
Definition: DetId.h:26
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)
void Fill(long long x)
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
U second(std::pair< T, U > const &p)
SiStripGainsPCLHarvester(const edm::ParameterSet &ps)
std::vector< MonitorElement * > FetchMonitor(std::vector< APVmon >, uint32_t, const TrackerTopology *topo=0)
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
std::vector< std::string > VChargeHisto
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
bin
set the eta bin as selection string.
virtual void checkAndRetrieveTopology(const edm::EventSetup &setup)
Definition: DetId.h:18
bool IsGoodLandauFit(double *FitResults)
double const BAD
Definition: Constants.h:17
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:277
void gainQualityMonitor(DQMStore::IBooker &ibooker_, const MonitorElement *Charge_Vs_Index) const
virtual int nstrips() const =0
const T & get() const
Definition: EventSetup.h:56
int subdetectorSide(uint32_t, const TrackerTopology *)
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
MonitorElement * book2DD(Args &&...args)
Definition: DQMStore.h:145
std::vector< std::pair< std::string, std::string > > monHnames(std::vector< std::string >, bool, const char *tag)
bool isValid() const
Definition: ESHandle.h:47
MonitorElement * book1DD(Args &&...args)
Definition: DQMStore.h:127
T const * product() const
Definition: ESHandle.h:86
int subdetectorId(uint32_t)
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