CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 
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 
50  // Switch on/off the DB writing
51  findTMeanAndSigma = pset.getUntrackedParameter<bool>("fitAndWrite", false);
52 
53  // The TDC time-window (ns)
54  maxTDCCounts = 5000 * pset.getUntrackedParameter<int>("tdcRescale", 1);
55  //The maximum number of digis per layer
56  maxDigiPerLayer = pset.getUntrackedParameter<int>("maxDigiPerLayer", 10);
57 
58  // The root file which will contain the histos
59  string rootFileName = pset.getUntrackedParameter<string>("rootFileName");
60  theFile = new TFile(rootFileName.c_str(), "RECREATE");
61  theFile->cd();
62  theFitter = new DTTimeBoxFitter();
63  if(debug)
64  theFitter->setVerbosity(1);
65 
66  double sigmaFit = pset.getUntrackedParameter<double>("sigmaTTrigFit",10.);
67  theFitter->setFitSigma(sigmaFit);
68 
69  doSubtractT0 = pset.getUntrackedParameter<bool>("doSubtractT0","false");
70  // Get the synchronizer
71  if(doSubtractT0) {
72  theSync = DTTTrigSyncFactory::get()->create(pset.getUntrackedParameter<string>("tTrigMode"),
73  pset.getUntrackedParameter<ParameterSet>("tTrigModeConfig"));
74  } else {
75  theSync = 0;
76  }
77 
78  checkNoisyChannels = pset.getUntrackedParameter<bool>("checkNoisyChannels","false");
79 
80  // the kfactor to be uploaded in the ttrig DB
81  kFactor = pset.getUntrackedParameter<double>("kFactor",-0.7);
82 
83  if(debug)
84  cout << "[DTTTrigCalibration]Constructor called!" << endl;
85 }
86 
87 
88 
89 // Destructor
91  if(debug)
92  cout << "[DTTTrigCalibration]Destructor called!" << endl;
93 
94  // // Delete all histos
95  // for(map<DTSuperLayerId, TH1F*>::const_iterator slHisto = theHistoMap.begin();
96  // slHisto != theHistoMap.end();
97  // slHisto++) {
98  // delete (*slHisto).second;
99  // }
100 
101  theFile->Close();
102  delete theFitter;
103 }
104 
105 
106 
109 
110  if(debug)
111  cout << "[DTTTrigCalibration] #Event: " << event.id().event() << endl;
112 
113  // Get the digis from the event
115  event.getByLabel(digiLabel, digis);
116 
118  if(checkNoisyChannels) {
119  // Get the map of noisy channels
120  eventSetup.get<DTStatusFlagRcd>().get(statusMap);
121  }
122 
123  if(doSubtractT0)
124  theSync->setES(eventSetup);
125 
126  //The chambers too noisy in this event
127  vector<DTChamberId> badChambers;
128 
129  // Iterate through all digi collections ordered by LayerId
131  for (dtLayerIt = digis->begin();
132  dtLayerIt != digis->end();
133  ++dtLayerIt){
134  // Get the iterators over the digis associated with this LayerId
135  const DTDigiCollection::Range& digiRange = (*dtLayerIt).second;
136 
137  const DTLayerId layerId = (*dtLayerIt).first;
138  const DTSuperLayerId slId = layerId.superlayerId();
139  const DTChamberId chId = slId.chamberId();
140  bool badChamber=false;
141 
142  if(debug)
143  cout<<"----------- Layer "<<layerId<<" -------------"<<endl;
144 
145  //Check if the layer is inside a noisy chamber
146  for(vector<DTChamberId>::const_iterator chamber = badChambers.begin(); chamber != badChambers.end(); ++chamber){
147  if((*chamber) == chId){
148  badChamber=true;
149  break;
150  }
151  }
152  if(badChamber) continue;
153 
154  //Check if the layer has too many digis
155  if((digiRange.second - digiRange.first) > maxDigiPerLayer){
156  if(debug)
157  cout<<"Layer "<<layerId<<"has too many digis ("<<(digiRange.second - digiRange.first)<<")"<<endl;
158  badChambers.push_back(chId);
159  continue;
160  }
161 
162  // Get the histo from the map
163  TH1F *hTBox = theHistoMap[slId];
164  if(hTBox == 0) {
165  // Book the histogram
166  theFile->cd();
167  hTBox = new TH1F(getTBoxName(slId).c_str(), "Time box (ns)", int(0.25*32.0*maxTDCCounts/25.0), 0, maxTDCCounts);
168  if(debug)
169  cout << " New Time Box: " << hTBox->GetName() << endl;
170  theHistoMap[slId] = hTBox;
171  }
172  TH1F *hO = theOccupancyMap[layerId];
173  if(hO == 0) {
174  // Book the histogram
175  theFile->cd();
176  hO = new TH1F(getOccupancyName(layerId).c_str(), "Occupancy", 100, 0, 100);
177  if(debug)
178  cout << " New Time Box: " << hO->GetName() << endl;
179  theOccupancyMap[layerId] = hO;
180  }
181 
182  // Loop over all digis in the given range
183  for (DTDigiCollection::const_iterator digi = digiRange.first;
184  digi != digiRange.second;
185  ++digi) {
186  const DTWireId wireId(layerId, (*digi).wire());
187 
188  // Check for noisy channels and skip them
189  if(checkNoisyChannels) {
190  bool isNoisy = false;
191  bool isFEMasked = false;
192  bool isTDCMasked = false;
193  bool isTrigMask = false;
194  bool isDead = false;
195  bool isNohv = false;
196  statusMap->cellStatus(wireId, isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
197  if(isNoisy) {
198  if(debug)
199  cout << "Wire: " << wireId << " is noisy, skipping!" << endl;
200  continue;
201  }
202  }
203  theFile->cd();
204  double offset = 0;
205  if(doSubtractT0) {
206  const DTLayer* layer = 0;//fake
207  const GlobalPoint glPt;//fake
208  offset = theSync->offset(layer, wireId, glPt);
209  }
210  hTBox->Fill((*digi).time()-offset);
211  if(debug) {
212  cout << " Filling Time Box: " << hTBox->GetName() << endl;
213  cout << " offset (ns): " << offset << endl;
214  cout << " time(ns): " << (*digi).time()-offset<< endl;
215  }
216  hO->Fill((*digi).wire());
217  }
218  }
219 }
220 
221 
223  if(debug)
224  cout << "[DTTTrigCalibration]Writing histos to file!" << endl;
225 
226  // Write all time boxes to file
227  theFile->cd();
228  for(map<DTSuperLayerId, TH1F*>::const_iterator slHisto = theHistoMap.begin();
229  slHisto != theHistoMap.end();
230  ++slHisto) {
231  (*slHisto).second->Write();
232  }
233  for(map<DTLayerId, TH1F*>::const_iterator slHisto = theOccupancyMap.begin();
234  slHisto != theOccupancyMap.end();
235  ++slHisto) {
236  (*slHisto).second->Write();
237  }
238 
239  if(findTMeanAndSigma) {
240  // Create the object to be written to DB
241  DTTtrig* tTrig = new DTTtrig();
242 
243  // Loop over the map, fit the histos and write the resulting values to the DB
244  for(map<DTSuperLayerId, TH1F*>::const_iterator slHisto = theHistoMap.begin();
245  slHisto != theHistoMap.end();
246  ++slHisto) {
247  pair<double, double> meanAndSigma = theFitter->fitTimeBox((*slHisto).second);
248  tTrig->set((*slHisto).first,
249  meanAndSigma.first,
250  meanAndSigma.second,
251  kFactor,
253 
254  if(debug) {
255  cout << " SL: " << (*slHisto).first
256  << " mean = " << meanAndSigma.first
257  << " sigma = " << meanAndSigma.second << endl;
258  }
259  }
260 
261  // Print the ttrig map
262  dumpTTrigMap(tTrig);
263 
264  // Plot the tTrig
265  plotTTrig(tTrig);
266 
267  if(debug)
268  cout << "[DTTTrigCalibration]Writing ttrig object to DB!" << endl;
269 
270 
271  // FIXME: to be read from cfg?
272  string tTrigRecord = "DTTtrigRcd";
273 
274  // Write the object to DB
275  DTCalibDBUtils::writeToDB(tTrigRecord, tTrig);
276  }
277 
278 }
279 
280 
281 
282 
284  string histoName;
285  stringstream theStream;
286  theStream << "Ch_" << slId.wheel() << "_" << slId.station() << "_" << slId.sector()
287  << "_SL" << slId.superlayer() << "_hTimeBox";
288  theStream >> histoName;
289  return histoName;
290 }
291 
293  string histoName;
294  stringstream theStream;
295  theStream << "Ch_" << slId.wheel() << "_" << slId.station() << "_" << slId.sector()
296  << "_SL" << slId.superlayer() << "_L"<< slId.layer() <<"_Occupancy";
297  theStream >> histoName;
298  return histoName;
299 }
300 
301 
302 void DTTTrigCalibration::dumpTTrigMap(const DTTtrig* tTrig) const {
303  static const double convToNs = 25./32.;
304  for(DTTtrig::const_iterator ttrig = tTrig->begin();
305  ttrig != tTrig->end(); ++ttrig) {
306  cout << "Wh: " << (*ttrig).first.wheelId
307  << " St: " << (*ttrig).first.stationId
308  << " Sc: " << (*ttrig).first.sectorId
309  << " Sl: " << (*ttrig).first.slId
310  << " TTrig mean (ns): " << (*ttrig).second.tTrig * convToNs
311  << " TTrig sigma (ns): " << (*ttrig).second.tTrms * convToNs<< endl;
312  }
313 }
314 
315 
316 void DTTTrigCalibration::plotTTrig(const DTTtrig* tTrig) const {
317 
318  TH1F* tTrig_YB1_Se10 = new TH1F("tTrig_YB1_Se10","tTrig YB1_Se10",15,1,16);
319  TH1F* tTrig_YB2_Se10 = new TH1F("tTrig_YB2_Se10","tTrig YB2_Se10",15,1,16);
320  TH1F* tTrig_YB2_Se11 = new TH1F("tTrig_YB2_Se11","tTrig YB2_Se11",12,1,13);
321 
322  static const double convToNs = 25./32.;
323  for(DTTtrig::const_iterator ttrig = tTrig->begin();
324  ttrig != tTrig->end(); ++ttrig) {
325 
326  // avoid to have wired numbers in the plot
327  float tTrigValue=0;
328  float tTrmsValue=0;
329  if ((*ttrig).second.tTrig * convToNs > 0 &&
330  (*ttrig).second.tTrig * convToNs < 32000 ) {
331  tTrigValue = (*ttrig).second.tTrig * convToNs;
332  tTrmsValue = (*ttrig).second.tTrms * convToNs;
333  }
334 
335  int binx;
336  string binLabel;
337  stringstream binLabelStream;
338  if ((*ttrig).first.sectorId != 14) {
339  binx = ((*ttrig).first.stationId-1)*3 + (*ttrig).first.slId;
340  binLabelStream << "MB"<<(*ttrig).first.stationId<<"_SL"<<(*ttrig).first.slId;
341  }
342  else {
343  binx = 12 + (*ttrig).first.slId;
344  binLabelStream << "MB14_SL"<<(*ttrig).first.slId;
345  }
346  binLabelStream >> binLabel;
347 
348  if ((*ttrig).first.wheelId == 2) {
349  if ((*ttrig).first.sectorId == 10 || (*ttrig).first.sectorId == 14) {
350  tTrig_YB2_Se10->Fill( binx,tTrigValue);
351  tTrig_YB2_Se10->SetBinError( binx, tTrmsValue);
352  tTrig_YB2_Se10->GetXaxis()->SetBinLabel(binx,binLabel.c_str());
353  tTrig_YB2_Se10->GetYaxis()->SetTitle("ns");
354  }
355  else {
356  tTrig_YB2_Se11->Fill( binx,tTrigValue);
357  tTrig_YB2_Se11->SetBinError( binx,tTrmsValue);
358  tTrig_YB2_Se11->GetXaxis()->SetBinLabel(binx,binLabel.c_str());
359  tTrig_YB2_Se11->GetYaxis()->SetTitle("ns");
360  }
361  }
362  else {
363  tTrig_YB1_Se10->Fill( binx,tTrigValue);
364  tTrig_YB1_Se10->SetBinError( binx,tTrmsValue);
365  tTrig_YB1_Se10->GetXaxis()->SetBinLabel(binx,binLabel.c_str());
366  tTrig_YB1_Se10->GetYaxis()->SetTitle("ns");
367  }
368  }
369 
370  tTrig_YB1_Se10->Write();
371  tTrig_YB2_Se10->Write();
372  tTrig_YB2_Se11->Write();
373 
374 }
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.
virtual ~DTTTrigCalibration()
Destructor.
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
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
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup)
Fill the time boxes.
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void plotTTrig(const DTTtrig *tTrig) const
std::string getOccupancyName(const DTLayerId &slId) const
void endJob()
Fit the time box rising edge and write the resulting ttrig to the DB.
int superlayer() const
Return the superlayer number (deprecated method name)
#define debug
Definition: HDRShower.cc:19
const T & get() const
Definition: EventSetup.h:56
const DTChamber * chamber() const
Definition: DTLayer.cc:58
std::vector< DTDigi >::const_iterator const_iterator
int sector() const
Definition: DTChamberId.h:61
const_iterator begin() const
Definition: DTTtrig.cc:354
std::pair< const_iterator, const_iterator > Range
tuple cout
Definition: gather_cfg.py:121
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
static void writeToDB(std::string record, T *payload)
T get(const Candidate &c)
Definition: component.h:55