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