CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/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 maxHFEnergy = 0.;
00550 
00551   Double_t maxHBPhi = -1000.;
00552   Double_t maxHEPhi = -1000.;
00553   Double_t maxHOPhi = -1000.;
00554   Double_t maxHFPhi = -1000.;
00555 
00556   Double_t maxHBEta = -1000.;
00557   Double_t maxHEEta = -1000.;
00558   Double_t maxHOEta = -1000.;
00559   Double_t maxHFEta = -1000.;
00560 
00561   Double_t PI = 3.141592653589;
00562 
00564   // get HBHE information
00566   std::vector<edm::Handle<HBHERecHitCollection> > hbhe;
00567   iEvent.getManyByType(hbhe);
00568   if (!hbhe[0].isValid()) {
00569     edm::LogWarning(MsgLoggerCat)
00570       << "Unable to find any HBHERecHitCollections in event!";
00571     return;
00572   } 
00573   std::vector<edm::Handle<HBHERecHitCollection> >::iterator ihbhe;
00574      
00575   int iHB = 0;
00576   int iHE = 0; 
00577   for (ihbhe = hbhe.begin(); ihbhe != hbhe.end(); ++ihbhe) {
00578 
00579     // find max values
00580     for (HBHERecHitCollection::const_iterator jhbhe = (*ihbhe)->begin();
00581          jhbhe != (*ihbhe)->end(); ++jhbhe) {
00582 
00583       HcalDetId cell(jhbhe->id());
00584       
00585       if (cell.subdet() == sdHcalBrl) {
00586         
00587         const CaloCellGeometry* cellGeometry =
00588           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00589         double fEta = cellGeometry->getPosition().eta () ;
00590         double fPhi = cellGeometry->getPosition().phi () ;
00591         if ( (jhbhe->energy()) > maxHBEnergy ) {
00592           maxHBEnergy = jhbhe->energy();
00593           maxHBPhi = fPhi;
00594           maxHOPhi = maxHBPhi;
00595           maxHBEta = fEta;
00596           maxHOEta = maxHBEta;
00597         }         
00598       }
00599         
00600       if (cell.subdet() == sdHcalEC) {
00601         
00602         const CaloCellGeometry* cellGeometry =
00603           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00604         double fEta = cellGeometry->getPosition().eta () ;
00605         double fPhi = cellGeometry->getPosition().phi () ;
00606         if ( (jhbhe->energy()) > maxHEEnergy ) {
00607           maxHEEnergy = jhbhe->energy();
00608           maxHEPhi = fPhi;
00609           maxHEEta = fEta;
00610         }         
00611       }
00612     } // end find max values
00613 
00614     for (HBHERecHitCollection::const_iterator jhbhe = (*ihbhe)->begin();
00615          jhbhe != (*ihbhe)->end(); ++jhbhe) {
00616 
00617       HcalDetId cell(jhbhe->id());
00618       
00619       if (cell.subdet() == sdHcalBrl) {
00620 
00621         ++iHB;
00622 
00623         const CaloCellGeometry* cellGeometry =
00624           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00625         double fEta = cellGeometry->getPosition().eta () ;
00626         double fPhi = cellGeometry->getPosition().phi () ;
00627 
00628         float deltaphi = maxHBPhi - fPhi;
00629         if (fPhi > maxHBPhi) { deltaphi = fPhi - maxHBPhi;}
00630         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00631         float deltaeta = fEta - maxHBEta;
00632         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00633 
00634         HBCalREC.push_back(jhbhe->energy());
00635         HBCalR.push_back(r);
00636         HBCalSHE.push_back(fHBEnergySimHits[cell.rawId()]);
00637       }
00638 
00639       if (cell.subdet() == sdHcalEC) {
00640 
00641         ++iHE;
00642 
00643         const CaloCellGeometry* cellGeometry =
00644           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00645         double fEta = cellGeometry->getPosition().eta () ;
00646         double fPhi = cellGeometry->getPosition().phi () ;
00647 
00648         float deltaphi = maxHEPhi - fPhi;
00649         if (fPhi > maxHEPhi) { deltaphi = fPhi - maxHEPhi;}
00650         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00651         float deltaeta = fEta - maxHEEta;
00652         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00653 
00654         HECalREC.push_back(jhbhe->energy());
00655         HECalR.push_back(r);
00656         HECalSHE.push_back(fHEEnergySimHits[cell.rawId()]);
00657       }
00658     }
00659   } // end loop through collection
00660 
00661                                                                       
00662   if (verbosity > 1) {
00663     eventout += "\n          Number of HBRecHits collected:............ ";
00664     eventout += iHB;
00665   }
00666   
00667   if (verbosity > 1) {
00668     eventout += "\n          Number of HERecHits collected:............ ";
00669     eventout += iHE;
00670   }
00671 
00673   // get HF information
00675   std::vector<edm::Handle<HFRecHitCollection> > hf;
00676   iEvent.getManyByType(hf);
00677   if (!hf[0].isValid()) {
00678     edm::LogWarning(MsgLoggerCat)
00679       << "Unable to find any HFRecHitCollections in event!";
00680     return;
00681   } 
00682   std::vector<edm::Handle<HFRecHitCollection> >::iterator ihf;
00683      
00684   int iHF = 0; 
00685   for (ihf = hf.begin(); ihf != hf.end(); ++ihf) {
00686 
00687     // find max values
00688     for (HFRecHitCollection::const_iterator jhf = (*ihf)->begin();
00689          jhf != (*ihf)->end(); ++jhf) {
00690 
00691       HcalDetId cell(jhf->id());
00692       
00693       if (cell.subdet() == sdHcalFwd) {
00694         
00695         const CaloCellGeometry* cellGeometry =
00696           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00697         double fEta = cellGeometry->getPosition().eta () ;
00698         double fPhi = cellGeometry->getPosition().phi () ;
00699         if ( (jhf->energy()) > maxHFEnergy ) {
00700           maxHFEnergy = jhf->energy();
00701           maxHFPhi = fPhi;
00702           maxHFEta = fEta;
00703         }         
00704       }
00705     } // end find max values
00706 
00707     for (HFRecHitCollection::const_iterator jhf = (*ihf)->begin();
00708          jhf != (*ihf)->end(); ++jhf) {
00709 
00710       HcalDetId cell(jhf->id());
00711       
00712       if (cell.subdet() == sdHcalFwd) {
00713 
00714         ++iHF;
00715 
00716         const CaloCellGeometry* cellGeometry =
00717           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00718         double fEta = cellGeometry->getPosition().eta () ;
00719         double fPhi = cellGeometry->getPosition().phi () ;
00720 
00721         float deltaphi = maxHBPhi - fPhi;
00722         if (fPhi > maxHFPhi) { deltaphi = fPhi - maxHFPhi;}
00723         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00724         float deltaeta = fEta - maxHFEta;
00725         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00726 
00727         HFCalREC.push_back(jhf->energy());
00728         HFCalR.push_back(r);
00729         HFCalSHE.push_back(fHFEnergySimHits[cell.rawId()]);
00730       }
00731     }
00732   } // end loop through collection
00733 
00734   if (verbosity > 1) {
00735     eventout += "\n          Number of HFDigis collected:.............. ";
00736     eventout += iHF;
00737   }
00738 
00740   // get HO information
00742   std::vector<edm::Handle<HORecHitCollection> > ho;
00743   iEvent.getManyByType(ho);
00744   if (!ho[0].isValid()) {
00745     edm::LogWarning(MsgLoggerCat)
00746       << "Unable to find any HORecHitCollections in event!";
00747     return;
00748   } 
00749   std::vector<edm::Handle<HORecHitCollection> >::iterator iho;
00750      
00751   int iHO = 0; 
00752   for (iho = ho.begin(); iho != ho.end(); ++iho) {
00753 
00754     for (HORecHitCollection::const_iterator jho = (*iho)->begin();
00755          jho != (*iho)->end(); ++jho) {
00756 
00757       HcalDetId cell(jho->id());
00758       
00759       if (cell.subdet() == sdHcalOut) {
00760 
00761         ++iHO;
00762 
00763         const CaloCellGeometry* cellGeometry =
00764           geometry->getSubdetectorGeometry (cell)->getGeometry (cell) ;
00765         double fEta = cellGeometry->getPosition().eta () ;
00766         double fPhi = cellGeometry->getPosition().phi () ;
00767 
00768         float deltaphi = maxHOPhi - fPhi;
00769         if (fPhi > maxHOPhi) { deltaphi = fPhi - maxHOPhi;}
00770         if (deltaphi > PI) { deltaphi = 2.0 * PI - deltaphi;}
00771         float deltaeta = fEta - maxHOEta;
00772         Double_t r = sqrt(deltaeta * deltaeta + deltaphi * deltaphi);
00773 
00774         HOCalREC.push_back(jho->energy());
00775         HOCalR.push_back(r);
00776         HOCalSHE.push_back(fHOEnergySimHits[cell.rawId()]);
00777       }
00778     }
00779   } // end loop through collection
00780 
00781   if (verbosity > 1) {
00782     eventout += "\n          Number of HODigis collected:.............. ";
00783     eventout += iHO;
00784   }
00785 
00786   if (verbosity > 0)
00787     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00788 
00789   return;
00790 }
00791 
00792 void GlobalRecHitsProducer::storeHCal(PGlobalRecHit& product)
00793 {
00794   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeHCal";
00795 
00796   if (verbosity > 2) {
00797     TString eventout("\n         nHBRecHits     = ");
00798     eventout += HBCalREC.size();
00799     for (unsigned int i = 0; i < HBCalREC.size(); ++i) {
00800       eventout += "\n      (REC, R, SHE) = (";
00801       eventout += HBCalREC[i];
00802       eventout += ", ";
00803       eventout += HBCalR[i];
00804       eventout += ", ";
00805       eventout += HBCalSHE[i];
00806       eventout += ")";
00807     }
00808     eventout += "\n         nHERecHits     = ";
00809     eventout += HECalREC.size();
00810     for (unsigned int i = 0; i < HECalREC.size(); ++i) {
00811       eventout += "\n      (REC, R, SHE) = (";
00812       eventout += HECalREC[i];
00813       eventout += ", ";
00814       eventout += HECalR[i];
00815       eventout += ", ";
00816       eventout += HECalSHE[i];
00817       eventout += ")";
00818     }
00819     eventout += "\n         nHFRecHits     = ";
00820     eventout += HFCalREC.size();
00821     for (unsigned int i = 0; i < HFCalREC.size(); ++i) {
00822       eventout += "\n      (REC, R, SHE) = (";
00823       eventout += HFCalREC[i];
00824       eventout += ", ";
00825       eventout += HFCalR[i];
00826       eventout += ", ";
00827       eventout += HFCalSHE[i];
00828       eventout += ")";
00829     }
00830     eventout += "\n         nHORecHits     = ";
00831     eventout += HOCalREC.size();
00832     for (unsigned int i = 0; i < HOCalREC.size(); ++i) {
00833       eventout += "\n      (REC, R, SHE) = (";
00834       eventout += HOCalREC[i];
00835       eventout += ", ";
00836       eventout += HOCalR[i];
00837       eventout += ", ";
00838       eventout += HOCalSHE[i];
00839       eventout += ")";
00840     }
00841 
00842     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
00843   }
00844 
00845   product.putHBCalRecHits(HBCalREC,HBCalR,HBCalSHE);
00846   product.putHECalRecHits(HECalREC,HECalR,HECalSHE);
00847   product.putHOCalRecHits(HOCalREC,HOCalR,HOCalSHE);
00848   product.putHFCalRecHits(HFCalREC,HFCalR,HFCalSHE);
00849 
00850   return;
00851 }
00852 
00853 void GlobalRecHitsProducer::fillTrk(edm::Event& iEvent, 
00854                                    const edm::EventSetup& iSetup)
00855 {
00856   std::string MsgLoggerCat = "GlobalRecHitsProducer_fillTrk";
00857 
00858   TString eventout;
00859   if (verbosity > 0)
00860     eventout = "\nGathering info:";  
00861 
00862   // get strip information
00863   edm::Handle<SiStripMatchedRecHit2DCollection> rechitsmatched;
00864   iEvent.getByLabel(SiStripSrc_, rechitsmatched);
00865   if (!rechitsmatched.isValid()) {
00866     edm::LogWarning(MsgLoggerCat)
00867       << "Unable to find stripmatchedrechits in event!";
00868     return;
00869   }  
00870 
00871   TrackerHitAssociator associate(iEvent,conf_);
00872 
00873   edm::ESHandle<TrackerGeometry> pDD;
00874   iSetup.get<TrackerDigiGeometryRecord>().get(pDD);
00875   if (!pDD.isValid()) {
00876     edm::LogWarning(MsgLoggerCat)
00877       << "Unable to find TrackerDigiGeometry in event!";
00878     return;
00879   }
00880   const TrackerGeometry &tracker(*pDD);
00881 
00882   int nStripBrl = 0, nStripFwd = 0;
00883 
00884   // loop over det units
00885   for (TrackerGeometry::DetContainer::const_iterator it = pDD->dets().begin();
00886        it != pDD->dets().end(); ++it) {
00887     
00888     uint32_t myid = ((*it)->geographicalId()).rawId();
00889     DetId detid = ((*it)->geographicalId());
00890 
00891     //loop over rechits-matched in the same subdetector
00892     SiStripMatchedRecHit2DCollection::const_iterator rechitmatchedMatch = rechitsmatched->find(detid);
00893       
00894       if (rechitmatchedMatch != rechitsmatched->end()) {
00895       SiStripMatchedRecHit2DCollection::DetSet rechitmatchedRange = *rechitmatchedMatch;
00896       SiStripMatchedRecHit2DCollection::DetSet::const_iterator rechitmatchedRangeIteratorBegin = rechitmatchedRange.begin();
00897       SiStripMatchedRecHit2DCollection::DetSet::const_iterator rechitmatchedRangeIteratorEnd   = rechitmatchedRange.end();
00898       SiStripMatchedRecHit2DCollection::DetSet::const_iterator itermatched = rechitmatchedRangeIteratorBegin;
00899         
00900       for ( itermatched = rechitmatchedRangeIteratorBegin; 
00901             itermatched != rechitmatchedRangeIteratorEnd;
00902             ++itermatched) {
00903 
00904         SiStripMatchedRecHit2D const rechit = *itermatched;
00905         LocalPoint position = rechit.localPosition();
00906         
00907         float mindist = 999999.;
00908         float distx = 999999.;
00909         float disty = 999999.;
00910         float dist = 999999.;
00911         std::pair<LocalPoint,LocalVector> closestPair;
00912         matched.clear();
00913         
00914         float rechitmatchedx = position.x();
00915         float rechitmatchedy = position.y();
00916 
00917         matched = associate.associateHit(rechit);
00918 
00919         if (!matched.empty()) {
00920           //project simhit;
00921           const GluedGeomDet* gluedDet = 
00922             (const GluedGeomDet*)tracker.idToDet(rechit.geographicalId());
00923           const StripGeomDetUnit* partnerstripdet =
00924             (StripGeomDetUnit*) gluedDet->stereoDet();
00925           std::pair<LocalPoint,LocalVector> hitPair;
00926           
00927           for(std::vector<PSimHit>::const_iterator m = matched.begin(); 
00928               m != matched.end(); m++){
00929             //project simhit;
00930             hitPair = projectHit((*m),partnerstripdet,gluedDet->surface());
00931             distx = fabs(rechitmatchedx - hitPair.first.x());
00932             disty = fabs(rechitmatchedy - hitPair.first.y());
00933             dist = sqrt(distx*distx+disty*disty);
00934 
00935             if(dist < mindist){
00936               mindist = dist;
00937               closestPair = hitPair;
00938             }
00939           }
00940           
00941           // get TIB
00942           if (detid.subdetId() == sdSiTIB) {
00943 
00944             TIBDetId tibid(myid);
00945             ++nStripBrl;
00946 
00947             if (tibid.layer() == 1) {
00948               TIBL1RX.push_back(rechitmatchedx);
00949               TIBL1RY.push_back(rechitmatchedy);
00950               TIBL1SX.push_back(closestPair.first.x());
00951               TIBL1SY.push_back(closestPair.first.y());
00952             }
00953             if (tibid.layer() == 2) {
00954               TIBL2RX.push_back(rechitmatchedx);
00955               TIBL2RY.push_back(rechitmatchedy);
00956               TIBL2SX.push_back(closestPair.first.x());
00957               TIBL2SY.push_back(closestPair.first.y());
00958             }   
00959             if (tibid.layer() == 3) {
00960               TIBL3RX.push_back(rechitmatchedx);
00961               TIBL3RY.push_back(rechitmatchedy);
00962               TIBL3SX.push_back(closestPair.first.x());
00963               TIBL3SY.push_back(closestPair.first.y());
00964             }
00965             if (tibid.layer() == 4) {
00966               TIBL4RX.push_back(rechitmatchedx);
00967               TIBL4RY.push_back(rechitmatchedy);
00968               TIBL4SX.push_back(closestPair.first.x());
00969               TIBL4SY.push_back(closestPair.first.y());
00970             }
00971           }
00972     
00973           // get TOB
00974           if (detid.subdetId() == sdSiTOB) {
00975 
00976             TOBDetId tobid(myid);
00977             ++nStripBrl;
00978 
00979             if (tobid.layer() == 1) {
00980               TOBL1RX.push_back(rechitmatchedx);
00981               TOBL1RY.push_back(rechitmatchedy);
00982               TOBL1SX.push_back(closestPair.first.x());
00983               TOBL1SY.push_back(closestPair.first.y());
00984             }
00985             if (tobid.layer() == 2) {
00986               TOBL2RX.push_back(rechitmatchedx);
00987               TOBL2RY.push_back(rechitmatchedy);
00988               TOBL2SX.push_back(closestPair.first.x());
00989               TOBL2SY.push_back(closestPair.first.y());
00990             }   
00991             if (tobid.layer() == 3) {
00992               TOBL3RX.push_back(rechitmatchedx);
00993               TOBL3RY.push_back(rechitmatchedy);
00994               TOBL3SX.push_back(closestPair.first.x());
00995               TOBL3SY.push_back(closestPair.first.y());
00996             }
00997             if (tobid.layer() == 4) {
00998               TOBL4RX.push_back(rechitmatchedx);
00999               TOBL4RY.push_back(rechitmatchedy);
01000               TOBL4SX.push_back(closestPair.first.x());
01001               TOBL4SY.push_back(closestPair.first.y());
01002             }
01003           }
01004 
01005           // get TID
01006           if (detid.subdetId() == sdSiTID) {
01007 
01008             TIDDetId tidid(myid);
01009             ++nStripFwd;
01010 
01011             if (tidid.wheel() == 1) {
01012               TIDW1RX.push_back(rechitmatchedx);
01013               TIDW1RY.push_back(rechitmatchedy);
01014               TIDW1SX.push_back(closestPair.first.x());
01015               TIDW1SY.push_back(closestPair.first.y());
01016             }
01017             if (tidid.wheel() == 2) {
01018               TIDW2RX.push_back(rechitmatchedx);
01019               TIDW2RY.push_back(rechitmatchedy);
01020               TIDW2SX.push_back(closestPair.first.x());
01021               TIDW2SY.push_back(closestPair.first.y());
01022             }   
01023             if (tidid.wheel() == 3) {
01024               TIDW3RX.push_back(rechitmatchedx);
01025               TIDW3RY.push_back(rechitmatchedy);
01026               TIDW3SX.push_back(closestPair.first.x());
01027               TIDW3SY.push_back(closestPair.first.y());
01028             }
01029           }
01030 
01031           // get TEC
01032           if (detid.subdetId() == sdSiTEC) {
01033 
01034             TECDetId tecid(myid);
01035             ++nStripFwd;
01036 
01037             if (tecid.wheel() == 1) {
01038               TECW1RX.push_back(rechitmatchedx);
01039               TECW1RY.push_back(rechitmatchedy);
01040               TECW1SX.push_back(closestPair.first.x());
01041               TECW1SY.push_back(closestPair.first.y());
01042             }
01043             if (tecid.wheel() == 2) {
01044               TECW2RX.push_back(rechitmatchedx);
01045               TECW2RY.push_back(rechitmatchedy);
01046               TECW2SX.push_back(closestPair.first.x());
01047               TECW2SY.push_back(closestPair.first.y());
01048             }   
01049             if (tecid.wheel() == 3) {
01050               TECW3RX.push_back(rechitmatchedx);
01051               TECW3RY.push_back(rechitmatchedy);
01052               TECW3SX.push_back(closestPair.first.x());
01053               TECW3SY.push_back(closestPair.first.y());
01054             }
01055             if (tecid.wheel() == 4) {
01056               TECW4RX.push_back(rechitmatchedx);
01057               TECW4RY.push_back(rechitmatchedy);
01058               TECW4SX.push_back(closestPair.first.x());
01059               TECW4SY.push_back(closestPair.first.y());
01060             }
01061             if (tecid.wheel() == 5) {
01062               TECW5RX.push_back(rechitmatchedx);
01063               TECW5RY.push_back(rechitmatchedy);
01064               TECW5SX.push_back(closestPair.first.x());
01065               TECW5SY.push_back(closestPair.first.y());
01066             }   
01067             if (tecid.wheel() == 6) {
01068               TECW6RX.push_back(rechitmatchedx);
01069               TECW6RY.push_back(rechitmatchedy);
01070               TECW6SX.push_back(closestPair.first.x());
01071               TECW6SY.push_back(closestPair.first.y());
01072             }
01073             if (tecid.wheel() == 7) {
01074               TECW7RX.push_back(rechitmatchedx);
01075               TECW7RY.push_back(rechitmatchedy);
01076               TECW7SX.push_back(closestPair.first.x());
01077               TECW7SY.push_back(closestPair.first.y());
01078             }   
01079             if (tecid.wheel() == 8) {
01080               TECW8RX.push_back(rechitmatchedx);
01081               TECW8RY.push_back(rechitmatchedy);
01082               TECW8SX.push_back(closestPair.first.x());
01083               TECW8SY.push_back(closestPair.first.y());
01084             }
01085           }
01086 
01087         } // end if matched empty
01088       }
01089     }
01090   } // end loop over det units
01091                                                                       
01092   if (verbosity > 1) {
01093     eventout += "\n          Number of BrlStripRecHits collected:...... ";
01094     eventout += nStripBrl;
01095   }
01096 
01097   if (verbosity > 1) {
01098     eventout += "\n          Number of FrwdStripRecHits collected:..... ";
01099     eventout += nStripFwd;
01100   }
01101 
01102   // get pixel information
01103   //Get RecHits
01104   edm::Handle<SiPixelRecHitCollection> recHitColl;
01105   iEvent.getByLabel(SiPxlSrc_, recHitColl);
01106   if (!recHitColl.isValid()) {
01107     edm::LogWarning(MsgLoggerCat)
01108       << "Unable to find SiPixelRecHitCollection in event!";
01109     return;
01110   }  
01111   
01112   //Get event setup
01113   edm::ESHandle<TrackerGeometry> geom;
01114   iSetup.get<TrackerDigiGeometryRecord>().get(geom); 
01115   if (!geom.isValid()) {
01116     edm::LogWarning(MsgLoggerCat)
01117       << "Unable to find TrackerDigiGeometry in event!";
01118     return;
01119   }
01120   //const TrackerGeometry& theTracker(*geom);
01121 
01122   int nPxlBrl = 0, nPxlFwd = 0;    
01123   //iterate over detunits
01124   for (TrackerGeometry::DetContainer::const_iterator it = geom->dets().begin();
01125        it != geom->dets().end(); ++it) {
01126 
01127     uint32_t myid = ((*it)->geographicalId()).rawId();
01128     DetId detId = ((*it)->geographicalId());
01129     int subid = detId.subdetId();
01130     
01131     if (! ((subid == sdPxlBrl) || (subid == sdPxlFwd))) continue;
01132     
01133     //const PixelGeomDetUnit * theGeomDet = 
01134     //  dynamic_cast<const PixelGeomDetUnit*>(theTracker.idToDet(detId) );
01135     
01136     SiPixelRecHitCollection::const_iterator pixeldet = recHitColl->find(detId);
01137     if (pixeldet == recHitColl->end()) continue;
01138     SiPixelRecHitCollection::DetSet pixelrechitRange = *pixeldet;
01139     SiPixelRecHitCollection::DetSet::const_iterator pixelrechitRangeIteratorBegin = pixelrechitRange.begin();
01140     SiPixelRecHitCollection::DetSet::const_iterator pixelrechitRangeIteratorEnd   = pixelrechitRange.end();
01141     SiPixelRecHitCollection::DetSet::const_iterator pixeliter = pixelrechitRangeIteratorBegin;
01142     std::vector<PSimHit> matched;
01143     
01144     //----Loop over rechits for this detId
01145     for ( ; pixeliter != pixelrechitRangeIteratorEnd; ++pixeliter) {
01146 
01147       matched.clear();
01148       matched = associate.associateHit(*pixeliter);
01149       
01150       if ( !matched.empty() ) {
01151 
01152         float closest = 9999.9;
01153         //std::vector<PSimHit>::const_iterator closestit = matched.begin();
01154         LocalPoint lp = pixeliter->localPosition();
01155         float rechit_x = lp.x();
01156         float rechit_y = lp.y();
01157 
01158         float sim_x = 0.;
01159         float sim_y = 0.;
01160         
01161         //loop over sim hits and fill closet
01162         for (std::vector<PSimHit>::const_iterator m = matched.begin(); 
01163              m != matched.end(); ++m) {
01164 
01165           float sim_x1 = (*m).entryPoint().x();
01166           float sim_x2 = (*m).exitPoint().x();
01167           float sim_xpos = 0.5*(sim_x1+sim_x2);
01168           
01169           float sim_y1 = (*m).entryPoint().y();
01170           float sim_y2 = (*m).exitPoint().y();
01171           float sim_ypos = 0.5*(sim_y1+sim_y2);
01172           
01173           float x_res = fabs(sim_xpos - rechit_x);
01174           float y_res = fabs(sim_ypos - rechit_y);
01175           
01176           float dist = sqrt(x_res*x_res + y_res*y_res);
01177           
01178           if ( dist < closest ) {
01179             closest = dist;
01180             sim_x = sim_xpos;
01181             sim_y = sim_ypos;
01182           }
01183         } // end sim hit loop
01184         
01185         // get Barrel pixels
01186         if (subid == sdPxlBrl) {
01187           PXBDetId bdetid(myid);
01188           ++nPxlBrl;
01189 
01190           if (bdetid.layer() == 1) {
01191             BRL1RX.push_back(rechit_x);
01192             BRL1RY.push_back(rechit_y);
01193             BRL1SX.push_back(sim_x);
01194             BRL1SY.push_back(sim_y);      
01195           }
01196           if (bdetid.layer() == 2) {
01197             BRL2RX.push_back(rechit_x);
01198             BRL2RY.push_back(rechit_y);
01199             BRL2SX.push_back(sim_x);
01200             BRL2SY.push_back(sim_y);              
01201           }
01202           if (bdetid.layer() == 3) {
01203             BRL3RX.push_back(rechit_x);
01204             BRL3RY.push_back(rechit_y);
01205             BRL3SX.push_back(sim_x);
01206             BRL3SY.push_back(sim_y);              
01207           }
01208         }
01209 
01210         // get Forward pixels
01211         if (subid == sdPxlFwd) {
01212           PXFDetId fdetid(myid);
01213           ++nPxlFwd;
01214 
01215           if (fdetid.disk() == 1) {
01216             if (fdetid.side() == 1) {
01217               FWD1nRX.push_back(rechit_x);
01218               FWD1nRY.push_back(rechit_y);
01219               FWD1nSX.push_back(sim_x);
01220               FWD1nSY.push_back(sim_y);   
01221             }
01222             if (fdetid.side() == 2) {
01223               FWD1pRX.push_back(rechit_x);
01224               FWD1pRY.push_back(rechit_y);
01225               FWD1pSX.push_back(sim_x);
01226               FWD1pSY.push_back(sim_y);
01227             }
01228           }
01229           if (fdetid.disk() == 2) {
01230             if (fdetid.side() == 1) {
01231               FWD2nRX.push_back(rechit_x);
01232               FWD2nRY.push_back(rechit_y);
01233               FWD2nSX.push_back(sim_x);
01234               FWD2nSY.push_back(sim_y);
01235             }
01236             if (fdetid.side() == 2) {
01237               FWD2pRX.push_back(rechit_x);
01238               FWD2pRY.push_back(rechit_y);
01239               FWD2pSX.push_back(sim_x);
01240               FWD2pSY.push_back(sim_y);
01241             }
01242           }
01243         }      
01244       } // end matched emtpy
01245     } // <-----end rechit loop 
01246   } // <------ end detunit loop  
01247 
01248                      
01249   if (verbosity > 1) {
01250     eventout += "\n          Number of BrlPixelRecHits collected:...... ";
01251     eventout += nPxlBrl;
01252   }
01253 
01254   if (verbosity > 1) {
01255     eventout += "\n          Number of FrwdPixelRecHits collected:..... ";
01256     eventout += nPxlFwd;
01257   }
01258 
01259   if (verbosity > 0)
01260     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
01261 
01262   return;
01263 }
01264 
01265 void GlobalRecHitsProducer::storeTrk(PGlobalRecHit& product)
01266 {
01267   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeTrk";
01268 
01269   if (verbosity > 2) {
01270     
01271     // strip output
01272     TString eventout("\n         nTIBL1     = ");
01273     eventout += TIBL1RX.size();
01274     for (unsigned int i = 0; i < TIBL1RX.size(); ++i) {
01275       eventout += "\n      (RX, RY, SX, SY) = (";
01276       eventout += TIBL1RX[i];
01277       eventout += ", ";
01278       eventout += TIBL1RY[i];
01279       eventout += ", ";
01280       eventout += TIBL1SX[i];
01281       eventout += ", ";
01282       eventout += TIBL1SY[i];
01283       eventout += ")";
01284     }
01285     eventout += "\n         nTIBL2     = ";
01286     eventout += TIBL2RX.size();
01287     for (unsigned int i = 0; i < TIBL2RX.size(); ++i) {
01288       eventout += "\n      (RX, RY, SX, SY) = (";
01289       eventout += TIBL2RX[i];
01290       eventout += ", ";
01291       eventout += TIBL2RY[i];
01292       eventout += ", ";
01293       eventout += TIBL2SX[i];
01294       eventout += ", ";
01295       eventout += TIBL2SY[i];
01296       eventout += ")";
01297     }
01298     eventout += "\n         nTIBL3     = ";
01299     eventout += TIBL3RX.size();
01300     for (unsigned int i = 0; i < TIBL3RX.size(); ++i) {
01301       eventout += "\n      (RX, RY, SX, SY) = (";
01302       eventout += TIBL3RX[i];
01303       eventout += ", ";
01304       eventout += TIBL3RY[i];
01305       eventout += ", ";
01306       eventout += TIBL3SX[i];
01307       eventout += ", ";
01308       eventout += TIBL3SY[i];
01309       eventout += ")";
01310     }
01311     eventout += "\n         nTIBL4     = ";
01312     eventout += TIBL4RX.size();
01313     for (unsigned int i = 0; i < TIBL4RX.size(); ++i) {
01314       eventout += "\n      (RX, RY, SX, SY) = (";
01315       eventout += TIBL4RX[i];
01316       eventout += ", ";
01317       eventout += TIBL4RY[i];
01318       eventout += ", ";
01319       eventout += TIBL4SX[i];
01320       eventout += ", ";
01321       eventout += TIBL4SY[i];
01322       eventout += ")";
01323     }
01324     eventout += "\n         nTOBL1     = ";
01325     eventout += TOBL1RX.size();
01326     for (unsigned int i = 0; i < TOBL1RX.size(); ++i) {
01327       eventout += "\n      (RX, RY, SX, SY) = (";
01328       eventout += TOBL1RX[i];
01329       eventout += ", ";
01330       eventout += TOBL1RY[i];
01331       eventout += ", ";
01332       eventout += TOBL1SX[i];
01333       eventout += ", ";
01334       eventout += TOBL1SY[i];
01335       eventout += ")";
01336     }
01337     eventout += "\n         nTOBL2     = ";
01338     eventout += TOBL2RX.size();
01339     for (unsigned int i = 0; i < TOBL2RX.size(); ++i) {
01340       eventout += "\n      (RX, RY, SX, SY) = (";
01341       eventout += TOBL2RX[i];
01342       eventout += ", ";
01343       eventout += TOBL2RY[i];
01344       eventout += ", ";
01345       eventout += TOBL2SX[i];
01346       eventout += ", ";
01347       eventout += TOBL2SY[i];
01348       eventout += ")";
01349     }
01350     eventout += "\n         nTOBL3     = ";
01351     eventout += TOBL3RX.size();
01352     for (unsigned int i = 0; i < TOBL3RX.size(); ++i) {
01353       eventout += "\n      (RX, RY, SX, SY) = (";
01354       eventout += TOBL3RX[i];
01355       eventout += ", ";
01356       eventout += TOBL3RY[i];
01357       eventout += ", ";
01358       eventout += TOBL3SX[i];
01359       eventout += ", ";
01360       eventout += TOBL3SY[i];
01361       eventout += ")";
01362     }
01363     eventout += "\n         nTOBL4     = ";
01364     eventout += TOBL4RX.size();
01365     for (unsigned int i = 0; i < TOBL4RX.size(); ++i) {
01366       eventout += "\n      (RX, RY, SX, SY) = (";
01367       eventout += TOBL4RX[i];
01368       eventout += ", ";
01369       eventout += TOBL4RY[i];
01370       eventout += ", ";
01371       eventout += TOBL4SX[i];
01372       eventout += ", ";
01373       eventout += TOBL4SY[i];
01374       eventout += ")";
01375     }
01376     eventout += "\n         nTIDW1     = ";
01377     eventout += TIDW1RX.size();
01378     for (unsigned int i = 0; i < TIDW1RX.size(); ++i) {
01379       eventout += "\n      (RX, RY, SX, SY) = (";
01380       eventout += TIDW1RX[i];
01381       eventout += ", ";
01382       eventout += TIDW1RY[i];
01383       eventout += ", ";
01384       eventout += TIDW1SX[i];
01385       eventout += ", ";
01386       eventout += TIDW1SY[i];
01387       eventout += ")";
01388     }
01389     eventout += "\n         nTIDW2     = ";
01390     eventout += TIDW2RX.size();
01391     for (unsigned int i = 0; i < TIDW2RX.size(); ++i) {
01392       eventout += "\n      (RX, RY, SX, SY) = (";
01393       eventout += TIDW2RX[i];
01394       eventout += ", ";
01395       eventout += TIDW2RY[i];
01396       eventout += ", ";
01397       eventout += TIDW2SX[i];
01398       eventout += ", ";
01399       eventout += TIDW2SY[i];
01400       eventout += ")";
01401     }
01402     eventout += "\n         nTIDW3     = ";
01403     eventout += TIDW3RX.size();
01404     for (unsigned int i = 0; i < TIDW3RX.size(); ++i) {
01405       eventout += "\n      (RX, RY, SX, SY) = (";
01406       eventout += TIDW3RX[i];
01407       eventout += ", ";
01408       eventout += TIDW3RY[i];
01409       eventout += ", ";
01410       eventout += TIDW3SX[i];
01411       eventout += ", ";
01412       eventout += TIDW3SY[i];
01413       eventout += ")";
01414     }
01415     eventout += "\n         nTECW1     = ";
01416     eventout += TECW1RX.size();
01417     for (unsigned int i = 0; i < TECW1RX.size(); ++i) {
01418       eventout += "\n      (RX, RY, SX, SY) = (";
01419       eventout += TECW1RX[i];
01420       eventout += ", ";
01421       eventout += TECW1RY[i];
01422       eventout += ", ";
01423       eventout += TECW1SX[i];
01424       eventout += ", ";
01425       eventout += TECW1SY[i];
01426       eventout += ")";
01427     }
01428     eventout += "\n         nTECW2     = ";
01429     eventout += TECW2RX.size();
01430     for (unsigned int i = 0; i < TECW2RX.size(); ++i) {
01431       eventout += "\n      (RX, RY, SX, SY) = (";
01432       eventout += TECW2RX[i];
01433       eventout += ", ";
01434       eventout += TECW2RY[i];
01435       eventout += ", ";
01436       eventout += TECW2SX[i];
01437       eventout += ", ";
01438       eventout += TECW2SY[i];
01439       eventout += ")";
01440     }
01441     eventout += "\n         nTECW3     = ";
01442     eventout += TECW3RX.size();
01443     for (unsigned int i = 0; i < TECW3RX.size(); ++i) {
01444       eventout += "\n      (RX, RY, SX, SY) = (";
01445       eventout += TECW3RX[i];
01446       eventout += ", ";
01447       eventout += TECW3RY[i];
01448       eventout += ", ";
01449       eventout += TECW3SX[i];
01450       eventout += ", ";
01451       eventout += TECW3SY[i];
01452       eventout += ")";
01453     }
01454     eventout += "\n         nTECW4     = ";
01455     eventout += TECW4RX.size();
01456     for (unsigned int i = 0; i < TECW4RX.size(); ++i) {
01457       eventout += "\n      (RX, RY, SX, SY) = (";
01458       eventout += TECW4RX[i];
01459       eventout += ", ";
01460       eventout += TECW4RY[i];
01461       eventout += ", ";
01462       eventout += TECW4SX[i];
01463       eventout += ", ";
01464       eventout += TECW4SY[i];
01465       eventout += ")";
01466     }
01467     eventout += "\n         nTECW5     = ";
01468     eventout += TECW5RX.size();
01469     for (unsigned int i = 0; i < TECW5RX.size(); ++i) {
01470       eventout += "\n      (RX, RY, SX, SY) = (";
01471       eventout += TECW5RX[i];
01472       eventout += ", ";
01473       eventout += TECW5RY[i];
01474       eventout += ", ";
01475       eventout += TECW5SX[i];
01476       eventout += ", ";
01477       eventout += TECW5SY[i];
01478       eventout += ")";
01479     }
01480     eventout += "\n         nTECW6     = ";
01481     eventout += TECW6RX.size();
01482     for (unsigned int i = 0; i < TECW6RX.size(); ++i) {
01483       eventout += "\n      (RX, RY, SX, SY) = (";
01484       eventout += TECW6RX[i];
01485       eventout += ", ";
01486       eventout += TECW6RY[i];
01487       eventout += ", ";
01488       eventout += TECW6SX[i];
01489       eventout += ", ";
01490       eventout += TECW6SY[i];
01491       eventout += ")";
01492     }
01493     eventout += "\n         nTECW7     = ";
01494     eventout += TECW7RX.size();
01495     for (unsigned int i = 0; i < TECW7RX.size(); ++i) {
01496       eventout += "\n      (RX, RY, SX, SY) = (";
01497       eventout += TECW7RX[i];
01498       eventout += ", ";
01499       eventout += TECW7RY[i];
01500       eventout += ", ";
01501       eventout += TECW7SX[i];
01502       eventout += ", ";
01503       eventout += TECW7SY[i];
01504       eventout += ")";
01505     }
01506     eventout += "\n         nTECW8     = ";
01507     eventout += TECW8RX.size();
01508     for (unsigned int i = 0; i < TECW8RX.size(); ++i) {
01509       eventout += "\n      (RX, RY, SX, SY) = (";
01510       eventout += TECW8RX[i];
01511       eventout += ", ";
01512       eventout += TECW8RY[i];
01513       eventout += ", ";
01514       eventout += TECW8SX[i];
01515       eventout += ", ";
01516       eventout += TECW8SY[i];
01517       eventout += ")";
01518     }
01519 
01520     // pixel output
01521     eventout += "\n         nBRL1     = ";
01522     eventout += BRL1RX.size();
01523     for (unsigned int i = 0; i < BRL1RX.size(); ++i) {
01524       eventout += "\n      (RX, RY, SX, SY) = (";
01525       eventout += BRL1RX[i];
01526       eventout += ", ";
01527       eventout += BRL1RY[i];
01528       eventout += ", ";
01529       eventout += BRL1SX[i];
01530       eventout += ", ";
01531       eventout += BRL1SY[i];
01532       eventout += ")";
01533     } 
01534     eventout += "\n         nBRL2     = ";
01535     eventout += BRL2RX.size();
01536     for (unsigned int i = 0; i < BRL2RX.size(); ++i) {
01537       eventout += "\n      (RX, RY, SX, SY) = (";
01538       eventout += BRL2RX[i];
01539       eventout += ", ";
01540       eventout += BRL2RY[i];
01541       eventout += ", ";
01542       eventout += BRL2SX[i];
01543       eventout += ", ";
01544       eventout += BRL2SY[i];
01545       eventout += ")";
01546     } 
01547     eventout += "\n         nBRL3     = ";
01548     eventout += BRL3RX.size();
01549     for (unsigned int i = 0; i < BRL3RX.size(); ++i) {
01550       eventout += "\n      (RX, RY, SX, SY) = (";
01551       eventout += BRL3RX[i];
01552       eventout += ", ";
01553       eventout += BRL3RY[i];
01554       eventout += ", ";
01555       eventout += BRL3SX[i];
01556       eventout += ", ";
01557       eventout += BRL3SY[i];
01558       eventout += ")";
01559     }    
01560     eventout += "\n         nFWD1p     = ";
01561     eventout += FWD1pRX.size();
01562     for (unsigned int i = 0; i < FWD1pRX.size(); ++i) {
01563       eventout += "\n      (RX, RY, SX, SY) = (";
01564       eventout += FWD1pRX[i];
01565       eventout += ", ";
01566       eventout += FWD1pRY[i];
01567       eventout += ", ";
01568       eventout += FWD1pSX[i];
01569       eventout += ", ";
01570       eventout += FWD1pSY[i];
01571       eventout += ")";
01572     } 
01573     eventout += "\n         nFWD1n     = ";
01574     eventout += FWD1nRX.size();
01575     for (unsigned int i = 0; i < FWD1nRX.size(); ++i) {
01576       eventout += "\n      (RX, RY, SX, SY) = (";
01577       eventout += FWD1nRX[i];
01578       eventout += ", ";
01579       eventout += FWD1nRY[i];
01580       eventout += ", ";
01581       eventout += FWD1nSX[i];
01582       eventout += ", ";
01583       eventout += FWD1nSY[i];
01584       eventout += ")";
01585     } 
01586     eventout += "\n         nFWD2p     = ";
01587     eventout += FWD2pRX.size();
01588     for (unsigned int i = 0; i < FWD2pRX.size(); ++i) {
01589       eventout += "\n      (RX, RY, SX, SY) = (";
01590       eventout += FWD2pRX[i];
01591       eventout += ", ";
01592       eventout += FWD2pRY[i];
01593       eventout += ", ";
01594       eventout += FWD2pSX[i];
01595       eventout += ", ";
01596       eventout += FWD2pSY[i];
01597       eventout += ")";
01598     }
01599     eventout += "\n         nFWD2p     = ";
01600     eventout += FWD2nRX.size();
01601     for (unsigned int i = 0; i < FWD2nRX.size(); ++i) {
01602       eventout += "\n      (RX, RY, SX, SY) = (";
01603       eventout += FWD2nRX[i];
01604       eventout += ", ";
01605       eventout += FWD2nRY[i];
01606       eventout += ", ";
01607       eventout += FWD2nSX[i];
01608       eventout += ", ";
01609       eventout += FWD2nSY[i];
01610       eventout += ")";
01611     } 
01612 
01613     edm::LogInfo(MsgLoggerCat) << eventout << "\n";  
01614   }
01615 
01616   // strip output
01617   product.putTIBL1RecHits(TIBL1RX,TIBL1RY,TIBL1SX,TIBL1SY);
01618   product.putTIBL2RecHits(TIBL2RX,TIBL2RY,TIBL2SX,TIBL2SY);
01619   product.putTIBL3RecHits(TIBL3RX,TIBL3RY,TIBL3SX,TIBL3SY);
01620   product.putTIBL4RecHits(TIBL4RX,TIBL4RY,TIBL4SX,TIBL4SY);
01621   product.putTOBL1RecHits(TOBL1RX,TOBL1RY,TOBL1SX,TOBL1SY);
01622   product.putTOBL2RecHits(TOBL2RX,TOBL2RY,TOBL2SX,TOBL2SY);
01623   product.putTOBL3RecHits(TOBL3RX,TOBL3RY,TOBL3SX,TOBL3SY);
01624   product.putTOBL4RecHits(TOBL4RX,TOBL4RY,TOBL4SX,TOBL4SY);
01625   product.putTIDW1RecHits(TIDW1RX,TIDW1RY,TIDW1SX,TIDW1SY);
01626   product.putTIDW2RecHits(TIDW2RX,TIDW2RY,TIDW2SX,TIDW2SY);
01627   product.putTIDW3RecHits(TIDW3RX,TIDW3RY,TIDW3SX,TIDW3SY);
01628   product.putTECW1RecHits(TECW1RX,TECW1RY,TECW1SX,TECW1SY);
01629   product.putTECW2RecHits(TECW2RX,TECW2RY,TECW2SX,TECW2SY);
01630   product.putTECW3RecHits(TECW3RX,TECW3RY,TECW3SX,TECW3SY);
01631   product.putTECW4RecHits(TECW4RX,TECW4RY,TECW4SX,TECW4SY);
01632   product.putTECW5RecHits(TECW5RX,TECW5RY,TECW5SX,TECW5SY);
01633   product.putTECW6RecHits(TECW6RX,TECW6RY,TECW6SX,TECW6SY);  
01634   product.putTECW7RecHits(TECW7RX,TECW7RY,TECW7SX,TECW7SY);
01635   product.putTECW8RecHits(TECW8RX,TECW8RY,TECW8SX,TECW8SY);  
01636 
01637   // pixel output
01638   product.putBRL1RecHits(BRL1RX,BRL1RY,BRL1SX,BRL1SY);
01639   product.putBRL2RecHits(BRL2RX,BRL2RY,BRL2SX,BRL2SY);
01640   product.putBRL3RecHits(BRL3RX,BRL3RY,BRL3SX,BRL3SY);
01641   product.putFWD1pRecHits(FWD1pRX,FWD1pRY,FWD1pSX,FWD1pSY);
01642   product.putFWD1nRecHits(FWD1nRX,FWD1nRY,FWD1nSX,FWD1nSY);
01643   product.putFWD2pRecHits(FWD2pRX,FWD2pRY,FWD2pSX,FWD2pSY);
01644   product.putFWD2nRecHits(FWD2nRX,FWD2nRY,FWD2nSX,FWD2nSY);
01645 
01646   return;
01647 }
01648 
01649 void GlobalRecHitsProducer::fillMuon(edm::Event& iEvent, 
01650                                    const edm::EventSetup& iSetup)
01651 {
01652   std::string MsgLoggerCat = "GlobalRecHitsProducer_fillMuon";
01653   
01654   TString eventout;
01655   if (verbosity > 0)
01656     eventout = "\nGathering info:";  
01657 
01658   // get DT information
01659   edm::ESHandle<DTGeometry> dtGeom;
01660   iSetup.get<MuonGeometryRecord>().get(dtGeom);
01661   if (!dtGeom.isValid()) {
01662     edm::LogWarning(MsgLoggerCat)
01663       << "Unable to find DTMuonGeometryRecord in event!";
01664     return;
01665   }  
01666 
01667   edm::Handle<edm::PSimHitContainer> dtsimHits;
01668   iEvent.getByLabel(MuDTSimSrc_, dtsimHits);
01669   if (!dtsimHits.isValid()) {
01670     edm::LogWarning(MsgLoggerCat)
01671       << "Unable to find dtsimHits in event!";
01672     return;
01673   } 
01674 
01675   std::map<DTWireId, edm::PSimHitContainer> simHitsPerWire =
01676     DTHitQualityUtils::mapSimHitsPerWire(*(dtsimHits.product()));
01677 
01678   edm::Handle<DTRecHitCollection> dtRecHits;
01679   iEvent.getByLabel(MuDTSrc_, dtRecHits);
01680   if (!dtRecHits.isValid()) {
01681     edm::LogWarning(MsgLoggerCat)
01682       << "Unable to find dtRecHits in event!";
01683     return;
01684   }   
01685 
01686   std::map<DTWireId, std::vector<DTRecHit1DPair> > recHitsPerWire =
01687     map1DRecHitsPerWire(dtRecHits.product());
01688 
01689 
01690   int nDt = compute(dtGeom.product(), simHitsPerWire, recHitsPerWire, 1);
01691                                                                     
01692   if (verbosity > 1) {
01693     eventout += "\n          Number of DtMuonRecHits collected:........ ";
01694     eventout += nDt;
01695   }
01696 
01697   // get CSC Strip information
01698   // get map of sim hits
01699   theMap.clear();
01700   //edm::Handle<CrossingFrame> cf;
01701   edm::Handle<CrossingFrame<PSimHit> > cf;
01702   //iEvent.getByType(cf);
01703   //if (!cf.isValid()) {
01704   //  edm::LogWarning(MsgLoggerCat)
01705   //    << "Unable to find CrossingFrame in event!";
01706   //  return;
01707   //}    
01708   //MixCollection<PSimHit> simHits(cf.product(), "MuonCSCHits");
01709   iEvent.getByLabel("mix","MuonCSCHits",cf);
01710   if (!cf.isValid()) {
01711     edm::LogWarning(MsgLoggerCat)
01712       << "Unable to find muo CSC  crossingFrame in event!";
01713     return;
01714   }
01715   MixCollection<PSimHit> simHits(cf.product());
01716 
01717   // arrange the hits by detUnit
01718   for(MixCollection<PSimHit>::MixItr hitItr = simHits.begin();
01719       hitItr != simHits.end(); ++hitItr)
01720   {
01721     theMap[hitItr->detUnitId()].push_back(*hitItr);
01722   }  
01723 
01724   // get geometry
01725   edm::ESHandle<CSCGeometry> hGeom;
01726   iSetup.get<MuonGeometryRecord>().get(hGeom);
01727   if (!hGeom.isValid()) {
01728     edm::LogWarning(MsgLoggerCat)
01729       << "Unable to find CSCMuonGeometryRecord in event!";
01730     return;
01731   }    
01732   const CSCGeometry *theCSCGeometry = &*hGeom;
01733 
01734   // get rechits
01735   edm::Handle<CSCRecHit2DCollection> hRecHits;
01736   iEvent.getByLabel(MuCSCSrc_, hRecHits);
01737   if (!hRecHits.isValid()) {
01738     edm::LogWarning(MsgLoggerCat)
01739       << "Unable to find CSC RecHits in event!";
01740     return;
01741   }    
01742   const CSCRecHit2DCollection *cscRecHits = hRecHits.product();
01743 
01744   int nCSC = 0;
01745   for (CSCRecHit2DCollection::const_iterator recHitItr = cscRecHits->begin();
01746        recHitItr != cscRecHits->end(); ++recHitItr) {
01747 
01748     int detId = (*recHitItr).cscDetId().rawId();
01749  
01750     edm::PSimHitContainer simHits;   
01751     std::map<int, edm::PSimHitContainer>::const_iterator mapItr = 
01752       theMap.find(detId);
01753     if (mapItr != theMap.end()) {
01754       simHits = mapItr->second;
01755     }
01756 
01757     if (simHits.size() == 1) {
01758       ++nCSC;
01759 
01760       const GeomDetUnit* detUnit = 
01761         theCSCGeometry->idToDetUnit(CSCDetId(detId));
01762       const CSCLayer *layer = dynamic_cast<const CSCLayer *>(detUnit); 
01763 
01764      int chamberType = layer->chamber()->specs()->chamberType();
01765       plotResolution(simHits[0], *recHitItr, layer, chamberType);
01766     }
01767   }
01768                                                 
01769   if (verbosity > 1) {
01770     eventout += "\n          Number of CSCRecHits collected:........... ";
01771     eventout += nCSC;
01772   }
01773 
01774   // get RPC information
01775   std::map<double, int> mapsim, maprec;
01776   std::map<int, double> nmapsim, nmaprec;
01777 
01778   edm::ESHandle<RPCGeometry> rpcGeom;
01779   iSetup.get<MuonGeometryRecord>().get(rpcGeom);
01780   if (!rpcGeom.isValid()) {
01781     edm::LogWarning(MsgLoggerCat)
01782       << "Unable to find RPCMuonGeometryRecord in event!";
01783     return;
01784   }  
01785 
01786   edm::Handle<edm::PSimHitContainer> simHit;
01787   iEvent.getByLabel(MuRPCSimSrc_, simHit);
01788   if (!simHit.isValid()) {
01789     edm::LogWarning(MsgLoggerCat)
01790       << "Unable to find RPCSimHit in event!";
01791     return;
01792   }    
01793 
01794   edm::Handle<RPCRecHitCollection> recHit;
01795   iEvent.getByLabel(MuRPCSrc_, recHit);
01796   if (!simHit.isValid()) {
01797     edm::LogWarning(MsgLoggerCat)
01798       << "Unable to find RPCRecHit in event!";
01799     return;
01800   } 
01801 
01802   int nRPC = 0;
01803   RPCRecHitCollection::const_iterator recIt;
01804   int nrec = 0;
01805   for (recIt = recHit->begin(); recIt != recHit->end(); ++recIt) {
01806     RPCDetId Rid = (RPCDetId)(*recIt).rpcId();
01807     const RPCRoll *roll = dynamic_cast<const RPCRoll*>(rpcGeom->roll(Rid));
01808     if (roll->isForward()) {
01809 
01810       if (verbosity > 1) {
01811         eventout += "\n          Number of RPCRecHits collected:........... ";
01812         eventout += nRPC;
01813       }
01814       
01815       if (verbosity > 0)
01816         edm::LogInfo(MsgLoggerCat) << eventout << "\n";
01817       return;
01818     }
01819     nrec = nrec + 1;
01820     LocalPoint rhitlocal = (*recIt).localPosition();
01821     double rhitlocalx = rhitlocal.x();
01822     maprec[rhitlocalx] = nrec; 
01823   }
01824 
01825   int i = 0;
01826   for (std::map<double,int>::iterator iter = maprec.begin();
01827        iter != maprec.end(); ++iter) {
01828     i = i + 1;
01829     nmaprec[i] = (*iter).first;
01830   }
01831                   
01832   edm::PSimHitContainer::const_iterator simIt;
01833   int nsim = 0;
01834   for (simIt = simHit->begin(); simIt != simHit->end(); simIt++) {
01835     int ptype = (*simIt).particleType();
01836     //RPCDetId Rsid = (RPCDetId)(*simIt).detUnitId();
01837     if (ptype == 13 || ptype == -13) {
01838       nsim = nsim + 1;
01839       LocalPoint shitlocal = (*simIt).localPosition();
01840       double shitlocalx = shitlocal.x();
01841       mapsim[shitlocalx] = nsim;
01842     }
01843   }
01844 
01845   i = 0;
01846   for (std::map<double,int>::iterator iter = mapsim.begin();
01847        iter != mapsim.end(); ++iter) {
01848     i = i + 1;
01849     nmapsim[i] = (*iter).first;
01850   }
01851 
01852   if (nsim == nrec) {
01853     for (int r = 0; r < nsim; r++) {
01854       ++nRPC;
01855       RPCRHX.push_back(nmaprec[r+1]);
01856       RPCSHX.push_back(nmapsim[r+1]);
01857     }
01858   }
01859                                                                   
01860   if (verbosity > 1) {
01861     eventout += "\n          Number of RPCRecHits collected:........... ";
01862     eventout += nRPC;
01863   }
01864 
01865   if (verbosity > 0)
01866     edm::LogInfo(MsgLoggerCat) << eventout << "\n";
01867   
01868   return;
01869 }
01870 
01871 void GlobalRecHitsProducer::storeMuon(PGlobalRecHit& product)
01872 {
01873   std::string MsgLoggerCat = "GlobalRecHitsProducer_storeMuon";
01874 
01875   if (verbosity > 2) {
01876 
01877     // dt output
01878     TString eventout("\n         nDT     = ");
01879     eventout += DTRHD.size();
01880     for (unsigned int i = 0; i < DTRHD.size(); ++i) {
01881       eventout += "\n      (RHD, SHD) = (";
01882       eventout += DTRHD[i];
01883       eventout += ", ";
01884       eventout += DTSHD[i];
01885       eventout += ")";
01886     }
01887 
01888     // CSC Strip
01889     eventout += "\n         nCSC     = ";
01890     eventout += CSCRHPHI.size();
01891     for (unsigned int i = 0; i < CSCRHPHI.size(); ++i) {
01892       eventout += "\n      (rhphi, rhperp, shphi) = (";
01893       eventout += CSCRHPHI[i];
01894       eventout += ", ";
01895       eventout += CSCRHPERP[i];
01896       eventout += ", ";
01897       eventout += CSCSHPHI[i];
01898       eventout += ")";
01899     }    
01900 
01901     // RPC
01902     eventout += "\n         nRPC     = ";
01903     eventout += RPCRHX.size();
01904     for (unsigned int i = 0; i < RPCRHX.size(); ++i) {
01905       eventout += "\n      (rhx, shx) = (";
01906       eventout += RPCRHX[i];
01907       eventout += ", ";
01908       eventout += RPCSHX[i];
01909       eventout += ")";
01910     }    
01911 
01912     edm::LogInfo(MsgLoggerCat) << eventout << "\n";  
01913   }
01914   
01915   product.putDTRecHits(DTRHD,DTSHD);
01916 
01917   product.putCSCRecHits(CSCRHPHI,CSCRHPERP,CSCSHPHI);
01918 
01919   product.putRPCRecHits(RPCRHX,RPCSHX);
01920 
01921   return;
01922 }
01923 
01924 void GlobalRecHitsProducer::clear()
01925 {
01926   std::string MsgLoggerCat = "GlobalRecHitsProducer_clear";
01927 
01928   if (verbosity > 0)
01929     edm::LogInfo(MsgLoggerCat)
01930       << "Clearing event holders"; 
01931 
01932   // reset electromagnetic info
01933   // EE info
01934   EERE.clear(); 
01935   EESHE.clear(); 
01936   // EB info
01937   EBRE.clear();  
01938   EBSHE.clear();
01939   // ES info
01940   ESRE.clear();
01941   ESSHE.clear();
01942 
01943   // reset HCal Info
01944   HBCalREC.clear();
01945   HBCalR.clear();
01946   HBCalSHE.clear();
01947   HECalREC.clear();
01948   HECalR.clear();
01949   HECalSHE.clear();
01950   HOCalREC.clear();
01951   HOCalR.clear();
01952   HOCalSHE.clear();
01953   HFCalREC.clear();
01954   HFCalR.clear();
01955   HFCalSHE.clear();  
01956 
01957   // reset Track Info
01958   TIBL1RX.clear(); 
01959   TIBL2RX.clear(); 
01960   TIBL3RX.clear(); 
01961   TIBL4RX.clear();
01962   TIBL1RY.clear(); 
01963   TIBL2RY.clear(); 
01964   TIBL3RY.clear(); 
01965   TIBL4RY.clear();
01966   TIBL1SX.clear(); 
01967   TIBL2SX.clear(); 
01968   TIBL3SX.clear(); 
01969   TIBL4SX.clear();
01970   TIBL1SY.clear(); 
01971   TIBL2SY.clear(); 
01972   TIBL3SY.clear(); 
01973   TIBL4SY.clear();  
01974 
01975   TOBL1RX.clear(); 
01976   TOBL2RX.clear(); 
01977   TOBL3RX.clear(); 
01978   TOBL4RX.clear();
01979   TOBL1RY.clear(); 
01980   TOBL2RY.clear(); 
01981   TOBL3RY.clear(); 
01982   TOBL4RY.clear();
01983   TOBL1SX.clear(); 
01984   TOBL2SX.clear(); 
01985   TOBL3SX.clear(); 
01986   TOBL4SX.clear();
01987   TOBL1SY.clear(); 
01988   TOBL2SY.clear(); 
01989   TOBL3SY.clear(); 
01990   TOBL4SY.clear();  
01991 
01992   TIDW1RX.clear(); 
01993   TIDW2RX.clear(); 
01994   TIDW3RX.clear(); 
01995   TIDW1RY.clear(); 
01996   TIDW2RY.clear(); 
01997   TIDW3RY.clear(); 
01998   TIDW1SX.clear(); 
01999   TIDW2SX.clear(); 
02000   TIDW3SX.clear();
02001   TIDW1SY.clear(); 
02002   TIDW2SY.clear(); 
02003   TIDW3SY.clear();  
02004 
02005   TECW1RX.clear();  
02006   TECW2RX.clear();  
02007   TECW3RX.clear();  
02008   TECW4RX.clear();  
02009   TECW5RX.clear();  
02010   TECW6RX.clear();  
02011   TECW7RX.clear();  
02012   TECW8RX.clear();  
02013   TECW1RY.clear();  
02014   TECW2RY.clear();  
02015   TECW3RY.clear();  
02016   TECW4RY.clear();  
02017   TECW5RY.clear();  
02018   TECW6RY.clear();  
02019   TECW7RY.clear();  
02020   TECW8RY.clear();  
02021   TECW1SX.clear();  
02022   TECW2SX.clear();  
02023   TECW3SX.clear();  
02024   TECW4SX.clear();  
02025   TECW5SX.clear();  
02026   TECW6SX.clear();  
02027   TECW7SX.clear();  
02028   TECW8SX.clear();  
02029   TECW1SY.clear();  
02030   TECW2SY.clear();  
02031   TECW3SY.clear();  
02032   TECW4SY.clear();  
02033   TECW5SY.clear();  
02034   TECW6SY.clear();  
02035   TECW7SY.clear();  
02036   TECW8SY.clear();  
02037 
02038   BRL1RX.clear();
02039   BRL1RY.clear();
02040   BRL1SX.clear();
02041   BRL1SY.clear();
02042   BRL2RX.clear();
02043   BRL2RY.clear();
02044   BRL2SX.clear();
02045   BRL2SY.clear();
02046   BRL3RX.clear();
02047   BRL3RY.clear();
02048   BRL3SX.clear();
02049   BRL3SY.clear();
02050 
02051   FWD1pRX.clear();
02052   FWD1pRY.clear();
02053   FWD1pSX.clear();
02054   FWD1pSY.clear();
02055   FWD1nRX.clear();
02056   FWD1nRY.clear();
02057   FWD1nSX.clear();
02058   FWD1nSY.clear();
02059   FWD2pRX.clear();
02060   FWD2pRY.clear();
02061   FWD2pSX.clear();
02062   FWD2pSY.clear();
02063   FWD2nRX.clear();
02064   FWD2nRY.clear();
02065   FWD2nSX.clear();
02066   FWD2nSY.clear();
02067 
02068   //muon clear
02069   DTRHD.clear();
02070   DTSHD.clear();
02071 
02072   CSCRHPHI.clear();
02073   CSCRHPERP.clear();
02074   CSCSHPHI.clear();
02075 
02076   RPCRHX.clear();
02077   RPCSHX.clear();
02078 
02079   return;
02080 }
02081 
02082 //needed by to do the residual for matched hits in SiStrip
02083 std::pair<LocalPoint,LocalVector> 
02084 GlobalRecHitsProducer::projectHit(const PSimHit& hit, 
02085                                   const StripGeomDetUnit* stripDet,
02086                                   const BoundPlane& plane) 
02087 {
02088   
02089   const StripTopology& topol = stripDet->specificTopology();
02090   GlobalPoint globalpos= stripDet->surface().toGlobal(hit.localPosition());
02091   LocalPoint localHit = plane.toLocal(globalpos);
02092   //track direction
02093   LocalVector locdir=hit.localDirection();
02094   //rotate track in new frame
02095   
02096   GlobalVector globaldir= stripDet->surface().toGlobal(locdir);
02097   LocalVector dir=plane.toLocal(globaldir);
02098   float scale = -localHit.z() / dir.z();
02099   
02100   LocalPoint projectedPos = localHit + scale*dir;
02101     
02102   float selfAngle = topol.stripAngle( topol.strip( hit.localPosition()));
02103 
02104   // vector along strip in hit frame 
02105   LocalVector stripDir( sin(selfAngle), cos(selfAngle), 0); 
02106   
02107   LocalVector 
02108     localStripDir(plane.toLocal(stripDet->surface().toGlobal(stripDir)));
02109   
02110   return std::pair<LocalPoint,LocalVector>( projectedPos, localStripDir);
02111 }
02112 
02113 // Return a map between DTRecHit1DPair and wireId
02114 std::map<DTWireId, std::vector<DTRecHit1DPair> >
02115 GlobalRecHitsProducer::map1DRecHitsPerWire(const DTRecHitCollection* 
02116                                            dt1DRecHitPairs) {
02117   std::map<DTWireId, std::vector<DTRecHit1DPair> > ret;
02118   
02119   for(DTRecHitCollection::const_iterator rechit = dt1DRecHitPairs->begin();
02120       rechit != dt1DRecHitPairs->end(); rechit++) {
02121     ret[(*rechit).wireId()].push_back(*rechit);
02122   }
02123   
02124   return ret;
02125 }
02126 
02127 // Compute SimHit distance from wire (cm)
02128 float GlobalRecHitsProducer::simHitDistFromWire(const DTLayer* layer,
02129                                                 DTWireId wireId,
02130                                                 const PSimHit& hit) {
02131   float xwire = layer->specificTopology().wirePosition(wireId.wire());
02132   LocalPoint entryP = hit.entryPoint();
02133   LocalPoint exitP = hit.exitPoint();
02134   float xEntry = entryP.x()-xwire;
02135   float xExit  = exitP.x()-xwire;
02136 
02137   //FIXME: check...  
02138   return fabs(xEntry - (entryP.z()*(xExit-xEntry))/(exitP.z()-entryP.z()));
02139 }
02140 
02141 // Find the RecHit closest to the muon SimHit
02142 template  <typename type>
02143 const type* 
02144 GlobalRecHitsProducer::findBestRecHit(const DTLayer* layer,
02145                                       DTWireId wireId,
02146                                       const std::vector<type>& recHits,
02147                                       const float simHitDist) {
02148   float res = 99999;
02149   const type* theBestRecHit = 0;
02150   // Loop over RecHits within the cell
02151   for(typename std::vector<type>::const_iterator recHit = recHits.begin();
02152       recHit != recHits.end();
02153       recHit++) {
02154     float distTmp = recHitDistFromWire(*recHit, layer);
02155     if(fabs(distTmp-simHitDist) < res) {
02156       res = fabs(distTmp-simHitDist);
02157       theBestRecHit = &(*recHit);
02158     }
02159   } // End of loop over RecHits within the cell
02160   
02161   return theBestRecHit;
02162 }
02163 
02164 // Compute the distance from wire (cm) of a hits in a DTRecHit1DPair
02165 float 
02166 GlobalRecHitsProducer::recHitDistFromWire(const DTRecHit1DPair& hitPair, 
02167                                           const DTLayer* layer) {
02168   // Compute the rechit distance from wire
02169   return fabs(hitPair.localPosition(DTEnums::Left).x() -
02170               hitPair.localPosition(DTEnums::Right).x())/2.;
02171 }
02172 
02173 // Compute the distance from wire (cm) of a hits in a DTRecHit1D
02174 float 
02175 GlobalRecHitsProducer::recHitDistFromWire(const DTRecHit1D& recHit, 
02176                                           const DTLayer* layer) {
02177   return fabs(recHit.localPosition().x() - 
02178               layer->specificTopology().wirePosition(recHit.wireId().wire()));
02179 }
02180 
02181 template  <typename type>
02182 int GlobalRecHitsProducer::compute(const DTGeometry *dtGeom,
02183                                    std::map<DTWireId, std::vector<PSimHit> > 
02184                                    simHitsPerWire,
02185                                    std::map<DTWireId, std::vector<type> > 
02186                                    recHitsPerWire,
02187                                    int step) {
02188 
02189   int nDt = 0;
02190   // Loop over cells with a muon SimHit
02191   for(std::map<DTWireId, std::vector<PSimHit> >::const_iterator wireAndSHits = 
02192         simHitsPerWire.begin();
02193       wireAndSHits != simHitsPerWire.end();
02194       wireAndSHits++) {
02195     DTWireId wireId = (*wireAndSHits).first;
02196     std::vector<PSimHit> simHitsInCell = (*wireAndSHits).second;
02197     
02198     // Get the layer
02199     const DTLayer* layer = dtGeom->layer(wireId);
02200     
02201     // Look for a mu hit in the cell
02202     const PSimHit* muSimHit = DTHitQualityUtils::findMuSimHit(simHitsInCell);
02203     if (muSimHit==0) {
02204       continue; // Skip this cell
02205     }
02206 
02207     // Find the distance of the simhit from the wire
02208     float simHitWireDist = simHitDistFromWire(layer, wireId, *muSimHit);
02209     // Skip simhits out of the cell
02210     if(simHitWireDist>2.1) {
02211       continue; // Skip this cell
02212     }
02213     //GlobalPoint simHitGlobalPos = layer->toGlobal(muSimHit->localPosition());
02214 
02215     // Look for RecHits in the same cell
02216     if(recHitsPerWire.find(wireId) == recHitsPerWire.end()) {
02217       continue; // No RecHit found in this cell
02218     } else {
02219 
02220       // vector<type> recHits = (*wireAndRecHits).second;
02221       std::vector<type> recHits = recHitsPerWire[wireId];
02222          
02223       // Find the best RecHit
02224       const type* theBestRecHit = 
02225         findBestRecHit(layer, wireId, recHits, simHitWireDist);
02226  
02227       float recHitWireDist =  recHitDistFromWire(*theBestRecHit, layer);
02228       
02229       ++nDt;
02230 
02231       DTRHD.push_back(recHitWireDist);
02232       DTSHD.push_back(simHitWireDist);
02233       
02234     } // find rechits
02235   } // loop over simhits
02236 
02237   return nDt;
02238 }
02239 
02240 void 
02241 GlobalRecHitsProducer::plotResolution(const PSimHit & simHit, 
02242                                       const CSCRecHit2D & recHit,
02243                                       const CSCLayer * layer, 
02244                                       int chamberType) {
02245   GlobalPoint simHitPos = layer->toGlobal(simHit.localPosition());
02246   GlobalPoint recHitPos = layer->toGlobal(recHit.localPosition());
02247   
02248   CSCRHPHI.push_back(recHitPos.phi());
02249   CSCRHPERP.push_back(recHitPos.perp());
02250   CSCSHPHI.push_back(simHitPos.phi());
02251 }
02252 
02253 //define this as a plug-in
02254 //DEFINE_FWK_MODULE(GlobalRecHitsProducer);