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