CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Validation/GlobalRecHits/src/GlobalRecHitsProducer.cc

Go to the documentation of this file.
00001 
00010 #include "Validation/GlobalRecHits/interface/GlobalRecHitsProducer.h"
00011 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00012 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
00013 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00014 
00015 GlobalRecHitsProducer::GlobalRecHitsProducer(const edm::ParameterSet& iPSet) :
00016   fName(""), verbosity(0), frequency(0), label(""), getAllProvenances(false),
00017   printProvenanceInfo(false), count(0)
00018 {
00019   std::string MsgLoggerCat = "GlobalRecHitsProducer_GlobalRecHitsProducer";
00020 
00021   // get information from parameter set
00022   fName = iPSet.getUntrackedParameter<std::string>("Name");
00023   verbosity = iPSet.getUntrackedParameter<int>("Verbosity");
00024   frequency = iPSet.getUntrackedParameter<int>("Frequency");
00025   label = iPSet.getParameter<std::string>("Label");
00026   edm::ParameterSet m_Prov =
00027     iPSet.getParameter<edm::ParameterSet>("ProvenanceLookup");
00028   getAllProvenances = 
00029     m_Prov.getUntrackedParameter<bool>("GetAllProvenances");
00030   printProvenanceInfo = 
00031     m_Prov.getUntrackedParameter<bool>("PrintProvenanceInfo");
00032 
00033   //get Labels to use to extract information
00034   ECalEBSrc_ = iPSet.getParameter<edm::InputTag>("ECalEBSrc");
00035   ECalUncalEBSrc_ = iPSet.getParameter<edm::InputTag>("ECalUncalEBSrc");
00036   ECalEESrc_ = iPSet.getParameter<edm::InputTag>("ECalEESrc");
00037   ECalUncalEESrc_ = iPSet.getParameter<edm::InputTag>("ECalUncalEESrc");
00038   ECalESSrc_ = iPSet.getParameter<edm::InputTag>("ECalESSrc");
00039   HCalSrc_ = iPSet.getParameter<edm::InputTag>("HCalSrc");
00040   SiStripSrc_ = iPSet.getParameter<edm::InputTag>("SiStripSrc"); 
00041   SiPxlSrc_ = iPSet.getParameter<edm::InputTag>("SiPxlSrc");
00042   MuDTSrc_ = iPSet.getParameter<edm::InputTag>("MuDTSrc");
00043   MuDTSimSrc_ = iPSet.getParameter<edm::InputTag>("MuDTSimSrc");
00044   MuCSCSrc_ = iPSet.getParameter<edm::InputTag>("MuCSCSrc");
00045   MuRPCSrc_ = iPSet.getParameter<edm::InputTag>("MuRPCSrc");
00046   MuRPCSimSrc_ = iPSet.getParameter<edm::InputTag>("MuRPCSimSrc");
00047 
00048   conf_ = iPSet;
00049 
00050   // use value of first digit to determine default output level (inclusive)
00051   // 0 is none, 1 is basic, 2 is fill output, 3 is gather output
00052   verbosity %= 10;
00053 
00054   // create persistent object
00055   produces<PGlobalRecHit>(label);
00056 
00057   // print out Parameter Set information being used
00058   if (verbosity >= 0) {
00059     edm::LogInfo(MsgLoggerCat) 
00060       << "\n===============================\n"
00061       << "Initialized as EDProducer with parameter values:\n"
00062       << "    Name           = " << fName << "\n"
00063       << "    Verbosity      = " << verbosity << "\n"
00064       << "    Frequency      = " << frequency << "\n"
00065       << "    Label          = " << label << "\n"
00066       << "    GetProv        = " << getAllProvenances << "\n"
00067       << "    PrintProv      = " << printProvenanceInfo << "\n"
00068       << "    ECalEBSrc      = " << ECalEBSrc_.label() 
00069       << ":" << ECalEBSrc_.instance() << "\n"
00070       << "    ECalUncalEBSrc = " << ECalUncalEBSrc_.label() 
00071       << ":" << ECalUncalEBSrc_.instance() << "\n"
00072       << "    ECalEESrc      = " << ECalEESrc_.label() 
00073       << ":" << ECalUncalEESrc_.instance() << "\n"
00074       << "    ECalUncalEESrc = " << ECalUncalEESrc_.label() 
00075       << ":" << ECalEESrc_.instance() << "\n"
00076       << "    ECalESSrc      = " << ECalESSrc_.label() 
00077       << ":" << ECalESSrc_.instance() << "\n"
00078       << "    HCalSrc        = " << HCalSrc_.label() 
00079       << ":" << HCalSrc_.instance() << "\n"
00080       << "    SiStripSrc     = " << SiStripSrc_.label() 
00081       << ":" << SiStripSrc_.instance() << "\n" 
00082       << "    SiPixelSrc     = " << SiPxlSrc_.label()
00083       << ":" << SiPxlSrc_.instance() << "\n"
00084       << "    MuDTSrc        = " << MuDTSrc_.label()
00085       << ":" << MuDTSrc_.instance() << "\n"
00086       << "    MuDTSimSrc     = " << MuDTSimSrc_.label()
00087       << ":" << MuDTSimSrc_.instance() << "\n"
00088       << "    MuCSCSrc       = " << MuCSCSrc_.label()
00089       << ":" << MuCSCSrc_.instance() << "\n"
00090       << "    MuRPCSrc       = " << MuRPCSrc_.label()
00091       << ":" << MuRPCSrc_.instance() << "\n"
00092       << "    MuRPCSimSrc    = " << MuRPCSimSrc_.label()
00093       << ":" << MuRPCSimSrc_.instance() << "\n"
00094       << "===============================\n";
00095   }
00096 }
00097 
00098 GlobalRecHitsProducer::~GlobalRecHitsProducer() 
00099 {
00100 }
00101 
00102 void GlobalRecHitsProducer::beginJob()
00103 {
00104   std::string MsgLoggerCat = "GlobalRecHitsProducer_beginJob";
00105 
00106   // clear storage vectors
00107   clear();
00108   return;
00109 }
00110 
00111 void GlobalRecHitsProducer::endJob()
00112 {
00113   std::string MsgLoggerCat = "GlobalRecHitsProducer_endJob";
00114   if (verbosity >= 0)
00115     edm::LogInfo(MsgLoggerCat) 
00116       << "Terminating having processed " << count << " events.";
00117   return;
00118 }
00119 
00120 void GlobalRecHitsProducer::produce(edm::Event& iEvent, 
00121                                   const edm::EventSetup& iSetup)
00122 {
00123   std::string MsgLoggerCat = "GlobalRecHitsProducer_produce";
00124 
00125   // keep track of number of events processed
00126   ++count;
00127 
00128   // get event id information
00129   int nrun = iEvent.id().run();
00130   int nevt = iEvent.id().event();
00131 
00132   if (verbosity > 0) {
00133     edm::LogInfo(MsgLoggerCat)
00134       << "Processing run " << nrun << ", event " << nevt
00135       << " (" << count << " events total)";
00136   } else if (verbosity == 0) {
00137     if (nevt%frequency == 0 || nevt == 1) {
00138       edm::LogInfo(MsgLoggerCat)
00139         << "Processing run " << nrun << ", event " << nevt
00140         << " (" << count << " events total)";
00141     }
00142   }
00143 
00144   // clear event holders
00145   clear();
00146 
00147   // look at information available in the event
00148   if (getAllProvenances) {
00149 
00150     std::vector<const edm::Provenance*> AllProv;
00151     iEvent.getAllProvenance(AllProv);
00152 
00153     if (verbosity >= 0)
00154       edm::LogInfo(MsgLoggerCat)
00155         << "Number of Provenances = " << AllProv.size();
00156 
00157     if (printProvenanceInfo && (verbosity >= 0)) {
00158       TString eventout("\nProvenance info:\n");      
00159 
00160       for (unsigned int i = 0; i < AllProv.size(); ++i) {
00161         eventout += "\n       ******************************";
00162         eventout += "\n       Module       : ";
00163         //eventout += (AllProv[i]->product).moduleLabel();
00164         eventout += AllProv[i]->moduleLabel();
00165         eventout += "\n       ProductID    : ";
00166         //eventout += (AllProv[i]->product).productID_.id_;
00167         eventout += AllProv[i]->productID().id();
00168         eventout += "\n       ClassName    : ";
00169         //eventout += (AllProv[i]->product).fullClassName_;
00170         eventout += AllProv[i]->className();
00171         eventout += "\n       InstanceName : ";
00172         //eventout += (AllProv[i]->product).productInstanceName_;
00173         eventout += AllProv[i]->productInstanceName();
00174         eventout += "\n       BranchName   : ";
00175         //eventout += (AllProv[i]->product).branchName_;
00176         eventout += AllProv[i]->branchName();
00177       }
00178       eventout += "\n       ******************************\n";
00179       edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00180       printProvenanceInfo = false;
00181     }
00182     getAllProvenances = false;
00183   }
00184 
00185   // call fill functions
00186   // gather Ecal information from event
00187   fillECal(iEvent, iSetup);
00188   // gather Hcal information from event
00189   fillHCal(iEvent, iSetup);
00190   // gather Track information from event
00191   fillTrk(iEvent, iSetup);
00192   // gather Muon information from event
00193   fillMuon(iEvent, iSetup);
00194 
00195   if (verbosity > 0)
00196     edm::LogInfo (MsgLoggerCat)
00197       << "Done gathering data from event.";
00198 
00199   // produce object to put into event
00200   std::auto_ptr<PGlobalRecHit> pOut(new PGlobalRecHit);
00201 
00202   if (verbosity > 2)
00203     edm::LogInfo (MsgLoggerCat)
00204       << "Saving event contents:";
00205 
00206   // call store functions
00207   // store ECal information in produce
00208   storeECal(*pOut);
00209   // store HCal information in produce
00210   storeHCal(*pOut);
00211   // store Track information in produce
00212   storeTrk(*pOut);
00213   // store Muon information in produce
00214   storeMuon(*pOut);
00215 
00216   // store information in event
00217   iEvent.put(pOut,label);
00218 
00219   return;
00220 }
00221 
00222 void GlobalRecHitsProducer::fillECal(edm::Event& iEvent, 
00223                                      const edm::EventSetup& iSetup)
00224 {
00225   std::string MsgLoggerCat = "GlobalRecHitsProducer_fillECal";
00226 
00227   TString eventout;
00228   if (verbosity > 0)
00229     eventout = "\nGathering info:";  
00230 
00231   // extract crossing frame from event
00232   //edm::Handle<CrossingFrame> crossingFrame;
00233   edm::Handle<CrossingFrame<PCaloHit> > crossingFrame;
00234   //iEvent.getByType(crossingFrame);
00235   //if (!crossingFrame.isValid()) {
00236   //  edm::LogWarning(MsgLoggerCat)
00237   //    << "Unable to find crossingFrame in event!";
00238   //  return;
00239   //}
00240 
00242   //extract EB information
00244   edm::Handle<EBUncalibratedRecHitCollection> EcalUncalibRecHitEB;
00245   iEvent.getByLabel(ECalUncalEBSrc_, EcalUncalibRecHitEB);
00246   if (!EcalUncalibRecHitEB.isValid()) {
00247     edm::LogWarning(MsgLoggerCat)
00248       << "Unable to find EcalUncalRecHitEB in event!";
00249     return;
00250   }  
00251 
00252   edm::Handle<EBRecHitCollection> EcalRecHitEB;
00253   iEvent.getByLabel(ECalEBSrc_, EcalRecHitEB);
00254   if (!EcalRecHitEB.isValid()) {
00255     edm::LogWarning(MsgLoggerCat)
00256       << "Unable to find EcalRecHitEB in event!";
00257     return;
00258   }  
00259 
00260   // loop over simhits
00261   const std::string barrelHitsName("EcalHitsEB");
00262   iEvent.getByLabel("mix",barrelHitsName,crossingFrame);
00263   if (!crossingFrame.isValid()) {
00264     edm::LogWarning(MsgLoggerCat)
00265       << "Unable to find cal barrel crossingFrame in event!";
00266     return;
00267   }
00268   //std::auto_ptr<MixCollection<PCaloHit> >
00269   //  barrelHits(new MixCollection<PCaloHit>
00270   //           (crossingFrame.product(), barrelHitsName));
00271   std::auto_ptr<MixCollection<PCaloHit> >
00272     barrelHits(new MixCollection<PCaloHit>(crossingFrame.product()));  
00273 
00274   // keep track of sum of simhit energy in each crystal
00275   MapType ebSimMap;
00276   for (MixCollection<PCaloHit>::MixItr hitItr 
00277          = barrelHits->begin();
00278        hitItr != barrelHits->end();
00279        ++hitItr) {
00280     
00281     EBDetId ebid = EBDetId(hitItr->id());
00282     
00283     uint32_t crystid = ebid.rawId();
00284     ebSimMap[crystid] += hitItr->energy();
00285   }
00286   
00287   int nEBRecHits = 0;
00288   // loop over RecHits
00289   const EBUncalibratedRecHitCollection *EBUncalibRecHit = 
00290     EcalUncalibRecHitEB.product();
00291   const EBRecHitCollection *EBRecHit = EcalRecHitEB.product();
00292 
00293   for (EcalUncalibratedRecHitCollection::const_iterator uncalibRecHit =
00294          EBUncalibRecHit->begin();
00295        uncalibRecHit != EBUncalibRecHit->end();
00296        ++uncalibRecHit) {
00297 
00298     EBDetId EBid = EBDetId(uncalibRecHit->id());
00299 
00300     EcalRecHitCollection::const_iterator myRecHit = EBRecHit->find(EBid);
00301 
00302     if (myRecHit != EBRecHit->end()) {
00303       ++nEBRecHits;
00304       EBRE.push_back(myRecHit->energy());
00305       EBSHE.push_back(ebSimMap[EBid.rawId()]);
00306     }
00307   }
00308                                                                        
00309   if (verbosity > 1) {
00310     eventout += "\n          Number of EBRecHits collected:............ ";
00311     eventout += nEBRecHits;
00312   }
00313 
00315   //extract EE information
00317   edm::Handle<EEUncalibratedRecHitCollection> EcalUncalibRecHitEE;
00318   iEvent.getByLabel(ECalUncalEESrc_, EcalUncalibRecHitEE);
00319   if (!EcalUncalibRecHitEE.isValid()) {
00320     edm::LogWarning(MsgLoggerCat)
00321       << "Unable to find EcalUncalRecHitEE in event!";
00322     return;
00323   }  
00324 
00325   edm::Handle<EERecHitCollection> EcalRecHitEE;
00326   iEvent.getByLabel(ECalEESrc_, EcalRecHitEE);
00327   if (!EcalRecHitEE.isValid()) {
00328     edm::LogWarning(MsgLoggerCat)
00329       << "Unable to find EcalRecHitEE in event!";
00330     return;
00331   }  
00332 
00333   // loop over simhits
00334   const std::string endcapHitsName("EcalHitsEE");
00335   iEvent.getByLabel("mix",endcapHitsName,crossingFrame);
00336   if (!crossingFrame.isValid()) {
00337     edm::LogWarning(MsgLoggerCat)
00338       << "Unable to find cal endcap crossingFrame in event!";
00339     return;
00340   }
00341   //std::auto_ptr<MixCollection<PCaloHit> >
00342   //  endcapHits(new MixCollection<PCaloHit>
00343   //           (crossingFrame.product(), endcapHitsName));
00344   std::auto_ptr<MixCollection<PCaloHit> >
00345     endcapHits(new MixCollection<PCaloHit>(crossingFrame.product()));  
00346 
00347   // keep track of sum of simhit energy in each crystal
00348   MapType eeSimMap;
00349   for (MixCollection<PCaloHit>::MixItr hitItr 
00350          = endcapHits->begin();
00351        hitItr != endcapHits->end();
00352        ++hitItr) {
00353     
00354     EEDetId eeid = EEDetId(hitItr->id());
00355     
00356     uint32_t crystid = eeid.rawId();
00357     eeSimMap[crystid] += hitItr->energy();
00358   }
00359   
00360   int nEERecHits = 0;
00361   // loop over RecHits
00362   const EEUncalibratedRecHitCollection *EEUncalibRecHit = 
00363     EcalUncalibRecHitEE.product();
00364   const EERecHitCollection *EERecHit = EcalRecHitEE.product();
00365 
00366   for (EcalUncalibratedRecHitCollection::const_iterator uncalibRecHit =
00367          EEUncalibRecHit->begin();
00368        uncalibRecHit != EEUncalibRecHit->end();
00369        ++uncalibRecHit) {
00370 
00371     EEDetId EEid = EEDetId(uncalibRecHit->id());
00372 
00373     EcalRecHitCollection::const_iterator myRecHit = EERecHit->find(EEid);
00374 
00375     if (myRecHit != EERecHit->end()) {
00376       ++nEERecHits;
00377       EERE.push_back(myRecHit->energy());
00378       EESHE.push_back(eeSimMap[EEid.rawId()]);
00379     }
00380   }
00381                                                                          
00382   if (verbosity > 1) {
00383     eventout += "\n          Number of EERecHits collected:............ ";
00384     eventout += nEERecHits;
00385   }
00386 
00388   //extract ES information
00390   edm::Handle<ESRecHitCollection> EcalRecHitES;
00391   iEvent.getByLabel(ECalESSrc_, EcalRecHitES);
00392   if (!EcalRecHitES.isValid()) {
00393     edm::LogWarning(MsgLoggerCat)
00394       << "Unable to find EcalRecHitES in event!";
00395     return;
00396   }  
00397 
00398   // loop over simhits
00399   const std::string preshowerHitsName("EcalHitsES");
00400   iEvent.getByLabel("mix",preshowerHitsName,crossingFrame);
00401   if (!crossingFrame.isValid()) {
00402     edm::LogWarning(MsgLoggerCat)
00403       << "Unable to find cal preshower crossingFrame in event!";
00404     return;
00405   }
00406   //std::auto_ptr<MixCollection<PCaloHit> >
00407   //  preshowerHits(new MixCollection<PCaloHit>
00408   //           (crossingFrame.product(), preshowerHitsName));
00409   std::auto_ptr<MixCollection<PCaloHit> >
00410     preshowerHits(new MixCollection<PCaloHit>(crossingFrame.product()));  
00411 
00412   // keep track of sum of simhit energy in each crystal
00413   MapType esSimMap;
00414   for (MixCollection<PCaloHit>::MixItr hitItr 
00415          = preshowerHits->begin();
00416        hitItr != preshowerHits->end();
00417        ++hitItr) {
00418     
00419     ESDetId esid = ESDetId(hitItr->id());
00420     
00421     uint32_t crystid = esid.rawId();
00422     esSimMap[crystid] += hitItr->energy();
00423   }
00424   
00425   int nESRecHits = 0;
00426   // loop over RecHits
00427   const ESRecHitCollection *ESRecHit = EcalRecHitES.product();
00428   for (EcalRecHitCollection::const_iterator recHit =
00429          ESRecHit->begin();
00430        recHit != ESRecHit->end();
00431        ++recHit) {
00432 
00433     ESDetId ESid = ESDetId(recHit->id());
00434 
00435     ++nESRecHits;
00436     ESRE.push_back(recHit->energy());
00437     ESSHE.push_back(esSimMap[ESid.rawId()]);
00438   }
00439                                                                       
00440   if (verbosity > 1) {
00441     eventout += "\n          Number of ESRecHits collected:............ ";
00442     eventout += nESRecHits;
00443   }
00444 
00445   if (verbosity > 0)
00446     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00447 
00448   return;
00449 }
00450 
00451 void GlobalRecHitsProducer::storeECal(PGlobalRecHit& product)
00452 {
00453   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeECal";
00454 
00455   if (verbosity > 2) {
00456     TString eventout("\n         nEBRecHits     = ");
00457     eventout += EBRE.size();
00458     for (unsigned int i = 0; i < EBRE.size(); ++i) {
00459       eventout += "\n      (RE, SHE) = (";
00460       eventout += EBRE[i];
00461       eventout += ", ";
00462       eventout += EBSHE[i];
00463       eventout += ")";
00464     }
00465     eventout += "\n         nEERecHits     = ";
00466     eventout += EERE.size();
00467     for (unsigned int i = 0; i < EERE.size(); ++i) {
00468       eventout += "\n      (RE, SHE) = (";
00469       eventout += EERE[i];
00470       eventout += ", ";
00471       eventout += EESHE[i];
00472       eventout += ")";
00473     }
00474     eventout += "\n         nESRecHits     = ";
00475     eventout += ESRE.size();
00476     for (unsigned int i = 0; i < ESRE.size(); ++i) {
00477       eventout += "\n      (RE, SHE) = (";
00478       eventout += ESRE[i];
00479       eventout += ", ";
00480       eventout += ESSHE[i];
00481       eventout += ")";
00482     }
00483     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00484   }
00485 
00486   product.putEBCalRecHits(EBRE,EBSHE);
00487   product.putEECalRecHits(EERE,EESHE);
00488   product.putESCalRecHits(ESRE,ESSHE);
00489 
00490   return;
00491 }
00492 
00493 void GlobalRecHitsProducer::fillHCal(edm::Event& iEvent, 
00494                                    const edm::EventSetup& iSetup)
00495 {
00496   std::string MsgLoggerCat = "GlobalRecHitsProducer_fillHCal";
00497 
00498   TString eventout;
00499   if (verbosity > 0)
00500     eventout = "\nGathering info:";  
00501 
00502   // get geometry
00503   edm::ESHandle<CaloGeometry> geometry;
00504   iSetup.get<CaloGeometryRecord>().get(geometry);
00505   if (!geometry.isValid()) {
00506     edm::LogWarning(MsgLoggerCat)
00507       << "Unable to find CaloGeometry in event!";
00508     return;
00509   }
00510 
00512   // extract simhit info
00514   edm::Handle<edm::PCaloHitContainer> hcalHits;
00515   iEvent.getByLabel(HCalSrc_,hcalHits);
00516   if (!hcalHits.isValid()) {
00517     edm::LogWarning(MsgLoggerCat)
00518       << "Unable to find hcalHits in event!";
00519     return;
00520   }  
00521   const edm::PCaloHitContainer *simhitResult = hcalHits.product();
00522   
00523   MapType fHBEnergySimHits;
00524   MapType fHEEnergySimHits;
00525   MapType fHOEnergySimHits;
00526   MapType fHFEnergySimHits;
00527   for (std::vector<PCaloHit>::const_iterator simhits = simhitResult->begin();
00528        simhits != simhitResult->end();
00529        ++simhits) {
00530     
00531     HcalDetId detId(simhits->id());
00532     uint32_t cellid = detId.rawId();
00533 
00534     if (detId.subdet() == sdHcalBrl){  
00535       fHBEnergySimHits[cellid] += simhits->energy(); 
00536     }
00537     if (detId.subdet() == sdHcalEC){  
00538       fHEEnergySimHits[cellid] += simhits->energy(); 
00539     }    
00540     if (detId.subdet() == sdHcalOut){  
00541       fHOEnergySimHits[cellid] += simhits->energy(); 
00542     }    
00543     if (detId.subdet() == sdHcalFwd){  
00544       fHFEnergySimHits[cellid] += simhits->energy(); 
00545     }    
00546   }
00547 
00548   // max values to be used (HO is found in HB)
00549   Double_t maxHBEnergy = 0.;
00550   Double_t maxHEEnergy = 0.;
00551   Double_t maxHFEnergy = 0.;
00552 
00553   Double_t maxHBPhi = -1000.;
00554   Double_t maxHEPhi = -1000.;
00555   Double_t maxHOPhi = -1000.;
00556   Double_t maxHFPhi = -1000.;
00557 
00558   Double_t maxHBEta = -1000.;
00559   Double_t maxHEEta = -1000.;
00560   Double_t maxHOEta = -1000.;
00561   Double_t maxHFEta = -1000.;
00562 
00563   Double_t PI = 3.141592653589;
00564 
00566   // get HBHE information
00568   std::vector<edm::Handle<HBHERecHitCollection> > hbhe;
00569   iEvent.getManyByType(hbhe);
00570   if (!hbhe[0].isValid()) {
00571     edm::LogWarning(MsgLoggerCat)
00572       << "Unable to find any HBHERecHitCollections in event!";
00573     return;
00574   } 
00575   std::vector<edm::Handle<HBHERecHitCollection> >::iterator ihbhe;
00576      
00577   int iHB = 0;
00578   int iHE = 0; 
00579   for (ihbhe = hbhe.begin(); ihbhe != hbhe.end(); ++ihbhe) {
00580 
00581     // find max values
00582     for (HBHERecHitCollection::const_iterator jhbhe = (*ihbhe)->begin();
00583          jhbhe != (*ihbhe)->end(); ++jhbhe) {
00584 
00585       HcalDetId cell(jhbhe->id());
00586       
00587       if (cell.subdet() == sdHcalBrl) {
00588         
00589         const CaloCellGeometry* cellGeometry =
00590           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00591         double fEta = cellGeometry->getPosition().eta () ;
00592         double fPhi = cellGeometry->getPosition().phi () ;
00593         if ( (jhbhe->energy()) > maxHBEnergy ) {
00594           maxHBEnergy = jhbhe->energy();
00595           maxHBPhi = fPhi;
00596           maxHOPhi = maxHBPhi;
00597           maxHBEta = fEta;
00598           maxHOEta = maxHBEta;
00599         }         
00600       }
00601         
00602       if (cell.subdet() == sdHcalEC) {
00603         
00604         const CaloCellGeometry* cellGeometry =
00605           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00606         double fEta = cellGeometry->getPosition().eta () ;
00607         double fPhi = cellGeometry->getPosition().phi () ;
00608         if ( (jhbhe->energy()) > maxHEEnergy ) {
00609           maxHEEnergy = jhbhe->energy();
00610           maxHEPhi = fPhi;
00611           maxHEEta = fEta;
00612         }         
00613       }
00614     } // end find max values
00615 
00616     for (HBHERecHitCollection::const_iterator jhbhe = (*ihbhe)->begin();
00617          jhbhe != (*ihbhe)->end(); ++jhbhe) {
00618 
00619       HcalDetId cell(jhbhe->id());
00620       
00621       if (cell.subdet() == sdHcalBrl) {
00622 
00623         ++iHB;
00624 
00625         const CaloCellGeometry* cellGeometry =
00626           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00627         double fEta = cellGeometry->getPosition().eta () ;
00628         double fPhi = cellGeometry->getPosition().phi () ;
00629 
00630         float deltaphi = maxHBPhi - fPhi;
00631         if (fPhi > maxHBPhi) { deltaphi = fPhi - maxHBPhi;}
00632         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00633         float deltaeta = fEta - maxHBEta;
00634         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00635 
00636         HBCalREC.push_back(jhbhe->energy());
00637         HBCalR.push_back(r);
00638         HBCalSHE.push_back(fHBEnergySimHits[cell.rawId()]);
00639       }
00640 
00641       if (cell.subdet() == sdHcalEC) {
00642 
00643         ++iHE;
00644 
00645         const CaloCellGeometry* cellGeometry =
00646           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00647         double fEta = cellGeometry->getPosition().eta () ;
00648         double fPhi = cellGeometry->getPosition().phi () ;
00649 
00650         float deltaphi = maxHEPhi - fPhi;
00651         if (fPhi > maxHEPhi) { deltaphi = fPhi - maxHEPhi;}
00652         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00653         float deltaeta = fEta - maxHEEta;
00654         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00655 
00656         HECalREC.push_back(jhbhe->energy());
00657         HECalR.push_back(r);
00658         HECalSHE.push_back(fHEEnergySimHits[cell.rawId()]);
00659       }
00660     }
00661   } // end loop through collection
00662 
00663                                                                       
00664   if (verbosity > 1) {
00665     eventout += "\n          Number of HBRecHits collected:............ ";
00666     eventout += iHB;
00667   }
00668   
00669   if (verbosity > 1) {
00670     eventout += "\n          Number of HERecHits collected:............ ";
00671     eventout += iHE;
00672   }
00673 
00675   // get HF information
00677   std::vector<edm::Handle<HFRecHitCollection> > hf;
00678   iEvent.getManyByType(hf);
00679   if (!hf[0].isValid()) {
00680     edm::LogWarning(MsgLoggerCat)
00681       << "Unable to find any HFRecHitCollections in event!";
00682     return;
00683   } 
00684   std::vector<edm::Handle<HFRecHitCollection> >::iterator ihf;
00685      
00686   int iHF = 0; 
00687   for (ihf = hf.begin(); ihf != hf.end(); ++ihf) {
00688 
00689     // find max values
00690     for (HFRecHitCollection::const_iterator jhf = (*ihf)->begin();
00691          jhf != (*ihf)->end(); ++jhf) {
00692 
00693       HcalDetId cell(jhf->id());
00694       
00695       if (cell.subdet() == sdHcalFwd) {
00696         
00697         const CaloCellGeometry* cellGeometry =
00698           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00699         double fEta = cellGeometry->getPosition().eta () ;
00700         double fPhi = cellGeometry->getPosition().phi () ;
00701         if ( (jhf->energy()) > maxHFEnergy ) {
00702           maxHFEnergy = jhf->energy();
00703           maxHFPhi = fPhi;
00704           maxHFEta = fEta;
00705         }         
00706       }
00707     } // end find max values
00708 
00709     for (HFRecHitCollection::const_iterator jhf = (*ihf)->begin();
00710          jhf != (*ihf)->end(); ++jhf) {
00711 
00712       HcalDetId cell(jhf->id());
00713       
00714       if (cell.subdet() == sdHcalFwd) {
00715 
00716         ++iHF;
00717 
00718         const CaloCellGeometry* cellGeometry =
00719           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00720         double fEta = cellGeometry->getPosition().eta () ;
00721         double fPhi = cellGeometry->getPosition().phi () ;
00722 
00723         float deltaphi = maxHBPhi - fPhi;
00724         if (fPhi > maxHFPhi) { deltaphi = fPhi - maxHFPhi;}
00725         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00726         float deltaeta = fEta - maxHFEta;
00727         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00728 
00729         HFCalREC.push_back(jhf->energy());
00730         HFCalR.push_back(r);
00731         HFCalSHE.push_back(fHFEnergySimHits[cell.rawId()]);
00732       }
00733     }
00734   } // end loop through collection
00735 
00736   if (verbosity > 1) {
00737     eventout += "\n          Number of HFDigis collected:.............. ";
00738     eventout += iHF;
00739   }
00740 
00742   // get HO information
00744   std::vector<edm::Handle<HORecHitCollection> > ho;
00745   iEvent.getManyByType(ho);
00746   if (!ho[0].isValid()) {
00747     edm::LogWarning(MsgLoggerCat)
00748       << "Unable to find any HORecHitCollections in event!";
00749     return;
00750   } 
00751   std::vector<edm::Handle<HORecHitCollection> >::iterator iho;
00752      
00753   int iHO = 0; 
00754   for (iho = ho.begin(); iho != ho.end(); ++iho) {
00755 
00756     for (HORecHitCollection::const_iterator jho = (*iho)->begin();
00757          jho != (*iho)->end(); ++jho) {
00758 
00759       HcalDetId cell(jho->id());
00760       
00761       if (cell.subdet() == sdHcalOut) {
00762 
00763         ++iHO;
00764 
00765         const CaloCellGeometry* cellGeometry =
00766           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00767         double fEta = cellGeometry->getPosition().eta () ;
00768         double fPhi = cellGeometry->getPosition().phi () ;
00769 
00770         float deltaphi = maxHOPhi - fPhi;
00771         if (fPhi > maxHOPhi) { deltaphi = fPhi - maxHOPhi;}
00772         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00773         float deltaeta = fEta - maxHOEta;
00774         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00775 
00776         HOCalREC.push_back(jho->energy());
00777         HOCalR.push_back(r);
00778         HOCalSHE.push_back(fHOEnergySimHits[cell.rawId()]);
00779       }
00780     }
00781   } // end loop through collection
00782 
00783   if (verbosity > 1) {
00784     eventout += "\n          Number of HODigis collected:.............. ";
00785     eventout += iHO;
00786   }
00787 
00788   if (verbosity > 0)
00789     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00790 
00791   return;
00792 }
00793 
00794 void GlobalRecHitsProducer::storeHCal(PGlobalRecHit& product)
00795 {
00796   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeHCal";
00797 
00798   if (verbosity > 2) {
00799     TString eventout("\n         nHBRecHits     = ");
00800     eventout += HBCalREC.size();
00801     for (unsigned int i = 0; i < HBCalREC.size(); ++i) {
00802       eventout += "\n      (REC, R, SHE) = (";
00803       eventout += HBCalREC[i];
00804       eventout += ", ";
00805       eventout += HBCalR[i];
00806       eventout += ", ";
00807       eventout += HBCalSHE[i];
00808       eventout += ")";
00809     }
00810     eventout += "\n         nHERecHits     = ";
00811     eventout += HECalREC.size();
00812     for (unsigned int i = 0; i < HECalREC.size(); ++i) {
00813       eventout += "\n      (REC, R, SHE) = (";
00814       eventout += HECalREC[i];
00815       eventout += ", ";
00816       eventout += HECalR[i];
00817       eventout += ", ";
00818       eventout += HECalSHE[i];
00819       eventout += ")";
00820     }
00821     eventout += "\n         nHFRecHits     = ";
00822     eventout += HFCalREC.size();
00823     for (unsigned int i = 0; i < HFCalREC.size(); ++i) {
00824       eventout += "\n      (REC, R, SHE) = (";
00825       eventout += HFCalREC[i];
00826       eventout += ", ";
00827       eventout += HFCalR[i];
00828       eventout += ", ";
00829       eventout += HFCalSHE[i];
00830       eventout += ")";
00831     }
00832     eventout += "\n         nHORecHits     = ";
00833     eventout += HOCalREC.size();
00834     for (unsigned int i = 0; i < HOCalREC.size(); ++i) {
00835       eventout += "\n      (REC, R, SHE) = (";
00836       eventout += HOCalREC[i];
00837       eventout += ", ";
00838       eventout += HOCalR[i];
00839       eventout += ", ";
00840       eventout += HOCalSHE[i];
00841       eventout += ")";
00842     }
00843 
00844     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00845   }
00846 
00847   product.putHBCalRecHits(HBCalREC,HBCalR,HBCalSHE);
00848   product.putHECalRecHits(HECalREC,HECalR,HECalSHE);
00849   product.putHOCalRecHits(HOCalREC,HOCalR,HOCalSHE);
00850   product.putHFCalRecHits(HFCalREC,HFCalR,HFCalSHE);
00851 
00852   return;
00853 }
00854 
00855 void GlobalRecHitsProducer::fillTrk(edm::Event& iEvent, 
00856                                    const edm::EventSetup& iSetup)
00857 {
00858   //Retrieve tracker topology from geometry
00859   edm::ESHandle<TrackerTopology> tTopoHandle;
00860   iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
00861   const TrackerTopology* const tTopo = tTopoHandle.product();
00862 
00863 
00864   std::string MsgLoggerCat = "GlobalRecHitsProducer_fillTrk";
00865 
00866   TString eventout;
00867   if (verbosity > 0)
00868     eventout = "\nGathering info:";  
00869 
00870   // get strip information
00871   edm::Handle<SiStripMatchedRecHit2DCollection> rechitsmatched;
00872   iEvent.getByLabel(SiStripSrc_, rechitsmatched);
00873   if (!rechitsmatched.isValid()) {
00874     edm::LogWarning(MsgLoggerCat)
00875       << "Unable to find stripmatchedrechits in event!";
00876     return;
00877   }  
00878 
00879   TrackerHitAssociator associate(iEvent,conf_);
00880 
00881   edm::ESHandle<TrackerGeometry> pDD;
00882   iSetup.get<TrackerDigiGeometryRecord>().get(pDD);
00883   if (!pDD.isValid()) {
00884     edm::LogWarning(MsgLoggerCat)
00885       << "Unable to find TrackerDigiGeometry in event!";
00886     return;
00887   }
00888   const TrackerGeometry &tracker(*pDD);
00889 
00890   int nStripBrl = 0, nStripFwd = 0;
00891 
00892   // loop over det units
00893   for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin();
00894        it != pDD->dets().end(); ++it) {
00895     
00896     uint32_t myid = ((*it)->geographicalId()).rawId();
00897     DetId detid = ((*it)->geographicalId());
00898 
00899     //loop over rechits-matched in the same subdetector
00900     SiStripMatchedRecHit2DCollection::const_iterator rechitmatchedMatch = rechitsmatched->find(detid);
00901       
00902       if (rechitmatchedMatch != rechitsmatched->end()) {
00903       SiStripMatchedRecHit2DCollection::DetSet rechitmatchedRange = *rechitmatchedMatch;
00904       SiStripMatchedRecHit2DCollection::DetSet::const_iterator rechitmatchedRangeIteratorBegin = rechitmatchedRange.begin();
00905       SiStripMatchedRecHit2DCollection::DetSet::const_iterator rechitmatchedRangeIteratorEnd   = rechitmatchedRange.end();
00906       SiStripMatchedRecHit2DCollection::DetSet::const_iterator itermatched = rechitmatchedRangeIteratorBegin;
00907         
00908       for ( itermatched = rechitmatchedRangeIteratorBegin; 
00909             itermatched != rechitmatchedRangeIteratorEnd;
00910             ++itermatched) {
00911 
00912         SiStripMatchedRecHit2D const rechit = *itermatched;
00913         LocalPoint position = rechit.localPosition();
00914         
00915         float mindist = 999999.;
00916         float distx = 999999.;
00917         float disty = 999999.;
00918         float dist = 999999.;
00919         std::pair<LocalPoint,LocalVector> closestPair;
00920         matched.clear();
00921         
00922         float rechitmatchedx = position.x();
00923         float rechitmatchedy = position.y();
00924 
00925         matched = associate.associateHit(rechit);
00926 
00927         if (!matched.empty()) {
00928           //project simhit;
00929           const GluedGeomDet* gluedDet = 
00930             (const GluedGeomDet*)tracker.idToDet(rechit.geographicalId());
00931           const StripGeomDetUnit* partnerstripdet =
00932             (StripGeomDetUnit*) gluedDet->stereoDet();
00933           std::pair<LocalPoint,LocalVector> hitPair;
00934           
00935           for(std::vector<PSimHit>::const_iterator m = matched.begin(); 
00936               m != matched.end(); m++){
00937             //project simhit;
00938             hitPair = projectHit((*m),partnerstripdet,gluedDet->surface());
00939             distx = fabs(rechitmatchedx - hitPair.first.x());
00940             disty = fabs(rechitmatchedy - hitPair.first.y());
00941             dist = sqrt(distx*distx+disty*disty);
00942 
00943             if(dist < mindist){
00944               mindist = dist;
00945               closestPair = hitPair;
00946             }
00947           }
00948           
00949           // get TIB
00950           if (detid.subdetId() == sdSiTIB) {
00951 
00952             
00953             ++nStripBrl;
00954 
00955             if (tTopo->tibLayer(myid) == 1) {
00956               TIBL1RX.push_back(rechitmatchedx);
00957               TIBL1RY.push_back(rechitmatchedy);
00958               TIBL1SX.push_back(closestPair.first.x());
00959               TIBL1SY.push_back(closestPair.first.y());
00960             }
00961             if (tTopo->tibLayer(myid) == 2) {
00962               TIBL2RX.push_back(rechitmatchedx);
00963               TIBL2RY.push_back(rechitmatchedy);
00964               TIBL2SX.push_back(closestPair.first.x());
00965               TIBL2SY.push_back(closestPair.first.y());
00966             }   
00967             if (tTopo->tibLayer(myid) == 3) {
00968               TIBL3RX.push_back(rechitmatchedx);
00969               TIBL3RY.push_back(rechitmatchedy);
00970               TIBL3SX.push_back(closestPair.first.x());
00971               TIBL3SY.push_back(closestPair.first.y());
00972             }
00973             if (tTopo->tibLayer(myid) == 4) {
00974               TIBL4RX.push_back(rechitmatchedx);
00975               TIBL4RY.push_back(rechitmatchedy);
00976               TIBL4SX.push_back(closestPair.first.x());
00977               TIBL4SY.push_back(closestPair.first.y());
00978             }
00979           }
00980     
00981           // get TOB
00982           if (detid.subdetId() == sdSiTOB) {
00983 
00984             
00985             ++nStripBrl;
00986 
00987             if (tTopo->tobLayer(myid) == 1) {
00988               TOBL1RX.push_back(rechitmatchedx);
00989               TOBL1RY.push_back(rechitmatchedy);
00990               TOBL1SX.push_back(closestPair.first.x());
00991               TOBL1SY.push_back(closestPair.first.y());
00992             }
00993             if (tTopo->tobLayer(myid) == 2) {
00994               TOBL2RX.push_back(rechitmatchedx);
00995               TOBL2RY.push_back(rechitmatchedy);
00996               TOBL2SX.push_back(closestPair.first.x());
00997               TOBL2SY.push_back(closestPair.first.y());
00998             }   
00999             if (tTopo->tobLayer(myid) == 3) {
01000               TOBL3RX.push_back(rechitmatchedx);
01001               TOBL3RY.push_back(rechitmatchedy);
01002               TOBL3SX.push_back(closestPair.first.x());
01003               TOBL3SY.push_back(closestPair.first.y());
01004             }
01005             if (tTopo->tobLayer(myid) == 4) {
01006               TOBL4RX.push_back(rechitmatchedx);
01007               TOBL4RY.push_back(rechitmatchedy);
01008               TOBL4SX.push_back(closestPair.first.x());
01009               TOBL4SY.push_back(closestPair.first.y());
01010             }
01011           }
01012 
01013           // get TID
01014           if (detid.subdetId() == sdSiTID) {
01015 
01016             
01017             ++nStripFwd;
01018 
01019             if (tTopo->tidWheel(myid) == 1) {
01020               TIDW1RX.push_back(rechitmatchedx);
01021               TIDW1RY.push_back(rechitmatchedy);
01022               TIDW1SX.push_back(closestPair.first.x());
01023               TIDW1SY.push_back(closestPair.first.y());
01024             }
01025             if (tTopo->tidWheel(myid) == 2) {
01026               TIDW2RX.push_back(rechitmatchedx);
01027               TIDW2RY.push_back(rechitmatchedy);
01028               TIDW2SX.push_back(closestPair.first.x());
01029               TIDW2SY.push_back(closestPair.first.y());
01030             }   
01031             if (tTopo->tidWheel(myid) == 3) {
01032               TIDW3RX.push_back(rechitmatchedx);
01033               TIDW3RY.push_back(rechitmatchedy);
01034               TIDW3SX.push_back(closestPair.first.x());
01035               TIDW3SY.push_back(closestPair.first.y());
01036             }
01037           }
01038 
01039           // get TEC
01040           if (detid.subdetId() == sdSiTEC) {
01041 
01042             
01043             ++nStripFwd;
01044 
01045             if (tTopo->tecWheel(myid) == 1) {
01046               TECW1RX.push_back(rechitmatchedx);
01047               TECW1RY.push_back(rechitmatchedy);
01048               TECW1SX.push_back(closestPair.first.x());
01049               TECW1SY.push_back(closestPair.first.y());
01050             }
01051             if (tTopo->tecWheel(myid) == 2) {
01052               TECW2RX.push_back(rechitmatchedx);
01053               TECW2RY.push_back(rechitmatchedy);
01054               TECW2SX.push_back(closestPair.first.x());
01055               TECW2SY.push_back(closestPair.first.y());
01056             }   
01057             if (tTopo->tecWheel(myid) == 3) {
01058               TECW3RX.push_back(rechitmatchedx);
01059               TECW3RY.push_back(rechitmatchedy);
01060               TECW3SX.push_back(closestPair.first.x());
01061               TECW3SY.push_back(closestPair.first.y());
01062             }
01063             if (tTopo->tecWheel(myid) == 4) {
01064               TECW4RX.push_back(rechitmatchedx);
01065               TECW4RY.push_back(rechitmatchedy);
01066               TECW4SX.push_back(closestPair.first.x());
01067               TECW4SY.push_back(closestPair.first.y());
01068             }
01069             if (tTopo->tecWheel(myid) == 5) {
01070               TECW5RX.push_back(rechitmatchedx);
01071               TECW5RY.push_back(rechitmatchedy);
01072               TECW5SX.push_back(closestPair.first.x());
01073               TECW5SY.push_back(closestPair.first.y());
01074             }   
01075             if (tTopo->tecWheel(myid) == 6) {
01076               TECW6RX.push_back(rechitmatchedx);
01077               TECW6RY.push_back(rechitmatchedy);
01078               TECW6SX.push_back(closestPair.first.x());
01079               TECW6SY.push_back(closestPair.first.y());
01080             }
01081             if (tTopo->tecWheel(myid) == 7) {
01082               TECW7RX.push_back(rechitmatchedx);
01083               TECW7RY.push_back(rechitmatchedy);
01084               TECW7SX.push_back(closestPair.first.x());
01085               TECW7SY.push_back(closestPair.first.y());
01086             }   
01087             if (tTopo->tecWheel(myid) == 8) {
01088               TECW8RX.push_back(rechitmatchedx);
01089               TECW8RY.push_back(rechitmatchedy);
01090               TECW8SX.push_back(closestPair.first.x());
01091               TECW8SY.push_back(closestPair.first.y());
01092             }
01093           }
01094 
01095         } // end if matched empty
01096       }
01097     }
01098   } // end loop over det units
01099                                                                       
01100   if (verbosity > 1) {
01101     eventout += "\n          Number of BrlStripRecHits collected:...... ";
01102     eventout += nStripBrl;
01103   }
01104 
01105   if (verbosity > 1) {
01106     eventout += "\n          Number of FrwdStripRecHits collected:..... ";
01107     eventout += nStripFwd;
01108   }
01109 
01110   // get pixel information
01111   //Get RecHits
01112   edm::Handle<SiPixelRecHitCollection> recHitColl;
01113   iEvent.getByLabel(SiPxlSrc_, recHitColl);
01114   if (!recHitColl.isValid()) {
01115     edm::LogWarning(MsgLoggerCat)
01116       << "Unable to find SiPixelRecHitCollection in event!";
01117     return;
01118   }  
01119   
01120   //Get event setup
01121   edm::ESHandle<TrackerGeometry> geom;
01122   iSetup.get<TrackerDigiGeometryRecord>().get(geom); 
01123   if (!geom.isValid()) {
01124     edm::LogWarning(MsgLoggerCat)
01125       << "Unable to find TrackerDigiGeometry in event!";
01126     return;
01127   }
01128   //const TrackerGeometry& theTracker(*geom);
01129 
01130   int nPxlBrl = 0, nPxlFwd = 0;    
01131   //iterate over detunits
01132   for (TrackerGeometry::DetContainer::const_iterator it = geom->dets().begin();
01133        it != geom->dets().end(); ++it) {
01134 
01135     uint32_t myid = ((*it)->geographicalId()).rawId();
01136     DetId detId = ((*it)->geographicalId());
01137     int subid = detId.subdetId();
01138     
01139     if (! ((subid == sdPxlBrl) || (subid == sdPxlFwd))) continue;
01140     
01141     //const PixelGeomDetUnit * theGeomDet = 
01142     //  dynamic_cast<const PixelGeomDetUnit*>(theTracker.idToDet(detId) );
01143     
01144     SiPixelRecHitCollection::const_iterator pixeldet = recHitColl->find(detId);
01145     if (pixeldet == recHitColl->end()) continue;
01146     SiPixelRecHitCollection::DetSet pixelrechitRange = *pixeldet;
01147     SiPixelRecHitCollection::DetSet::const_iterator pixelrechitRangeIteratorBegin = pixelrechitRange.begin();
01148     SiPixelRecHitCollection::DetSet::const_iterator pixelrechitRangeIteratorEnd   = pixelrechitRange.end();
01149     SiPixelRecHitCollection::DetSet::const_iterator pixeliter = pixelrechitRangeIteratorBegin;
01150     std::vector<PSimHit> matched;
01151     
01152     //----Loop over rechits for this detId
01153     for ( ; pixeliter != pixelrechitRangeIteratorEnd; ++pixeliter) {
01154 
01155       matched.clear();
01156       matched = associate.associateHit(*pixeliter);
01157       
01158       if ( !matched.empty() ) {
01159 
01160         float closest = 9999.9;
01161         //std::vector<PSimHit>::const_iterator closestit = matched.begin();
01162         LocalPoint lp = pixeliter->localPosition();
01163         float rechit_x = lp.x();
01164         float rechit_y = lp.y();
01165 
01166         float sim_x = 0.;
01167         float sim_y = 0.;
01168         
01169         //loop over sim hits and fill closet
01170         for (std::vector<PSimHit>::const_iterator m = matched.begin(); 
01171              m != matched.end(); ++m) {
01172 
01173           float sim_x1 = (*m).entryPoint().x();
01174           float sim_x2 = (*m).exitPoint().x();
01175           float sim_xpos = 0.5*(sim_x1+sim_x2);
01176           
01177           float sim_y1 = (*m).entryPoint().y();
01178           float sim_y2 = (*m).exitPoint().y();
01179           float sim_ypos = 0.5*(sim_y1+sim_y2);
01180           
01181           float x_res = fabs(sim_xpos - rechit_x);
01182           float y_res = fabs(sim_ypos - rechit_y);
01183           
01184           float dist = sqrt(x_res*x_res + y_res*y_res);
01185           
01186           if ( dist < closest ) {
01187             closest = dist;
01188             sim_x = sim_xpos;
01189             sim_y = sim_ypos;
01190           }
01191         } // end sim hit loop
01192         
01193         // get Barrel pixels
01194         if (subid == sdPxlBrl) {
01195           
01196           ++nPxlBrl;
01197 
01198           if (tTopo->pxbLayer(myid) == 1) {
01199             BRL1RX.push_back(rechit_x);
01200             BRL1RY.push_back(rechit_y);
01201             BRL1SX.push_back(sim_x);
01202             BRL1SY.push_back(sim_y);      
01203           }
01204           if (tTopo->pxbLayer(myid) == 2) {
01205             BRL2RX.push_back(rechit_x);
01206             BRL2RY.push_back(rechit_y);
01207             BRL2SX.push_back(sim_x);
01208             BRL2SY.push_back(sim_y);              
01209           }
01210           if (tTopo->pxbLayer(myid) == 3) {
01211             BRL3RX.push_back(rechit_x);
01212             BRL3RY.push_back(rechit_y);
01213             BRL3SX.push_back(sim_x);
01214             BRL3SY.push_back(sim_y);              
01215           }
01216         }
01217 
01218         // get Forward pixels
01219         if (subid == sdPxlFwd) {
01220           
01221           ++nPxlFwd;
01222 
01223           if (tTopo->pxfDisk(myid) == 1) {
01224             if (tTopo->pxfSide(myid) == 1) {
01225               FWD1nRX.push_back(rechit_x);
01226               FWD1nRY.push_back(rechit_y);
01227               FWD1nSX.push_back(sim_x);
01228               FWD1nSY.push_back(sim_y);   
01229             }
01230             if (tTopo->pxfSide(myid) == 2) {
01231               FWD1pRX.push_back(rechit_x);
01232               FWD1pRY.push_back(rechit_y);
01233               FWD1pSX.push_back(sim_x);
01234               FWD1pSY.push_back(sim_y);
01235             }
01236           }
01237           if (tTopo->pxfDisk(myid) == 2) {
01238             if (tTopo->pxfSide(myid) == 1) {
01239               FWD2nRX.push_back(rechit_x);
01240               FWD2nRY.push_back(rechit_y);
01241               FWD2nSX.push_back(sim_x);
01242               FWD2nSY.push_back(sim_y);
01243             }
01244             if (tTopo->pxfSide(myid) == 2) {
01245               FWD2pRX.push_back(rechit_x);
01246               FWD2pRY.push_back(rechit_y);
01247               FWD2pSX.push_back(sim_x);
01248               FWD2pSY.push_back(sim_y);
01249             }
01250           }
01251         }      
01252       } // end matched emtpy
01253     } // <-----end rechit loop 
01254   } // <------ end detunit loop  
01255 
01256                      
01257   if (verbosity > 1) {
01258     eventout += "\n          Number of BrlPixelRecHits collected:...... ";
01259     eventout += nPxlBrl;
01260   }
01261 
01262   if (verbosity > 1) {
01263     eventout += "\n          Number of FrwdPixelRecHits collected:..... ";
01264     eventout += nPxlFwd;
01265   }
01266 
01267   if (verbosity > 0)
01268     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
01269 
01270   return;
01271 }
01272 
01273 void GlobalRecHitsProducer::storeTrk(PGlobalRecHit& product)
01274 {
01275   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeTrk";
01276 
01277   if (verbosity > 2) {
01278     
01279     // strip output
01280     TString eventout("\n         nTIBL1     = ");
01281     eventout += TIBL1RX.size();
01282     for (unsigned int i = 0; i < TIBL1RX.size(); ++i) {
01283       eventout += "\n      (RX, RY, SX, SY) = (";
01284       eventout += TIBL1RX[i];
01285       eventout += ", ";
01286       eventout += TIBL1RY[i];
01287       eventout += ", ";
01288       eventout += TIBL1SX[i];
01289       eventout += ", ";
01290       eventout += TIBL1SY[i];
01291       eventout += ")";
01292     }
01293     eventout += "\n         nTIBL2     = ";
01294     eventout += TIBL2RX.size();
01295     for (unsigned int i = 0; i < TIBL2RX.size(); ++i) {
01296       eventout += "\n      (RX, RY, SX, SY) = (";
01297       eventout += TIBL2RX[i];
01298       eventout += ", ";
01299       eventout += TIBL2RY[i];
01300       eventout += ", ";
01301       eventout += TIBL2SX[i];
01302       eventout += ", ";
01303       eventout += TIBL2SY[i];
01304       eventout += ")";
01305     }
01306     eventout += "\n         nTIBL3     = ";
01307     eventout += TIBL3RX.size();
01308     for (unsigned int i = 0; i < TIBL3RX.size(); ++i) {
01309       eventout += "\n      (RX, RY, SX, SY) = (";
01310       eventout += TIBL3RX[i];
01311       eventout += ", ";
01312       eventout += TIBL3RY[i];
01313       eventout += ", ";
01314       eventout += TIBL3SX[i];
01315       eventout += ", ";
01316       eventout += TIBL3SY[i];
01317       eventout += ")";
01318     }
01319     eventout += "\n         nTIBL4     = ";
01320     eventout += TIBL4RX.size();
01321     for (unsigned int i = 0; i < TIBL4RX.size(); ++i) {
01322       eventout += "\n      (RX, RY, SX, SY) = (";
01323       eventout += TIBL4RX[i];
01324       eventout += ", ";
01325       eventout += TIBL4RY[i];
01326       eventout += ", ";
01327       eventout += TIBL4SX[i];
01328       eventout += ", ";
01329       eventout += TIBL4SY[i];
01330       eventout += ")";
01331     }
01332     eventout += "\n         nTOBL1     = ";
01333     eventout += TOBL1RX.size();
01334     for (unsigned int i = 0; i < TOBL1RX.size(); ++i) {
01335       eventout += "\n      (RX, RY, SX, SY) = (";
01336       eventout += TOBL1RX[i];
01337       eventout += ", ";
01338       eventout += TOBL1RY[i];
01339       eventout += ", ";
01340       eventout += TOBL1SX[i];
01341       eventout += ", ";
01342       eventout += TOBL1SY[i];
01343       eventout += ")";
01344     }
01345     eventout += "\n         nTOBL2     = ";
01346     eventout += TOBL2RX.size();
01347     for (unsigned int i = 0; i < TOBL2RX.size(); ++i) {
01348       eventout += "\n      (RX, RY, SX, SY) = (";
01349       eventout += TOBL2RX[i];
01350       eventout += ", ";
01351       eventout += TOBL2RY[i];
01352       eventout += ", ";
01353       eventout += TOBL2SX[i];
01354       eventout += ", ";
01355       eventout += TOBL2SY[i];
01356       eventout += ")";
01357     }
01358     eventout += "\n         nTOBL3     = ";
01359     eventout += TOBL3RX.size();
01360     for (unsigned int i = 0; i < TOBL3RX.size(); ++i) {
01361       eventout += "\n      (RX, RY, SX, SY) = (";
01362       eventout += TOBL3RX[i];
01363       eventout += ", ";
01364       eventout += TOBL3RY[i];
01365       eventout += ", ";
01366       eventout += TOBL3SX[i];
01367       eventout += ", ";
01368       eventout += TOBL3SY[i];
01369       eventout += ")";
01370     }
01371     eventout += "\n         nTOBL4     = ";
01372     eventout += TOBL4RX.size();
01373     for (unsigned int i = 0; i < TOBL4RX.size(); ++i) {
01374       eventout += "\n      (RX, RY, SX, SY) = (";
01375       eventout += TOBL4RX[i];
01376       eventout += ", ";
01377       eventout += TOBL4RY[i];
01378       eventout += ", ";
01379       eventout += TOBL4SX[i];
01380       eventout += ", ";
01381       eventout += TOBL4SY[i];
01382       eventout += ")";
01383     }
01384     eventout += "\n         nTIDW1     = ";
01385     eventout += TIDW1RX.size();
01386     for (unsigned int i = 0; i < TIDW1RX.size(); ++i) {
01387       eventout += "\n      (RX, RY, SX, SY) = (";
01388       eventout += TIDW1RX[i];
01389       eventout += ", ";
01390       eventout += TIDW1RY[i];
01391       eventout += ", ";
01392       eventout += TIDW1SX[i];
01393       eventout += ", ";
01394       eventout += TIDW1SY[i];
01395       eventout += ")";
01396     }
01397     eventout += "\n         nTIDW2     = ";
01398     eventout += TIDW2RX.size();
01399     for (unsigned int i = 0; i < TIDW2RX.size(); ++i) {
01400       eventout += "\n      (RX, RY, SX, SY) = (";
01401       eventout += TIDW2RX[i];
01402       eventout += ", ";
01403       eventout += TIDW2RY[i];
01404       eventout += ", ";
01405       eventout += TIDW2SX[i];
01406       eventout += ", ";
01407       eventout += TIDW2SY[i];
01408       eventout += ")";
01409     }
01410     eventout += "\n         nTIDW3     = ";
01411     eventout += TIDW3RX.size();
01412     for (unsigned int i = 0; i < TIDW3RX.size(); ++i) {
01413       eventout += "\n      (RX, RY, SX, SY) = (";
01414       eventout += TIDW3RX[i];
01415       eventout += ", ";
01416       eventout += TIDW3RY[i];
01417       eventout += ", ";
01418       eventout += TIDW3SX[i];
01419       eventout += ", ";
01420       eventout += TIDW3SY[i];
01421       eventout += ")";
01422     }
01423     eventout += "\n         nTECW1     = ";
01424     eventout += TECW1RX.size();
01425     for (unsigned int i = 0; i < TECW1RX.size(); ++i) {
01426       eventout += "\n      (RX, RY, SX, SY) = (";
01427       eventout += TECW1RX[i];
01428       eventout += ", ";
01429       eventout += TECW1RY[i];
01430       eventout += ", ";
01431       eventout += TECW1SX[i];
01432       eventout += ", ";
01433       eventout += TECW1SY[i];
01434       eventout += ")";
01435     }
01436     eventout += "\n         nTECW2     = ";
01437     eventout += TECW2RX.size();
01438     for (unsigned int i = 0; i < TECW2RX.size(); ++i) {
01439       eventout += "\n      (RX, RY, SX, SY) = (";
01440       eventout += TECW2RX[i];
01441       eventout += ", ";
01442       eventout += TECW2RY[i];
01443       eventout += ", ";
01444       eventout += TECW2SX[i];
01445       eventout += ", ";
01446       eventout += TECW2SY[i];
01447       eventout += ")";
01448     }
01449     eventout += "\n         nTECW3     = ";
01450     eventout += TECW3RX.size();
01451     for (unsigned int i = 0; i < TECW3RX.size(); ++i) {
01452       eventout += "\n      (RX, RY, SX, SY) = (";
01453       eventout += TECW3RX[i];
01454       eventout += ", ";
01455       eventout += TECW3RY[i];
01456       eventout += ", ";
01457       eventout += TECW3SX[i];
01458       eventout += ", ";
01459       eventout += TECW3SY[i];
01460       eventout += ")";
01461     }
01462     eventout += "\n         nTECW4     = ";
01463     eventout += TECW4RX.size();
01464     for (unsigned int i = 0; i < TECW4RX.size(); ++i) {
01465       eventout += "\n      (RX, RY, SX, SY) = (";
01466       eventout += TECW4RX[i];
01467       eventout += ", ";
01468       eventout += TECW4RY[i];
01469       eventout += ", ";
01470       eventout += TECW4SX[i];
01471       eventout += ", ";
01472       eventout += TECW4SY[i];
01473       eventout += ")";
01474     }
01475     eventout += "\n         nTECW5     = ";
01476     eventout += TECW5RX.size();
01477     for (unsigned int i = 0; i < TECW5RX.size(); ++i) {
01478       eventout += "\n      (RX, RY, SX, SY) = (";
01479       eventout += TECW5RX[i];
01480       eventout += ", ";
01481       eventout += TECW5RY[i];
01482       eventout += ", ";
01483       eventout += TECW5SX[i];
01484       eventout += ", ";
01485       eventout += TECW5SY[i];
01486       eventout += ")";
01487     }
01488     eventout += "\n         nTECW6     = ";
01489     eventout += TECW6RX.size();
01490     for (unsigned int i = 0; i < TECW6RX.size(); ++i) {
01491       eventout += "\n      (RX, RY, SX, SY) = (";
01492       eventout += TECW6RX[i];
01493       eventout += ", ";
01494       eventout += TECW6RY[i];
01495       eventout += ", ";
01496       eventout += TECW6SX[i];
01497       eventout += ", ";
01498       eventout += TECW6SY[i];
01499       eventout += ")";
01500     }
01501     eventout += "\n         nTECW7     = ";
01502     eventout += TECW7RX.size();
01503     for (unsigned int i = 0; i < TECW7RX.size(); ++i) {
01504       eventout += "\n      (RX, RY, SX, SY) = (";
01505       eventout += TECW7RX[i];
01506       eventout += ", ";
01507       eventout += TECW7RY[i];
01508       eventout += ", ";
01509       eventout += TECW7SX[i];
01510       eventout += ", ";
01511       eventout += TECW7SY[i];
01512       eventout += ")";
01513     }
01514     eventout += "\n         nTECW8     = ";
01515     eventout += TECW8RX.size();
01516     for (unsigned int i = 0; i < TECW8RX.size(); ++i) {
01517       eventout += "\n      (RX, RY, SX, SY) = (";
01518       eventout += TECW8RX[i];
01519       eventout += ", ";
01520       eventout += TECW8RY[i];
01521       eventout += ", ";
01522       eventout += TECW8SX[i];
01523       eventout += ", ";
01524       eventout += TECW8SY[i];
01525       eventout += ")";
01526     }
01527 
01528     // pixel output
01529     eventout += "\n         nBRL1     = ";
01530     eventout += BRL1RX.size();
01531     for (unsigned int i = 0; i < BRL1RX.size(); ++i) {
01532       eventout += "\n      (RX, RY, SX, SY) = (";
01533       eventout += BRL1RX[i];
01534       eventout += ", ";
01535       eventout += BRL1RY[i];
01536       eventout += ", ";
01537       eventout += BRL1SX[i];
01538       eventout += ", ";
01539       eventout += BRL1SY[i];
01540       eventout += ")";
01541     } 
01542     eventout += "\n         nBRL2     = ";
01543     eventout += BRL2RX.size();
01544     for (unsigned int i = 0; i < BRL2RX.size(); ++i) {
01545       eventout += "\n      (RX, RY, SX, SY) = (";
01546       eventout += BRL2RX[i];
01547       eventout += ", ";
01548       eventout += BRL2RY[i];
01549       eventout += ", ";
01550       eventout += BRL2SX[i];
01551       eventout += ", ";
01552       eventout += BRL2SY[i];
01553       eventout += ")";
01554     } 
01555     eventout += "\n         nBRL3     = ";
01556     eventout += BRL3RX.size();
01557     for (unsigned int i = 0; i < BRL3RX.size(); ++i) {
01558       eventout += "\n      (RX, RY, SX, SY) = (";
01559       eventout += BRL3RX[i];
01560       eventout += ", ";
01561       eventout += BRL3RY[i];
01562       eventout += ", ";
01563       eventout += BRL3SX[i];
01564       eventout += ", ";
01565       eventout += BRL3SY[i];
01566       eventout += ")";
01567     }    
01568     eventout += "\n         nFWD1p     = ";
01569     eventout += FWD1pRX.size();
01570     for (unsigned int i = 0; i < FWD1pRX.size(); ++i) {
01571       eventout += "\n      (RX, RY, SX, SY) = (";
01572       eventout += FWD1pRX[i];
01573       eventout += ", ";
01574       eventout += FWD1pRY[i];
01575       eventout += ", ";
01576       eventout += FWD1pSX[i];
01577       eventout += ", ";
01578       eventout += FWD1pSY[i];
01579       eventout += ")";
01580     } 
01581     eventout += "\n         nFWD1n     = ";
01582     eventout += FWD1nRX.size();
01583     for (unsigned int i = 0; i < FWD1nRX.size(); ++i) {
01584       eventout += "\n      (RX, RY, SX, SY) = (";
01585       eventout += FWD1nRX[i];
01586       eventout += ", ";
01587       eventout += FWD1nRY[i];
01588       eventout += ", ";
01589       eventout += FWD1nSX[i];
01590       eventout += ", ";
01591       eventout += FWD1nSY[i];
01592       eventout += ")";
01593     } 
01594     eventout += "\n         nFWD2p     = ";
01595     eventout += FWD2pRX.size();
01596     for (unsigned int i = 0; i < FWD2pRX.size(); ++i) {
01597       eventout += "\n      (RX, RY, SX, SY) = (";
01598       eventout += FWD2pRX[i];
01599       eventout += ", ";
01600       eventout += FWD2pRY[i];
01601       eventout += ", ";
01602       eventout += FWD2pSX[i];
01603       eventout += ", ";
01604       eventout += FWD2pSY[i];
01605       eventout += ")";
01606     }
01607     eventout += "\n         nFWD2p     = ";
01608     eventout += FWD2nRX.size();
01609     for (unsigned int i = 0; i < FWD2nRX.size(); ++i) {
01610       eventout += "\n      (RX, RY, SX, SY) = (";
01611       eventout += FWD2nRX[i];
01612       eventout += ", ";
01613       eventout += FWD2nRY[i];
01614       eventout += ", ";
01615       eventout += FWD2nSX[i];
01616       eventout += ", ";
01617       eventout += FWD2nSY[i];
01618       eventout += ")";
01619     } 
01620 
01621     edm::LogInfo(MsgLoggerCat) << eventout << "\n";  
01622   }
01623 
01624   // strip output
01625   product.putTIBL1RecHits(TIBL1RX,TIBL1RY,TIBL1SX,TIBL1SY);
01626   product.putTIBL2RecHits(TIBL2RX,TIBL2RY,TIBL2SX,TIBL2SY);
01627   product.putTIBL3RecHits(TIBL3RX,TIBL3RY,TIBL3SX,TIBL3SY);
01628   product.putTIBL4RecHits(TIBL4RX,TIBL4RY,TIBL4SX,TIBL4SY);
01629   product.putTOBL1RecHits(TOBL1RX,TOBL1RY,TOBL1SX,TOBL1SY);
01630   product.putTOBL2RecHits(TOBL2RX,TOBL2RY,TOBL2SX,TOBL2SY);
01631   product.putTOBL3RecHits(TOBL3RX,TOBL3RY,TOBL3SX,TOBL3SY);
01632   product.putTOBL4RecHits(TOBL4RX,TOBL4RY,TOBL4SX,TOBL4SY);
01633   product.putTIDW1RecHits(TIDW1RX,TIDW1RY,TIDW1SX,TIDW1SY);
01634   product.putTIDW2RecHits(TIDW2RX,TIDW2RY,TIDW2SX,TIDW2SY);
01635   product.putTIDW3RecHits(TIDW3RX,TIDW3RY,TIDW3SX,TIDW3SY);
01636   product.putTECW1RecHits(TECW1RX,TECW1RY,TECW1SX,TECW1SY);
01637   product.putTECW2RecHits(TECW2RX,TECW2RY,TECW2SX,TECW2SY);
01638   product.putTECW3RecHits(TECW3RX,TECW3RY,TECW3SX,TECW3SY);
01639   product.putTECW4RecHits(TECW4RX,TECW4RY,TECW4SX,TECW4SY);
01640   product.putTECW5RecHits(TECW5RX,TECW5RY,TECW5SX,TECW5SY);
01641   product.putTECW6RecHits(TECW6RX,TECW6RY,TECW6SX,TECW6SY);  
01642   product.putTECW7RecHits(TECW7RX,TECW7RY,TECW7SX,TECW7SY);
01643   product.putTECW8RecHits(TECW8RX,TECW8RY,TECW8SX,TECW8SY);  
01644 
01645   // pixel output
01646   product.putBRL1RecHits(BRL1RX,BRL1RY,BRL1SX,BRL1SY);
01647   product.putBRL2RecHits(BRL2RX,BRL2RY,BRL2SX,BRL2SY);
01648   product.putBRL3RecHits(BRL3RX,BRL3RY,BRL3SX,BRL3SY);
01649   product.putFWD1pRecHits(FWD1pRX,FWD1pRY,FWD1pSX,FWD1pSY);
01650   product.putFWD1nRecHits(FWD1nRX,FWD1nRY,FWD1nSX,FWD1nSY);
01651   product.putFWD2pRecHits(FWD2pRX,FWD2pRY,FWD2pSX,FWD2pSY);
01652   product.putFWD2nRecHits(FWD2nRX,FWD2nRY,FWD2nSX,FWD2nSY);
01653 
01654   return;
01655 }
01656 
01657 void GlobalRecHitsProducer::fillMuon(edm::Event& iEvent, 
01658                                    const edm::EventSetup& iSetup)
01659 {
01660   std::string MsgLoggerCat = "GlobalRecHitsProducer_fillMuon";
01661   
01662   TString eventout;
01663   if (verbosity > 0)
01664     eventout = "\nGathering info:";  
01665 
01666   // get DT information
01667   edm::ESHandle<DTGeometry> dtGeom;
01668   iSetup.get<MuonGeometryRecord>().get(dtGeom);
01669   if (!dtGeom.isValid()) {
01670     edm::LogWarning(MsgLoggerCat)
01671       << "Unable to find DTMuonGeometryRecord in event!";
01672     return;
01673   }  
01674 
01675   edm::Handle<edm::PSimHitContainer> dtsimHits;
01676   iEvent.getByLabel(MuDTSimSrc_, dtsimHits);
01677   if (!dtsimHits.isValid()) {
01678     edm::LogWarning(MsgLoggerCat)
01679       << "Unable to find dtsimHits in event!";
01680     return;
01681   } 
01682 
01683   std::map<DTWireId, edm::PSimHitContainer> simHitsPerWire =
01684     DTHitQualityUtils::mapSimHitsPerWire(*(dtsimHits.product()));
01685 
01686   edm::Handle<DTRecHitCollection> dtRecHits;
01687   iEvent.getByLabel(MuDTSrc_, dtRecHits);
01688   if (!dtRecHits.isValid()) {
01689     edm::LogWarning(MsgLoggerCat)
01690       << "Unable to find dtRecHits in event!";
01691     return;
01692   }   
01693 
01694   std::map<DTWireId, std::vector<DTRecHit1DPair> > recHitsPerWire =
01695     map1DRecHitsPerWire(dtRecHits.product());
01696 
01697 
01698   int nDt = compute(dtGeom.product(), simHitsPerWire, recHitsPerWire, 1);
01699                                                                     
01700   if (verbosity > 1) {
01701     eventout += "\n          Number of DtMuonRecHits collected:........ ";
01702     eventout += nDt;
01703   }
01704 
01705   // get CSC Strip information
01706   // get map of sim hits
01707   theMap.clear();
01708   //edm::Handle<CrossingFrame> cf;
01709   edm::Handle<CrossingFrame<PSimHit> > cf;
01710   //iEvent.getByType(cf);
01711   //if (!cf.isValid()) {
01712   //  edm::LogWarning(MsgLoggerCat)
01713   //    << "Unable to find CrossingFrame in event!";
01714   //  return;
01715   //}    
01716   //MixCollection<PSimHit> simHits(cf.product(), "MuonCSCHits");
01717   iEvent.getByLabel("mix","MuonCSCHits",cf);
01718   if (!cf.isValid()) {
01719     edm::LogWarning(MsgLoggerCat)
01720       << "Unable to find muo CSC  crossingFrame in event!";
01721     return;
01722   }
01723   MixCollection<PSimHit> simHits(cf.product());
01724 
01725   // arrange the hits by detUnit
01726   for(MixCollection<PSimHit>::MixItr hitItr = simHits.begin();
01727       hitItr != simHits.end(); ++hitItr)
01728   {
01729     theMap[hitItr->detUnitId()].push_back(*hitItr);
01730   }  
01731 
01732   // get geometry
01733   edm::ESHandle<CSCGeometry> hGeom;
01734   iSetup.get<MuonGeometryRecord>().get(hGeom);
01735   if (!hGeom.isValid()) {
01736     edm::LogWarning(MsgLoggerCat)
01737       << "Unable to find CSCMuonGeometryRecord in event!";
01738     return;
01739   }    
01740   const CSCGeometry *theCSCGeometry = &*hGeom;
01741 
01742   // get rechits
01743   edm::Handle<CSCRecHit2DCollection> hRecHits;
01744   iEvent.getByLabel(MuCSCSrc_, hRecHits);
01745   if (!hRecHits.isValid()) {
01746     edm::LogWarning(MsgLoggerCat)
01747       << "Unable to find CSC RecHits in event!";
01748     return;
01749   }    
01750   const CSCRecHit2DCollection *cscRecHits = hRecHits.product();
01751 
01752   int nCSC = 0;
01753   for (CSCRecHit2DCollection::const_iterator recHitItr = cscRecHits->begin();
01754        recHitItr != cscRecHits->end(); ++recHitItr) {
01755 
01756     int detId = (*recHitItr).cscDetId().rawId();
01757  
01758     edm::PSimHitContainer simHits;   
01759     std::map<int, edm::PSimHitContainer>::const_iterator mapItr = 
01760       theMap.find(detId);
01761     if (mapItr != theMap.end()) {
01762       simHits = mapItr->second;
01763     }
01764 
01765     if (simHits.size() == 1) {
01766       ++nCSC;
01767 
01768       const GeomDetUnit* detUnit = 
01769         theCSCGeometry->idToDetUnit(CSCDetId(detId));
01770       const CSCLayer *layer = dynamic_cast<const CSCLayer *>(detUnit); 
01771 
01772      int chamberType = layer->chamber()->specs()->chamberType();
01773       plotResolution(simHits[0], *recHitItr, layer, chamberType);
01774     }
01775   }
01776                                                 
01777   if (verbosity > 1) {
01778     eventout += "\n          Number of CSCRecHits collected:........... ";
01779     eventout += nCSC;
01780   }
01781 
01782   // get RPC information
01783   std::map<double, int> mapsim, maprec;
01784   std::map<int, double> nmapsim, nmaprec;
01785 
01786   edm::ESHandle<RPCGeometry> rpcGeom;
01787   iSetup.get<MuonGeometryRecord>().get(rpcGeom);
01788   if (!rpcGeom.isValid()) {
01789     edm::LogWarning(MsgLoggerCat)
01790       << "Unable to find RPCMuonGeometryRecord in event!";
01791     return;
01792   }  
01793 
01794   edm::Handle<edm::PSimHitContainer> simHit;
01795   iEvent.getByLabel(MuRPCSimSrc_, simHit);
01796   if (!simHit.isValid()) {
01797     edm::LogWarning(MsgLoggerCat)
01798       << "Unable to find RPCSimHit in event!";
01799     return;
01800   }    
01801 
01802   edm::Handle<RPCRecHitCollection> recHit;
01803   iEvent.getByLabel(MuRPCSrc_, recHit);
01804   if (!simHit.isValid()) {
01805     edm::LogWarning(MsgLoggerCat)
01806       << "Unable to find RPCRecHit in event!";
01807     return;
01808   } 
01809 
01810   int nRPC = 0;
01811   RPCRecHitCollection::const_iterator recIt;
01812   int nrec = 0;
01813   for (recIt = recHit->begin(); recIt != recHit->end(); ++recIt) {
01814     RPCDetId Rid = (RPCDetId)(*recIt).rpcId();
01815     const RPCRoll *roll = dynamic_cast<const RPCRoll*>(rpcGeom->roll(Rid));
01816     if (roll->isForward()) {
01817 
01818       if (verbosity > 1) {
01819         eventout += "\n          Number of RPCRecHits collected:........... ";
01820         eventout += nRPC;
01821       }
01822       
01823       if (verbosity > 0)
01824         edm::LogInfo(MsgLoggerCat) << eventout << "\n";
01825       return;
01826     }
01827     nrec = nrec + 1;
01828     LocalPoint rhitlocal = (*recIt).localPosition();
01829     double rhitlocalx = rhitlocal.x();
01830     maprec[rhitlocalx] = nrec; 
01831   }
01832 
01833   int i = 0;
01834   for (std::map<double,int>::iterator iter = maprec.begin();
01835        iter != maprec.end(); ++iter) {
01836     i = i + 1;
01837     nmaprec[i] = (*iter).first;
01838   }
01839                   
01840   edm::PSimHitContainer::const_iterator simIt;
01841   int nsim = 0;
01842   for (simIt = simHit->begin(); simIt != simHit->end(); simIt++) {
01843     int ptype = (*simIt).particleType();
01844     //RPCDetId Rsid = (RPCDetId)(*simIt).detUnitId();
01845     if (ptype == 13 || ptype == -13) {
01846       nsim = nsim + 1;
01847       LocalPoint shitlocal = (*simIt).localPosition();
01848       double shitlocalx = shitlocal.x();
01849       mapsim[shitlocalx] = nsim;
01850     }
01851   }
01852 
01853   i = 0;
01854   for (std::map<double,int>::iterator iter = mapsim.begin();
01855        iter != mapsim.end(); ++iter) {
01856     i = i + 1;
01857     nmapsim[i] = (*iter).first;
01858   }
01859 
01860   if (nsim == nrec) {
01861     for (int r = 0; r < nsim; r++) {
01862       ++nRPC;
01863       RPCRHX.push_back(nmaprec[r+1]);
01864       RPCSHX.push_back(nmapsim[r+1]);
01865     }
01866   }
01867                                                                   
01868   if (verbosity > 1) {
01869     eventout += "\n          Number of RPCRecHits collected:........... ";
01870     eventout += nRPC;
01871   }
01872 
01873   if (verbosity > 0)
01874     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
01875   
01876   return;
01877 }
01878 
01879 void GlobalRecHitsProducer::storeMuon(PGlobalRecHit& product)
01880 {
01881   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeMuon";
01882 
01883   if (verbosity > 2) {
01884 
01885     // dt output
01886     TString eventout("\n         nDT     = ");
01887     eventout += DTRHD.size();
01888     for (unsigned int i = 0; i < DTRHD.size(); ++i) {
01889       eventout += "\n      (RHD, SHD) = (";
01890       eventout += DTRHD[i];
01891       eventout += ", ";
01892       eventout += DTSHD[i];
01893       eventout += ")";
01894     }
01895 
01896     // CSC Strip
01897     eventout += "\n         nCSC     = ";
01898     eventout += CSCRHPHI.size();
01899     for (unsigned int i = 0; i < CSCRHPHI.size(); ++i) {
01900       eventout += "\n      (rhphi, rhperp, shphi) = (";
01901       eventout += CSCRHPHI[i];
01902       eventout += ", ";
01903       eventout += CSCRHPERP[i];
01904       eventout += ", ";
01905       eventout += CSCSHPHI[i];
01906       eventout += ")";
01907     }    
01908 
01909     // RPC
01910     eventout += "\n         nRPC     = ";
01911     eventout += RPCRHX.size();
01912     for (unsigned int i = 0; i < RPCRHX.size(); ++i) {
01913       eventout += "\n      (rhx, shx) = (";
01914       eventout += RPCRHX[i];
01915       eventout += ", ";
01916       eventout += RPCSHX[i];
01917       eventout += ")";
01918     }    
01919 
01920     edm::LogInfo(MsgLoggerCat) << eventout << "\n";  
01921   }
01922   
01923   product.putDTRecHits(DTRHD,DTSHD);
01924 
01925   product.putCSCRecHits(CSCRHPHI,CSCRHPERP,CSCSHPHI);
01926 
01927   product.putRPCRecHits(RPCRHX,RPCSHX);
01928 
01929   return;
01930 }
01931 
01932 void GlobalRecHitsProducer::clear()
01933 {
01934   std::string MsgLoggerCat = "GlobalRecHitsProducer_clear";
01935 
01936   if (verbosity > 0)
01937     edm::LogInfo(MsgLoggerCat)
01938       << "Clearing event holders"; 
01939 
01940   // reset electromagnetic info
01941   // EE info
01942   EERE.clear(); 
01943   EESHE.clear(); 
01944   // EB info
01945   EBRE.clear();  
01946   EBSHE.clear();
01947   // ES info
01948   ESRE.clear();
01949   ESSHE.clear();
01950 
01951   // reset HCal Info
01952   HBCalREC.clear();
01953   HBCalR.clear();
01954   HBCalSHE.clear();
01955   HECalREC.clear();
01956   HECalR.clear();
01957   HECalSHE.clear();
01958   HOCalREC.clear();
01959   HOCalR.clear();
01960   HOCalSHE.clear();
01961   HFCalREC.clear();
01962   HFCalR.clear();
01963   HFCalSHE.clear();  
01964 
01965   // reset Track Info
01966   TIBL1RX.clear(); 
01967   TIBL2RX.clear(); 
01968   TIBL3RX.clear(); 
01969   TIBL4RX.clear();
01970   TIBL1RY.clear(); 
01971   TIBL2RY.clear(); 
01972   TIBL3RY.clear(); 
01973   TIBL4RY.clear();
01974   TIBL1SX.clear(); 
01975   TIBL2SX.clear(); 
01976   TIBL3SX.clear(); 
01977   TIBL4SX.clear();
01978   TIBL1SY.clear(); 
01979   TIBL2SY.clear(); 
01980   TIBL3SY.clear(); 
01981   TIBL4SY.clear();  
01982 
01983   TOBL1RX.clear(); 
01984   TOBL2RX.clear(); 
01985   TOBL3RX.clear(); 
01986   TOBL4RX.clear();
01987   TOBL1RY.clear(); 
01988   TOBL2RY.clear(); 
01989   TOBL3RY.clear(); 
01990   TOBL4RY.clear();
01991   TOBL1SX.clear(); 
01992   TOBL2SX.clear(); 
01993   TOBL3SX.clear(); 
01994   TOBL4SX.clear();
01995   TOBL1SY.clear(); 
01996   TOBL2SY.clear(); 
01997   TOBL3SY.clear(); 
01998   TOBL4SY.clear();  
01999 
02000   TIDW1RX.clear(); 
02001   TIDW2RX.clear(); 
02002   TIDW3RX.clear(); 
02003   TIDW1RY.clear(); 
02004   TIDW2RY.clear(); 
02005   TIDW3RY.clear(); 
02006   TIDW1SX.clear(); 
02007   TIDW2SX.clear(); 
02008   TIDW3SX.clear();
02009   TIDW1SY.clear(); 
02010   TIDW2SY.clear(); 
02011   TIDW3SY.clear();  
02012 
02013   TECW1RX.clear();  
02014   TECW2RX.clear();  
02015   TECW3RX.clear();  
02016   TECW4RX.clear();  
02017   TECW5RX.clear();  
02018   TECW6RX.clear();  
02019   TECW7RX.clear();  
02020   TECW8RX.clear();  
02021   TECW1RY.clear();  
02022   TECW2RY.clear();  
02023   TECW3RY.clear();  
02024   TECW4RY.clear();  
02025   TECW5RY.clear();  
02026   TECW6RY.clear();  
02027   TECW7RY.clear();  
02028   TECW8RY.clear();  
02029   TECW1SX.clear();  
02030   TECW2SX.clear();  
02031   TECW3SX.clear();  
02032   TECW4SX.clear();  
02033   TECW5SX.clear();  
02034   TECW6SX.clear();  
02035   TECW7SX.clear();  
02036   TECW8SX.clear();  
02037   TECW1SY.clear();  
02038   TECW2SY.clear();  
02039   TECW3SY.clear();  
02040   TECW4SY.clear();  
02041   TECW5SY.clear();  
02042   TECW6SY.clear();  
02043   TECW7SY.clear();  
02044   TECW8SY.clear();  
02045 
02046   BRL1RX.clear();
02047   BRL1RY.clear();
02048   BRL1SX.clear();
02049   BRL1SY.clear();
02050   BRL2RX.clear();
02051   BRL2RY.clear();
02052   BRL2SX.clear();
02053   BRL2SY.clear();
02054   BRL3RX.clear();
02055   BRL3RY.clear();
02056   BRL3SX.clear();
02057   BRL3SY.clear();
02058 
02059   FWD1pRX.clear();
02060   FWD1pRY.clear();
02061   FWD1pSX.clear();
02062   FWD1pSY.clear();
02063   FWD1nRX.clear();
02064   FWD1nRY.clear();
02065   FWD1nSX.clear();
02066   FWD1nSY.clear();
02067   FWD2pRX.clear();
02068   FWD2pRY.clear();
02069   FWD2pSX.clear();
02070   FWD2pSY.clear();
02071   FWD2nRX.clear();
02072   FWD2nRY.clear();
02073   FWD2nSX.clear();
02074   FWD2nSY.clear();
02075 
02076   //muon clear
02077   DTRHD.clear();
02078   DTSHD.clear();
02079 
02080   CSCRHPHI.clear();
02081   CSCRHPERP.clear();
02082   CSCSHPHI.clear();
02083 
02084   RPCRHX.clear();
02085   RPCSHX.clear();
02086 
02087   return;
02088 }
02089 
02090 //needed by to do the residual for matched hits in SiStrip
02091 std::pair<LocalPoint,LocalVector> 
02092 GlobalRecHitsProducer::projectHit(const PSimHit& hit, 
02093                                   const StripGeomDetUnit* stripDet,
02094                                   const BoundPlane& plane) 
02095 {
02096   
02097   const StripTopology& topol = stripDet->specificTopology();
02098   GlobalPoint globalpos= stripDet->surface().toGlobal(hit.localPosition());
02099   LocalPoint localHit = plane.toLocal(globalpos);
02100   //track direction
02101   LocalVector locdir=hit.localDirection();
02102   //rotate track in new frame
02103   
02104   GlobalVector globaldir= stripDet->surface().toGlobal(locdir);
02105   LocalVector dir=plane.toLocal(globaldir);
02106   float scale = -localHit.z() / dir.z();
02107   
02108   LocalPoint projectedPos = localHit + scale*dir;
02109     
02110   float selfAngle = topol.stripAngle( topol.strip( hit.localPosition()));
02111 
02112   // vector along strip in hit frame 
02113   LocalVector stripDir( sin(selfAngle), cos(selfAngle), 0); 
02114   
02115   LocalVector 
02116     localStripDir(plane.toLocal(stripDet->surface().toGlobal(stripDir)));
02117   
02118   return std::pair<LocalPoint,LocalVector>( projectedPos, localStripDir);
02119 }
02120 
02121 // Return a map between DTRecHit1DPair and wireId
02122 std::map<DTWireId, std::vector<DTRecHit1DPair> >
02123 GlobalRecHitsProducer::map1DRecHitsPerWire(const DTRecHitCollection* 
02124                                            dt1DRecHitPairs) {
02125   std::map<DTWireId, std::vector<DTRecHit1DPair> > ret;
02126   
02127   for(DTRecHitCollection::const_iterator rechit = dt1DRecHitPairs->begin();
02128       rechit != dt1DRecHitPairs->end(); rechit++) {
02129     ret[(*rechit).wireId()].push_back(*rechit);
02130   }
02131   
02132   return ret;
02133 }
02134 
02135 // Compute SimHit distance from wire (cm)
02136 float GlobalRecHitsProducer::simHitDistFromWire(const DTLayer* layer,
02137                                                 DTWireId wireId,
02138                                                 const PSimHit& hit) {
02139   float xwire = layer->specificTopology().wirePosition(wireId.wire());
02140   LocalPoint entryP = hit.entryPoint();
02141   LocalPoint exitP = hit.exitPoint();
02142   float xEntry = entryP.x()-xwire;
02143   float xExit  = exitP.x()-xwire;
02144 
02145   //FIXME: check...  
02146   return fabs(xEntry - (entryP.z()*(xExit-xEntry))/(exitP.z()-entryP.z()));
02147 }
02148 
02149 // Find the RecHit closest to the muon SimHit
02150 template  <typename type>
02151 const type* 
02152 GlobalRecHitsProducer::findBestRecHit(const DTLayer* layer,
02153                                       DTWireId wireId,
02154                                       const std::vector<type>& recHits,
02155                                       const float simHitDist) {
02156   float res = 99999;
02157   const type* theBestRecHit = 0;
02158   // Loop over RecHits within the cell
02159   for(typename std::vector<type>::const_iterator recHit = recHits.begin();
02160       recHit != recHits.end();
02161       recHit++) {
02162     float distTmp = recHitDistFromWire(*recHit, layer);
02163     if(fabs(distTmp-simHitDist) < res) {
02164       res = fabs(distTmp-simHitDist);
02165       theBestRecHit = &(*recHit);
02166     }
02167   } // End of loop over RecHits within the cell
02168   
02169   return theBestRecHit;
02170 }
02171 
02172 // Compute the distance from wire (cm) of a hits in a DTRecHit1DPair
02173 float 
02174 GlobalRecHitsProducer::recHitDistFromWire(const DTRecHit1DPair& hitPair, 
02175                                           const DTLayer* layer) {
02176   // Compute the rechit distance from wire
02177   return fabs(hitPair.localPosition(DTEnums::Left).x() -
02178               hitPair.localPosition(DTEnums::Right).x())/2.;
02179 }
02180 
02181 // Compute the distance from wire (cm) of a hits in a DTRecHit1D
02182 float 
02183 GlobalRecHitsProducer::recHitDistFromWire(const DTRecHit1D& recHit, 
02184                                           const DTLayer* layer) {
02185   return fabs(recHit.localPosition().x() - 
02186               layer->specificTopology().wirePosition(recHit.wireId().wire()));
02187 }
02188 
02189 template  <typename type>
02190 int GlobalRecHitsProducer::compute(const DTGeometry *dtGeom,
02191                                    const std::map<DTWireId, std::vector<PSimHit> >& 
02192                                    _simHitsPerWire,
02193                                    const std::map<DTWireId, std::vector<type> >& 
02194                                    _recHitsPerWire,
02195                                    int step) {
02196   
02197   std::map<DTWireId, std::vector<PSimHit> > simHitsPerWire = _simHitsPerWire;
02198   std::map<DTWireId, std::vector<type> > recHitsPerWire = _recHitsPerWire;
02199   int nDt = 0;
02200   // Loop over cells with a muon SimHit
02201   for(std::map<DTWireId, std::vector<PSimHit> >::const_iterator wireAndSHits = 
02202         simHitsPerWire.begin();
02203       wireAndSHits != simHitsPerWire.end();
02204       wireAndSHits++) {
02205     DTWireId wireId = (*wireAndSHits).first;
02206     std::vector<PSimHit> simHitsInCell = (*wireAndSHits).second;
02207     
02208     // Get the layer
02209     const DTLayer* layer = dtGeom->layer(wireId);
02210     
02211     // Look for a mu hit in the cell
02212     const PSimHit* muSimHit = DTHitQualityUtils::findMuSimHit(simHitsInCell);
02213     if (muSimHit==0) {
02214       continue; // Skip this cell
02215     }
02216 
02217     // Find the distance of the simhit from the wire
02218     float simHitWireDist = simHitDistFromWire(layer, wireId, *muSimHit);
02219     // Skip simhits out of the cell
02220     if(simHitWireDist>2.1) {
02221       continue; // Skip this cell
02222     }
02223     //GlobalPoint simHitGlobalPos = layer->toGlobal(muSimHit->localPosition());
02224 
02225     // Look for RecHits in the same cell
02226     if(recHitsPerWire.find(wireId) == recHitsPerWire.end()) {
02227       continue; // No RecHit found in this cell
02228     } else {
02229 
02230       // vector<type> recHits = (*wireAndRecHits).second;
02231       std::vector<type> recHits = recHitsPerWire[wireId];
02232          
02233       // Find the best RecHit
02234       const type* theBestRecHit = 
02235         findBestRecHit(layer, wireId, recHits, simHitWireDist);
02236  
02237       float recHitWireDist =  recHitDistFromWire(*theBestRecHit, layer);
02238       
02239       ++nDt;
02240 
02241       DTRHD.push_back(recHitWireDist);
02242       DTSHD.push_back(simHitWireDist);
02243       
02244     } // find rechits
02245   } // loop over simhits
02246 
02247   return nDt;
02248 }
02249 
02250 void 
02251 GlobalRecHitsProducer::plotResolution(const PSimHit & simHit, 
02252                                       const CSCRecHit2D & recHit,
02253                                       const CSCLayer * layer, 
02254                                       int chamberType) {
02255   GlobalPoint simHitPos = layer->toGlobal(simHit.localPosition());
02256   GlobalPoint recHitPos = layer->toGlobal(recHit.localPosition());
02257   
02258   CSCRHPHI.push_back(recHitPos.phi());
02259   CSCRHPERP.push_back(recHitPos.perp());
02260   CSCSHPHI.push_back(simHitPos.phi());
02261 }
02262 
02263 //define this as a plug-in
02264 //DEFINE_FWK_MODULE(GlobalRecHitsProducer);