CMS 3D CMS Logo

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