00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "CalibMuon/DTCalibration/plugins/DTVDriftCalibration.h"
00011 #include "CalibMuon/DTCalibration/interface/DTMeanTimerFitter.h"
00012 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00013
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017
00018 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00019 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00020
00021 #include "CalibMuon/DTDigiSync/interface/DTTTrigSyncFactory.h"
00022 #include "CalibMuon/DTDigiSync/interface/DTTTrigBaseSync.h"
00023
00024 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00025 #include "CondFormats/DTObjects/interface/DTMtime.h"
00026
00027 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00028 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00029
00030
00031 #include <map>
00032 #include <iostream>
00033 #include <fstream>
00034 #include <string>
00035 #include <sstream>
00036 #include "TFile.h"
00037 #include "TH1.h"
00038 #include "TF1.h"
00039 #include "TROOT.h"
00040
00041
00042 TH1F * hChi2;
00043 extern void bookHistos();
00044
00045 using namespace std;
00046 using namespace edm;
00047 using namespace dttmaxenums;
00048
00049
00050 DTVDriftCalibration::DTVDriftCalibration(const ParameterSet& pset): select_(pset) {
00051
00052
00053 theRecHits4DLabel = pset.getParameter<InputTag>("recHits4DLabel");
00054
00055
00056 string rootFileName = pset.getUntrackedParameter<string>("rootFileName");
00057 theFile = new TFile(rootFileName.c_str(), "RECREATE");
00058 theFile->cd();
00059
00060 debug = pset.getUntrackedParameter<bool>("debug", false);
00061
00062 theFitter = new DTMeanTimerFitter(theFile);
00063 if(debug)
00064 theFitter->setVerbosity(1);
00065
00066 hChi2 = new TH1F("hChi2","Chi squared tracks",100,0,100);
00067 h2DSegmRPhi = new h2DSegm("SLRPhi");
00068 h2DSegmRZ = new h2DSegm("SLRZ");
00069 h4DSegmAllCh = new h4DSegm("AllCh");
00070 bookHistos();
00071
00072 findVDriftAndT0 = pset.getUntrackedParameter<bool>("fitAndWrite", false);
00073
00074
00075 theCalibChamber = pset.getUntrackedParameter<string>("calibChamber", "All");
00076
00077
00078 theVDriftOutputFile = pset.getUntrackedParameter<string>("vDriftFileName");
00079
00080
00081 theSync = DTTTrigSyncFactory::get()->create(pset.getParameter<string>("tTrigMode"),
00082 pset.getParameter<ParameterSet>("tTrigModeConfig"));
00083
00084
00085 theCalibFilePar = pset.getUntrackedParameter<ParameterSet>("calibFileConfig");
00086
00087
00088 string tMaxGranularity = pset.getUntrackedParameter<string>("tMaxGranularity","bySL");
00089
00090
00091 if(tMaxGranularity != "bySL"){
00092 LogError("Calibration") << "[DTVDriftCalibration] tMaxGranularity will be fixed to bySL.";
00093 tMaxGranularity = "bySL";
00094 }
00095
00096 if(tMaxGranularity == "bySL") {
00097 theGranularity = bySL;
00098 } else if(tMaxGranularity == "byChamber"){
00099 theGranularity = byChamber;
00100 } else if(tMaxGranularity== "byPartition") {
00101 theGranularity = byPartition;
00102 } else throw cms::Exception("Configuration") << "[DTVDriftCalibration] Check parameter tMaxGranularity: "
00103 << tMaxGranularity << " option not available";
00104
00105
00106 LogVerbatim("Calibration") << "[DTVDriftCalibration]Constructor called!";
00107 }
00108
00109 DTVDriftCalibration::~DTVDriftCalibration(){
00110 theFile->Close();
00111 delete theFitter;
00112 LogVerbatim("Calibration") << "[DTVDriftCalibration]Destructor called!";
00113 }
00114
00115 void DTVDriftCalibration::analyze(const Event & event, const EventSetup& eventSetup) {
00116 LogTrace("Calibration") << "--- [DTVDriftCalibration] Event analysed #Run: " << event.id().run()
00117 << " #Event: " << event.id().event();
00118 theFile->cd();
00119 DTChamberId chosenChamberId;
00120
00121 if(theCalibChamber != "All") {
00122 stringstream linestr;
00123 int selWheel, selStation, selSector;
00124 linestr << theCalibChamber;
00125 linestr >> selWheel >> selStation >> selSector;
00126 chosenChamberId = DTChamberId(selWheel, selStation, selSector);
00127 LogTrace("Calibration") << "chosen chamber " << chosenChamberId;
00128 }
00129
00130
00131 ESHandle<DTGeometry> dtGeom;
00132 eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00133
00134
00135 Handle<DTRecSegment4DCollection> all4DSegments;
00136 event.getByLabel(theRecHits4DLabel, all4DSegments);
00137
00138
00139
00140
00141
00142
00143
00144
00145 theSync->setES(eventSetup);
00146
00147
00148 DTRecSegment4DCollection::id_iterator chamberIdIt;
00149 for (chamberIdIt = all4DSegments->id_begin();
00150 chamberIdIt != all4DSegments->id_end();
00151 ++chamberIdIt){
00152
00153
00154 const DTChamber* chamber = dtGeom->chamber(*chamberIdIt);
00155 LogTrace("Calibration") << "Chamber Id: " << *chamberIdIt;
00156
00157
00158 if((theCalibChamber != "All") && ((*chamberIdIt) != chosenChamberId))
00159 continue;
00160
00161
00162 DTRecSegment4DCollection::range range = all4DSegments->get((*chamberIdIt));
00163
00164
00165 for (DTRecSegment4DCollection::const_iterator segment = range.first;
00166 segment!=range.second; ++segment){
00167
00168 if( !(*segment).hasZed() && !(*segment).hasPhi() ){
00169 LogError("Calibration") << "4D segment without Z and Phi segments";
00170 continue;
00171 }
00172
00173 LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
00174 << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
00175
00176 if( !select_(*segment, event, eventSetup) ) continue;
00177
00178 LocalPoint phiSeg2DPosInCham;
00179 LocalVector phiSeg2DDirInCham;
00180 map<DTSuperLayerId,vector<DTRecHit1D> > hitsBySLMap;
00181 if((*segment).hasPhi()){
00182 const DTChamberRecSegment2D* phiSeg = (*segment).phiSegment();
00183 phiSeg2DPosInCham = phiSeg->localPosition();
00184 phiSeg2DDirInCham = phiSeg->localDirection();
00185 vector<DTRecHit1D> phiHits = phiSeg->specificRecHits();
00186 for(vector<DTRecHit1D>::const_iterator hit = phiHits.begin();
00187 hit != phiHits.end(); ++hit) {
00188 DTWireId wireId = (*hit).wireId();
00189 DTSuperLayerId slId = wireId.superlayerId();
00190 hitsBySLMap[slId].push_back(*hit);
00191 }
00192 }
00193
00194 LocalVector zSeg2DDirInCham;
00195 LocalPoint zSeg2DPosInCham;
00196 if((*segment).hasZed()) {
00197 const DTSLRecSegment2D* zSeg = (*segment).zSegment();
00198 const DTSuperLayer* sl = chamber->superLayer(zSeg->superLayerId());
00199 zSeg2DPosInCham = chamber->toLocal(sl->toGlobal((*zSeg).localPosition()));
00200 zSeg2DDirInCham = chamber->toLocal(sl->toGlobal((*zSeg).localDirection()));
00201 hitsBySLMap[zSeg->superLayerId()] = zSeg->specificRecHits();
00202 }
00203
00204 LocalPoint segment4DLocalPos = (*segment).localPosition();
00205 LocalVector segment4DLocalDir = (*segment).localDirection();
00206 double chiSquare = ((*segment).chi2()/(*segment).degreesOfFreedom());
00207
00208 hChi2->Fill(chiSquare);
00209 if((*segment).hasPhi())
00210 h2DSegmRPhi->Fill(phiSeg2DPosInCham.x(), phiSeg2DDirInCham.x()/phiSeg2DDirInCham.z());
00211 if((*segment).hasZed())
00212 h2DSegmRZ->Fill(zSeg2DPosInCham.y(), zSeg2DDirInCham.y()/zSeg2DDirInCham.z());
00213
00214 if((*segment).hasZed() && (*segment).hasPhi())
00215 h4DSegmAllCh->Fill(segment4DLocalPos.x(),
00216 segment4DLocalPos.y(),
00217 atan(segment4DLocalDir.x()/segment4DLocalDir.z())*180./Geom::pi(),
00218 atan(segment4DLocalDir.y()/segment4DLocalDir.z())*180./Geom::pi(),
00219 180 - segment4DLocalDir.theta()*180./Geom::pi());
00220 else if((*segment).hasPhi())
00221 h4DSegmAllCh->Fill(segment4DLocalPos.x(),
00222 atan(segment4DLocalDir.x()/segment4DLocalDir.z())*180./Geom::pi());
00223 else if((*segment).hasZed())
00224 LogWarning("Calibration") << "4d segment with only Z";
00225
00226
00227 for(map<DTSuperLayerId,vector<DTRecHit1D> >::const_iterator slIdAndHits = hitsBySLMap.begin(); slIdAndHits != hitsBySLMap.end(); ++slIdAndHits) {
00228 if (slIdAndHits->second.size() < 3) continue;
00229 DTSuperLayerId slId = slIdAndHits->first;
00230
00231
00232 DTTMax slSeg(slIdAndHits->second, *(chamber->superLayer(slIdAndHits->first)),chamber->toGlobal((*segment).localDirection()), chamber->toGlobal((*segment).localPosition()), theSync);
00233
00234 if(theGranularity == bySL) {
00235 vector<const TMax*> tMaxes = slSeg.getTMax(slId);
00236 DTWireId wireId(slId, 0, 0);
00237 theFile->cd();
00238 cellInfo* cell = theWireIdAndCellMap[wireId];
00239 if (cell==0) {
00240 TString name = (((((TString) "TMax"+(long) slId.wheel()) +(long) slId.station())
00241 +(long) slId.sector())+(long) slId.superLayer());
00242 cell = new cellInfo(name);
00243 theWireIdAndCellMap[wireId] = cell;
00244 }
00245 cell->add(tMaxes);
00246 cell->update();
00247 }
00248 else {
00249 LogWarning("Calibration") << "[DTVDriftCalibration] ###Warning: the chosen granularity is not implemented yet, only bySL available!";
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 }
00273 }
00274 }
00275 }
00276
00277 void DTVDriftCalibration::endJob() {
00278 theFile->cd();
00279 gROOT->GetList()->Write();
00280 h2DSegmRPhi->Write();
00281 h2DSegmRZ->Write();
00282 h4DSegmAllCh->Write();
00283 hChi2->Write();
00284
00285 DTCalibrationMap calibValuesFile(theCalibFilePar);
00286
00287 DTMtime* mTime = new DTMtime();
00288
00289
00290 if(theGranularity == bySL) {
00291 for(map<DTWireId, cellInfo*>::const_iterator wireCell = theWireIdAndCellMap.begin();
00292 wireCell != theWireIdAndCellMap.end(); wireCell++) {
00293 cellInfo* cell= theWireIdAndCellMap[(*wireCell).first];
00294 hTMaxCell* cellHists = cell->getHists();
00295 theFile->cd();
00296 cellHists->Write();
00297 if(findVDriftAndT0) {
00298
00299 DTWireId wireId = (*wireCell).first;
00300 vector<float> newConstants;
00301 TString N=(((((TString) "TMax"+(long) wireId.wheel()) +(long) wireId.station())
00302 +(long) wireId.sector())+(long) wireId.superLayer());
00303 vector<float> vDriftAndReso = theFitter->evaluateVDriftAndReso(N);
00304
00305
00306 if(vDriftAndReso.front() == -1)
00307 continue;
00308 const DTCalibrationMap::CalibConsts* oldConstants = calibValuesFile.getConsts(wireId);
00309 if(oldConstants != 0) {
00310 newConstants.push_back((*oldConstants)[0]);
00311 newConstants.push_back((*oldConstants)[1]);
00312 newConstants.push_back((*oldConstants)[2]);
00313 } else {
00314 newConstants.push_back(-1);
00315 newConstants.push_back(-1);
00316 newConstants.push_back(-1);
00317 }
00318 for(int ivd=0; ivd<=5;ivd++) {
00319
00320
00321 newConstants.push_back(vDriftAndReso[ivd]);
00322 }
00323
00324 calibValuesFile.addCell(calibValuesFile.getKey(wireId), newConstants);
00325
00326
00327 mTime->set((wireId.layerId()).superlayerId(),
00328 vDriftAndReso[0],
00329 vDriftAndReso[1],
00330 DTVelocityUnits::cm_per_ns);
00331 LogTrace("Calibration") << " SL: " << (wireId.layerId()).superlayerId()
00332 << " vDrift = " << vDriftAndReso[0]
00333 << " reso = " << vDriftAndReso[1];
00334 }
00335 }
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 calibValuesFile.writeConsts(theVDriftOutputFile);
00371
00372 LogVerbatim("Calibration") << "[DTVDriftCalibration]Writing vdrift object to DB!";
00373
00374
00375 string record = "DTMtimeRcd";
00376 DTCalibDBUtils::writeToDB<DTMtime>(record, mTime);
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 void DTVDriftCalibration::cellInfo::add(vector<const TMax*> tMaxes) {
00400 float tmax123 = -1.;
00401 float tmax124 = -1.;
00402 float tmax134 = -1.;
00403 float tmax234 = -1.;
00404 SigmaFactor s124 = noR;
00405 SigmaFactor s134 = noR;
00406 unsigned t0_123 = 0;
00407 unsigned t0_124 = 0;
00408 unsigned t0_134 = 0;
00409 unsigned t0_234 = 0;
00410 unsigned hSubGroup = 0;
00411 for (vector<const TMax*>::const_iterator it=tMaxes.begin(); it!=tMaxes.end();
00412 ++it) {
00413 if(*it == 0) {
00414 continue;
00415 }
00416 else {
00417
00418 if (addedCells.size()==4 ||
00419 find(addedCells.begin(), addedCells.end(), (*it)->cells)
00420 != addedCells.end()) {
00421 continue;
00422 }
00423 addedCells.push_back((*it)->cells);
00424 SigmaFactor sigma = (*it)->sigma;
00425 float t = (*it)->t;
00426 TMaxCells cells = (*it)->cells;
00427 unsigned t0Factor = (*it)->t0Factor;
00428 hSubGroup = (*it)->hSubGroup;
00429 if(t < 0.) continue;
00430 switch(cells) {
00431 case notInit : cout << "Error: no cell type assigned to TMax" << endl; break;
00432 case c123 : tmax123 =t; t0_123 = t0Factor; break;
00433 case c124 : tmax124 =t; s124 = sigma; t0_124 = t0Factor; break;
00434 case c134 : tmax134 =t; s134 = sigma; t0_134 = t0Factor; break;
00435 case c234 : tmax234 =t; t0_234 = t0Factor; break;
00436 }
00437 }
00438 }
00439
00440 histos->Fill(tmax123, tmax124, tmax134, tmax234, s124, s134, t0_123,
00441 t0_124, t0_134, t0_234, hSubGroup);
00442 }