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