CMS 3D CMS Logo

MuonSeedBuilder.cc

Go to the documentation of this file.
00001 
00008 #include <RecoMuon/MuonSeedGenerator/src/MuonSeedBuilder.h>
00009 #include <RecoMuon/MuonSeedGenerator/src/MuonSeedCreator.h>
00010 
00011 // Data Formats 
00012 #include <DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h>
00013 #include <DataFormats/TrajectorySeed/interface/TrajectorySeed.h>
00014 #include <DataFormats/CSCRecHit/interface/CSCRecHit2DCollection.h>
00015 #include <DataFormats/CSCRecHit/interface/CSCRecHit2D.h>
00016 #include <DataFormats/CSCRecHit/interface/CSCSegmentCollection.h>
00017 #include <DataFormats/CSCRecHit/interface/CSCSegment.h>
00018 #include <DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h>
00019 #include <DataFormats/DTRecHit/interface/DTRecSegment4D.h>
00020 
00021 // Geometry
00022 #include <Geometry/CommonDetUnit/interface/GeomDet.h>
00023 #include <TrackingTools/DetLayers/interface/DetLayer.h>
00024 #include <RecoMuon/MeasurementDet/interface/MuonDetLayerMeasurements.h>
00025 #include <RecoMuon/DetLayers/interface/MuonDetLayerGeometry.h>
00026 #include <RecoMuon/Records/interface/MuonRecoGeometryRecord.h>
00027 
00028 // muon service
00029 #include <TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h>
00030 #include <DataFormats/TrajectoryState/interface/PTrajectoryStateOnDet.h>
00031 
00032 // Framework
00033 #include <FWCore/ParameterSet/interface/ParameterSet.h>
00034 #include "FWCore/Framework/interface/Event.h"
00035 #include <FWCore/Framework/interface/ESHandle.h>
00036 #include <FWCore/MessageLogger/interface/MessageLogger.h> 
00037 #include <DataFormats/Common/interface/Handle.h>
00038 
00039 // C++
00040 #include <vector>
00041 #include <deque>
00042 #include <utility>
00043 
00044 //typedef std::pair<double, TrajectorySeed> seedpr ;
00045 //static bool ptDecreasing(const seedpr s1, const seedpr s2) { return ( s1.first > s2.first ); }
00046 static bool lengthSorting(const TrajectorySeed s1, const TrajectorySeed s2) { return ( s1.nHits() > s2.nHits() ); }
00047 
00048 /*
00049  * Constructor
00050  */
00051 MuonSeedBuilder::MuonSeedBuilder(const edm::ParameterSet& pset){
00052 
00053   // Local Debug flag
00054   debug                = pset.getParameter<bool>("DebugMuonSeed");
00055 
00056   // enable the DT chamber
00057   enableDTMeasurement  = pset.getParameter<bool>("EnableDTMeasurement");
00058   theDTSegmentLabel    = pset.getParameter<edm::InputTag>("DTSegmentLabel");
00059 
00060   // enable the CSC chamber
00061   enableCSCMeasurement = pset.getParameter<bool>("EnableCSCMeasurement");
00062   theCSCSegmentLabel   = pset.getParameter<edm::InputTag>("CSCSegmentLabel");
00063 
00064   // Parameters for seed creation in endcap region
00065   minCSCHitsPerSegment = pset.getParameter<int>("minCSCHitsPerSegment");
00066   maxDeltaEtaCSC       = pset.getParameter<double>("maxDeltaEtaCSC");
00067   maxDeltaPhiCSC       = pset.getParameter<double>("maxDeltaPhiCSC");
00068 
00069   // Parameters for seed creation in overlap region
00070   maxDeltaEtaOverlap   = pset.getParameter<double>("maxDeltaEtaOverlap");
00071   maxDeltaPhiOverlap   = pset.getParameter<double>("maxDeltaPhiOverlap");
00072 
00073   // Parameters for seed creation in barrel region
00074   minDTHitsPerSegment  = pset.getParameter<int>("minDTHitsPerSegment");
00075   maxDeltaEtaDT        = pset.getParameter<double>("maxDeltaEtaDT");
00076   maxDeltaPhiDT        = pset.getParameter<double>("maxDeltaPhiDT");
00077 
00078   // Parameters to suppress combinatorics (ghosts)
00079   maxEtaResolutionDT   = pset.getParameter<double>("maxEtaResolutionDT"); 
00080   maxPhiResolutionDT   = pset.getParameter<double>("maxPhiResolutionDT"); 
00081   maxEtaResolutionCSC  = pset.getParameter<double>("maxEtaResolutionCSC"); 
00082   maxPhiResolutionCSC  = pset.getParameter<double>("maxPhiResolutionCSC"); 
00083 
00084   // muon service
00085   edm::ParameterSet serviceParameters = pset.getParameter<edm::ParameterSet>("ServiceParameters");
00086   theService        = new MuonServiceProxy(serviceParameters);
00087 
00088   // Class for forming muon seeds
00089   muonSeedCreate_      = new MuonSeedCreator( pset ); 
00090 
00091 }
00092 
00093 /*
00094  * Destructor
00095  */
00096 MuonSeedBuilder::~MuonSeedBuilder(){
00097 
00098   delete muonSeedCreate_;
00099   if (theService) delete theService;
00100 }
00101 
00102 /* 
00103  * build
00104  *
00105  * Where the segments are sorted out to make a protoTrack (vector of matching segments in different 
00106  * stations/layers).  The protoTrack is then passed to the seed creator to create CSC, DT or overlap seeds
00107  *
00108  */
00109 //void MuonSeedBuilder::build( MuonDetLayerMeasurements muonMeasurements, TrajectorySeedCollection& theSeeds ) {
00110 int MuonSeedBuilder::build( edm::Event& event, const edm::EventSetup& eventSetup, TrajectorySeedCollection& theSeeds ) {
00111 
00112 
00113   // Pass the Magnetic Field to where the seed is create
00114   muonSeedCreate_->setBField(BField);
00115 
00116   // Create temporary collection of seeds which will be cleaned up to remove combinatorics
00117   std::vector<TrajectorySeed> rawSeeds;
00118   std::vector<float> etaOfSeed;
00119   std::vector<float> phiOfSeed;
00120   std::vector<int> nSegOnSeed;
00121 
00122  // Instantiate the accessor (get the segments: DT + CSC but not RPC=false)
00123   MuonDetLayerMeasurements muonMeasurements(theDTSegmentLabel,theCSCSegmentLabel,edm::InputTag(),
00124                                             enableDTMeasurement,enableCSCMeasurement,false);
00125 
00126  // Instantiate the accessor (get the segments: DT + CSC but not RPC=false)
00127  // MuonDetLayerMeasurements muonMeasurements(enableDTMeasurement,enableCSCMeasurement,false,
00128  //                                          theDTSegmentLabel.label(),theCSCSegmentLabel.label());
00129 
00130 
00131   // 1) Get the various stations and store segments in containers for each station (layers)
00132  
00133   // 1a. get the DT segments by stations (layers):
00134   std::vector<DetLayer*> dtLayers = muonLayers->allDTLayers();
00135  
00136   SegmentContainer DTlist4 = muonMeasurements.recHits( dtLayers[3], event );
00137   SegmentContainer DTlist3 = muonMeasurements.recHits( dtLayers[2], event );
00138   SegmentContainer DTlist2 = muonMeasurements.recHits( dtLayers[1], event );
00139   SegmentContainer DTlist1 = muonMeasurements.recHits( dtLayers[0], event );
00140 
00141   // Initialize flags that a given segment has been allocated to a seed
00142   BoolContainer usedDTlist4(DTlist4.size(), false);
00143   BoolContainer usedDTlist3(DTlist3.size(), false);
00144   BoolContainer usedDTlist2(DTlist2.size(), false);
00145   BoolContainer usedDTlist1(DTlist1.size(), false);
00146 
00147   if (debug) {
00148     std::cout << "*** Number of DT segments is: " << DTlist4.size()+DTlist3.size()+DTlist2.size()+DTlist1.size() << std::endl;
00149     std::cout << "In MB1: " << DTlist1.size() << std::endl;
00150     std::cout << "In MB2: " << DTlist2.size() << std::endl;
00151     std::cout << "In MB3: " << DTlist3.size() << std::endl;
00152     std::cout << "In MB4: " << DTlist4.size() << std::endl;
00153   }
00154 
00155   // 1b. get the CSC segments by stations (layers):
00156   // 1b.1 Global z < 0
00157   std::vector<DetLayer*> cscBackwardLayers = muonLayers->backwardCSCLayers();    
00158   SegmentContainer CSClist4B = muonMeasurements.recHits( cscBackwardLayers[4], event );
00159   SegmentContainer CSClist3B = muonMeasurements.recHits( cscBackwardLayers[3], event );
00160   SegmentContainer CSClist2B = muonMeasurements.recHits( cscBackwardLayers[2], event );
00161   SegmentContainer CSClist1B = muonMeasurements.recHits( cscBackwardLayers[1], event ); // ME1/2 and 1/3
00162   SegmentContainer CSClist0B = muonMeasurements.recHits( cscBackwardLayers[0], event ); // ME11
00163 
00164   BoolContainer usedCSClist4B(CSClist4B.size(), false);
00165   BoolContainer usedCSClist3B(CSClist3B.size(), false);
00166   BoolContainer usedCSClist2B(CSClist2B.size(), false);
00167   BoolContainer usedCSClist1B(CSClist1B.size(), false);
00168   BoolContainer usedCSClist0B(CSClist0B.size(), false);
00169 
00170   // 1b.2 Global z > 0
00171   std::vector<DetLayer*> cscForwardLayers = muonLayers->forwardCSCLayers();
00172   SegmentContainer CSClist4F = muonMeasurements.recHits( cscForwardLayers[4], event );
00173   SegmentContainer CSClist3F = muonMeasurements.recHits( cscForwardLayers[3], event );
00174   SegmentContainer CSClist2F = muonMeasurements.recHits( cscForwardLayers[2], event );
00175   SegmentContainer CSClist1F = muonMeasurements.recHits( cscForwardLayers[1], event );
00176   SegmentContainer CSClist0F = muonMeasurements.recHits( cscForwardLayers[0], event );
00177 
00178   BoolContainer usedCSClist4F(CSClist4F.size(), false);
00179   BoolContainer usedCSClist3F(CSClist3F.size(), false);
00180   BoolContainer usedCSClist2F(CSClist2F.size(), false);
00181   BoolContainer usedCSClist1F(CSClist1F.size(), false);
00182   BoolContainer usedCSClist0F(CSClist0F.size(), false);
00183 
00184   if (debug) {
00185     std::cout << "*** Number of CSC segments is " << CSClist4F.size()+CSClist3F.size()+CSClist2F.size()+CSClist1F.size()+CSClist0F.size()+CSClist4B.size()+CSClist3B.size()+CSClist2B.size()+CSClist1B.size()+CSClist0B.size()<< std::endl;
00186     std::cout << "In ME11: " << CSClist0F.size()+CSClist0B.size() << std::endl;
00187     std::cout << "In ME12: " << CSClist1F.size()+CSClist1B.size() << std::endl;
00188     std::cout << "In ME2 : " << CSClist2F.size()+CSClist2B.size() << std::endl;
00189     std::cout << "In ME3 : " << CSClist3F.size()+CSClist3B.size() << std::endl;
00190     std::cout << "In ME4 : " << CSClist4F.size()+CSClist4B.size() << std::endl;
00191   }
00192 
00193 
00194   /* ******************************************************************************************************************
00195    * Form seeds in barrel region
00196    *
00197    * Proceed from inside -> out
00198    * ******************************************************************************************************************/
00199 
00200 
00201   // Loop over all possible MB1 segment to form seeds:
00202   int index = -1;
00203   for (SegmentContainer::iterator it = DTlist1.begin(); it != DTlist1.end(); ++it ){
00204 
00205     index++;
00206 
00207     if (usedDTlist1[index] == true) continue;
00208     if ( int ((*it)->recHits().size()) < minDTHitsPerSegment ) continue;
00209     if ((*it)->dimension() != 4) continue;
00210 
00211     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00212     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00213     
00214     // Global position of starting point for protoTrack
00215     GlobalPoint gp = (*it)->globalPosition();
00216     float eta_temp = gp.eta();
00217     float phi_temp = gp.phi();
00218     bool showeringBefore = false;
00219     NShowerSeg = 0; 
00220     if ( IdentifyShowering( DTlist1, usedDTlist1, eta_temp, phi_temp, -1, NShowerSeg )  ) showeringBefore = true ;
00221     int NShowers = 0; 
00222     if ( showeringBefore ) {
00223         //usedDTlist1[index] = true;
00224         NShowers++ ;
00225     }
00226      
00227     SegmentContainer protoTrack;
00228     protoTrack.push_back(*it);
00229 
00230     std::vector<int> layers;
00231     layers.push_back(-1);
00232 
00233     // Try adding segment from other stations
00234     if (foundMatchingSegment(3, protoTrack, DTlist2, usedDTlist2, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(-2);
00235     if ( showeringBefore )  NShowers++ ;
00236     if (foundMatchingSegment(3, protoTrack, DTlist3, usedDTlist3, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(-3);
00237     if ( showeringBefore )  NShowers++ ;
00238     if (foundMatchingSegment(3, protoTrack, DTlist4, usedDTlist4, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(-4);
00239     if ( showeringBefore )  NShowers++ ;
00240 
00241     // Check if in overlap region
00242     if (eta_temp > 0.8) {
00243       if (foundMatchingSegment(2, protoTrack, CSClist1F, usedCSClist1F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00244       if ( showeringBefore )  NShowers++ ;
00245       if (foundMatchingSegment(2, protoTrack, CSClist2F, usedCSClist2F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00246       if ( showeringBefore )  NShowers++ ;
00247       if (foundMatchingSegment(2, protoTrack, CSClist3F, usedCSClist3F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00248       if ( showeringBefore )  NShowers++ ;
00249     } 
00250     else if (eta_temp < -0.8) {
00251       if (foundMatchingSegment(2, protoTrack, CSClist1B, usedCSClist1B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00252       if ( showeringBefore )  NShowers++ ;
00253       if (foundMatchingSegment(2, protoTrack, CSClist2B, usedCSClist2B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00254       if ( showeringBefore )  NShowers++ ;
00255       if (foundMatchingSegment(2, protoTrack, CSClist3B, usedCSClist3B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00256       if ( showeringBefore )  NShowers++ ;
00257     }
00258  
00259     unsigned nLayers = layers.size();
00260 
00261     // adding showering information   
00262     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00263        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00264            protoTrack.push_back( ShoweringSegments[i] );
00265            layers.push_back( ShoweringLayers[i] );
00266        }
00267        ShoweringSegments.clear() ;  
00268        ShoweringLayers.clear() ;  
00269     }
00270 
00271     TrajectorySeed thisSeed;
00272 
00273     if ( nLayers < 2 ) {
00274         thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00275     } else {
00276       if ( layers[nLayers-1] > 0 ) {
00277         thisSeed = muonSeedCreate_->createSeed(2, protoTrack, layers, NShowers, NShowerSeg );
00278       } else {
00279         thisSeed = muonSeedCreate_->createSeed(3, protoTrack, layers, NShowers, NShowerSeg ); 
00280       }
00281     }
00282     // Add the seeds to master collection
00283     rawSeeds.push_back(thisSeed); 
00284     etaOfSeed.push_back(eta_temp);
00285     phiOfSeed.push_back(phi_temp);
00286     nSegOnSeed.push_back( protoTrack.size() );
00287 
00288     // Marked segment as used
00289     usedDTlist1[index] = true;
00290   }
00291 
00292 
00293   // Loop over all possible MB2 segment to form seeds:
00294   index = -1;
00295   for (SegmentContainer::iterator it = DTlist2.begin(); it != DTlist2.end(); ++it ){
00296 
00297     index++;
00298 
00299     if (usedDTlist2[index] == true) continue;
00300     if ( int ((*it)->recHits().size()) < minDTHitsPerSegment ) continue;  
00301     if ((*it)->dimension() != 4) continue;
00302 
00303     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00304     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00305 
00306     // Global position of starting point for protoTrack
00307     GlobalPoint gp = (*it)->globalPosition();
00308     float eta_temp = gp.eta();
00309     float phi_temp = gp.phi();
00310     bool showeringBefore = false; 
00311     NShowerSeg = 0; 
00312     if ( IdentifyShowering( DTlist2, usedDTlist2, eta_temp, phi_temp, -2, NShowerSeg)  ) showeringBefore = true ;
00313     int NShowers = 0; 
00314     if ( showeringBefore ) {
00315        // usedDTlist2[index] = true;
00316         NShowers++ ;
00317     }
00318 
00319     SegmentContainer protoTrack;
00320     protoTrack.push_back(*it);
00321 
00322     std::vector<int> layers;
00323     layers.push_back(-2);
00324 
00325  
00326     // Try adding segment from other stations
00327     if (foundMatchingSegment(3, protoTrack, DTlist3, usedDTlist3, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(-3);
00328     if ( showeringBefore )  NShowers++ ;
00329     if (foundMatchingSegment(3, protoTrack, DTlist4, usedDTlist4, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(-4);
00330     if ( showeringBefore )  NShowers++ ;
00331 
00332     // Check if in overlap region
00333     if (eta_temp > 0.8) {
00334       if (foundMatchingSegment(2, protoTrack, CSClist1F, usedCSClist1F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00335       if ( showeringBefore )  NShowers++ ;
00336       if (foundMatchingSegment(2, protoTrack, CSClist2F, usedCSClist2F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00337       if ( showeringBefore )  NShowers++ ;
00338       if (foundMatchingSegment(2, protoTrack, CSClist3F, usedCSClist3F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00339       if ( showeringBefore )  NShowers++ ;
00340     }
00341     else if (eta_temp < -0.8) {
00342       if (foundMatchingSegment(2, protoTrack, CSClist1B, usedCSClist1B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00343       if ( showeringBefore )  NShowers++ ;
00344       if (foundMatchingSegment(2, protoTrack, CSClist2B, usedCSClist2B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00345       if ( showeringBefore )  NShowers++ ;
00346       if (foundMatchingSegment(2, protoTrack, CSClist3B, usedCSClist3B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00347       if ( showeringBefore )  NShowers++ ;
00348     }
00349 
00350     unsigned nLayers = layers.size();
00351     // adding showering information   
00352     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00353        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00354            protoTrack.push_back( ShoweringSegments[i] );
00355            layers.push_back( ShoweringLayers[i] );
00356        }
00357        ShoweringSegments.clear() ;  
00358        ShoweringLayers.clear() ;  
00359     }
00360 
00361   
00362     //if ( nLayers < 2 ) continue;
00363     TrajectorySeed thisSeed;
00364 
00365     if ( nLayers < 2 ) {
00366         thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00367     } else {
00368       if ( layers[nLayers-1] > 0 ) {
00369         thisSeed = muonSeedCreate_->createSeed(2, protoTrack, layers, NShowers, NShowerSeg );
00370       } else {
00371         thisSeed = muonSeedCreate_->createSeed(3, protoTrack, layers, NShowers, NShowerSeg ); 
00372       }
00373     }
00374     // Add the seeds to master collection
00375     rawSeeds.push_back(thisSeed); 
00376     etaOfSeed.push_back(eta_temp);
00377     phiOfSeed.push_back(phi_temp);
00378     nSegOnSeed.push_back( protoTrack.size() );
00379 
00380     // Marked segment as used
00381     usedDTlist2[index] = true;
00382   }
00383 
00384 
00385   // Loop over all possible MB3 segment to form seeds:
00386   index = -1;
00387   for (SegmentContainer::iterator it = DTlist3.begin(); it != DTlist3.end(); ++it ){
00388 
00389     index++;
00390 
00391     if (usedDTlist3[index] == true) continue;
00392     if ( int ((*it)->recHits().size()) < minDTHitsPerSegment ) continue;  
00393     if ((*it)->dimension() != 4) continue;
00394 
00395     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00396     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00397 
00398     // Global position of starting point for protoTrack
00399     GlobalPoint gp = (*it)->globalPosition();
00400     float eta_temp = gp.eta();
00401     float phi_temp = gp.phi();
00402     bool showeringBefore = false; 
00403     NShowerSeg = 0; 
00404     if ( IdentifyShowering( DTlist3, usedDTlist3, eta_temp, phi_temp, -3, NShowerSeg)  ) showeringBefore = true ;
00405     int NShowers = 0; 
00406     if ( showeringBefore ) {
00407        // usedDTlist3[index] = true;
00408         NShowers++ ;
00409     }
00410 
00411     SegmentContainer protoTrack;
00412     protoTrack.push_back(*it);
00413 
00414     std::vector<int> layers;
00415     layers.push_back(-3);
00416  
00417     // Try adding segment from other stations
00418     if (foundMatchingSegment(3, protoTrack, DTlist4, usedDTlist4, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(-4);
00419     if ( showeringBefore )  NShowers++ ;
00420 
00421     // Check if in overlap region
00422     if (eta_temp > 0.8) {
00423       if (foundMatchingSegment(2, protoTrack, CSClist1F, usedCSClist1F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00424       if ( showeringBefore )  NShowers++ ;
00425       if (foundMatchingSegment(2, protoTrack, CSClist2F, usedCSClist2F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00426       if ( showeringBefore )  NShowers++ ;
00427       if (foundMatchingSegment(2, protoTrack, CSClist3F, usedCSClist3F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00428       if ( showeringBefore )  NShowers++ ;
00429     }
00430     else if (eta_temp < -0.8) {
00431       if (foundMatchingSegment(2, protoTrack, CSClist1B, usedCSClist1B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00432       if ( showeringBefore )  NShowers++ ;
00433       if (foundMatchingSegment(2, protoTrack, CSClist2B, usedCSClist2B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00434       if ( showeringBefore )  NShowers++ ;
00435       if (foundMatchingSegment(2, protoTrack, CSClist3B, usedCSClist3B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00436       if ( showeringBefore )  NShowers++ ;
00437     }
00438     
00439     unsigned nLayers = layers.size();
00440     // adding showering information   
00441     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00442        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00443            protoTrack.push_back( ShoweringSegments[i] );
00444            layers.push_back( ShoweringLayers[i] );
00445        }
00446        ShoweringSegments.clear() ;  
00447        ShoweringLayers.clear() ;  
00448     }
00449 
00450     
00451     TrajectorySeed thisSeed;
00452     if ( nLayers < 2 ) {
00453         thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00454     }else {
00455       if ( layers[nLayers-1] > 0 ) {
00456         thisSeed = muonSeedCreate_->createSeed(2, protoTrack, layers, NShowers, NShowerSeg );
00457       } else {
00458         thisSeed = muonSeedCreate_->createSeed(3, protoTrack, layers, NShowers, NShowerSeg ); 
00459       }
00460     }
00461 
00462     // Add the seeds to master collection
00463     rawSeeds.push_back(thisSeed); 
00464     etaOfSeed.push_back(eta_temp);
00465     phiOfSeed.push_back(phi_temp);
00466     nSegOnSeed.push_back( protoTrack.size() );
00467 
00468     // Marked segment as used
00469     usedDTlist3[index] = true;
00470   }
00471 
00472   /* *********************************************************************************************************************
00473    * Form seeds from backward endcap
00474    *
00475    * Proceed from inside -> out
00476    * *********************************************************************************************************************/
00477 
00478   // Loop over all possible ME11 segment to form seeds:
00479   index = -1;
00480 
00481   for (SegmentContainer::iterator it = CSClist0B.begin(); it != CSClist0B.end(); ++it ){
00482 
00483     index++;
00484 
00485     if (usedCSClist0B[index] == true) continue;
00486     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;  
00487 
00488     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00489     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00490 
00491     // Global position of starting point for protoTrack
00492     GlobalPoint gp = (*it)->globalPosition();
00493     float eta_temp = gp.eta();
00494     float phi_temp = gp.phi();
00495     bool showeringBefore = false; 
00496     NShowerSeg = 0; 
00497     if ( IdentifyShowering( CSClist0B, usedCSClist0B, eta_temp, phi_temp, 0, NShowerSeg)  ) showeringBefore = true ;
00498     int NShowers = 0; 
00499     if ( showeringBefore ) {
00500        // usedCSClist0B[index] = true;
00501         NShowers++ ;
00502     }
00503 
00504     SegmentContainer protoTrack;
00505     protoTrack.push_back(*it);
00506 
00507     std::vector<int> layers;
00508     layers.push_back(0);
00509 
00510     // Try adding segment from other station
00511     if (foundMatchingSegment(1, protoTrack, CSClist1B, usedCSClist1B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00512     if ( showeringBefore )  NShowers++ ;
00513     if (foundMatchingSegment(1, protoTrack, CSClist2B, usedCSClist2B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00514     if ( showeringBefore )  NShowers++ ;
00515     if (foundMatchingSegment(1, protoTrack, CSClist3B, usedCSClist3B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00516     if ( showeringBefore )  NShowers++ ;
00517     if (foundMatchingSegment(1, protoTrack, CSClist4B, usedCSClist4B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00518     if ( showeringBefore )  NShowers++ ;
00519 
00520     unsigned nLayers = layers.size();
00521 
00522     // adding showering information   
00523     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00524        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00525            protoTrack.push_back( ShoweringSegments[i] );
00526            layers.push_back( ShoweringLayers[i] );
00527        }
00528        ShoweringSegments.clear() ;  
00529        ShoweringLayers.clear() ;  
00530     }
00531 
00532     //if ( nLayers < 2 ) continue;
00533 
00534     TrajectorySeed thisSeed;
00535     if ( nLayers < 2 ) {
00536         thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00537     }else {
00538       if ( fabs( gp.eta() ) > 1.7  ) {
00539         thisSeed = muonSeedCreate_->createSeed(5, protoTrack, layers, NShowers, NShowerSeg );
00540       } else {
00541         thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
00542       }
00543     }
00544 
00545     // Add the seeds to master collection
00546     rawSeeds.push_back(thisSeed);
00547     etaOfSeed.push_back(eta_temp);
00548     phiOfSeed.push_back(phi_temp);
00549     nSegOnSeed.push_back( nLayers );
00550 
00551     // mark this segment as used
00552     usedCSClist0B[index] = true;
00553   }
00554 
00555 
00556   // Loop over all possible ME1/2 ME1/3 segment to form seeds:
00557   index = -1;
00558   for (SegmentContainer::iterator it = CSClist1B.begin(); it != CSClist1B.end(); ++it ){
00559 
00560     index++;
00561 
00562     if (usedCSClist1B[index] == true) continue;
00563     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;  
00564 
00565     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00566     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00567 
00568     // Global position of starting point for protoTrack
00569     GlobalPoint gp = (*it)->globalPosition();
00570     float eta_temp = gp.eta();
00571     float phi_temp = gp.phi();
00572     bool showeringBefore = false;
00573     NShowerSeg = 0; 
00574     if ( IdentifyShowering( CSClist1B, usedCSClist1B, eta_temp, phi_temp, 1, NShowerSeg)  ) showeringBefore = true ;
00575     int NShowers = 0; 
00576     if ( showeringBefore ) {
00577     //    usedCSClist1B[index] = true;
00578         NShowers++ ;
00579     }
00580 
00581     SegmentContainer protoTrack;
00582     protoTrack.push_back(*it);
00583     
00584     std::vector<int> layers;
00585     layers.push_back(1);
00586 
00587     // Try adding segment from other stations
00588     if (foundMatchingSegment(1, protoTrack, CSClist2B, usedCSClist2B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00589     if ( showeringBefore )  NShowers++ ;
00590     if (foundMatchingSegment(1, protoTrack, CSClist3B, usedCSClist3B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00591     if ( showeringBefore )  NShowers++ ;
00592     if (foundMatchingSegment(1, protoTrack, CSClist4B, usedCSClist4B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00593     if ( showeringBefore )  NShowers++ ;
00594 
00595     unsigned nLayers = layers.size();
00596 
00597     // adding showering information   
00598     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00599        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00600            protoTrack.push_back( ShoweringSegments[i] );
00601            layers.push_back( ShoweringLayers[i] );
00602        }
00603        ShoweringSegments.clear() ;  
00604        ShoweringLayers.clear() ;  
00605     }
00606 
00607     //if ( nLayers < 2 ) continue;
00608 
00609      TrajectorySeed thisSeed; 
00610      if ( nLayers < 2) {
00611        thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00612      } else {
00613        thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
00614      }
00615      // Add the seeds to master collection
00616      rawSeeds.push_back(thisSeed);
00617      etaOfSeed.push_back(eta_temp);
00618      phiOfSeed.push_back(phi_temp);
00619      nSegOnSeed.push_back( nLayers );
00620 
00621      // mark this segment as used
00622      usedCSClist1B[index] = true;
00623 
00624   }
00625 
00626 
00627   // Loop over all possible ME2 segment to form seeds:
00628   index = -1;
00629   for (SegmentContainer::iterator it = CSClist2B.begin(); it != CSClist2B.end(); ++it ){
00630 
00631     index++;
00632 
00633     if (usedCSClist2B[index] == true) continue;
00634     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;  
00635 
00636     double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00637     if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00638 
00639     // Global position of starting point for protoTrack
00640     GlobalPoint gp = (*it)->globalPosition();
00641     float eta_temp = gp.eta();
00642     float phi_temp = gp.phi();
00643     bool showeringBefore = false; 
00644     NShowerSeg = 0; 
00645     if ( IdentifyShowering( CSClist2B, usedCSClist2B, eta_temp, phi_temp, 2, NShowerSeg)  ) showeringBefore = true ;
00646     int NShowers = 0; 
00647     if ( showeringBefore ) {
00648        // usedCSClist2B[index] = true;
00649         NShowers++ ;
00650     }
00651 
00652     SegmentContainer protoTrack;
00653     protoTrack.push_back(*it);
00654 
00655     std::vector<int> layers;
00656     layers.push_back(2);
00657     
00658     // Try adding segment from other stations
00659     if (foundMatchingSegment(1, protoTrack, CSClist3B, usedCSClist3B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00660     if ( showeringBefore )  NShowers++ ;
00661     if (foundMatchingSegment(1, protoTrack, CSClist4B, usedCSClist4B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00662     if ( showeringBefore )  NShowers++ ;
00663 
00664     unsigned nLayers = layers.size();
00665 
00666     // adding showering information   
00667     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00668        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00669            protoTrack.push_back( ShoweringSegments[i] );
00670            layers.push_back( ShoweringLayers[i] );
00671        }
00672        ShoweringSegments.clear() ;  
00673        ShoweringLayers.clear() ;  
00674     }
00675 
00676     //if ( nLayers < 2 ) continue;
00677 
00678     TrajectorySeed thisSeed; 
00679     if ( nLayers < 2) {
00680        thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00681     } else {
00682        thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
00683     }
00684 
00685     // Add the seeds to master collection
00686     rawSeeds.push_back(thisSeed);
00687     etaOfSeed.push_back(eta_temp);
00688     phiOfSeed.push_back(phi_temp);
00689     nSegOnSeed.push_back( nLayers );
00690     // mark this segment as used
00691     usedCSClist2B[index] = true;
00692   }
00693 
00694   // Loop over all possible ME3 segment to form seeds:
00695   index = -1;
00696   for (SegmentContainer::iterator it = CSClist3B.begin(); it != CSClist3B.end(); ++it ){
00697 
00698     index++;
00699 
00700     if (usedCSClist3B[index] == true) continue;
00701     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;  
00702 
00703     double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00704     if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00705 
00706     // Global position of starting point for protoTrack
00707     GlobalPoint gp = (*it)->globalPosition();
00708     float eta_temp = gp.eta();
00709     float phi_temp = gp.phi();
00710     bool showeringBefore = false; 
00711     NShowerSeg = 0; 
00712     if ( IdentifyShowering( CSClist3B, usedCSClist3B, eta_temp, phi_temp, 3, NShowerSeg)  ) showeringBefore = true ;
00713     int NShowers = 0; 
00714     if ( showeringBefore ) {
00715     //    usedCSClist3B[index] = true;
00716         NShowers++ ;
00717     }
00718 
00719     SegmentContainer protoTrack;
00720     protoTrack.push_back(*it);
00721 
00722     std::vector<int> layers;
00723     layers.push_back(2);
00724     
00725     // Try adding segment from other stations
00726     if (foundMatchingSegment(1, protoTrack, CSClist4B, usedCSClist4B, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00727     if ( showeringBefore )  NShowers++ ;
00728 
00729     unsigned nLayers = layers.size();
00730 
00731     // adding showering information   
00732     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00733        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00734            protoTrack.push_back( ShoweringSegments[i] );
00735            layers.push_back( ShoweringLayers[i] );
00736        }
00737        ShoweringSegments.clear() ;  
00738        ShoweringLayers.clear() ;  
00739     }
00740 
00741     // mark this segment as used
00742     usedCSClist3B[index] = true;
00743 
00744     if ( nLayers < 2 ) continue;
00745     TrajectorySeed thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg ); 
00746 
00747     // Add the seeds to master collection
00748     rawSeeds.push_back(thisSeed);
00749     etaOfSeed.push_back(eta_temp);
00750     phiOfSeed.push_back(phi_temp);
00751     nSegOnSeed.push_back( nLayers );
00752   }
00753 
00754 
00755   /* *****************************************************************************************************************
00756    * Form seeds from forward endcap
00757    *
00758    * Proceed from inside -> out
00759    * *****************************************************************************************************************/
00760 
00761   // Loop over all possible ME11 segment to form seeds:
00762   index = -1;
00763   for (SegmentContainer::iterator it = CSClist0F.begin(); it != CSClist0F.end(); ++it ){
00764 
00765     index++;
00766   
00767     if (usedCSClist0F[index] == true) continue;
00768     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;
00769     
00770     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00771     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00772 
00773     // Global position of starting point for protoTrack
00774     GlobalPoint gp = (*it)->globalPosition();
00775     float eta_temp = gp.eta();  
00776     float phi_temp = gp.phi();      
00777     bool showeringBefore = false; 
00778     NShowerSeg = 0; 
00779     if ( IdentifyShowering( CSClist0F, usedCSClist0F, eta_temp, phi_temp, 0, NShowerSeg)  ) showeringBefore = true ;
00780     int NShowers = 0; 
00781     if ( showeringBefore ) {
00782        // usedCSClist0F[index] = true;
00783         NShowers++ ;
00784     }
00785     
00786     SegmentContainer protoTrack;
00787     protoTrack.push_back(*it);
00788 
00789     std::vector<int> layers;
00790     layers.push_back(0);
00791 
00792     // Try adding segment from other station
00793     if (foundMatchingSegment(1, protoTrack, CSClist1F, usedCSClist1F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(1);
00794     if ( showeringBefore )  NShowers++ ;
00795     if (foundMatchingSegment(1, protoTrack, CSClist2F, usedCSClist2F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00796     if ( showeringBefore )  NShowers++ ;
00797     if (foundMatchingSegment(1, protoTrack, CSClist3F, usedCSClist3F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00798     if ( showeringBefore )  NShowers++ ;
00799     if (foundMatchingSegment(1, protoTrack, CSClist4F, usedCSClist4F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00800     if ( showeringBefore )  NShowers++ ;
00801 
00802     unsigned nLayers = layers.size();
00803 
00804     // adding showering information   
00805     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00806        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00807            protoTrack.push_back( ShoweringSegments[i] );
00808            layers.push_back( ShoweringLayers[i] );
00809        }
00810        ShoweringSegments.clear() ;  
00811        ShoweringLayers.clear() ;  
00812     }
00813 
00814     //if ( nLayers < 2 ) continue;
00815 
00816     TrajectorySeed thisSeed;
00817     if ( nLayers < 2 ) {
00818         thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00819     } else {
00820       if ( fabs( gp.eta() ) > 1.7  ) {
00821         thisSeed = muonSeedCreate_->createSeed(5, protoTrack, layers, NShowers, NShowerSeg );
00822       } else {
00823         thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
00824       }
00825     }
00826     // Add the seeds to master collection
00827     rawSeeds.push_back(thisSeed);
00828     etaOfSeed.push_back(eta_temp);
00829     phiOfSeed.push_back(phi_temp);
00830     nSegOnSeed.push_back( nLayers );
00831 
00832     // mark this segment as used
00833     usedCSClist0F[index] = true;
00834   }
00835   
00836 
00837   // Loop over all possible ME1/2 ME1/3 segment to form seeds:
00838   index = -1;
00839   for (SegmentContainer::iterator it = CSClist1F.begin(); it != CSClist1F.end(); ++it ){
00840     
00841     index++;
00842     
00843     if (usedCSClist1F[index] == true) continue;
00844     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;
00845   
00846     //double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00847     //if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00848 
00849     // Global position of starting point for protoTrack
00850     GlobalPoint gp = (*it)->globalPosition();
00851     float eta_temp = gp.eta();
00852     float phi_temp = gp.phi();
00853     bool showeringBefore = false; 
00854     NShowerSeg = 0; 
00855     if ( IdentifyShowering( CSClist1F, usedCSClist1F, eta_temp, phi_temp, 1, NShowerSeg)  ) showeringBefore = true ;
00856     int NShowers = 0; 
00857     if ( showeringBefore ) {
00858      //   usedCSClist1F[index] = true;
00859         NShowers++ ;
00860     }
00861     
00862     SegmentContainer protoTrack;
00863     protoTrack.push_back(*it);
00864    
00865     std::vector<int> layers;
00866     layers.push_back(1);
00867 
00868     // Try adding segment from other stations
00869     if (foundMatchingSegment(1, protoTrack, CSClist2F, usedCSClist2F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(2);
00870     if ( showeringBefore )  NShowers++ ;
00871     if (foundMatchingSegment(1, protoTrack, CSClist3F, usedCSClist3F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00872     if ( showeringBefore )  NShowers++ ;
00873     if (foundMatchingSegment(1, protoTrack, CSClist4F, usedCSClist4F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00874     if ( showeringBefore )  NShowers++ ;
00875 
00876     unsigned nLayers = layers.size();
00877   
00878     // adding showering information   
00879     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00880        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00881            protoTrack.push_back( ShoweringSegments[i] );
00882            layers.push_back( ShoweringLayers[i] );
00883        }
00884        ShoweringSegments.clear() ;  
00885        ShoweringLayers.clear() ;  
00886     }
00887 
00888     //if ( nLayers < 2 ) continue; 
00889 
00890     TrajectorySeed thisSeed; 
00891     if ( nLayers < 2) {
00892       thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00893     } else {
00894       thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
00895     }
00896   
00897     // Add the seeds to master collection
00898     rawSeeds.push_back(thisSeed);
00899     etaOfSeed.push_back(eta_temp);
00900     phiOfSeed.push_back(phi_temp);
00901     nSegOnSeed.push_back( nLayers );
00902 
00903     // mark this segment as used
00904     usedCSClist1F[index] = true;
00905   } 
00906 
00907 
00908   // Loop over all possible ME2 segment to form seeds:
00909   index = -1;
00910   for (SegmentContainer::iterator it = CSClist2F.begin(); it != CSClist2F.end(); ++it ){
00911   
00912     index++;
00913 
00914     if (usedCSClist2F[index] == true) continue;
00915     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;
00916   
00917     double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00918     if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00919 
00920     // Global position of starting point for protoTrack
00921     GlobalPoint gp = (*it)->globalPosition();
00922     float eta_temp = gp.eta();  
00923     float phi_temp = gp.phi();   
00924     bool showeringBefore = false; 
00925     NShowerSeg = 0; 
00926     if ( IdentifyShowering( CSClist2F, usedCSClist2F, eta_temp, phi_temp, 2, NShowerSeg)  ) showeringBefore = true ;
00927     int NShowers = 0; 
00928     if ( showeringBefore ) {
00929     //    usedCSClist2F[index] = true;
00930         NShowers++ ;
00931     }
00932    
00933     SegmentContainer protoTrack;
00934     protoTrack.push_back(*it);
00935   
00936     std::vector<int> layers;
00937     layers.push_back(2);
00938 
00939     // Try adding segment from other stations
00940     if (foundMatchingSegment(1, protoTrack, CSClist3F, usedCSClist3F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(3);
00941     if ( showeringBefore )  NShowers++ ;
00942     if (foundMatchingSegment(1, protoTrack, CSClist4F, usedCSClist4F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
00943     if ( showeringBefore )  NShowers++ ;
00944   
00945     unsigned nLayers = layers.size();
00946 
00947     // adding showering information   
00948     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
00949        for (size_t i=0; i< ShoweringSegments.size(); i++) {
00950            protoTrack.push_back( ShoweringSegments[i] );
00951            layers.push_back( ShoweringLayers[i] );
00952        }
00953        ShoweringSegments.clear() ;  
00954        ShoweringLayers.clear() ;  
00955     }
00956 
00957     //if ( nLayers < 2 ) continue;
00958 
00959     TrajectorySeed thisSeed; 
00960     if ( nLayers < 2) {
00961       thisSeed = muonSeedCreate_->createSeed(4, protoTrack, layers, NShowers, NShowerSeg );
00962     } else {
00963       thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
00964     }
00965       
00966     // Add the seeds to master collection
00967     rawSeeds.push_back(thisSeed);  
00968     etaOfSeed.push_back(eta_temp); 
00969     phiOfSeed.push_back(phi_temp);
00970     nSegOnSeed.push_back( nLayers );
00971     
00972     // mark this segment as used
00973     usedCSClist2F[index] = true;
00974   }
00975 
00976   // Loop over all possible ME3 segment to form seeds:
00977   index = -1;
00978   for (SegmentContainer::iterator it = CSClist3F.begin(); it != CSClist3F.end(); ++it ){
00979   
00980     index++;
00981 
00982     if (usedCSClist3F[index] == true) continue;
00983     if ( int ((*it)->recHits().size()) < minCSCHitsPerSegment ) continue;
00984   
00985     double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
00986     if ( ((*it)->chi2()/dof) > 20000.0 ) continue;
00987 
00988     // Global position of starting point for protoTrack
00989     GlobalPoint gp = (*it)->globalPosition();
00990     float eta_temp = gp.eta();  
00991     float phi_temp = gp.phi();   
00992     bool showeringBefore = false; 
00993     NShowerSeg = 0; 
00994     if ( IdentifyShowering( CSClist3F, usedCSClist3F, eta_temp, phi_temp, 3, NShowerSeg)  ) showeringBefore = true ;
00995     int NShowers = 0; 
00996     if ( showeringBefore ) {
00997      //   usedCSClist3F[index] = true;
00998         NShowers++ ;
00999     }
01000    
01001     SegmentContainer protoTrack;
01002     protoTrack.push_back(*it);
01003   
01004     std::vector<int> layers;
01005     layers.push_back(2);
01006 
01007     // Try adding segment from other stations
01008     if (foundMatchingSegment(1, protoTrack, CSClist4F, usedCSClist4F, eta_temp, phi_temp, layers[layers.size()-1], showeringBefore )) layers.push_back(4);
01009     if ( showeringBefore )  NShowers++ ;
01010   
01011     unsigned nLayers = layers.size();
01012 
01013     // adding showering information   
01014     if ( nLayers < 2 && ShoweringSegments.size() > 0 ) {
01015        for (size_t i=0; i< ShoweringSegments.size(); i++) {
01016            protoTrack.push_back( ShoweringSegments[i] );
01017            layers.push_back( ShoweringLayers[i] );
01018        }
01019        ShoweringSegments.clear() ;  
01020        ShoweringLayers.clear() ;  
01021     }
01022 
01023     // mark this segment as used
01024     usedCSClist3F[index] = true;
01025 
01026     if ( nLayers < 2 ) continue;
01027 
01028     TrajectorySeed thisSeed = muonSeedCreate_->createSeed(1, protoTrack, layers, NShowers, NShowerSeg );
01029       
01030     // Add the seeds to master collection
01031     rawSeeds.push_back(thisSeed);  
01032     etaOfSeed.push_back(eta_temp); 
01033     phiOfSeed.push_back(phi_temp);
01034     nSegOnSeed.push_back( nLayers );
01035     
01036   }
01037 
01038 
01039   /* *********************************************************************************************************************
01040    * Clean up raw seed collection and pass to master collection
01041    * *********************************************************************************************************************/
01042 
01043   if (debug) std::cout << "*** CLEAN UP " << std::endl;
01044   if (debug) std::cout << "Number of seeds BEFORE " << rawSeeds.size() << std::endl;
01045 
01046   //std::cout << " === start cleaning ==== " << rawSeeds.size() << std::endl;
01047 
01048   int goodSeeds = 0;
01049 
01050   theSeeds = seedCleaner(eventSetup,rawSeeds);
01051   goodSeeds = theSeeds.size();
01052 
01053   if (debug) std::cout << "Number of seeds AFTER " << goodSeeds << std::endl;
01054 
01055 
01056   return goodSeeds;
01057 }
01058 
01059 
01060 
01061 /* *********************************************************************************************************************
01062  * Try to match segment from different station (layer)
01063  *
01064  * Note type = 1 --> endcap
01065  *           = 2 --> overlap
01066  *           = 3 --> barrel
01067  * *********************************************************************************************************************/
01068 
01070 bool MuonSeedBuilder::foundMatchingSegment( int type, SegmentContainer& protoTrack, SegmentContainer& segs, 
01071      BoolContainer& usedSeg, float& eta_last, float& phi_last, int& lastLayer, bool& showeringBefore  ) {
01072 
01073   bool ok = false;
01074   int scanlayer = (lastLayer < 0 ) ?  (lastLayer-1) : (lastLayer+1) ;
01075 
01076   if ( IdentifyShowering( segs, usedSeg, eta_last, phi_last, scanlayer, NShowerSeg )  ) {
01077      showeringBefore = true; 
01078      return ok ;
01079   }
01080 
01081   // Setup the searching cone-size
01082   // searching cone-size should changes with bending power
01083   double maxdEta;
01084   double maxdPhi;
01085   if ( type == 1 ) { 
01086     // CSC
01087     maxdEta = maxDeltaEtaCSC;
01088     if ( lastLayer == 0 || lastLayer == 1 ) {
01089        if ( fabs(eta_last) < 2.1 ) {
01090           maxdPhi = maxDeltaPhiCSC;
01091        } else {
01092           maxdPhi = 0.06;
01093        } 
01094     } else if (lastLayer== 2 ) {
01095        maxdPhi = 0.5*maxDeltaPhiCSC;
01096     } else  {
01097        maxdPhi = 0.2*maxDeltaPhiCSC;
01098     }
01099   } else if ( type == 2 ) { 
01100     // Overlap
01101     maxdEta = maxDeltaEtaOverlap;
01102     if ( lastLayer == -1 ) {
01103        maxdPhi = maxDeltaPhiDT;
01104     } else {
01105        maxdPhi = maxDeltaPhiOverlap;
01106     }
01107   } else {
01108     // DT
01109     maxdEta = maxDeltaEtaDT;
01110     if ( lastLayer == -1 ) {
01111        maxdPhi = maxDeltaPhiDT;
01112     } else if ( lastLayer == -2 ) {
01113        maxdPhi = 0.8*maxDeltaPhiDT;
01114     } else  {
01115        maxdPhi = 0.4*maxDeltaPhiDT;
01116     }
01117   }
01118   
01119   // if previous layer showers, limite the maxdPhi < 0.06
01120   if ( showeringBefore && maxdPhi > 0.03  ) maxdPhi = 0.03;
01121   // reset the showering flag
01122   showeringBefore = false ;
01123 
01124   // global phi/eta from previous segment 
01125   float eta_temp = eta_last;
01126   float phi_temp = phi_last;
01127 
01128   // Index counter to keep track of element used in segs 
01129   int          index = -1;
01130   int          best_match = index;
01131   float        best_R = sqrt( (maxdEta*maxdEta) + (maxdPhi*maxdPhi) );
01132   float        best_chi2 = 200;
01133   int          best_dimension = 2;
01134   int          best_nhits = minDTHitsPerSegment;
01135   if( type == 1 ) best_nhits = minCSCHitsPerSegment;
01136   // Loop over segments in other station (layer) and find candidate match 
01137   for (SegmentContainer::iterator it=segs.begin(); it!=segs.end(); ++it){
01138 
01139     index++;
01140 
01141     // Not to get confused:  eta_last is from the previous layer.
01142     // This is only to find the best set of segments by comparing at the distance layer-by-layer 
01143     GlobalPoint gp2 = (*it)->globalPosition(); 
01144     double dh = fabs( gp2.eta() - eta_temp ); 
01145     double df = fabs( gp2.phi() - phi_temp );
01146     double dR = sqrt( (dh*dh) + (df*df) );
01147 
01148     // dEta and dPhi should be within certain range
01149     bool case1 = (  dh  < maxdEta && df < maxdPhi ) ? true:false ;
01150     // for DT station 4 ; CSCSegment is always 4D 
01151     bool case2 = (  ((*it)->dimension()!= 4) && (dh< 0.5) && (df < maxdPhi) )  ? true:false ;
01152     if ( !case1 && !case2  ) continue;
01153      
01154     int NRechits = NRecHitsFromSegment( &*(*it) ) ;
01155     // crapy fucking way to get the fucking number of fucking rechits
01156     /*
01157     int NRechits = 0 ; 
01158     DetId geoId = (*it)->geographicalId();
01159     if ( geoId.subdetId() == MuonSubdetId::DT ) {
01160        DTChamberId DT_Id( geoId );
01161        std::vector<TrackingRecHit*> DThits = (*it)->recHits();
01162        int dt1DHits = 0;
01163        for (size_t j=0; j< DThits.size(); j++) {
01164            dt1DHits += (DThits[j]->recHits()).size();
01165        }
01166        NRechits = dt1DHits ;
01167     }
01168     if ( geoId.subdetId() == MuonSubdetId::CSC ) {
01169        NRechits = ((*it)->recHits()).size() ;
01170     }
01171     */
01172     if ( NRechits < best_nhits ) continue;
01173     best_nhits = NRechits ; 
01174 
01175     // reject 2D segments if 4D segments are available 
01176     if ( (*it)->dimension() < best_dimension ) continue;
01177     best_dimension = (*it)->dimension();
01178 
01179     // pick the segment with best chi2/dof within a fixed cone size
01180     if ( dR > best_R ) continue;
01181 
01182     // select smaller chi2/dof
01183     double dof = static_cast<double>( (*it)->degreesOfFreedom() ) ;
01185     if ((*it)->chi2()/dof < 0.001 && NRechits < 6 && type == 1) continue; 
01186     if ( (*it)->chi2()/dof > best_chi2 ) continue;
01187     best_chi2 = (*it)->chi2()/dof ;
01188     best_match = index;
01189     // propagate the eta and phi to next layer
01190     if ((*it)->dimension() != 4 ) {
01191        phi_last = phi_last;
01192        eta_last = eta_last;
01193     } else {
01194        phi_last = gp2.phi(); 
01195        eta_last = gp2.eta();
01196     }
01197   }   
01198 
01199   if (best_match < 0) return ok;
01200 
01201   index = -1;
01202    
01203   // Add best matching segment to protoTrack:
01204   for (SegmentContainer::iterator it=segs.begin(); it!=segs.end(); ++it){
01205       index++;
01206       if (index != best_match) continue;
01207       protoTrack.push_back(*it);
01208       usedSeg[best_match] = true;
01209       ok = true;     
01210   }  
01211   return ok; 
01212 }
01213 
01214 std::vector<TrajectorySeed> MuonSeedBuilder::seedCleaner(const edm::EventSetup& eventSetup, std::vector<TrajectorySeed>& seeds ) {
01215 
01216   theService->update(eventSetup);
01217   
01218   std::vector<TrajectorySeed> FinalSeeds;
01219   
01220   //std::cout<<" 1 **** SeedGrouping ***** "<<std::endl;
01221   // group the seeds
01222   std::vector<SeedContainer> theCollection = GroupSeeds(seeds);
01223 
01224   //std::cout<<" 2 **** SeedCleaning ***** "<<std::endl;
01225 
01226   // ckeck each group and pick the good one
01227   for (size_t i=0; i< theCollection.size(); i++ ) {
01228 
01229       // pick the seed w/ more than 1 segments and w/ 1st layer segment information
01230       SeedContainer goodSeeds   = SeedCandidates( theCollection[i], true );
01231       SeedContainer otherSeeds  = SeedCandidates( theCollection[i], false );
01232       if ( MomentumFilter( goodSeeds ) )  {
01233          //std::cout<<" == type1 "<<std::endl;
01234          std::vector<TrajectorySeed> betterSeeds = LengthFilter( goodSeeds ); 
01235          TrajectorySeed bestSeed   = BetterChi2( betterSeeds );
01236          //TrajectorySeed bestSeed   = BetterDirection( betterSeeds );
01237          FinalSeeds.push_back( bestSeed );
01238       } 
01239       else if( MomentumFilter( otherSeeds ) ) {
01240          //std::cout<<" == type2 "<<std::endl;
01241          SeedContainer betterSeeds = LengthFilter( otherSeeds ); 
01242          TrajectorySeed bestSeed   = BetterChi2( betterSeeds );
01243          //TrajectorySeed bestSeed   = BetterDirection( betterSeeds );
01244          FinalSeeds.push_back( bestSeed );
01245       } 
01246       else {
01247          //std::cout<<" == type3 "<<std::endl;
01248          SeedContainer betterSeeds = LengthFilter( theCollection[i] ); 
01249          TrajectorySeed bestSeed   = BetterChi2( betterSeeds );
01250          //TrajectorySeed bestSeed   = BetterDirection( betterSeeds );
01251          FinalSeeds.push_back( bestSeed );
01252       }
01253       //std::cout<<"  **** Fine one seed ***** "<<std::endl;
01254   }  
01255   return FinalSeeds ; 
01256 
01257 }
01258 
01259 
01260 TrajectorySeed MuonSeedBuilder::BetterDirection(std::vector<TrajectorySeed>& seeds ) {
01261 
01262   float bestProjErr  = 9999.; 
01263   int winner = 0 ;
01264   AlgebraicSymMatrix mat(5,0) ; 
01265   for ( size_t i = 0; i < seeds.size(); i++ ) {
01266 
01267       edm::OwnVector<TrackingRecHit>::const_iterator r1 = seeds[i].recHits().first ;
01268       mat = r1->parametersError().similarityT( r1->projectionMatrix() );
01269       float ddx = mat[1][1]; 
01270       float ddy = mat[2][2]; 
01271       float dxx = mat[3][3]; 
01272       float dyy = mat[4][4];
01273       float projectErr = sqrt( (ddx*10000.) + (ddy*10000.) + dxx + dyy ) ;
01274 
01275       if ( projectErr > bestProjErr ) continue;
01276       winner = static_cast<int>(i) ;
01277       bestProjErr = projectErr ;
01278   }
01279   return seeds[winner];
01280 
01281 }
01282 
01283 TrajectorySeed MuonSeedBuilder::BetterChi2(std::vector<TrajectorySeed>& seeds ) {
01284 
01285   if ( seeds.size() == 1 ) return seeds[0];
01286 
01287   int winner = 0 ;
01288   std::vector<double> bestChi2(4,99999.);  
01289   std::vector<int>    moreHits(4,0);  
01290   for ( size_t i = 0; i < seeds.size(); i++ ) {
01291 
01292       // 1. fill out the Nchi2 of segments of the seed 
01293       //GlobalVector mom = SeedMomentum( seeds[i] ); // temporary use for debugging
01294       //double pt = sqrt( (mom.x()*mom.x()) + (mom.y()*mom.y()) );
01295       //std::cout<<" > SEED"<<i<<"  pt:"<<pt<< std::endl;
01296 
01297       int it = -1;
01298       std::vector<double> theChi2(4,99999.);  
01299       std::vector<int>    theHits(4,0);  
01300       for (edm::OwnVector<TrackingRecHit>::const_iterator r1 = seeds[i].recHits().first; r1 != seeds[i].recHits().second; r1++){
01301           it++;
01302           //std::cout<<"    segmet : "<<it <<std::endl; 
01303           if ( it > 3) break;
01304           theHits[it] = NRecHitsFromSegment( *r1 );
01305           theChi2[it] = NChi2OfSegment( *r1 );
01306       }
01307       //std::cout<<" -----  "<<std::endl;
01308 
01309       // 2. longer segment
01310       for (int j =0; j<4; j++) {
01311 
01312           if ( theHits[j] <  moreHits[j] ) break;
01313 
01314           if ( theHits[j] == moreHits[j] ) { 
01316             bool decisionMade = false ;
01317             for (int k =0; k<4; k++) {
01318                if ( theChi2[k] >  bestChi2[k] ) break;
01319                if ( theChi2[k] == bestChi2[k] ) continue;
01320                if ( theChi2[k] <  bestChi2[k] ) {
01321                   bestChi2 = theChi2 ;
01322                   winner = static_cast<int>(i) ;
01323                   decisionMade = true;
01324                 }
01325                 break;
01326             }
01327             if ( decisionMade) break;
01328             if (!decisionMade) continue;
01329           }
01330 
01331           if ( theHits[j] >  moreHits[j] ) {
01332              moreHits = theHits ;
01333              winner = static_cast<int>(i) ;
01334           }
01335           break;
01336       }
01337   }
01338   //std::cout<<" Winner is "<< winner <<std::endl;
01339   return seeds[winner];
01340 }
01341 
01342 SeedContainer MuonSeedBuilder::LengthFilter(std::vector<TrajectorySeed>& seeds ) {
01343  
01344   SeedContainer longSeeds; 
01345   int NSegs = 0;
01346   for (size_t i = 0; i< seeds.size(); i++) {
01347     
01348       int theLength = static_cast<int>( seeds[i].nHits());
01349       if ( theLength > NSegs ) {
01350          NSegs = theLength ;
01351          longSeeds.clear();
01352          longSeeds.push_back( seeds[i] );
01353       } 
01354       else if ( theLength == NSegs ) {
01355          longSeeds.push_back( seeds[i] );
01356       } else {
01357          continue;
01358       } 
01359   }
01360   //std::cout<<" final Length :"<<NSegs<<std::endl;
01361 
01362   return longSeeds ; 
01363   
01364 }
01365 
01366 bool MuonSeedBuilder::MomentumFilter(std::vector<TrajectorySeed>& seeds ) {
01367 
01368   bool findgoodMomentum = false;
01369   SeedContainer goodMomentumSeeds = seeds;
01370   seeds.clear();
01371   for ( size_t i = 0; i < goodMomentumSeeds.size(); i++ ) {
01372        GlobalVector mom = SeedMomentum( goodMomentumSeeds[i] );
01373        double pt = sqrt( (mom.x()*mom.x()) + (mom.y()*mom.y()) );
01374        //if ( pt < 6.  || pt > 3000. ) continue;
01375        if ( pt < 6. ) continue;
01376        //std::cout<<" passed momentum :"<< pt <<std::endl;
01377        seeds.push_back( goodMomentumSeeds[i] );
01378        findgoodMomentum = true;  
01379   }
01380   if ( seeds.size() == 0 ) seeds = goodMomentumSeeds;
01381 
01382   return findgoodMomentum;
01383 }
01384 
01385 SeedContainer MuonSeedBuilder::SeedCandidates( std::vector<TrajectorySeed>& seeds, bool good ) {
01386 
01387   SeedContainer theCandidate;
01388   theCandidate.clear();
01389 
01390   bool longSeed = false;  
01391   bool withFirstLayer = false ;
01392 
01393   //std::cout<<"***** Seed Classification *****"<< seeds.size() <<std::endl;
01394   for ( size_t i = 0; i < seeds.size(); i++ ) {
01395 
01396       if (seeds[i].nHits() > 1 ) longSeed = true ;
01397       //std::cout<<"  Seed: "<<i<< std::endl;
01398       int idx = 0;
01399       for (edm::OwnVector<TrackingRecHit>::const_iterator r1 = seeds[i].recHits().first; r1 != seeds[i].recHits().second; r1++){
01400 
01401          idx++;
01402          const GeomDet* gdet = theService->trackingGeometry()->idToDet( (*r1).geographicalId() );
01403          DetId geoId = gdet->geographicalId();
01404 
01405          if ( geoId.subdetId() == MuonSubdetId::DT ) {
01406             DTChamberId DT_Id( (*r1).geographicalId() );
01407             //std::cout<<" ID:"<<DT_Id <<" pos:"<< r1->localPosition()  <<std::endl;
01408             if (DT_Id.station() != 1)  continue;
01409             withFirstLayer = true;
01410          }
01411          if ( geoId.subdetId() == MuonSubdetId::CSC ) {
01412             idx++;
01413             CSCDetId CSC_Id = CSCDetId( (*r1).geographicalId() );
01414             //std::cout<<" ID:"<<CSC_Id <<" pos:"<< r1->localPosition()  <<std::endl;
01415             if (CSC_Id.station() != 1)  continue;
01416             withFirstLayer = true;
01417          }
01418       }
01419       bool goodseed = (longSeed && withFirstLayer) ? true : false ;
01420   
01421       if ( goodseed == good )  theCandidate.push_back( seeds[i] );
01422   }
01423   return theCandidate;
01424 
01425 }
01426 
01427 std::vector<SeedContainer> MuonSeedBuilder::GroupSeeds( std::vector<TrajectorySeed>& seeds) {
01428 
01429   std::vector<SeedContainer> seedCollection;
01430   seedCollection.clear();
01431   std::vector<TrajectorySeed> theGroup ;
01432   std::vector<bool> usedSeed(seeds.size(),false);
01433 
01434   // categorize seeds by comparing overlapping segments or a certian eta-phi cone 
01435   for (unsigned int i= 0; i<seeds.size(); i++){
01436     
01437     if (usedSeed[i]) continue;
01438     theGroup.push_back( seeds[i] );
01439     usedSeed[i] = true ;
01440 
01441     GlobalPoint pos1 = SeedPosition( seeds[i]);
01442 
01443     for (unsigned int j= i+1; j<seeds.size(); j++){
01444  
01445        // seeds with overlaaping segments will be grouped together
01446        unsigned int overlapping = OverlapSegments(seeds[i], seeds[j]) ;
01447        if ( !usedSeed[j] && overlapping > 0 ) {
01448           // reject the identical seeds
01449           if ( seeds[i].nHits() == overlapping && seeds[j].nHits() == overlapping ) {
01450              usedSeed[j] = true ;
01451              continue;
01452           }
01453           theGroup.push_back( seeds[j] ); 
01454           usedSeed[j] = true ;
01455        }
01456        if (usedSeed[j]) continue;
01457 
01458        // seeds in a certain cone are grouped together  
01459        GlobalPoint pos2 = SeedPosition( seeds[j]);
01460        double dh = pos1.eta() - pos2.eta() ;
01461        double df = pos1.phi() - pos2.phi() ;
01462        double dR = sqrt( (dh*dh) + (df*df) );
01463 
01464        if ( dR > 0.3 && seeds[j].nHits() == 1) continue;
01465        if ( dR > 0.2 && seeds[j].nHits() >  1) continue;
01466        theGroup.push_back( seeds[j] ); 
01467        usedSeed[j] = true ;
01468     }
01469     sort(theGroup.begin(), theGroup.end(), lengthSorting ) ;
01470     seedCollection.push_back(theGroup);
01471     theGroup.clear(); 
01472   }
01473   return seedCollection;
01474 
01475 }
01476 
01477 unsigned int MuonSeedBuilder::OverlapSegments( TrajectorySeed seed1, TrajectorySeed seed2 ) {
01478 
01479   unsigned int overlapping = 0;
01480   for (edm::OwnVector<TrackingRecHit>::const_iterator r1 = seed1.recHits().first; r1 != seed1.recHits().second; r1++){
01481       
01482       DetId id1 = (*r1).geographicalId();
01483       const GeomDet* gdet1 = theService->trackingGeometry()->idToDet( id1 );
01484       GlobalPoint gp1 = gdet1->toGlobal( (*r1).localPosition() );
01485 
01486       for (edm::OwnVector<TrackingRecHit>::const_iterator r2 = seed2.recHits().first; r2 != seed2.recHits().second; r2++){
01487 
01488           DetId id2 = (*r2).geographicalId();
01489           if (id1 != id2 ) continue;
01490 
01491           const GeomDet* gdet2 = theService->trackingGeometry()->idToDet( id2 );
01492           GlobalPoint gp2 = gdet2->toGlobal( (*r2).localPosition() );
01493 
01494           double dx = gp1.x() - gp2.x() ;
01495           double dy = gp1.y() - gp2.y() ;
01496           double dz = gp1.z() - gp2.z() ;
01497           double dL = sqrt( dx*dx + dy*dy + dz*dz);
01498 
01499           if ( dL < 1. ) overlapping ++;
01500       
01501       }
01502   }
01503   return overlapping ;
01504 
01505 }
01506 
01507 GlobalPoint MuonSeedBuilder::SeedPosition( TrajectorySeed seed ) {
01508 
01509   TrajectoryStateTransform tsTransform;
01510 
01511   PTrajectoryStateOnDet pTSOD = seed.startingState();
01512   DetId SeedDetId(pTSOD.detId());
01513   const GeomDet* geoDet = theService->trackingGeometry()->idToDet( SeedDetId );
01514   TrajectoryStateOnSurface SeedTSOS = tsTransform.transientState(pTSOD, &(geoDet->surface()), &*theService->magneticField());
01515   GlobalPoint  pos  = SeedTSOS.globalPosition();
01516 
01517   return pos ;
01518 
01519 }
01520 
01521 GlobalVector MuonSeedBuilder::SeedMomentum( TrajectorySeed seed ) {
01522 
01523   TrajectoryStateTransform tsTransform;
01524 
01525   PTrajectoryStateOnDet pTSOD = seed.startingState();
01526   DetId SeedDetId(pTSOD.detId());
01527   const GeomDet* geoDet = theService->trackingGeometry()->idToDet( SeedDetId );
01528   TrajectoryStateOnSurface SeedTSOS = tsTransform.transientState(pTSOD, &(geoDet->surface()), &*theService->magneticField());
01529   GlobalVector  mom  = SeedTSOS.globalMomentum();
01530 
01531   return mom ;
01532 
01533 }
01534 
01535 int MuonSeedBuilder::NRecHitsFromSegment( const TrackingRecHit& rhit ) {
01536 
01537       int NRechits = 0 ; 
01538       const GeomDet* gdet = theService->trackingGeometry()->idToDet( rhit.geographicalId() );
01539       MuonTransientTrackingRecHit::MuonRecHitPointer theSeg = 
01540       MuonTransientTrackingRecHit::specificBuild(gdet, rhit.clone() );
01541 
01542       DetId geoId = gdet->geographicalId();
01543       if ( geoId.subdetId() == MuonSubdetId::DT ) {
01544          DTChamberId DT_Id( rhit.geographicalId() );
01545          std::vector<TrackingRecHit*> DThits = theSeg->recHits();
01546          int dt1DHits = 0;
01547          for (size_t j=0; j< DThits.size(); j++) {
01548              dt1DHits += (DThits[j]->recHits()).size();
01549          }
01550          NRechits = dt1DHits ;
01551          //std::cout<<" D_rh("<< dt1DHits  <<") " ;
01552       }
01553 
01554       if ( geoId.subdetId() == MuonSubdetId::CSC ) {
01555          NRechits = (theSeg->recHits()).size() ;
01556          //std::cout<<" C_rh("<< NRechits <<") " ;
01557       }
01558       return NRechits ;
01559 }
01560 
01561 int MuonSeedBuilder::NRecHitsFromSegment( MuonTransientTrackingRecHit *rhit ) {
01562 
01563     int NRechits = 0 ; 
01564     DetId geoId = rhit->geographicalId();
01565     if ( geoId.subdetId() == MuonSubdetId::DT ) {
01566        DTChamberId DT_Id( geoId );
01567        std::vector<TrackingRecHit*> DThits = rhit->recHits();
01568        int dt1DHits = 0;
01569        for (size_t j=0; j< DThits.size(); j++) {
01570            dt1DHits += (DThits[j]->recHits()).size();
01571        }
01572        NRechits = dt1DHits ;
01573        //std::cout<<" D_rh("<< dt1DHits  <<") " ;
01574     }
01575     if ( geoId.subdetId() == MuonSubdetId::CSC ) {
01576        NRechits = (rhit->recHits()).size() ;
01577        //std::cout<<" C_rh("<<(rhit->recHits()).size() <<") " ;
01578     }
01579     return NRechits;
01580 
01581 }
01582 
01583 double MuonSeedBuilder::NChi2OfSegment( const TrackingRecHit& rhit ) {
01584 
01585       double NChi2 = 999999. ; 
01586       const GeomDet* gdet = theService->trackingGeometry()->idToDet( rhit.geographicalId() );
01587       MuonTransientTrackingRecHit::MuonRecHitPointer theSeg = 
01588       MuonTransientTrackingRecHit::specificBuild(gdet, rhit.clone() );
01589 
01590       double dof = static_cast<double>( theSeg->degreesOfFreedom() );
01591       NChi2 = theSeg->chi2() / dof ;
01592       //std::cout<<" Chi2 = "<< NChi2  <<" |" ;
01593 
01594       return NChi2 ;
01595 }
01596 
01597 bool MuonSeedBuilder::IdentifyShowering( SegmentContainer& segs, BoolContainer& usedSeg, float& eta_last, float& phi_last, int layer, int& NShoweringSegments ) {
01598 
01599   bool showering  = false ;  
01600 
01601   int  nSeg   = 0 ;
01602   int  nRhits = 0 ;
01603   double nChi2  = 9999. ;
01604   int theOrigin = -1;
01605   std::vector<int> badtag;
01606   int    index = -1;
01607   double aveEta = 0.0;
01608   for (SegmentContainer::iterator it = segs.begin(); it != segs.end(); ++it){
01609 
01610       index++;
01611       GlobalPoint gp = (*it)->globalPosition(); 
01612       double dh = gp.eta() - eta_last ;
01613       double df = gp.phi() - phi_last ;
01614       double dR = sqrt( (dh*dh) + (df*df) ) ;
01615 
01616       double dof = static_cast<double>( (*it)->degreesOfFreedom() );
01617       double nX2 = (*it)->chi2() / dof ;
01618 
01619       bool isDT = false ; 
01620       DetId geoId = (*it)->geographicalId();
01621       if ( geoId.subdetId() == MuonSubdetId::DT ) isDT = true;
01622 
01623       if (dR < 0.3 ) {
01624          nSeg++ ;
01625          badtag.push_back( index ) ;
01626          aveEta += fabs( gp.eta() ) ; 
01627          // pick up the best segment from showering chamber 
01628          int rh = NRecHitsFromSegment( &*(*it) );
01629          if (rh < 6 && !isDT) continue;
01630          if (rh < 12 && isDT) continue;
01631          if ( rh > nRhits ) { 
01632             nRhits = rh ;
01633             if ( nX2 > nChi2 ) continue ;
01634             if (layer != 0 && layer != 1 && layer != -1 ) {
01635                theOrigin = index ;
01636             }
01637          }
01638       }
01639 
01640   }
01641   aveEta =  aveEta/static_cast<double>(nSeg) ;
01642   bool isME11A = (aveEta >= 2.1 &&  layer == 0) ? true : false ;
01643   bool isME12  = (aveEta >  1.2 && aveEta <= 1.65 && layer == 1) ? true : false ;
01644   bool isME11  = (aveEta >  1.65 && aveEta <= 2.1 && layer == 0) ? true : false ;
01645   bool is1stLayer = (layer == -1 || layer == 0 || isME12 || isME11 || isME11A) ? true : false ;
01646 
01647   NShoweringSegments += nSeg;
01648 
01649   if ( nSeg  > 3 && !isME11A ) showering = true ;
01650   if ( nSeg  > 6 &&  isME11A ) showering = true ;
01651 
01652   // if showering, flag all segments in order to skip this layer for pt estimation except 1st layer
01653   //std::cout<<" from Showering "<<std::endl;
01654   if (showering && !is1stLayer ) {
01655      for (std::vector<int>::iterator it = badtag.begin(); it != badtag.end(); ++it ) { 
01656          usedSeg[*it] = true;              
01657          if ( (*it) != theOrigin ) continue; 
01658          ShoweringSegments.push_back( segs[*it] );
01659          ShoweringLayers.push_back( layer );
01660      }
01661   }
01662   return showering ;
01663 
01664 }
01665 
01666 double MuonSeedBuilder::etaError(const GlobalPoint gp, double rErr) {
01667 
01668   double dHdTheta = 0.0;
01669   double dThetadR = 0.0;
01670   double etaErr = 1.0;
01671 
01672   if (gp.perp() != 0) {
01673 
01674      dHdTheta = ( gp.mag()+gp.z() )/gp.perp();
01675      dThetadR = gp.z() / gp.perp2() ;
01676      etaErr =  0.25 * (dHdTheta * dThetadR) * (dHdTheta * dThetadR) * rErr ;
01677   }
01678  
01679   return etaErr;
01680 }
01681 

Generated on Tue Jun 9 17:44:26 2009 for CMSSW by  doxygen 1.5.4