CMS 3D CMS Logo

DTSegment2DQuality.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2007/10/25 11:58:37 $
00005  *  $Revision: 1.1 $
00006  *  \author S. Bolognesi and G. Cerminara - INFN Torino
00007  */
00008 
00009 #include "DTSegment2DQuality.h"
00010 #include "Validation/DTRecHits/interface/DTHitQualityUtils.h"
00011 
00012 #include "Geometry/DTGeometry/interface/DTSuperLayer.h"
00013 #include "DataFormats/MuonDetId/interface/DTWireId.h"
00014 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00015 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00016 
00017 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00018 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00019 #include "DataFormats/DTRecHit/interface/DTRecSegment2DCollection.h"
00020 
00021 #include "FWCore/Framework/interface/MakerMacros.h"
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/Event.h"
00024 #include "FWCore/Framework/interface/ESHandle.h"
00025 
00026 #include "Histograms.h"
00027 
00028 #include "TFile.h"
00029 
00030 #include <iostream>
00031 #include <map>
00032 
00033 using namespace std;
00034 using namespace edm;
00035 
00036 // Constructor
00037 DTSegment2DQuality::DTSegment2DQuality(const ParameterSet& pset)  {
00038    // Get the debug parameter for verbose output
00039   debug = pset.getUntrackedParameter<bool>("debug");
00040   DTHitQualityUtils::debug = debug;
00041   rootFileName = pset.getUntrackedParameter<string>("rootFileName");
00042   // the name of the simhit collection
00043   simHitLabel = pset.getUntrackedParameter<string>("simHitLabel", "SimG4Object");
00044   // the name of the 2D rec hit collection
00045   segment2DLabel = pset.getUntrackedParameter<string>("segment2DLabel");
00046 
00047   //sigma resolution on position
00048   sigmaResPos = pset.getParameter<double>("sigmaResPos");
00049   //sigma resolution on angle
00050   sigmaResAngle = pset.getParameter<double>("sigmaResAngle");
00051 
00052   // Create the root file
00053   theFile = new TFile(rootFileName.c_str(), "RECREATE");
00054   theFile->cd();
00055 
00056   if(debug)
00057     cout << "[DTSegment2DQuality] Constructor called" << endl;
00058 
00059   h2DHitRPhi = new HRes2DHit ("RPhi");
00060   h2DHitRZ= new HRes2DHit ("RZ");
00061   h2DHitRZ_W0= new HRes2DHit ("RZ_W0");
00062   h2DHitRZ_W1= new HRes2DHit ("RZ_W1");
00063   h2DHitRZ_W2= new HRes2DHit ("RZ_W2");
00064 
00065   h2DHitEff_RPhi= new HEff2DHit ("RPhi");
00066   h2DHitEff_RZ= new HEff2DHit ("RZ");
00067   h2DHitEff_RZ_W0= new HEff2DHit ("RZ_W0");
00068   h2DHitEff_RZ_W1= new HEff2DHit ("RZ_W1");
00069   h2DHitEff_RZ_W2= new HEff2DHit ("RZ_W2");
00070 }
00071 
00072 // Destructor
00073 DTSegment2DQuality::~DTSegment2DQuality(){
00074 
00075   if(debug)
00076     cout << "[DTSegment2DQuality] Destructor called" << endl;
00077 }
00078 
00079 void DTSegment2DQuality::endJob() {
00080   // Write the histos to file
00081   theFile->cd();
00082 
00083   h2DHitRPhi->Write();
00084   h2DHitRZ->Write();
00085   h2DHitRZ_W0->Write();
00086   h2DHitRZ_W1->Write();
00087   h2DHitRZ_W2->Write();
00088 
00089   h2DHitEff_RPhi->ComputeEfficiency();
00090   h2DHitEff_RZ->ComputeEfficiency();
00091   h2DHitEff_RZ_W0->ComputeEfficiency();
00092   h2DHitEff_RZ_W1->ComputeEfficiency();
00093   h2DHitEff_RZ_W2->ComputeEfficiency();
00094 
00095   h2DHitEff_RPhi->Write();
00096   h2DHitEff_RZ->Write();
00097   h2DHitEff_RZ_W0->Write();
00098   h2DHitEff_RZ_W1->Write();
00099   h2DHitEff_RZ_W2->Write();
00100 
00101   theFile->Close();
00102 } 
00103 
00104 // The real analysis
00105 void DTSegment2DQuality::analyze(const Event & event, const EventSetup& eventSetup){
00106   if(debug)
00107     cout << "--- [DTSegment2DQuality] Analysing Event: #Run: " << event.id().run()
00108          << " #Event: " << event.id().event() << endl;
00109   theFile->cd();
00110 
00111   // Get the DT Geometry
00112   ESHandle<DTGeometry> dtGeom;
00113   eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00114 
00115   // Get the SimHit collection from the event
00116   edm::Handle<PSimHitContainer> simHits;
00117   event.getByLabel(simHitLabel, "MuonDTHits", simHits); //FIXME: second string to be removed
00118 
00119   //Map simHits by sl
00120   map<DTSuperLayerId, PSimHitContainer > simHitsPerSl;
00121   for(PSimHitContainer::const_iterator simHit = simHits->begin();
00122       simHit != simHits->end(); simHit++){
00123     // Create the id of the sl (the simHits in the DT known their wireId)
00124     DTSuperLayerId slId = ((DTWireId(simHit->detUnitId())).layerId()).superlayerId();
00125     // Fill the map
00126     simHitsPerSl[slId].push_back(*simHit);
00127   }
00128 
00129   // Get the 2D rechits from the event
00130   Handle<DTRecSegment2DCollection> segment2Ds;
00131   event.getByLabel(segment2DLabel, segment2Ds);
00132     
00133   // Loop over all superlayers containing a segment
00134   DTRecSegment2DCollection::id_iterator slId;
00135   for (slId = segment2Ds->id_begin();
00136        slId != segment2Ds->id_end();
00137        ++slId){
00138 
00139       //------------------------- simHits ---------------------------//
00140     //Get simHits of each superlayer
00141     PSimHitContainer simHits =  simHitsPerSl[(*slId)];
00142        
00143     // Map simhits per wire
00144     map<DTWireId, PSimHitContainer > simHitsPerWire = DTHitQualityUtils::mapSimHitsPerWire(simHits);
00145     map<DTWireId, const PSimHit*> muSimHitPerWire = DTHitQualityUtils::mapMuSimHitsPerWire(simHitsPerWire);
00146     int nMuSimHit = muSimHitPerWire.size();
00147     if(nMuSimHit == 0 || nMuSimHit == 1) {
00148       if(debug && nMuSimHit == 1)
00149         cout << "[DTSegment2DQuality] Only " << nMuSimHit << " mu SimHit in this SL, skipping!" << endl;
00150       continue; // If no or only one mu SimHit is found skip this SL
00151     } 
00152     if(debug)
00153       cout << "=== SL " << (*slId) << " has " << nMuSimHit << " SimHits" << endl;
00154   
00155     //Find outer and inner mu SimHit to build a segment
00156     pair<const PSimHit*, const PSimHit*> inAndOutSimHit = DTHitQualityUtils::findMuSimSegment(muSimHitPerWire); 
00157     //Check that outermost and innermost SimHit are not the same
00158     if(inAndOutSimHit.first ==inAndOutSimHit.second ) {
00159       cout << "[DTHitQualityUtils]***Warning: outermost and innermost SimHit are the same!" << endl;
00160       continue;
00161     }
00162 
00163     //Find direction and position of the sim Segment in SL RF
00164     pair<LocalVector, LocalPoint> dirAndPosSimSegm = DTHitQualityUtils::findMuSimSegmentDirAndPos(inAndOutSimHit,
00165                                                                                                   (*slId),&(*dtGeom));
00166 
00167     LocalVector simSegmLocalDir = dirAndPosSimSegm.first;
00168     LocalPoint simSegmLocalPos = dirAndPosSimSegm.second;
00169     if(debug)
00170       cout<<"  Simulated segment:  local direction "<<simSegmLocalDir<<endl
00171         <<"                      local position  "<<simSegmLocalPos<<endl;
00172     const DTSuperLayer* superLayer = dtGeom->superLayer(*slId);
00173     GlobalPoint simSegmGlobalPos = superLayer->toGlobal(simSegmLocalPos);
00174 
00175     //Atan(x/z) angle and x position in SL RF
00176     float angleSimSeg = DTHitQualityUtils::findSegmentAlphaAndBeta(simSegmLocalDir).first;
00177     float posSimSeg = simSegmLocalPos.x(); 
00178     //Position (in eta,phi coordinates) in the global RF
00179     float etaSimSeg = simSegmGlobalPos.eta(); 
00180     float phiSimSeg = simSegmGlobalPos.phi();
00181 
00182 
00183     //---------------------------- recHits --------------------------//
00184     // Get the range of rechit for the corresponding slId
00185     bool recHitFound = false;
00186     DTRecSegment2DCollection::range range = segment2Ds->get(*slId);
00187     int nsegm = distance(range.first, range.second);
00188     if(debug)
00189       cout << "   Sl: " << *slId << " has " << nsegm
00190            << " 2D segments" << endl;
00191 
00192     if (nsegm!=0) {
00193       // Find the best RecHit: look for the 2D RecHit with the angle closest
00194       // to that of segment made of SimHits. 
00195       // RecHits must have delta alpha and delta position within 5 sigma of
00196       // the residual distribution (we are looking for residuals of segments
00197       // usefull to the track fit) for efficency purpose
00198       const DTRecSegment2D* bestRecHit = 0;
00199       bool bestRecHitFound = false;
00200       double deltaAlpha = 99999;
00201   
00202       // Loop over the recHits of this slId
00203       for (DTRecSegment2DCollection::const_iterator segment2D = range.first;
00204            segment2D!=range.second;
00205            ++segment2D){
00206         // Check the dimension
00207         if((*segment2D).dimension() != 2) {
00208       if(debug) cout << "[DTSegment2DQuality]***Error: This is not 2D segment!!!" << endl;
00209           abort();
00210         }
00211         // Segment Local Direction and position (in SL RF)
00212         LocalVector recSegDirection = (*segment2D).localDirection();
00213         LocalPoint recSegPosition = (*segment2D).localPosition();
00214       
00215         float recSegAlpha = DTHitQualityUtils::findSegmentAlphaAndBeta(recSegDirection).first;
00216         if(debug)
00217       cout << "  RecSegment direction: " << recSegDirection << endl
00218              << "             position : " << recSegPosition << endl
00219              << "             alpha    : " << recSegAlpha << endl;
00220       
00221         if(fabs(recSegAlpha - angleSimSeg) < deltaAlpha) {
00222           deltaAlpha = fabs(recSegAlpha - angleSimSeg);
00223           bestRecHit = &(*segment2D);
00224           bestRecHitFound = true;
00225         }
00226       }  // End of Loop over all 2D RecHits
00227 
00228       if(bestRecHitFound) {
00229         // Best rechit direction and position in SL RF
00230         LocalPoint bestRecHitLocalPos = bestRecHit->localPosition();
00231         LocalVector bestRecHitLocalDir = bestRecHit->localDirection();
00232 
00233     LocalError bestRecHitLocalPosErr = bestRecHit->localPositionError();
00234     LocalError bestRecHitLocalDirErr = bestRecHit->localDirectionError();
00235 
00236         float angleBestRHit = DTHitQualityUtils::findSegmentAlphaAndBeta(bestRecHitLocalDir).first;
00237 
00238         if(fabs(angleBestRHit - angleSimSeg) < 5*sigmaResAngle &&
00239            fabs(bestRecHitLocalPos.x() - posSimSeg) < 5*sigmaResPos) {
00240           recHitFound = true;
00241         }
00242 
00243         // Fill Residual histos
00244         HRes2DHit *hRes = 0;
00245         if((*slId).superlayer() == 1 || (*slId).superlayer() == 3) { //RPhi SL
00246           hRes = h2DHitRPhi;
00247         } else if((*slId).superlayer() == 2) { //RZ SL
00248           h2DHitRZ->Fill(angleSimSeg,
00249                     angleBestRHit,
00250                     posSimSeg,
00251                     bestRecHitLocalPos.x(),
00252                     etaSimSeg,
00253                     phiSimSeg,
00254                     sqrt(bestRecHitLocalPosErr.xx()),
00255                     sqrt(bestRecHitLocalDirErr.xx())
00256                     );
00257           if(abs((*slId).wheel()) == 0)
00258             hRes = h2DHitRZ_W0;
00259           else if(abs((*slId).wheel()) == 1)
00260             hRes = h2DHitRZ_W1;
00261           else if(abs((*slId).wheel()) == 2)
00262             hRes = h2DHitRZ_W2;
00263         }
00264         hRes->Fill(angleSimSeg,
00265                angleBestRHit,
00266                posSimSeg,
00267                bestRecHitLocalPos.x(),
00268                etaSimSeg,
00269                phiSimSeg,
00270                sqrt(bestRecHitLocalPosErr.xx()),
00271                sqrt(bestRecHitLocalDirErr.xx())
00272                );
00273       }
00274     } //end of if(nsegm!=0)
00275 
00276       // Fill Efficiency plot
00277     HEff2DHit *hEff = 0;
00278     if((*slId).superlayer() == 1 || (*slId).superlayer() == 3) { //RPhi SL
00279       hEff = h2DHitEff_RPhi;
00280     } else if((*slId).superlayer() == 2) { //RZ SL
00281       h2DHitEff_RZ->Fill(etaSimSeg, phiSimSeg, posSimSeg, angleSimSeg, recHitFound);
00282       if(abs((*slId).wheel()) == 0)
00283         hEff = h2DHitEff_RZ_W0;
00284       else if(abs((*slId).wheel()) == 1)
00285         hEff = h2DHitEff_RZ_W1;
00286       else if(abs((*slId).wheel()) == 2)
00287         hEff = h2DHitEff_RZ_W2;
00288     }
00289     hEff->Fill(etaSimSeg, phiSimSeg, posSimSeg, angleSimSeg, recHitFound);
00290   } // End of loop over superlayers
00291 }
00292 
00293 
00294 
00295 
00296 
00297 

Generated on Tue Jun 9 17:49:03 2009 for CMSSW by  doxygen 1.5.4