CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CalibMuon/DTCalibration/plugins/DTVDriftSegmentCalibration.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2010/11/19 14:02:08 $
00006  *  $Revision: 1.3 $
00007  *  \author A. Vilela Pereira
00008  */
00009 
00010 #include "DTVDriftSegmentCalibration.h"
00011 
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "FWCore/Framework/interface/EventSetup.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/Utilities/interface/InputTag.h"
00018 
00019 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00020 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00021 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00022 #include "CondFormats/DTObjects/interface/DTMtime.h"
00023 
00024 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00025 #include "CalibMuon/DTCalibration/interface/DTSegmentSelector.h"
00026 
00027 #include <string>
00028 #include <sstream>
00029 #include "TFile.h"
00030 #include "TH1F.h"
00031 #include "TH2F.h"
00032 
00033 using namespace std;
00034 using namespace edm;
00035 
00036 DTVDriftSegmentCalibration::DTVDriftSegmentCalibration(const ParameterSet& pset):
00037   select_(pset),
00038   theRecHits4DLabel_(pset.getParameter<InputTag>("recHits4DLabel")),
00039   //writeVDriftDB_(pset.getUntrackedParameter<bool>("writeVDriftDB", false)),
00040   theCalibChamber_(pset.getUntrackedParameter<string>("calibChamber", "All")) {
00041 
00042   LogVerbatim("Calibration") << "[DTVDriftSegmentCalibration] Constructor called!";
00043 
00044   // the root file which will contain the histos
00045   string rootFileName = pset.getUntrackedParameter<string>("rootFileName","DTVDriftHistos.root");
00046   rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
00047   rootFile_->cd();
00048 }
00049 
00050 void DTVDriftSegmentCalibration::beginJob(){
00051   TH1::SetDefaultSumw2(true);
00052 }
00053 
00054 void DTVDriftSegmentCalibration::beginRun(const edm::Run& run, const edm::EventSetup& setup) {}
00055 
00056 DTVDriftSegmentCalibration::~DTVDriftSegmentCalibration(){
00057   rootFile_->Close();
00058   LogVerbatim("Calibration") << "[DTVDriftSegmentCalibration] Destructor called!";
00059 }
00060 
00061 void DTVDriftSegmentCalibration::analyze(const Event & event, const EventSetup& eventSetup) {
00062   rootFile_->cd();
00063 
00064   // Get the DT Geometry
00065   ESHandle<DTGeometry> dtGeom;
00066   eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00067 
00068   // Get the rechit collection from the event
00069   Handle<DTRecSegment4DCollection> all4DSegments;
00070   event.getByLabel(theRecHits4DLabel_, all4DSegments); 
00071 
00072   DTChamberId chosenChamberId;
00073   if(theCalibChamber_ != "All") {
00074     stringstream linestr;
00075     int selWheel, selStation, selSector;
00076     linestr << theCalibChamber_;
00077     linestr >> selWheel >> selStation >> selSector;
00078     chosenChamberId = DTChamberId(selWheel, selStation, selSector);
00079     LogVerbatim("Calibration") << " Chosen chamber: " << chosenChamberId << endl;
00080   }
00081   // Loop over segments by chamber
00082   DTRecSegment4DCollection::id_iterator chamberIdIt;
00083   for(chamberIdIt = all4DSegments->id_begin(); chamberIdIt != all4DSegments->id_end(); ++chamberIdIt){
00084 
00085     // Calibrate just the chosen chamber/s    
00086     if((theCalibChamber_ != "All") && ((*chamberIdIt) != chosenChamberId)) continue;
00087 
00088     // Book histos
00089     if(theVDriftHistoMapTH1F_.find(*chamberIdIt) == theVDriftHistoMapTH1F_.end()){
00090       LogTrace("Calibration") << "   Booking histos for Chamber: " << *chamberIdIt;
00091       bookHistos(*chamberIdIt);
00092     }
00093    
00094     // Get the chamber from the setup
00095     const DTChamber* chamber = dtGeom->chamber(*chamberIdIt);
00096     // Get the range for the corresponding ChamberId
00097     DTRecSegment4DCollection::range range = all4DSegments->get((*chamberIdIt));
00098     // Loop over the rechits of this DetUnit
00099     for(DTRecSegment4DCollection::const_iterator segment  = range.first;
00100                                                  segment != range.second; ++segment){
00101 
00102       
00103       LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
00104                               << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
00105       
00106       if( !select_(*segment, event, eventSetup) ) continue;
00107 
00108       // Fill v-drift values
00109       if( (*segment).hasPhi() ) {
00110          //if( segment->phiSegment()->ist0Valid() ){
00111          double segmentVDrift = segment->phiSegment()->vDrift();
00112          if( segmentVDrift != 0.00 ){   
00113             (theVDriftHistoMapTH1F_[*chamberIdIt])[0]->Fill(segmentVDrift);
00114             (theVDriftHistoMapTH2F_[*chamberIdIt])[0]->Fill(segment->localPosition().x(),segmentVDrift);
00115             (theVDriftHistoMapTH2F_[*chamberIdIt])[1]->Fill(segment->localPosition().y(),segmentVDrift);
00116         }
00117       }
00118       // Probably not meaningful 
00119       if( (*segment).hasZed() ){
00120          //if( segment->zSegment()->ist0Valid() ){
00121          double segmentVDrift = segment->zSegment()->vDrift();
00122          if( segmentVDrift != 0.00 ){
00123             (theVDriftHistoMapTH1F_[*chamberIdIt])[1]->Fill(segmentVDrift);
00124         }
00125       }
00126     } // DTRecSegment4DCollection::const_iterator segment
00127   } // DTRecSegment4DCollection::id_iterator chamberIdIt
00128 } // DTVDriftSegmentCalibration::analyze
00129 
00130 void DTVDriftSegmentCalibration::endJob() {
00131   rootFile_->cd();
00132   
00133   LogVerbatim("Calibration") << "[DTVDriftSegmentCalibration] Writing histos to file!" << endl;
00134 
00135   for(ChamberHistosMapTH1F::const_iterator itChHistos = theVDriftHistoMapTH1F_.begin(); itChHistos != theVDriftHistoMapTH1F_.end(); ++itChHistos){
00136      vector<TH1F*>::const_iterator itHistTH1F = (*itChHistos).second.begin();
00137      vector<TH1F*>::const_iterator itHistTH1F_end = (*itChHistos).second.end();
00138      for(; itHistTH1F != itHistTH1F_end; ++itHistTH1F) (*itHistTH1F)->Write();
00139 
00140      vector<TH2F*>::const_iterator itHistTH2F = theVDriftHistoMapTH2F_[(*itChHistos).first].begin();
00141      vector<TH2F*>::const_iterator itHistTH2F_end = theVDriftHistoMapTH2F_[(*itChHistos).first].end();
00142      for(; itHistTH2F != itHistTH2F_end; ++itHistTH2F) (*itHistTH2F)->Write();
00143   }
00144 
00145   /*if(writeVDriftDB_){
00146      // ...
00147   }*/ 
00148 }
00149 
00150 // Book a set of histograms for a given Chamber
00151 void DTVDriftSegmentCalibration::bookHistos(DTChamberId chId) {
00152 
00153   // Compose the chamber name
00154   stringstream wheel; wheel << chId.wheel();
00155   stringstream station; station << chId.station();
00156   stringstream sector; sector << chId.sector();
00157 
00158   string chHistoName =
00159     "_W" + wheel.str() +
00160     "_St" + station.str() +
00161     "_Sec" + sector.str();
00162 
00163   vector<TH1F*> histosTH1F;
00164   histosTH1F.push_back(new TH1F(("hRPhiVDriftCorr" + chHistoName).c_str(), "v-drift corr. from Phi segments", 200, -0.4, 0.4));
00165   if(chId.station() != 4) histosTH1F.push_back(new TH1F(("hRZVDriftCorr" + chHistoName).c_str(), "v-drift corr. from Z segments", 200, -0.4, 0.4));
00166   
00167   vector<TH2F*> histosTH2F;
00168   histosTH2F.push_back(new TH2F(("hRPhiVDriftCorrVsSegmPosX" + chHistoName).c_str(), "v-drift corr. vs. segment x position", 250, -125., 125., 200, -0.4, 0.4));
00169   histosTH2F.push_back(new TH2F(("hRPhiVDriftCorrVsSegmPosY" + chHistoName).c_str(), "v-drift corr. vs. segment y position", 250, -125., 125., 200, -0.4, 0.4));
00170   //if(chId.station() != 4) ...
00171 
00172   theVDriftHistoMapTH1F_[chId] = histosTH1F;
00173   theVDriftHistoMapTH2F_[chId] = histosTH2F;
00174 }