CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/RecoMuon/MuonSeedGenerator/src/RPCSeedrecHitFinder.cc

Go to the documentation of this file.
00001 
00007 #include "RecoMuon/MuonSeedGenerator/src/RPCSeedrecHitFinder.h"
00008 #include <DataFormats/TrackingRecHit/interface/TrackingRecHit.h>
00009 #include <DataFormats/RPCRecHit/interface/RPCRecHit.h>
00010 
00011 using namespace std;
00012 using namespace edm;
00013 
00014 
00015 // Comparator function must be global function?
00016 // Could not be included in .h file, or there will be 2 lessPhi() functions in both RPCSeedGenerator and RPCSeedFinder module
00017 bool lessPhi(const MuonTransientTrackingRecHit::ConstMuonRecHitPointer& it1, const MuonTransientTrackingRecHit::ConstMuonRecHitPointer& it2)
00018 {
00019     // Don't need to use value() in Geom::Phi to short
00020     return (it1->globalPosition().phi() < it2->globalPosition().phi());
00021 }
00022 
00023 RPCSeedrecHitFinder::RPCSeedrecHitFinder() {
00024 
00025     // Initiate the member
00026     isLayerset = false;
00027     isConfigured = false;
00028     isInputset = false;
00029     isOutputset = false;
00030     BxRange = 0;
00031     MaxDeltaPhi = 0;
00032     ClusterSet.clear();
00033     LayersinRPC.clear();
00034     therecHits.clear();
00035 }
00036 
00037 RPCSeedrecHitFinder::~RPCSeedrecHitFinder() {
00038 
00039 }
00040 
00041 void RPCSeedrecHitFinder::configure(const edm::ParameterSet& iConfig) {
00042 
00043     // Set the configuration
00044     BxRange = iConfig.getParameter<unsigned int>("BxRange");
00045     MaxDeltaPhi = iConfig.getParameter<double>("MaxDeltaPhi");
00046     ClusterSet = iConfig.getParameter< std::vector<int> >("ClusterSet");
00047 
00048     // Set the signal open
00049     isConfigured = true;
00050 }
00051 
00052 void RPCSeedrecHitFinder::setInput(MuonRecHitContainer (&recHits)[RPCLayerNumber]) {
00053 
00054     for(unsigned int i = 0; i < RPCLayerNumber; i++)
00055         recHitsRPC[i] = &recHits[i];
00056     isInputset = true;
00057 }
00058 
00059 void RPCSeedrecHitFinder::unsetInput() {
00060 
00061     isInputset = false;
00062 }
00063 void RPCSeedrecHitFinder::setOutput(RPCSeedFinder *Seed) {
00064 
00065     theSeed = Seed;
00066     isOutputset = true;
00067 }
00068 
00069 void RPCSeedrecHitFinder::setLayers(const std::vector<unsigned int>& Layers) {
00070 
00071     LayersinRPC = Layers;
00072     isLayerset = true;
00073 }
00074 
00075 void RPCSeedrecHitFinder::fillrecHits() {
00076 
00077     if(isLayerset == false || isConfigured == false || isOutputset == false || isInputset == false)
00078     {
00079         cout << "Not set the IO or not configured yet" << endl;
00080         return;
00081     }
00082     cout << "Now fill recHits from Layers: ";
00083     for(unsigned int k = 0; k < LayersinRPC.size(); k++)
00084         cout << LayersinRPC[k] <<" ";
00085     cout << endl;
00086     unsigned int LayerIndex = 0;
00087     therecHits.clear();
00088     complete(LayerIndex);
00089 
00090     // Unset the signal
00091     LayersinRPC.clear();
00092     therecHits.clear();
00093     isLayerset = false;
00094 }
00095 
00096 void RPCSeedrecHitFinder::complete(unsigned int LayerIndex) {
00097 
00098     for(MuonRecHitContainer::const_iterator it = recHitsRPC[LayersinRPC[LayerIndex]]->begin(); it != recHitsRPC[LayersinRPC[LayerIndex]]->end(); it++)  
00099     {
00100         cout << "Completing layer[" << LayersinRPC[LayerIndex] << "]." << endl;
00101 
00102         // Check validation
00103         if(!(*it)->isValid())
00104             continue;
00105 
00106         // Check BX range, be sure there is only RPCRecHit in the MuonRecHitContainer when use the dynamic_cast
00107         TrackingRecHit* thisTrackingRecHit = (*it)->hit()->clone();
00108         // Should also delete the RPCRecHit object cast by dynamic_cast<> ?
00109         RPCRecHit* thisRPCRecHit = dynamic_cast<RPCRecHit*>(thisTrackingRecHit);
00110         int BX = thisRPCRecHit->BunchX();
00111         int ClusterSize = thisRPCRecHit->clusterSize();
00112         delete thisTrackingRecHit;
00113         // Check BX
00114         if((unsigned int)abs(BX) > BxRange)
00115             continue;
00116         // Check cluster size
00117         bool Clustercheck = false;
00118         if(ClusterSet.size() == 0)
00119             Clustercheck = true;
00120         for(std::vector<int>::const_iterator CluIter = ClusterSet.begin(); CluIter != ClusterSet.end(); CluIter++)
00121             if(ClusterSize == (*CluIter))
00122                 Clustercheck = true;
00123         if(Clustercheck != true)
00124             continue;
00125         // Check the recHits Phi range
00126         GlobalPoint pos = (*it)->globalPosition();
00127         double Phi = pos.phi();
00128         cout << "Phi: " << Phi << endl;
00129         // The recHits should locate in some phi range
00130         therecHits.push_back(*it);
00131         double deltaPhi = getdeltaPhifromrecHits();
00132         cout << "Delta phi: "<< deltaPhi << endl;
00133         therecHits.pop_back();
00134         if(deltaPhi > MaxDeltaPhi)
00135             continue;
00136 
00137         // If pass all, add to the seed
00138         therecHits.push_back(*it);
00139         cout << "RecHit's global position: " << pos.x() << ", " << pos.y() << ", " << pos.z() << endl;
00140 
00141         // Check if this recHit is the last one in the seed
00142         // If it is the last one, calculate the seed
00143         if(LayerIndex == (LayersinRPC.size()-1))
00144         {
00145             cout << "Check and fill one seed." << endl;
00146             checkandfill();
00147         }
00148         // If it is not the last one, continue to fill the seed from other layers
00149         else
00150             complete(LayerIndex+1);
00151 
00152         // Remember to pop the recHit before add another one from the same layer!
00153         therecHits.pop_back();
00154     }
00155 }
00156 
00157 double RPCSeedrecHitFinder::getdeltaPhifromrecHits() {
00158 
00159     ConstMuonRecHitContainer sortRecHits = therecHits;
00160     sort(sortRecHits.begin(), sortRecHits.end(), lessPhi);
00161     cout << "Sorted recHit's Phi: ";
00162     for(ConstMuonRecHitContainer::const_iterator iter = sortRecHits.begin(); iter != sortRecHits.end(); iter++)
00163         cout << (*iter)->globalPosition().phi() << ", ";
00164     cout << endl;
00165     // Calculate the deltaPhi, take care Geom::Phi always in range [-pi,pi)
00166     // In case of some deltaPhi larger then Pi, use value() in Geom::Phi to get the true value in radians of Phi, then do the calculation
00167     double deltaPhi = 0;
00168     if(sortRecHits.size() <= 1)
00169         return deltaPhi;
00170     if(sortRecHits.size() == 2)
00171     {
00172         ConstMuonRecHitContainer::const_iterator iter1 = sortRecHits.begin();
00173         ConstMuonRecHitContainer::const_iterator iter2 = sortRecHits.begin();
00174         iter2++;
00175         deltaPhi = (((*iter2)->globalPosition().phi().value() - (*iter1)->globalPosition().phi().value()) > M_PI) ? (2 * M_PI - ((*iter2)->globalPosition().phi().value() - (*iter1)->globalPosition().phi().value())) : ((*iter2)->globalPosition().phi().value() - (*iter1)->globalPosition().phi().value());
00176         return deltaPhi;
00177     }
00178     else
00179     {
00180         deltaPhi = 2 * M_PI;
00181         int n = 0;
00182         for(ConstMuonRecHitContainer::const_iterator iter = sortRecHits.begin(); iter != sortRecHits.end(); iter++)
00183         {   
00184             cout << "Before this loop deltaPhi is " << deltaPhi << endl;
00185             n++;
00186             double deltaPhi_more = 0;
00187             double deltaPhi_less = 0;
00188             if(iter == sortRecHits.begin())
00189             {
00190                 cout << "Calculateing frist loop..." << endl;
00191                 ConstMuonRecHitContainer::const_iterator iter_more = ++iter;
00192                 --iter;
00193                 ConstMuonRecHitContainer::const_iterator iter_less = sortRecHits.end();
00194                 --iter_less;
00195                 cout << "more_Phi: " << (*iter_more)->globalPosition().phi() << ", less_Phi: " << (*iter_less)->globalPosition().phi() << ", iter_Phi: " << (*iter)->globalPosition().phi() << endl;
00196                 deltaPhi_more = (2 * M_PI) - ((*iter_more)->globalPosition().phi().value() - (*iter)->globalPosition().phi().value());
00197                 deltaPhi_less = (*iter_less)->globalPosition().phi().value() - (*iter)->globalPosition().phi().value();
00198             }
00199             else if(iter == (--sortRecHits.end()))
00200             {
00201                 cout << "Calculateing last loop..." << endl;
00202                 ConstMuonRecHitContainer::const_iterator iter_less = --iter;
00203                 ++iter;
00204                 ConstMuonRecHitContainer::const_iterator iter_more = sortRecHits.begin();
00205                 cout << "more_Phi: " << (*iter_more)->globalPosition().phi() << ", less_Phi: " << (*iter_less)->globalPosition().phi() << ", iter_Phi: " << (*iter)->globalPosition().phi() << endl;
00206                 deltaPhi_less = (2 * M_PI) - ((*iter)->globalPosition().phi().value() - (*iter_less)->globalPosition().phi().value());
00207                 deltaPhi_more = (*iter)->globalPosition().phi().value() - (*iter_more)->globalPosition().phi().value();
00208             }
00209             else
00210             {
00211                 cout << "Calculateing " << n << "st loop..." << endl;
00212                 ConstMuonRecHitContainer::const_iterator iter_less = --iter;
00213                 ++iter;
00214                 ConstMuonRecHitContainer::const_iterator iter_more = ++iter;
00215                 --iter;
00216                 cout << "more_Phi: " << (*iter_more)->globalPosition().phi() << ", less_Phi: " << (*iter_less)->globalPosition().phi() << ", iter_Phi: " << (*iter)->globalPosition().phi() << endl;
00217                 deltaPhi_less = (2 * M_PI) - ((*iter)->globalPosition().phi().value() - (*iter_less)->globalPosition().phi().value());
00218                 deltaPhi_more = (2 * M_PI) - ((*iter_more)->globalPosition().phi().value() - (*iter)->globalPosition().phi().value());
00219             }
00220             if(deltaPhi > deltaPhi_more)
00221                 deltaPhi = deltaPhi_more;
00222             if(deltaPhi > deltaPhi_less)
00223                 deltaPhi = deltaPhi_less;
00224 
00225             cout << "For this loop deltaPhi_more is " << deltaPhi_more << endl;
00226             cout << "For this loop deltaPhi_less is " << deltaPhi_less << endl;
00227             cout << "For this loop deltaPhi is " << deltaPhi << endl;
00228         }
00229         return deltaPhi;
00230     }
00231 }
00232 
00233 void RPCSeedrecHitFinder::checkandfill() {
00234 
00235     if(therecHits.size() >= 3)
00236     {
00237         theSeed->setrecHits(therecHits); 
00238         theSeed->seed();
00239     }
00240     else
00241         cout << "Layer less than 3, could not fill a RPCSeedFinder" << endl;
00242 }