00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "DQM/DTMonitorModule/interface/DTCalibValidation.h"
00011
00012
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 #include "DQMServices/Core/interface/DQMStore.h"
00018 #include "DQMServices/Core/interface/MonitorElement.h"
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020
00021
00022 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00023 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00024
00025
00026 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00027 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00028
00029
00030 #include <iterator>
00031
00032 using namespace edm;
00033 using namespace std;
00034
00035
00036 DTCalibValidation::DTCalibValidation(const ParameterSet& pset) {
00037
00038
00039
00040
00041
00042
00043 theDbe = edm::Service<DQMStore>().operator->();
00044
00045
00046
00047
00048
00049 theDbe->setCurrentFolder("DT/DTCalibValidation");
00050
00051 parameters = pset;
00052 }
00053
00054
00055 DTCalibValidation::~DTCalibValidation(){
00056 }
00057
00058
00059 void DTCalibValidation::beginJob(){
00060
00061
00062 recHits1DLabel = parameters.getUntrackedParameter<string>("recHits1DLabel");
00063
00064 segment2DLabel = parameters.getUntrackedParameter<string>("segment2DLabel");
00065
00066 segment4DLabel = parameters.getUntrackedParameter<string>("segment4DLabel");
00067
00068 wrongSegment = 0;
00069
00070 rightSegment = 0;
00071
00072 detailedAnalysis = parameters.getUntrackedParameter<bool>("detailedAnalysis",false);
00073
00074 nevent=0;
00075
00076 }
00077
00078 void DTCalibValidation::beginRun(const Run& run, const EventSetup& setup) {
00079
00080
00081 setup.get<MuonGeometryRecord>().get(dtGeom);
00082
00083
00084 vector<DTChamber*>::const_iterator ch_it = dtGeom->chambers().begin();
00085 vector<DTChamber*>::const_iterator ch_end = dtGeom->chambers().end();
00086 for (; ch_it != ch_end; ++ch_it) {
00087 DTChamberId chId = (*ch_it)->id();
00088 vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin();
00089 vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
00090
00091 for(; sl_it != sl_end; ++sl_it) {
00092 DTSuperLayerId slId = (*sl_it)->id();
00093 if(detailedAnalysis){
00094
00095 for(int step = 1; step <= 3; ++step) bookHistos(slId,step);
00096 } else {
00097
00098 bookHistos(slId,3);
00099 }
00100 }
00101 }
00102
00103 }
00104
00105
00106 void DTCalibValidation::endJob(){
00107
00108 LogVerbatim("DTCalibValidation") << "Segments used to compute residuals: " << rightSegment;
00109 LogVerbatim("DTCalibValidation") << "Segments not used to compute residuals: " << wrongSegment;
00110
00111
00112 bool outputMEsInRootFile = parameters.getParameter<bool>("OutputMEsInRootFile");
00113 std::string outputFileName = parameters.getParameter<std::string>("OutputFileName");
00114 if(outputMEsInRootFile){
00115 theDbe->showDirStructure();
00116 theDbe->save(outputFileName);
00117 }
00118
00119 theDbe->rmdir("DT/DTCalibValidation");
00120 }
00121
00122
00123
00124 void DTCalibValidation::analyze(const edm::Event& event, const edm::EventSetup& setup) {
00125
00126 ++nevent;
00127 LogTrace("DTCalibValidation") << "[DTCalibValidation] Analyze #Run: " << event.id().run()
00128 << " #Event: " << nevent;
00129
00130
00131 map<DTWireId,vector<DTRecHit1DPair> > recHitsPerWire_1S;
00132
00133
00134 map<DTWireId,vector<DTRecHit1D> > recHitsPerWire_2S;
00135
00136 if(detailedAnalysis){
00137 LogTrace("DTCalibValidation") << " -- DTRecHit S1: begin analysis:";
00138
00139 Handle<DTRecHitCollection> dtRecHits;
00140 event.getByLabel(recHits1DLabel, dtRecHits);
00141 recHitsPerWire_1S = map1DRecHitsPerWire(dtRecHits.product());
00142
00143 LogTrace("DTCalibValidation") << " -- DTRecHit S2: begin analysis:";
00144
00145 Handle<DTRecSegment2DCollection> segment2Ds;
00146 event.getByLabel(segment2DLabel, segment2Ds);
00147 recHitsPerWire_2S = map1DRecHitsPerWire(segment2Ds.product());
00148 }
00149
00150
00151 LogTrace("DTCalibValidation") << " -- DTRecHit S3: begin analysis:";
00152
00153 Handle<DTRecSegment4DCollection> segment4Ds;
00154 event.getByLabel(segment4DLabel, segment4Ds);
00155 map<DTWireId,vector<DTRecHit1D> > recHitsPerWire_3S = map1DRecHitsPerWire(segment4Ds.product());
00156
00157
00158
00159 for(DTRecSegment4DCollection::const_iterator segment = segment4Ds->begin();
00160 segment != segment4Ds->end();
00161 ++segment) {
00162
00163 if(detailedAnalysis){
00164 LogTrace("DTCalibValidation") << "Anlysis on recHit at step 1";
00165 compute(dtGeom.product(), (*segment), recHitsPerWire_1S, 1);
00166
00167 LogTrace("DTCalibValidation") << "Anlysis on recHit at step 2";
00168 compute(dtGeom.product(), (*segment), recHitsPerWire_2S, 2);
00169 }
00170
00171 LogTrace("DTCalibValidation") << "Anlysis on recHit at step 3";
00172 compute(dtGeom.product(), (*segment), recHitsPerWire_3S, 3);
00173 }
00174
00175 }
00176
00177
00178
00179 map<DTWireId, vector<DTRecHit1DPair> >
00180 DTCalibValidation::map1DRecHitsPerWire(const DTRecHitCollection* dt1DRecHitPairs) {
00181 map<DTWireId, vector<DTRecHit1DPair> > ret;
00182
00183 for(DTRecHitCollection::const_iterator rechit = dt1DRecHitPairs->begin();
00184 rechit != dt1DRecHitPairs->end(); ++rechit) {
00185 ret[(*rechit).wireId()].push_back(*rechit);
00186 }
00187
00188 return ret;
00189 }
00190
00191
00192
00193 map<DTWireId, vector<DTRecHit1D> >
00194 DTCalibValidation::map1DRecHitsPerWire(const DTRecSegment2DCollection* segment2Ds) {
00195 map<DTWireId, vector<DTRecHit1D> > ret;
00196
00197
00198 for(DTRecSegment2DCollection::const_iterator segment = segment2Ds->begin();
00199 segment != segment2Ds->end();
00200 ++segment) {
00201 vector<DTRecHit1D> component1DHits= (*segment).specificRecHits();
00202
00203 for(vector<DTRecHit1D>::const_iterator hit = component1DHits.begin();
00204 hit != component1DHits.end(); ++hit) {
00205 ret[(*hit).wireId()].push_back(*hit);
00206 }
00207 }
00208 return ret;
00209 }
00210
00211
00212 map<DTWireId, std::vector<DTRecHit1D> >
00213 DTCalibValidation::map1DRecHitsPerWire(const DTRecSegment4DCollection* segment4Ds) {
00214 map<DTWireId, vector<DTRecHit1D> > ret;
00215
00216 for(DTRecSegment4DCollection::const_iterator segment = segment4Ds->begin();
00217 segment != segment4Ds->end();
00218 ++segment) {
00219
00220 vector<const TrackingRecHit*> segment2Ds = (*segment).recHits();
00221
00222 for(vector<const TrackingRecHit*>::const_iterator segment2D = segment2Ds.begin();
00223 segment2D != segment2Ds.end();
00224 ++segment2D) {
00225
00226 vector<const TrackingRecHit*> hits = (*segment2D)->recHits();
00227
00228 for(vector<const TrackingRecHit*>::const_iterator hit = hits.begin();
00229 hit != hits.end(); ++hit) {
00230 const DTRecHit1D* hit1D = dynamic_cast<const DTRecHit1D*>(*hit);
00231 ret[hit1D->wireId()].push_back(*hit1D);
00232 }
00233 }
00234 }
00235
00236 return ret;
00237 }
00238
00239
00240
00241
00242 template <typename type>
00243 const type*
00244 DTCalibValidation::findBestRecHit(const DTLayer* layer,
00245 DTWireId wireId,
00246 const vector<type>& recHits,
00247 const float segmDist) {
00248 float res = 99999;
00249 const type* theBestRecHit = 0;
00250
00251 for(typename vector<type>::const_iterator recHit = recHits.begin();
00252 recHit != recHits.end();
00253 ++recHit) {
00254 float distTmp = recHitDistFromWire(*recHit, layer);
00255 if(fabs(distTmp-segmDist) < res) {
00256 res = fabs(distTmp-segmDist);
00257 theBestRecHit = &(*recHit);
00258 }
00259 }
00260
00261 return theBestRecHit;
00262 }
00263
00264
00265
00266 float
00267 DTCalibValidation::recHitDistFromWire(const DTRecHit1DPair& hitPair, const DTLayer* layer) {
00268 return fabs(hitPair.localPosition(DTEnums::Left).x() -
00269 hitPair.localPosition(DTEnums::Right).x())/2.;
00270 }
00271
00272
00273
00274 float
00275 DTCalibValidation::recHitDistFromWire(const DTRecHit1D& recHit, const DTLayer* layer) {
00276 return fabs(recHit.localPosition().x() - layer->specificTopology().wirePosition(recHit.wireId().wire()));
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 }
00297
00298
00299
00300
00301
00302 float
00303 DTCalibValidation::recHitPosition(const DTRecHit1DPair& hitPair, const DTLayer* layer, const DTChamber* chamber, float segmentPos, int sl) {
00304
00305
00306 GlobalPoint hitPosGlob_right = layer->toGlobal(hitPair.localPosition(DTEnums::Right));
00307 LocalPoint hitPosInChamber_right = chamber->toLocal(hitPosGlob_right);
00308 GlobalPoint hitPosGlob_left = layer->toGlobal(hitPair.localPosition(DTEnums::Left));
00309 LocalPoint hitPosInChamber_left = chamber->toLocal(hitPosGlob_left);
00310
00311 float recHitPos=-1;
00312 if(sl != 2){
00313 if(fabs(hitPosInChamber_left.x()-segmentPos)<fabs(hitPosInChamber_right.x()-segmentPos))
00314 recHitPos = hitPosInChamber_left.x();
00315 else
00316 recHitPos = hitPosInChamber_right.x();
00317 }
00318 else{
00319 if(fabs(hitPosInChamber_left.y()-segmentPos)<fabs(hitPosInChamber_right.y()-segmentPos))
00320 recHitPos = hitPosInChamber_left.y();
00321 else
00322 recHitPos = hitPosInChamber_right.y();
00323 }
00324
00325 return recHitPos;
00326 }
00327
00328
00329
00330 float
00331 DTCalibValidation::recHitPosition(const DTRecHit1D& recHit, const DTLayer* layer, const DTChamber* chamber, float segmentPos, int sl) {
00332
00333
00334 GlobalPoint recHitPosGlob = layer->toGlobal(recHit.localPosition());
00335 LocalPoint recHitPosInChamber = chamber->toLocal(recHitPosGlob);
00336
00337 float recHitPos = -1;
00338 if(sl != 2)
00339 recHitPos = recHitPosInChamber.x();
00340 else
00341 recHitPos = recHitPosInChamber.y();
00342
00343 return recHitPos;
00344
00345 }
00346
00347
00348
00349
00350 template <typename type>
00351 void DTCalibValidation::compute(const DTGeometry *dtGeom,
00352 const DTRecSegment4D& segment,
00353 std::map<DTWireId, std::vector<type> > recHitsPerWire,
00354 int step) {
00355 bool computeResidual = true;
00356
00357
00358 vector<DTRecHit1D> recHits1D_S3;
00359
00360
00361
00362 const DTChamberRecSegment2D* phiSeg = segment.phiSegment();
00363 if(phiSeg){
00364 vector<DTRecHit1D> phiRecHits = phiSeg->specificRecHits();
00365 if(phiRecHits.size() != 8) {
00366 LogTrace("DTCalibValidation") << "[DTCalibValidation] Phi segments has: " << phiRecHits.size()
00367 << " hits, skipping";
00368 computeResidual = false;
00369 }
00370 copy(phiRecHits.begin(), phiRecHits.end(), back_inserter(recHits1D_S3));
00371 }
00372 if(!phiSeg){
00373 LogTrace("DTCalibValidation") << " [DTCalibValidation] 4D segment has not the phi segment! ";
00374 computeResidual = false;
00375 }
00376
00377 if(segment.dimension() == 4) {
00378 const DTSLRecSegment2D* zSeg = segment.zSegment();
00379 if(zSeg){
00380 vector<DTRecHit1D> zRecHits = zSeg->specificRecHits();
00381 if(zRecHits.size() != 4) {
00382 LogTrace("DTCalibValidation") << "[DTCalibValidation] Theta segments has: " << zRecHits.size()
00383 << " hits, skipping";
00384 computeResidual = false;
00385 }
00386 copy(zRecHits.begin(), zRecHits.end(), back_inserter(recHits1D_S3));
00387 }
00388 if(!zSeg){
00389 LogTrace("DTCalibValidation") << " [DTCalibValidation] 4D segment has not the z segment! ";
00390 computeResidual = false;
00391 }
00392 }
00393
00394 if(!computeResidual)
00395 ++wrongSegment;
00396 if(computeResidual){
00397 ++rightSegment;
00398
00399 for(vector<DTRecHit1D>::const_iterator recHit1D = recHits1D_S3.begin();
00400 recHit1D != recHits1D_S3.end();
00401 ++recHit1D) {
00402 const DTWireId wireId = (*recHit1D).wireId();
00403
00404
00405 const DTLayer* layer = dtGeom->layer(wireId);
00406 float wireX = layer->specificTopology().wirePosition(wireId.wire());
00407
00408
00409
00410
00411 LocalPoint wirePosInLay(wireX,(*recHit1D).localPosition().y(),(*recHit1D).localPosition().z());
00412 GlobalPoint wirePosGlob = layer->toGlobal(wirePosInLay);
00413 const DTChamber* chamber = dtGeom->chamber((*recHit1D).wireId().layerId().chamberId());
00414 LocalPoint wirePosInChamber = chamber->toLocal(wirePosGlob);
00415
00416
00417 LocalPoint segPosAtZWire = segment.localPosition()
00418 + segment.localDirection()*wirePosInChamber.z()/cos(segment.localDirection().theta());
00419
00420
00421 int sl = wireId.superlayer();
00422 float SegmDistance = -1;
00423 if(sl == 1 || sl == 3) {
00424
00425 SegmDistance = fabs(wirePosInChamber.x() - segPosAtZWire.x());
00426 LogTrace("DTCalibValidation") << "SegmDistance: " << SegmDistance;
00427 } else if(sl == 2) {
00428
00429 SegmDistance = fabs(segPosAtZWire.y() - wirePosInChamber.y());
00430 LogTrace("DTCalibValidation") << "SegmDistance: " << SegmDistance;
00431 }
00432 if(SegmDistance > 2.1)
00433 LogTrace("DTCalibValidation") << " Warning: dist segment-wire: " << SegmDistance;
00434
00435
00436 if(recHitsPerWire.find(wireId) == recHitsPerWire.end()) {
00437 LogTrace("DTCalibValidation") << " No RecHit found at Step: " << step << " in cell: " << wireId;
00438 } else {
00439 vector<type> recHits = recHitsPerWire[wireId];
00440 LogTrace("DTCalibValidation") << " " << recHits.size() << " RecHits, Step " << step << " in channel: " << wireId;
00441
00442
00443 const DTLayer* layer = dtGeom->layer(wireId);
00444
00445 const type* theBestRecHit = findBestRecHit(layer, wireId, recHits, SegmDistance);
00446
00447 float recHitWireDist = recHitDistFromWire(*theBestRecHit, layer);
00448 LogTrace("DTCalibValidation") << "recHitWireDist: " << recHitWireDist;
00449
00450
00451 float residualOnDistance = recHitWireDist - SegmDistance;
00452 LogTrace("DTCalibValidation") << "WireId: " << wireId << " ResidualOnDistance: " << residualOnDistance;
00453 float residualOnPosition = -1;
00454 float recHitPos = -1;
00455 if(sl == 1 || sl == 3) {
00456 recHitPos = recHitPosition(*theBestRecHit, layer, chamber, segPosAtZWire.x(), sl);
00457 residualOnPosition = recHitPos - segPosAtZWire.x();
00458 }
00459 else{
00460 recHitPos = recHitPosition(*theBestRecHit, layer, chamber, segPosAtZWire.y(), sl);
00461 residualOnPosition = recHitPos - segPosAtZWire.y();
00462 }
00463 LogTrace("DTCalibValidation") << "WireId: " << wireId << " ResidualOnPosition: " << residualOnPosition;
00464
00465
00466 if(sl == 1 || sl == 3)
00467 fillHistos(wireId.superlayerId(), SegmDistance, residualOnDistance, (wirePosInChamber.x() - segPosAtZWire.x()), residualOnPosition, step);
00468 else
00469 fillHistos(wireId.superlayerId(), SegmDistance, residualOnDistance, (wirePosInChamber.y() - segPosAtZWire.y()), residualOnPosition, step);
00470
00471
00472 }
00473 }
00474 }
00475
00476 }
00477
00478
00479
00480
00481 void DTCalibValidation::bookHistos(DTSuperLayerId slId, int step) {
00482 LogTrace("DTCalibValidation") << " Booking histos for SL: " << slId;
00483
00484
00485 stringstream wheel; wheel << slId.wheel();
00486 stringstream station; station << slId.station();
00487 stringstream sector; sector << slId.sector();
00488 stringstream superLayer; superLayer << slId.superlayer();
00489
00490 stringstream Step; Step << step;
00491
00492
00493 string slHistoName =
00494 "_STEP" + Step.str() +
00495 "_W" + wheel.str() +
00496 "_St" + station.str() +
00497 "_Sec" + sector.str() +
00498 "_SL" + superLayer.str();
00499
00500 theDbe->setCurrentFolder("DT/DTCalibValidation/Wheel" + wheel.str() +
00501 "/Station" + station.str() +
00502 "/Sector" + sector.str());
00503
00504 vector<MonitorElement *> histos;
00505
00506 histos.push_back(theDbe->book1D("hResDist"+slHistoName,
00507 "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
00508 200, -0.4, 0.4));
00509 histos.push_back(theDbe->book2D("hResDistVsDist"+slHistoName,
00510 "Residuals on the distance (cm) from wire (rec_hit - segm_extr) vs distance (cm)",
00511 100, 0, 2.5, 200, -0.4, 0.4));
00512 if(detailedAnalysis){
00513 histos.push_back(theDbe->book1D("hResPos"+slHistoName,
00514 "Residuals on the position from wire (rec_hit - segm_extr) (cm)",
00515 200, -0.4, 0.4));
00516 histos.push_back(theDbe->book2D("hResPosVsPos"+slHistoName,
00517 "Residuals on the position (cm) from wire (rec_hit - segm_extr) vs distance (cm)",
00518 200, -2.5, 2.5, 200, -0.4, 0.4));
00519 }
00520
00521 histosPerSL[make_pair(slId, step)] = histos;
00522 }
00523
00524
00525
00526
00527 void DTCalibValidation::fillHistos(DTSuperLayerId slId,
00528 float distance,
00529 float residualOnDistance,
00530 float position,
00531 float residualOnPosition,
00532 int step) {
00533
00534 vector<MonitorElement *> histos = histosPerSL[make_pair(slId,step)];
00535 histos[0]->Fill(residualOnDistance);
00536 histos[1]->Fill(distance, residualOnDistance);
00537 if(detailedAnalysis){
00538 histos[2]->Fill(residualOnPosition);
00539 histos[3]->Fill(position, residualOnPosition);
00540 }
00541 }
00542