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