CMS 3D CMS Logo

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