CMS 3D CMS Logo

DTTTrigCalibration.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author G. Cerminara - INFN Torino
5  */
10 
15 
18 
20 
22 
24 
25 
28 
29 
30 #include "TFile.h"
31 #include "TH1F.h"
32 #include "TGraph.h"
33 
34 class DTLayer;
35 
36 using namespace std;
37 using namespace edm;
38 // using namespace cond;
39 
40 
41 
42 // Constructor
44  // Get the debug parameter for verbose output
45  debug = pset.getUntrackedParameter<bool>("debug");
46 
47  // Get the label to retrieve digis from the event
48  digiLabel = pset.getUntrackedParameter<string>("digiLabel");
49  consumes< DTDigiCollection >(edm::InputTag(digiLabel));
50 
51  // Switch on/off the DB writing
52  findTMeanAndSigma = pset.getUntrackedParameter<bool>("fitAndWrite", false);
53 
54  // The TDC time-window (ns)
55  maxTDCCounts = 5000 * pset.getUntrackedParameter<int>("tdcRescale", 1);
56  //The maximum number of digis per layer
57  maxDigiPerLayer = pset.getUntrackedParameter<int>("maxDigiPerLayer", 10);
58 
59  // The root file which will contain the histos
60  string rootFileName = pset.getUntrackedParameter<string>("rootFileName");
61  theFile = new TFile(rootFileName.c_str(), "RECREATE");
62  theFile->cd();
63  theFitter = std::make_unique<DTTimeBoxFitter>();
64  if(debug)
65  theFitter->setVerbosity(1);
66 
67  double sigmaFit = pset.getUntrackedParameter<double>("sigmaTTrigFit",10.);
68  theFitter->setFitSigma(sigmaFit);
69 
70  doSubtractT0 = pset.getUntrackedParameter<bool>("doSubtractT0","false");
71  // Get the synchronizer
72  if(doSubtractT0) {
73  theSync = std::unique_ptr<DTTTrigBaseSync>{DTTTrigSyncFactory::get()->create(pset.getUntrackedParameter<string>("tTrigMode"),
74  pset.getUntrackedParameter<ParameterSet>("tTrigModeConfig"))};
75  }
76 
77  checkNoisyChannels = pset.getUntrackedParameter<bool>("checkNoisyChannels","false");
78 
79  // the kfactor to be uploaded in the ttrig DB
80  kFactor = pset.getUntrackedParameter<double>("kFactor",-0.7);
81 
82  if(debug)
83  cout << "[DTTTrigCalibration]Constructor called!" << endl;
84 }
85 
86 
87 
88 // Destructor
90  if(debug)
91  cout << "[DTTTrigCalibration]Destructor called!" << endl;
92 
93  // // Delete all histos
94  // for(map<DTSuperLayerId, TH1F*>::const_iterator slHisto = theHistoMap.begin();
95  // slHisto != theHistoMap.end();
96  // slHisto++) {
97  // delete (*slHisto).second;
98  // }
99 
100  theFile->Close();
101 }
102 
103 
104 
107 
108  if(debug)
109  cout << "[DTTTrigCalibration] #Event: " << event.id().event() << endl;
110 
111  // Get the digis from the event
113  event.getByLabel(digiLabel, digis);
114 
115  ESHandle<DTStatusFlag> statusMap;
116  if(checkNoisyChannels) {
117  // Get the map of noisy channels
118  eventSetup.get<DTStatusFlagRcd>().get(statusMap);
119  }
120 
121  if(doSubtractT0)
122  theSync->setES(eventSetup);
123 
124  //The chambers too noisy in this event
125  vector<DTChamberId> badChambers;
126 
127  // Iterate through all digi collections ordered by LayerId
129  for (dtLayerIt = digis->begin();
130  dtLayerIt != digis->end();
131  ++dtLayerIt){
132  // Get the iterators over the digis associated with this LayerId
133  const DTDigiCollection::Range& digiRange = (*dtLayerIt).second;
134 
135  const DTLayerId layerId = (*dtLayerIt).first;
136  const DTSuperLayerId slId = layerId.superlayerId();
137  const DTChamberId chId = slId.chamberId();
138  bool badChamber=false;
139 
140  if(debug)
141  cout<<"----------- Layer "<<layerId<<" -------------"<<endl;
142 
143  //Check if the layer is inside a noisy chamber
144  for(vector<DTChamberId>::const_iterator chamber = badChambers.begin(); chamber != badChambers.end(); ++chamber){
145  if((*chamber) == chId){
146  badChamber=true;
147  break;
148  }
149  }
150  if(badChamber) continue;
151 
152  //Check if the layer has too many digis
153  if((digiRange.second - digiRange.first) > maxDigiPerLayer){
154  if(debug)
155  cout<<"Layer "<<layerId<<"has too many digis ("<<(digiRange.second - digiRange.first)<<")"<<endl;
156  badChambers.push_back(chId);
157  continue;
158  }
159 
160  // Get the histo from the map
161  TH1F *hTBox = theHistoMap[slId];
162  if(hTBox == nullptr) {
163  // Book the histogram
164  theFile->cd();
165  hTBox = new TH1F(getTBoxName(slId).c_str(), "Time box (ns)", int(0.25*32.0*maxTDCCounts/25.0), 0, maxTDCCounts);
166  if(debug)
167  cout << " New Time Box: " << hTBox->GetName() << endl;
168  theHistoMap[slId] = hTBox;
169  }
170  TH1F *hO = theOccupancyMap[layerId];
171  if(hO == nullptr) {
172  // Book the histogram
173  theFile->cd();
174  hO = new TH1F(getOccupancyName(layerId).c_str(), "Occupancy", 100, 0, 100);
175  if(debug)
176  cout << " New Time Box: " << hO->GetName() << endl;
177  theOccupancyMap[layerId] = hO;
178  }
179 
180  // Loop over all digis in the given range
181  for (DTDigiCollection::const_iterator digi = digiRange.first;
182  digi != digiRange.second;
183  ++digi) {
184  const DTWireId wireId(layerId, (*digi).wire());
185 
186  // Check for noisy channels and skip them
187  if(checkNoisyChannels) {
188  bool isNoisy = false;
189  bool isFEMasked = false;
190  bool isTDCMasked = false;
191  bool isTrigMask = false;
192  bool isDead = false;
193  bool isNohv = false;
194  statusMap->cellStatus(wireId, isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
195  if(isNoisy) {
196  if(debug)
197  cout << "Wire: " << wireId << " is noisy, skipping!" << endl;
198  continue;
199  }
200  }
201  theFile->cd();
202  double offset = 0;
203  if(doSubtractT0) {
204  const DTLayer* layer = nullptr;//fake
205  const GlobalPoint glPt;//fake
206  offset = theSync->offset(layer, wireId, glPt);
207  }
208  hTBox->Fill((*digi).time()-offset);
209  if(debug) {
210  cout << " Filling Time Box: " << hTBox->GetName() << endl;
211  cout << " offset (ns): " << offset << endl;
212  cout << " time(ns): " << (*digi).time()-offset<< endl;
213  }
214  hO->Fill((*digi).wire());
215  }
216  }
217 }
218 
219 
221  if(debug)
222  cout << "[DTTTrigCalibration]Writing histos to file!" << endl;
223 
224  // Write all time boxes to file
225  theFile->cd();
226  for(map<DTSuperLayerId, TH1F*>::const_iterator slHisto = theHistoMap.begin();
227  slHisto != theHistoMap.end();
228  ++slHisto) {
229  (*slHisto).second->Write();
230  }
231  for(map<DTLayerId, TH1F*>::const_iterator slHisto = theOccupancyMap.begin();
232  slHisto != theOccupancyMap.end();
233  ++slHisto) {
234  (*slHisto).second->Write();
235  }
236 
237  if(findTMeanAndSigma) {
238  // Create the object to be written to DB
239  DTTtrig* tTrig = new DTTtrig();
240 
241  // Loop over the map, fit the histos and write the resulting values to the DB
242  for(map<DTSuperLayerId, TH1F*>::const_iterator slHisto = theHistoMap.begin();
243  slHisto != theHistoMap.end();
244  ++slHisto) {
245  pair<double, double> meanAndSigma = theFitter->fitTimeBox((*slHisto).second);
246  tTrig->set((*slHisto).first,
247  meanAndSigma.first,
248  meanAndSigma.second,
249  kFactor,
251 
252  if(debug) {
253  cout << " SL: " << (*slHisto).first
254  << " mean = " << meanAndSigma.first
255  << " sigma = " << meanAndSigma.second << endl;
256  }
257  }
258 
259  // Print the ttrig map
260  dumpTTrigMap(tTrig);
261 
262  // Plot the tTrig
263  plotTTrig(tTrig);
264 
265  if(debug)
266  cout << "[DTTTrigCalibration]Writing ttrig object to DB!" << endl;
267 
268 
269  // FIXME: to be read from cfg?
270  string tTrigRecord = "DTTtrigRcd";
271 
272  // Write the object to DB
273  DTCalibDBUtils::writeToDB(tTrigRecord, tTrig);
274  }
275 
276 }
277 
278 
279 
280 
282  string histoName;
283  stringstream theStream;
284  theStream << "Ch_" << slId.wheel() << "_" << slId.station() << "_" << slId.sector()
285  << "_SL" << slId.superlayer() << "_hTimeBox";
286  theStream >> histoName;
287  return histoName;
288 }
289 
291  string histoName;
292  stringstream theStream;
293  theStream << "Ch_" << slId.wheel() << "_" << slId.station() << "_" << slId.sector()
294  << "_SL" << slId.superlayer() << "_L"<< slId.layer() <<"_Occupancy";
295  theStream >> histoName;
296  return histoName;
297 }
298 
299 
300 void DTTTrigCalibration::dumpTTrigMap(const DTTtrig* tTrig) const {
301  static const double convToNs = 25./32.;
302  for(DTTtrig::const_iterator ttrig = tTrig->begin();
303  ttrig != tTrig->end(); ++ttrig) {
304  cout << "Wh: " << (*ttrig).first.wheelId
305  << " St: " << (*ttrig).first.stationId
306  << " Sc: " << (*ttrig).first.sectorId
307  << " Sl: " << (*ttrig).first.slId
308  << " TTrig mean (ns): " << (*ttrig).second.tTrig * convToNs
309  << " TTrig sigma (ns): " << (*ttrig).second.tTrms * convToNs<< endl;
310  }
311 }
312 
313 
314 void DTTTrigCalibration::plotTTrig(const DTTtrig* tTrig) const {
315 
316  TH1F* tTrig_YB1_Se10 = new TH1F("tTrig_YB1_Se10","tTrig YB1_Se10",15,1,16);
317  TH1F* tTrig_YB2_Se10 = new TH1F("tTrig_YB2_Se10","tTrig YB2_Se10",15,1,16);
318  TH1F* tTrig_YB2_Se11 = new TH1F("tTrig_YB2_Se11","tTrig YB2_Se11",12,1,13);
319 
320  static const double convToNs = 25./32.;
321  for(DTTtrig::const_iterator ttrig = tTrig->begin();
322  ttrig != tTrig->end(); ++ttrig) {
323 
324  // avoid to have wired numbers in the plot
325  float tTrigValue=0;
326  float tTrmsValue=0;
327  if ((*ttrig).second.tTrig * convToNs > 0 &&
328  (*ttrig).second.tTrig * convToNs < 32000 ) {
329  tTrigValue = (*ttrig).second.tTrig * convToNs;
330  tTrmsValue = (*ttrig).second.tTrms * convToNs;
331  }
332 
333  int binx;
334  string binLabel;
335  stringstream binLabelStream;
336  if ((*ttrig).first.sectorId != 14) {
337  binx = ((*ttrig).first.stationId-1)*3 + (*ttrig).first.slId;
338  binLabelStream << "MB"<<(*ttrig).first.stationId<<"_SL"<<(*ttrig).first.slId;
339  }
340  else {
341  binx = 12 + (*ttrig).first.slId;
342  binLabelStream << "MB14_SL"<<(*ttrig).first.slId;
343  }
344  binLabelStream >> binLabel;
345 
346  if ((*ttrig).first.wheelId == 2) {
347  if ((*ttrig).first.sectorId == 10 || (*ttrig).first.sectorId == 14) {
348  tTrig_YB2_Se10->Fill( binx,tTrigValue);
349  tTrig_YB2_Se10->SetBinError( binx, tTrmsValue);
350  tTrig_YB2_Se10->GetXaxis()->SetBinLabel(binx,binLabel.c_str());
351  tTrig_YB2_Se10->GetYaxis()->SetTitle("ns");
352  }
353  else {
354  tTrig_YB2_Se11->Fill( binx,tTrigValue);
355  tTrig_YB2_Se11->SetBinError( binx,tTrmsValue);
356  tTrig_YB2_Se11->GetXaxis()->SetBinLabel(binx,binLabel.c_str());
357  tTrig_YB2_Se11->GetYaxis()->SetTitle("ns");
358  }
359  }
360  else {
361  tTrig_YB1_Se10->Fill( binx,tTrigValue);
362  tTrig_YB1_Se10->SetBinError( binx,tTrmsValue);
363  tTrig_YB1_Se10->GetXaxis()->SetBinLabel(binx,binLabel.c_str());
364  tTrig_YB1_Se10->GetYaxis()->SetTitle("ns");
365  }
366  }
367 
368  tTrig_YB1_Se10->Write();
369  tTrig_YB2_Se10->Write();
370  tTrig_YB2_Se11->Write();
371 
372 }
DTTTrigCalibration(const edm::ParameterSet &pset)
Constructor.
T getUntrackedParameter(std::string const &, T const &) const
int set(int wheelId, int stationId, int sectorId, int slId, float tTrig, float tTrms, float kFact, DTTimeUnits::type unit)
Definition: DTTtrig.cc:248
DTChamberId chamberId() const
Return the corresponding ChamberId.
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
Fill the time boxes.
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:59
std::string getTBoxName(const DTSuperLayerId &slId) const
std::vector< std::pair< DTTtrigId, DTTtrigData > >::const_iterator const_iterator
Access methods to data.
Definition: DTTtrig.h:183
~DTTTrigCalibration() override
Destructor.
void plotTTrig(const DTTtrig *tTrig) const
std::string getOccupancyName(const DTLayerId &slId) const
int superlayer() const
Return the superlayer number (deprecated method name)
#define debug
Definition: HDRShower.cc:19
std::vector< DigiType >::const_iterator const_iterator
int cellStatus(int wheelId, int stationId, int sectorId, int slId, int layerId, int cellId, bool &noiseFlag, bool &feMask, bool &tdcMask, bool &trigMask, bool &deadFlag, bool &nohvFlag) const
get content
Definition: DTStatusFlag.h:100
HLT enums.
int sector() const
Definition: DTChamberId.h:61
T get() const
Definition: EventSetup.h:71
const_iterator begin() const
Definition: DTTtrig.cc:354
std::pair< const_iterator, const_iterator > Range
int station() const
Return the station number.
Definition: DTChamberId.h:51
void dumpTTrigMap(const DTTtrig *tTrig) const
const_iterator end() const
Definition: DTTtrig.cc:359
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
void endJob() override
Fit the time box rising edge and write the resulting ttrig to the DB.
static void writeToDB(std::string record, T *payload)
T get(const Candidate &c)
Definition: component.h:55
Definition: event.py:1