CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/RecoLocalMuon/CSCEfficiency/src/CSCEfficiency.cc

Go to the documentation of this file.
00001 /*
00002  *  Routine to calculate CSC efficiencies 
00003  *  Comments about the program logic are denoted by //----
00004  * 
00005  *  Stoyan Stoynev, Northwestern University.
00006  */ 
00007   
00008 #include "RecoLocalMuon/CSCEfficiency/src/CSCEfficiency.h"
00009 
00010 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
00011 #include "TrackPropagation/SteppingHelixPropagator/interface/SteppingHelixPropagator.h"
00012 #include "FWCore/Common/interface/TriggerNames.h"
00013 #include "DataFormats/MuonReco/interface/Muon.h"
00014 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00015 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
00016 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00017 using namespace std;
00018 using namespace edm;
00019 
00020 //---- The Analysis  (main)
00021 bool CSCEfficiency::filter(Event & event, const EventSetup& eventSetup){
00022   passTheEvent = false;
00023   DataFlow->Fill(0.);  
00024   MuonPatternRecoDumper debug;
00025  
00026   //---- increment counter
00027   nEventsAnalyzed++;
00028   // printalot debug output
00029   printalot = (nEventsAnalyzed < int(printout_NEvents)); // 
00030   int iRun   = event.id().run();
00031   int iEvent = event.id().event();
00032   if(0==fmod(double (nEventsAnalyzed) ,double(1000) )){
00033     if(printalot){
00034       printf("\n==enter==CSCEfficiency===== run %i\tevent %i\tn Analyzed %i\n",iRun,iEvent,nEventsAnalyzed);
00035     }
00036   }
00037   theService->update(eventSetup);  
00038   //---- These declarations create handles to the types of records that you want
00039   //---- to retrieve from event "e".
00040   if (printalot) printf("\tget handles for digi collections\n");
00041   
00042   //---- Pass the handle to the method "getByType", which is used to retrieve
00043   //---- one and only one instance of the type in question out of event "e". If
00044   //---- zero or more than one instance exists in the event an exception is thrown.
00045   if (printalot) printf("\tpass handles\n");
00046   edm::Handle<CSCALCTDigiCollection> alcts;
00047   edm::Handle<CSCCLCTDigiCollection> clcts;
00048   edm::Handle<CSCCorrelatedLCTDigiCollection> correlatedlcts;
00049   edm::Handle<CSCWireDigiCollection> wires;
00050   edm::Handle<CSCStripDigiCollection> strips;
00051   edm::Handle<CSCRecHit2DCollection> rechits; 
00052   edm::Handle<CSCSegmentCollection> segments;
00053   //edm::Handle<reco::TrackCollection> saMuons;
00054   edm::Handle<edm::View<reco::Track> > trackCollectionH;
00055   edm::Handle<edm::PSimHitContainer> simhits;
00056 
00057   if(useDigis){
00058     event.getByLabel(alctDigiTag_, alcts);
00059     event.getByLabel(clctDigiTag_, clcts);
00060     event.getByLabel(corrlctDigiTag_, correlatedlcts);
00061 
00062     event.getByLabel( stripDigiTag_, strips);
00063     event.getByLabel( wireDigiTag_,  wires);
00064   }
00065   if(!isData){
00066     event.getByLabel(simHitTag, simhits);
00067   }
00068   event.getByLabel(rechitDigiTag_,rechits); 
00069   event.getByLabel(segmentDigiTag_, segments);
00070   //event.getByLabel(saMuonTag,saMuons);
00071   event.getByLabel(tracksTag,trackCollectionH);
00072   const edm::View<reco::Track>  trackCollection = *(trackCollectionH.product());
00073 
00074   //---- Get the CSC Geometry :
00075   if (printalot) printf("\tget the CSC geometry.\n");
00076   edm::ESHandle<CSCGeometry> cscGeom;
00077   eventSetup.get<MuonGeometryRecord>().get(cscGeom);
00078 
00079   // use theTrackingGeometry instead of cscGeom?
00080   ESHandle<GlobalTrackingGeometry> theTrackingGeometry;
00081   eventSetup.get<GlobalTrackingGeometryRecord>().get(theTrackingGeometry);
00082 
00083   bool triggerPassed = true;
00084   if(useTrigger){
00085     // access the trigger information
00086     // trigger names can be find in HLTrigger/Configuration/python/HLT_2E30_cff.py (or?)
00087    // get hold of TriggerResults
00088     edm::Handle<edm::TriggerResults> hltR;
00089     event.getByLabel(hlTriggerResults_,hltR);
00090     const edm::TriggerNames & triggerNames = event.triggerNames(*hltR);
00091     triggerPassed = applyTrigger(hltR, triggerNames);
00092   }
00093   if(!triggerPassed){
00094     return triggerPassed;
00095   }
00096   DataFlow->Fill(1.);   
00097   GlobalPoint gpZero(0.,0.,0.);
00098   if(theService->magneticField()->inTesla(gpZero).mag2()<0.1){ 
00099     magField = false;
00100   }
00101   else{
00102     magField = true;
00103   }
00104 
00105   //---- store info from digis
00106   fillDigiInfo(alcts, clcts, correlatedlcts, wires, strips, simhits, rechits, segments, cscGeom);
00107   //
00108   Handle<reco::MuonCollection> muons;
00109   edm::InputTag muonTag_("muons");
00110   event.getByLabel(muonTag_,muons);
00111 
00112   edm::Handle<reco::BeamSpot> beamSpotHandle;
00113   event.getByLabel("offlineBeamSpot", beamSpotHandle);
00114   reco::BeamSpot vertexBeamSpot = *beamSpotHandle;
00115   //
00116   std::vector <reco::MuonCollection::const_iterator> goodMuons_it;
00117   unsigned int nPositiveZ = 0;
00118   unsigned int nNegativeZ = 0;
00119   float muonOuterZPosition = -99999.;
00120   if(isIPdata){
00121     if (printalot)std::cout<<" muons.size() = "<<muons->size() <<std::endl;
00122     for ( reco::MuonCollection::const_iterator muon = muons->begin(); muon != muons->end(); ++muon ) {
00123       DataFlow->Fill(31.);      
00124       if (printalot) {
00125         std::cout<<"  iMuon = "<<muon-muons->begin()<<" charge = "<<muon->charge()<<" p = "<<muon->p()<<" pt = "<<muon->pt()<<
00126           " eta = "<<muon->eta()<<" phi = "<<muon->phi()<<
00127           " matches = "<<
00128           muon->matches().size()<<" matched Seg = "<<muon->numberOfMatches(reco::Muon::SegmentAndTrackArbitration)<<" GLB/TR/STA = "<<
00129           muon->isGlobalMuon()<<"/"<<muon->isTrackerMuon()<<"/"<<muon->isStandAloneMuon()<<std::endl;
00130       }
00131       if(!(muon->isTrackerMuon() && muon->isGlobalMuon())){
00132         continue;
00133       }
00134       DataFlow->Fill(32.);
00135       double relISO = ( muon->isolationR03().sumPt +
00136                         muon->isolationR03().emEt +
00137                         muon->isolationR03().hadEt)/muon->track()->pt();
00138       if (printalot) {
00139         std::cout<<" relISO = "<<relISO<<" emVetoEt = "<<muon->isolationR03().emVetoEt<<" caloComp = "<<
00140           muon::caloCompatibility(*(muon))<<" dxy = "<<fabs(muon->track()->dxy(vertexBeamSpot.position()))<<std::endl;
00141       }
00142       if(
00143          //relISO>0.1 || muon::caloCompatibility(*(muon))<.90 ||
00144          fabs(muon->track()->dxy(vertexBeamSpot.position()))>0.2 || muon->pt()<6.){
00145         continue;
00146       }
00147       DataFlow->Fill(33.);
00148       if(muon->track()->hitPattern().numberOfValidPixelHits()<1 ||
00149          muon->track()->hitPattern().numberOfValidTrackerHits()<11 ||
00150          muon->combinedMuon()->hitPattern().numberOfValidMuonHits()<1 ||
00151          muon->combinedMuon()->normalizedChi2()>10. ||
00152          muon->numberOfMatches()<2){
00153         continue;
00154       }
00155       DataFlow->Fill(34.);
00156       float zOuter = muon->combinedMuon()->outerPosition().z();
00157       float rhoOuter = muon->combinedMuon()->outerPosition().rho();
00158       bool passDepth = true;
00159       // barrel region 
00160       //if ( fabs(zOuter) < 660. && rhoOuter > 400. && rhoOuter < 480.){
00161       if ( fabs(zOuter) < 660. && rhoOuter > 400. && rhoOuter < 540.){ 
00162         passDepth = false;
00163       }
00164       // endcap region
00165       //else if( fabs(zOuter) > 550. && fabs(zOuter) < 650. && rhoOuter < 300.){
00166       else if( fabs(zOuter) > 550. && fabs(zOuter) < 650. && rhoOuter < 300.){
00167         passDepth = false;
00168       }
00169       // overlap region
00170       //else if ( fabs(zOuter) > 680. && fabs(zOuter) < 730. && rhoOuter < 480.){
00171       else if ( fabs(zOuter) > 680. && fabs(zOuter) < 880. && rhoOuter < 540.){
00172         passDepth = false;
00173       }
00174       if(!passDepth){
00175         continue;
00176       }
00177       DataFlow->Fill(35.);
00178       goodMuons_it.push_back(muon);
00179       if(muon->track()->momentum().z()>0.){
00180         ++nPositiveZ;
00181       }
00182       if(muon->track()->momentum().z()<0.){
00183         ++nNegativeZ;
00184       }
00185     }
00186   }
00187 
00188   //
00189 
00190 
00191   if (printalot) std::cout<<"Start track loop over "<<trackCollection.size()<<" tracks"<<std::endl;
00192   for(edm::View<reco::Track>::size_type i=0; i<trackCollection.size(); ++i) {
00193     DataFlow->Fill(2.);
00194     edm::RefToBase<reco::Track> track(trackCollectionH, i);
00195     //std::cout<<" iTR = "<<i<<" eta = "<<track->eta()<<" phi = "<<track->phi()<<std::cout<<" pt = "<<track->pt()<<std::endl;
00196     if(isIPdata){
00197       if (printalot){
00198         std::cout<<" nNegativeZ = "<<nNegativeZ<<" nPositiveZ = "<<nPositiveZ<<std::endl;
00199       }
00200       if(nNegativeZ>1 || nPositiveZ>1){
00201         break;
00202       }
00203       bool trackOK = false;
00204       if (printalot){
00205         std::cout<<" goodMuons_it.size() = "<<goodMuons_it.size()<<std::endl;
00206       }
00207       for(size_t iM=0;iM<goodMuons_it.size();++iM){
00208         //std::cout<<" iM = "<<iM<<" eta = "<<goodMuons_it[iM]->track()->eta()<<
00209         //" phi = "<<goodMuons_it[iM]->track()->phi()<<
00210         //" pt = "<<goodMuons_it[iM]->track()->pt()<<std::endl;
00211         float deltaR = pow(track->phi()-goodMuons_it[iM]->track()->phi(),2) +
00212           pow(track->eta()-goodMuons_it[iM]->track()->eta(),2);
00213         deltaR = sqrt(deltaR);
00214         if (printalot){
00215           std::cout<<" TR mu match to a tr: deltaR = "<<deltaR<<" dPt = "<<
00216             track->pt()-goodMuons_it[iM]->track()->pt()<<std::endl;
00217         }
00218         if(deltaR>0.01 || fabs(track->pt()-goodMuons_it[iM]->track()->pt())>0.1 ){
00219           continue;
00220         }
00221         else{
00222           trackOK = true;
00223           if (printalot){
00224             std::cout<<" trackOK "<<std::endl;
00225           }
00226           muonOuterZPosition = goodMuons_it[iM]->combinedMuon()->outerPosition().z();
00227           break;
00228           //++nChosenTracks;
00229         }
00230       }
00231       if(!trackOK){
00232         if (printalot){
00233           std::cout<<" failed: trackOK "<<std::endl;
00234         }
00235         continue;
00236       }
00237     }
00238     else{
00239       //---- Do we need a better "clean track" definition?
00240       if(trackCollection.size()>2){
00241         break;
00242       }
00243       DataFlow->Fill(3.);
00244       if(!i && 2==trackCollection.size()){
00245         edm::View<reco::Track>::size_type tType = 1;
00246         edm::RefToBase<reco::Track> trackTwo(trackCollectionH, tType);
00247         if(track->outerPosition().z()*trackTwo->outerPosition().z()>0){// in one and the same "endcap"
00248           break;
00249         }
00250       }
00251     }
00252     DataFlow->Fill(4.);
00253     if (printalot){
00254       std::cout<<"i track = "<<i<<" P = "<<track->p()<<" chi2/ndf = "<<track->normalizedChi2()<<" nSeg = "<<segments->size()<<std::endl;
00255       std::cout<<"quality undef/loose/tight/high/confirmed/goodIt/size "<<
00256         track->quality(reco::Track::undefQuality)<<"/"<<
00257         track->quality(reco::Track::loose)<<"/"<<
00258         track->quality(reco::Track::tight)<<"/"<<
00259         track->quality(reco::Track::highPurity)<<"/"<<
00260         track->quality(reco::Track::confirmed)<<"/"<<
00261         track->quality(reco::Track::goodIterative)<<"/"<<
00262         track->quality(reco::Track::qualitySize)<<
00263         std::endl;
00264       std::cout<<" pt = "<< track->pt()<<" +-"<<track->ptError()<<" q/pt = "<<track->qoverp()<<" +- "<<track->qoverpError()<<std::endl;
00265       //std::cout<<" const Pmin = "<<minTrackMomentum<<" pMax = "<<maxTrackMomentum<<" maxNormChi2 = "<<maxNormChi2<<std::endl;
00266       std::cout<<" track inner position = "<<track->innerPosition()<<" outer position = "<<track->outerPosition()<<std::endl;
00267       std::cout<<"track eta (outer) = "<<track->outerPosition().eta()<<" phi (outer) = "<<
00268         track->outerPosition().phi()<<std::endl;
00269       if(fabs(track->innerPosition().z())>500.){      
00270         DetId innerDetId(track->innerDetId());
00271         std::cout<<" dump inner state MUON detid  = "<<debug.dumpMuonId(innerDetId)<<std::endl;
00272       }
00273       if(fabs(track->outerPosition().z())>500.){
00274         DetId outerDetId(track->outerDetId());
00275         std::cout<<" dump outer state MUON detid  = "<<debug.dumpMuonId(outerDetId)<<std::endl;
00276       }
00277 
00278       std::cout<<" nHits = "<<track->found()<<std::endl;
00279       /*
00280       trackingRecHit_iterator rhbegin = track->recHitsBegin();
00281       trackingRecHit_iterator rhend = track->recHitsEnd();
00282       int iRH = 0;
00283       for(trackingRecHit_iterator recHit = rhbegin; recHit != rhend; ++recHit){
00284         const GeomDet* geomDet = theTrackingGeometry->idToDet((*recHit)->geographicalId());
00285         std::cout<<"hit "<<iRH<<" loc pos = " <<(*recHit)->localPosition()<<
00286           " glob pos = " <<geomDet->toGlobal((*recHit)->localPosition())<<std::endl;
00287         ++iRH;
00288       }
00289       */
00290     }
00291     float dpT_ov_pT = 0.;
00292     if(fabs(track->pt())>0.001){
00293       dpT_ov_pT =  track->ptError()/ track->pt();
00294     }
00295     //---- These define a "good" track
00296     if(track->normalizedChi2()>maxNormChi2){// quality
00297       break;
00298     }
00299     DataFlow->Fill(5.);
00300     if(track->found()<minTrackHits){// enough data points
00301       break;
00302     }
00303     DataFlow->Fill(6.);
00304     if(!segments->size()){// better have something in the CSC 
00305       break;
00306     }
00307     DataFlow->Fill(7.);
00308     if(magField && (track->p()<minP || track->p()>maxP)){// proper energy range 
00309       break;
00310     }
00311     DataFlow->Fill(8.);
00312     if(magField && (dpT_ov_pT >0.5) ){// not too crazy uncertainty
00313       break;
00314     }
00315     DataFlow->Fill(9.);
00316 
00317     passTheEvent = true; 
00318     if (printalot) std::cout<<"good Track"<<std::endl;
00319     CLHEP::Hep3Vector r3T_inner(track->innerPosition().x(),track->innerPosition().y(),track->innerPosition().z());
00320     CLHEP::Hep3Vector r3T(track->outerPosition().x(),track->outerPosition().y(),track->outerPosition().z());
00321     chooseDirection(r3T_inner, r3T);// for non-IP
00322 
00323     CLHEP::Hep3Vector p3T(track->outerMomentum().x(),track->outerMomentum().y(),track->outerMomentum().z());
00324     CLHEP::Hep3Vector p3_propagated, r3_propagated;
00325     AlgebraicSymMatrix66 cov_propagated, covT;
00326     covT *= 1e-20;
00327     cov_propagated *= 1e-20;
00328     int charge = track->charge(); 
00329     FreeTrajectoryState ftsStart = getFromCLHEP(p3T, r3T, charge, covT, &*(theService->magneticField()));
00330     if (printalot){
00331       std::cout<<" p = "<<track->p()<<" norm chi2 = "<<track->normalizedChi2()<<std::endl;
00332       std::cout<<" dump the very first FTS  = "<<debug.dumpFTS(ftsStart)<<std::endl;
00333     }
00334     TrajectoryStateOnSurface tSOSDest;
00335     int endcap = 0;
00336     //---- which endcap to look at
00337     if(track->outerPosition().z()>0){
00338       endcap = 1;
00339     }
00340     else{
00341       endcap = 2;
00342     }
00343     int chamber = 1;
00344     //---- a "refference" CSCDetId for each ring
00345     std::vector< CSCDetId > refME;
00346     for(int iS=1;iS<5;++iS){
00347       for(int iR=1;iR<4;++iR){
00348         if(1!=iS && iR>2){
00349           continue;
00350         }
00351         else if(4==iS && iR>1){
00352           continue;
00353         } 
00354         refME.push_back( CSCDetId(endcap, iS, iR, chamber));
00355       }
00356     }
00357     //---- loop over the "refference" CSCDetIds
00358     for(size_t iSt = 0; iSt<refME.size();++iSt){
00359       if (printalot){
00360         std::cout<<"loop iStatation = "<<iSt<<std::endl;
00361         std::cout<<"refME[iSt]: st = "<<refME[iSt].station()<<" rg = "<<refME[iSt].ring()<<std::endl;
00362       }
00363       std::map <std::string, bool> chamberTypes;
00364       chamberTypes["ME11"] = false;
00365       chamberTypes["ME12"] = false;
00366       chamberTypes["ME13"] = false;
00367       chamberTypes["ME21"] = false;
00368       chamberTypes["ME22"] = false;
00369       chamberTypes["ME31"] = false;
00370       chamberTypes["ME32"] = false;
00371       chamberTypes["ME41"] = false;
00372       const CSCChamber* cscChamber_base = cscGeom->chamber(refME[iSt].chamberId());
00373       DetId detId = cscChamber_base->geographicalId();
00374       if (printalot){
00375         std::cout<<" base iStation : eta = "<<cscGeom->idToDet(detId)->surface().position().eta()<<" phi = "<<
00376           cscGeom->idToDet(detId)->surface().position().phi() << " y = " <<cscGeom->idToDet(detId)->surface().position().y()<<std::endl;
00377         std::cout<<" dump base iStation detid  = "<<debug.dumpMuonId(detId)<<std::endl;
00378         std::cout<<" dump FTS start  = "<<debug.dumpFTS(ftsStart)<<std::endl;
00379       }
00380       //---- propagate to this ME
00381       tSOSDest = propagate(ftsStart, cscGeom->idToDet(detId)->surface());
00382       if(tSOSDest.isValid()){
00383         ftsStart = *tSOSDest.freeState();
00384         if (printalot) std::cout<<"  dump FTS end   = "<<debug.dumpFTS(ftsStart)<<std::endl;
00385         getFromFTS(ftsStart, p3_propagated, r3_propagated, charge, cov_propagated);
00386         float feta =  fabs(r3_propagated.eta());
00387         float phi =  r3_propagated.phi();
00388         //---- which rings are (possibly) penetrated
00389         ringCandidates(refME[iSt].station(), feta, chamberTypes);
00390 
00391         map<std::string,bool>::iterator iter;   
00392         int iterations = 0;
00393         //---- loop over ring candidates 
00394         for( iter = chamberTypes.begin(); iter != chamberTypes.end(); iter++ ) {
00395           ++iterations;
00396           //---- is this ME a machinig candidate station 
00397           if(iter->second && (iterations-1)==int(iSt)){
00398             if (printalot){
00399               std::cout<<" Chamber type "<< iter->first<<" is a candidate..."<<std::endl;
00400               std::cout<<" station() = "<< refME[iSt].station()<<" ring() = "<<refME[iSt].ring()<<" iSt = "<<iSt<<std::endl;
00401             }
00402             std::vector <int> coupleOfChambers;
00403             //---- which chamber (and its closes neighbor) is penetrated by the track - candidates
00404             chamberCandidates(refME[iSt].station(), refME[iSt].ring(), phi, coupleOfChambers);
00405             //---- loop over the two chamber candidates
00406             for(size_t iCh =0;iCh<coupleOfChambers.size();++iCh){
00407               DataFlow->Fill(11.);  
00408               if (printalot) std::cout<<" Check chamber N = "<<coupleOfChambers.at(iCh)<<std::endl;;
00409               if((!getAbsoluteEfficiency) && (true == emptyChambers
00410                                               [refME[iSt].endcap()-1]
00411                                               [refME[iSt].station()-1]
00412                                               [refME[iSt].ring()-1]
00413                                               [coupleOfChambers.at(iCh)-FirstCh])){
00414                 continue;
00415               }
00416               CSCDetId theCSCId(refME[iSt].endcap(), refME[iSt].station(), refME[iSt].ring(), coupleOfChambers.at(iCh));
00417               const CSCChamber* cscChamber = cscGeom->chamber(theCSCId.chamberId());
00418               const BoundPlane bpCh = cscGeom->idToDet(cscChamber->geographicalId())->surface();
00419               float zFTS = ftsStart.position().z();
00420               float dz = fabs(bpCh.position().z() - zFTS);
00421               float zDistInner = track->innerPosition().z() - bpCh.position().z();
00422               float zDistOuter = track->outerPosition().z() - bpCh.position().z();
00423               //---- only detectors between the inner and outer points of the track are considered for non IP-data
00424               if(printalot){
00425                 std::cout<<" zIn = "<<track->innerPosition().z()<<" zOut = "<<track->outerPosition().z()<<" zSurf = "<<bpCh.position().z()<<std::endl;
00426               }
00427               if(!isIPdata && (zDistInner*zDistOuter>0. || fabs(zDistInner)<15. || fabs(zDistOuter)<15.)){ // for non IP-data
00428                 if(printalot){
00429                   std::cout<<" Not an intermediate (as defined) point... Skip."<<std::endl;
00430                 }
00431                 continue;
00432               }
00433               if(isIPdata && fabs(track->eta())<1.8){
00434                 if(fabs(muonOuterZPosition) - fabs(bpCh.position().z())<0 || 
00435                    fabs(muonOuterZPosition-bpCh.position().z())<15.){
00436                   continue;
00437                 }
00438               }
00439               DataFlow->Fill(13.);
00440               //---- propagate to the chamber (from this ME) if it is a different surface (odd/even chambers)
00441               if(dz>0.1){// i.e. non-zero (float 0 check is bad) 
00442                 //if(fabs(zChanmber - zFTS ) > 0.1){
00443                 tSOSDest = propagate(ftsStart, cscGeom->idToDet(cscChamber->geographicalId())->surface());                   
00444                 if(tSOSDest.isValid()){
00445                   ftsStart = *tSOSDest.freeState();
00446                 }
00447                 else{
00448                   if(printalot) std::cout<<"TSOS not valid! Break."<<std::endl;
00449                   break;
00450                 }
00451               }
00452               else{
00453                 if(printalot) std::cout<<" info: dz<0.1"<<std::endl;
00454               }
00455               DataFlow->Fill(15.);  
00456               FreeTrajectoryState ftsInit = ftsStart;
00457               bool inDeadZone = false;
00458               //---- loop over the 6 layers
00459               for(int iLayer = 0;iLayer<6;++iLayer){
00460                 bool extrapolationPassed = true;
00461                 if (printalot){
00462                   std::cout<<" iLayer = "<<iLayer<<"   dump FTS init  = "<<debug.dumpFTS(ftsInit)<<std::endl;
00463                   std::cout<<" dump detid  = "<<debug.dumpMuonId(cscChamber->geographicalId())<<std::endl;
00464                   std::cout<<"Surface to propagate to:  pos = "<<cscChamber->layer(iLayer+1)->surface().position()<<" eta = "
00465                            <<cscChamber->layer(iLayer+1)->surface().position().eta()<<" phi = "
00466                            <<cscChamber->layer(iLayer+1)->surface().position().phi()<<std::endl;
00467                 }
00468                 //---- propagate to this layer
00469                 tSOSDest = propagate(ftsInit, cscChamber->layer(iLayer+1)->surface());
00470                 if(tSOSDest.isValid()){
00471                   ftsInit = *tSOSDest.freeState();
00472                   if (printalot) std::cout<<" Propagation between layers successful:  dump FTS end  = "<<debug.dumpFTS(ftsInit)<<std::endl;
00473                   getFromFTS(ftsInit, p3_propagated, r3_propagated, charge, cov_propagated);
00474                 }
00475                 else{
00476                   if (printalot) std::cout<<"Propagation between layers not successful - notValid TSOS"<<std::endl;
00477                   extrapolationPassed = false;
00478                   inDeadZone = true;
00479                 }
00480                 //}
00481                 //---- Extrapolation passed? For each layer?
00482                 if(extrapolationPassed){
00483                   GlobalPoint theExtrapolationPoint(r3_propagated.x(),r3_propagated.y(),r3_propagated.z());
00484                   LocalPoint theLocalPoint = cscChamber->layer(iLayer+1)->toLocal(theExtrapolationPoint);
00485                   //std::cout<<" Candidate chamber: extrapolated LocalPoint = "<<theLocalPoint<<std::endl;
00486                   inDeadZone = ( inDeadZone ||
00487                                  !inSensitiveLocalRegion(theLocalPoint.x(), theLocalPoint.y(), 
00488                                                          refME[iSt].station(), refME[iSt].ring()));
00489                   if (printalot){
00490                     std::cout<<" Candidate chamber: extrapolated LocalPoint = "<<theLocalPoint<<"inDeadZone = "<<inDeadZone<<std::endl;
00491                   }
00492                   //---- break if in dead zone for any layer ("clean" tracks)
00493                   if(inDeadZone){
00494                     break;
00495                   }
00496                 }
00497                 else{
00498                   break;
00499                 }
00500               }
00501               DataFlow->Fill(17.);
00502               //---- Is a track in a sensitive area for each layer?  
00503               if(!inDeadZone){//---- for any layer
00504                 DataFlow->Fill(19.);  
00505                 if (printalot) std::cout<<"Do efficiencies..."<<std::endl;
00506                 //---- Do efficiencies
00507                 // angle cuts applied (if configured)
00508                 bool angle_flag = true; angle_flag = efficienciesPerChamber(theCSCId, cscChamber, ftsStart);  
00509                 if(useDigis && angle_flag){
00510                   bool stripANDwire_flag; stripANDwire_flag = stripWire_Efficiencies(theCSCId, ftsStart);
00511                 }
00512                 if(angle_flag){
00513                   bool recHitANDsegment_flag; recHitANDsegment_flag = recHitSegment_Efficiencies(theCSCId, cscChamber, ftsStart);
00514                   if(!isData){
00515                     recSimHitEfficiency(theCSCId, ftsStart);
00516                   }
00517                 }
00518               }
00519               else{
00520                 if(printalot) std::cout<<" Not in active area for all layers"<<std::endl;
00521               }
00522             }
00523             if(tSOSDest.isValid()){
00524               ftsStart = *tSOSDest.freeState();
00525             }
00526           }
00527         }
00528       }
00529       else{
00530         if (printalot) std::cout<<" TSOS not valid..."<<std::endl;
00531       }
00532     }
00533   }
00534   //---- End
00535   if (printalot) printf("==exit===CSCEfficiency===== run %i\tevent %i\n\n",iRun,iEvent);
00536   return passTheEvent;
00537 }
00538 
00539 //
00540 bool CSCEfficiency::inSensitiveLocalRegion(double xLocal, double yLocal, int station, int ring){
00541   //---- Good region means sensitive area of a chamber. "Local" stands for the local system 
00542   bool pass = false;
00543   std::vector <double> chamberBounds(3);// the sensitive area
00544   float y_center = 99999.;
00545   //---- hardcoded... not good
00546   if(station>1 && station<5){
00547     if(2==ring){
00548       chamberBounds[0] = 66.46/2; // (+-)x1 shorter
00549       chamberBounds[1] = 127.15/2; // (+-)x2 longer 
00550       chamberBounds[2] = 323.06/2;
00551       y_center = -0.95;
00552     }
00553     else{
00554       if(2==station){
00555         chamberBounds[0] = 54.00/2; // (+-)x1 shorter
00556         chamberBounds[1] = 125.71/2; // (+-)x2 longer 
00557         chamberBounds[2] = 189.66/2;
00558         y_center = -0.955;
00559       }
00560       else if(3==station){
00561         chamberBounds[0] = 61.40/2; // (+-)x1 shorter
00562         chamberBounds[1] = 125.71/2; // (+-)x2 longer 
00563         chamberBounds[2] = 169.70/2;
00564         y_center = -0.97;
00565       }
00566       else if(4==station){
00567         chamberBounds[0] = 69.01/2; // (+-)x1 shorter
00568         chamberBounds[1] = 125.65/2; // (+-)x2 longer 
00569         chamberBounds[2] = 149.42/2;
00570         y_center = -0.94;
00571       }
00572     }
00573   }
00574   else if(1==station){
00575     if(3==ring){
00576       chamberBounds[0] = 63.40/2; // (+-)x1 shorter
00577       chamberBounds[1] = 92.10/2; // (+-)x2 longer 
00578       chamberBounds[2] = 164.16/2;
00579       y_center = -1.075;
00580     }
00581     else if(2==ring){
00582       chamberBounds[0] = 51.00/2; // (+-)x1 shorter
00583       chamberBounds[1] = 83.74/2; // (+-)x2 longer 
00584       chamberBounds[2] = 174.49/2;
00585       y_center = -0.96;
00586     }
00587     else{// to be investigated
00588       chamberBounds[0] = 30./2;//40./2; // (+-)x1 shorter
00589       chamberBounds[1] = 60./2;//100./2; // (+-)x2 longer 
00590       chamberBounds[2] = 160./2;//142./2;
00591       y_center = 0.;
00592     }
00593   }
00594   double yUp = chamberBounds[2] + y_center;
00595   double yDown = - chamberBounds[2] + y_center;
00596   double xBound1Shifted = chamberBounds[0]-distanceFromDeadZone;//
00597   double xBound2Shifted = chamberBounds[1]-distanceFromDeadZone;//
00598   double lineSlope = (yUp - yDown)/(xBound2Shifted-xBound1Shifted);
00599   double lineConst = yUp - lineSlope*xBound2Shifted;
00600   double yBoundary =  lineSlope*abs(xLocal) + lineConst;
00601   pass = checkLocal(yLocal, yBoundary, station, ring);
00602   return pass;
00603 }
00604 
00605 bool CSCEfficiency::checkLocal(double yLocal, double yBoundary, int station, int ring){
00606 //---- check if it is in a good local region (sensitive area - geometrical and HV boundaries excluded) 
00607   bool pass = false;
00608   std::vector <float> deadZoneCenter(6);
00609   const float deadZoneHalf = 0.32*7/2;// wire spacing * (wires missing + 1)/2
00610   float cutZone = deadZoneHalf + distanceFromDeadZone;//cm
00611   //---- hardcoded... not good
00612   if(station>1 && station<5){
00613     if(2==ring){
00614       deadZoneCenter[0]= -162.48 ;
00615       deadZoneCenter[1] = -81.8744;
00616       deadZoneCenter[2] = -21.18165;
00617       deadZoneCenter[3] = 39.51105;
00618       deadZoneCenter[4] = 100.2939;
00619       deadZoneCenter[5] = 160.58;
00620       
00621       if(yLocal >yBoundary &&
00622          ((yLocal> deadZoneCenter[0] + cutZone && yLocal< deadZoneCenter[1] - cutZone) ||
00623           (yLocal> deadZoneCenter[1] + cutZone && yLocal< deadZoneCenter[2] - cutZone) ||
00624           (yLocal> deadZoneCenter[2] + cutZone && yLocal< deadZoneCenter[3] - cutZone) ||
00625           (yLocal> deadZoneCenter[3] + cutZone && yLocal< deadZoneCenter[4] - cutZone) ||
00626           (yLocal> deadZoneCenter[4] + cutZone && yLocal< deadZoneCenter[5] - cutZone))){
00627         pass = true;
00628       }
00629     }
00630     else if(1==ring){
00631       if(2==station){
00632         deadZoneCenter[0]= -95.94 ;
00633         deadZoneCenter[1] = -27.47;
00634         deadZoneCenter[2] = 33.67;
00635         deadZoneCenter[3] = 93.72;
00636       }
00637       else if(3==station){
00638         deadZoneCenter[0]= -85.97 ;
00639         deadZoneCenter[1] = -36.21;
00640         deadZoneCenter[2] = 23.68;
00641         deadZoneCenter[3] = 84.04;
00642       }
00643       else if(4==station){
00644         deadZoneCenter[0]= -75.82;
00645         deadZoneCenter[1] = -26.14;
00646         deadZoneCenter[2] = 23.85;
00647         deadZoneCenter[3] = 73.91;
00648       }
00649       if(yLocal >yBoundary &&
00650          ((yLocal> deadZoneCenter[0] + cutZone && yLocal< deadZoneCenter[1] - cutZone) ||
00651           (yLocal> deadZoneCenter[1] + cutZone && yLocal< deadZoneCenter[2] - cutZone) ||
00652           (yLocal> deadZoneCenter[2] + cutZone && yLocal< deadZoneCenter[3] - cutZone))){
00653         pass = true;
00654       }
00655     }
00656   }
00657   else if(1==station){
00658     if(3==ring){
00659       deadZoneCenter[0]= -83.155 ;
00660       deadZoneCenter[1] = -22.7401;
00661       deadZoneCenter[2] = 27.86665;
00662       deadZoneCenter[3] = 81.005;
00663       if(yLocal > yBoundary &&
00664          ((yLocal> deadZoneCenter[0] + cutZone && yLocal< deadZoneCenter[1] - cutZone) ||
00665           (yLocal> deadZoneCenter[1] + cutZone && yLocal< deadZoneCenter[2] - cutZone) ||
00666           (yLocal> deadZoneCenter[2] + cutZone && yLocal< deadZoneCenter[3] - cutZone))){
00667         pass = true;
00668       }
00669     }
00670     else if(2==ring){
00671       deadZoneCenter[0]= -86.285 ;
00672       deadZoneCenter[1] = -32.88305;
00673       deadZoneCenter[2] = 32.867423;
00674       deadZoneCenter[3] = 88.205;
00675       if(yLocal > (yBoundary) &&
00676          ((yLocal> deadZoneCenter[0] + cutZone && yLocal< deadZoneCenter[1] - cutZone) ||
00677           (yLocal> deadZoneCenter[1] + cutZone && yLocal< deadZoneCenter[2] - cutZone) ||
00678           (yLocal> deadZoneCenter[2] + cutZone && yLocal< deadZoneCenter[3] - cutZone))){
00679         pass = true;
00680       }
00681     }
00682     else{
00683       deadZoneCenter[0]= -81.0;
00684       deadZoneCenter[1] = 81.0;
00685       if(yLocal > (yBoundary) &&
00686          ((yLocal> deadZoneCenter[0] + cutZone && yLocal< deadZoneCenter[1] - cutZone) )){
00687         pass = true;
00688       }
00689     }
00690   }
00691   return pass;
00692 }
00693 
00694 void CSCEfficiency::fillDigiInfo(edm::Handle<CSCALCTDigiCollection> &alcts, 
00695                                  edm::Handle<CSCCLCTDigiCollection> &clcts, 
00696                                  edm::Handle<CSCCorrelatedLCTDigiCollection> &correlatedlcts,
00697                                  edm::Handle<CSCWireDigiCollection> &wires,
00698                                  edm::Handle<CSCStripDigiCollection> &strips,
00699                                  edm::Handle<edm::PSimHitContainer> &simhits,
00700                                  edm::Handle<CSCRecHit2DCollection> &rechits,
00701                                  edm::Handle<CSCSegmentCollection> &segments,
00702                                  edm::ESHandle<CSCGeometry> &cscGeom){
00703   for(int iE=0;iE<2;iE++){
00704     for(int iS=0;iS<4;iS++){
00705       for(int iR=0;iR<4;iR++){
00706         for(int iC=0;iC<NumCh;iC++){
00707           allSegments[iE][iS][iR][iC].clear(); 
00708           allCLCT[iE][iS][iR][iC] = allALCT[iE][iS][iR][iC] = allCorrLCT[iE][iS][iR][iC] = false;
00709           for(int iL=0;iL<6;iL++){
00710             allStrips[iE][iS][iR][iC][iL].clear(); 
00711             allWG[iE][iS][iR][iC][iL].clear();
00712             allRechits[iE][iS][iR][iC][iL].clear(); 
00713             allSimhits[iE][iS][iR][iC][iL].clear(); 
00714           }
00715         }
00716       }
00717     }
00718   }
00719   //
00720   if(useDigis){
00721     fillLCT_info(alcts, clcts, correlatedlcts);
00722     fillWG_info(wires, cscGeom);
00723     fillStrips_info(strips);
00724   }
00725   fillRechitsSegments_info(rechits, segments, cscGeom);
00726   if(!isData){
00727     fillSimhit_info(simhits);
00728   }
00729 }
00730 
00731 
00732 void CSCEfficiency::fillLCT_info(edm::Handle<CSCALCTDigiCollection> &alcts, 
00733                                  edm::Handle<CSCCLCTDigiCollection> &clcts, 
00734                                  edm::Handle<CSCCorrelatedLCTDigiCollection> &correlatedlcts ){
00735   //---- ALCTDigis
00736   int nSize = 0;
00737   for (CSCALCTDigiCollection::DigiRangeIterator j=alcts->begin(); j!=alcts->end(); j++) {
00738     ++nSize;
00739     const CSCDetId& id = (*j).first;
00740     const CSCALCTDigiCollection::Range& range =(*j).second;
00741     for (CSCALCTDigiCollection::const_iterator digiIt =
00742            range.first; digiIt!=range.second;
00743          ++digiIt){
00744       // Valid digi in the chamber (or in neighbouring chamber) 
00745       if((*digiIt).isValid()){
00746         allALCT[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh] = true;
00747       }
00748     }// for digis in layer
00749   }// end of for (j=...
00750   ALCTPerEvent->Fill(nSize);
00751   //---- CLCTDigis
00752   nSize = 0;
00753   for (CSCCLCTDigiCollection::DigiRangeIterator j=clcts->begin(); j!=clcts->end(); j++) {
00754     ++nSize;
00755     const CSCDetId& id = (*j).first;
00756     std::vector<CSCCLCTDigi>::const_iterator digiIt = (*j).second.first;
00757     std::vector<CSCCLCTDigi>::const_iterator last = (*j).second.second;
00758     for( ; digiIt != last; ++digiIt) {
00759       // Valid digi in the chamber (or in neighbouring chamber) 
00760       if((*digiIt).isValid()){
00761         allCLCT[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh] = true;
00762       }
00763     }
00764   }
00765   CLCTPerEvent->Fill(nSize);
00766   //---- CorrLCTDigis
00767   for (CSCCorrelatedLCTDigiCollection::DigiRangeIterator j=correlatedlcts->begin(); j!=correlatedlcts->end(); j++) {
00768     const CSCDetId& id = (*j).first;
00769     std::vector<CSCCorrelatedLCTDigi>::const_iterator digiIt = (*j).second.first;
00770     std::vector<CSCCorrelatedLCTDigi>::const_iterator last = (*j).second.second;
00771     for( ; digiIt != last; ++digiIt) {
00772       // Valid digi in the chamber (or in neighbouring chamber) 
00773       if((*digiIt).isValid()){
00774         allCorrLCT[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh] = true;
00775       }
00776     }
00777   }
00778 }
00779 //
00780 void CSCEfficiency::fillWG_info(edm::Handle<CSCWireDigiCollection> &wires, edm::ESHandle<CSCGeometry> &cscGeom){
00781   //---- WIRE GROUPS
00782   for (CSCWireDigiCollection::DigiRangeIterator j=wires->begin(); j!=wires->end(); j++) {
00783     CSCDetId id = (CSCDetId)(*j).first;
00784     const CSCLayer *layer_p = cscGeom->layer (id);
00785     const CSCLayerGeometry *layerGeom = layer_p->geometry ();
00786     //
00787 
00788     const std::vector<float> LayerBounds = layerGeom->parameters ();
00789     std::vector<CSCWireDigi>::const_iterator digiItr = (*j).second.first;
00790     std::vector<CSCWireDigi>::const_iterator last = (*j).second.second;
00791     //
00792     for( ; digiItr != last; ++digiItr) {
00793       std::pair < int, float > WG_pos(digiItr->getWireGroup(), layerGeom->yOfWireGroup(digiItr->getWireGroup())); 
00794       std::pair <std::pair < int, float >, int >  LayerSignal(WG_pos, digiItr->getTimeBin()); 
00795       
00796       //---- AllWG contains basic information about WG (WG number and Y-position, time bin)
00797       allWG[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh]
00798         [id.layer()-1].push_back(LayerSignal);
00799       if(printalot){
00800         //std::cout<<" WG check : "<<std::endl;
00801         //printf("\t\tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",id.endcap(),id.station(),id.ring(),id.chamber(),id.layer());
00802         //std::cout<<" WG size = "<<allWG[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh]
00803         //[id.layer()-1].size()<<std::endl;
00804       }
00805     }
00806   }
00807 }
00808 void CSCEfficiency::fillStrips_info(edm::Handle<CSCStripDigiCollection> &strips){
00809   //---- STRIPS
00810   for (CSCStripDigiCollection::DigiRangeIterator j=strips->begin(); j!=strips->end(); j++) {
00811     CSCDetId id = (CSCDetId)(*j).first;
00812     int largestADCValue = -1;
00813     int largestStrip = -1;
00814     std::vector<CSCStripDigi>::const_iterator digiItr = (*j).second.first;
00815     std::vector<CSCStripDigi>::const_iterator last = (*j).second.second;
00816     for( ; digiItr != last; ++digiItr) {
00817       int maxADC=largestADCValue;
00818       int myStrip = digiItr->getStrip();
00819       std::vector<int> myADCVals = digiItr->getADCCounts();
00820       bool thisStripFired = false;
00821       float thisPedestal = 0.5*(float)(myADCVals[0]+myADCVals[1]);
00822       float threshold = 13.3 ;
00823       float diff = 0.;
00824       float peakADC  = -1000.;
00825       int peakTime = -1;
00826       for (unsigned int iCount = 0; iCount < myADCVals.size(); iCount++) {
00827         diff = (float)myADCVals[iCount]-thisPedestal;
00828         if (diff > threshold) { 
00829           thisStripFired = true; 
00830           if (myADCVals[iCount] > largestADCValue) {
00831             largestADCValue = myADCVals[iCount];
00832             largestStrip = myStrip;
00833           }
00834         }
00835         if (diff > threshold && diff > peakADC) {
00836           peakADC  = diff;
00837           peakTime = iCount;
00838         }
00839       }
00840       if(largestADCValue>maxADC){// FIX IT!!!
00841         maxADC = largestADCValue;
00842         std::pair <int, float> LayerSignal (myStrip, peakADC);
00843         
00844         //---- AllStrips contains basic information about strips 
00845         //---- (strip number and peak signal for most significant strip in the layer) 
00846         allStrips[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-1][id.layer()-1].clear();
00847         allStrips[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-1][id.layer()-1].push_back(LayerSignal);
00848       }
00849     }
00850   }
00851 }
00852 void CSCEfficiency::fillSimhit_info(edm::Handle<edm::PSimHitContainer> &simhits){
00853   //---- SIMHITS
00854   edm::PSimHitContainer::const_iterator dSHsimIter;
00855   for (dSHsimIter = simhits->begin(); dSHsimIter != simhits->end(); dSHsimIter++){
00856     // Get DetID for this simHit:
00857     CSCDetId sId = (CSCDetId)(*dSHsimIter).detUnitId();
00858     std::pair <LocalPoint, int> simHitPos((*dSHsimIter).localPosition(), (*dSHsimIter).particleType());  
00859     allSimhits[sId.endcap()-1][sId.station()-1][sId.ring()-1][sId.chamber()-FirstCh][sId.layer()-1].push_back(simHitPos);
00860   }
00861 }
00862 //
00863 void CSCEfficiency::fillRechitsSegments_info(edm::Handle<CSCRecHit2DCollection> &rechits,
00864                                              edm::Handle<CSCSegmentCollection> &segments,
00865                                              edm::ESHandle<CSCGeometry> &cscGeom
00866                                              ){
00867   //---- RECHITS AND SEGMENTS
00868   //---- Loop over rechits 
00869   if (printalot){ 
00870     //printf("\tGet the recHits collection.\t ");
00871     printf("  The size of the rechit collection is %i\n",int(rechits->size()));
00872     //printf("\t...start loop over rechits...\n");
00873   }
00874   recHitsPerEvent->Fill(rechits->size());
00875   //---- Build iterator for rechits and loop :
00876   CSCRecHit2DCollection::const_iterator recIt;
00877   for (recIt = rechits->begin(); recIt != rechits->end(); recIt++) {
00878     //---- Find chamber with rechits in CSC 
00879     CSCDetId id = (CSCDetId)(*recIt).cscDetId();
00880     if (printalot){
00881       const CSCLayer* csclayer = cscGeom->layer( id);
00882       LocalPoint rhitlocal = (*recIt).localPosition();  
00883       LocalError rerrlocal = (*recIt).localPositionError();  
00884       GlobalPoint rhitglobal= csclayer->toGlobal(rhitlocal);
00885       printf("\t\tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",id.endcap(),id.station(),id.ring(),id.chamber(),id.layer());
00886       printf("\t\tx,y,z: %f, %f, %f\texx,eey,exy: %f, %f, %f\tglobal x,y,z: %f, %f, %f \n",
00887              rhitlocal.x(), rhitlocal.y(), rhitlocal.z(), rerrlocal.xx(), rerrlocal.yy(), rerrlocal.xy(),
00888              rhitglobal.x(), rhitglobal.y(), rhitglobal.z());
00889     }
00890     std::pair <LocalPoint, bool> recHitPos((*recIt).localPosition(), false);  
00891     allRechits[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh][id.layer()-1].push_back(recHitPos);
00892   }
00893   //---- "Empty" chambers
00894   for(int iE=0;iE<2;iE++){
00895     for(int iS=0;iS<4;iS++){
00896       for(int iR=0;iR<4;iR++){
00897         for(int iC=0;iC<NumCh;iC++){
00898           int numLayers = 0;
00899           for(int iL=0;iL<6;iL++){
00900             if(allRechits[iE][iS][iR][iC][iL].size()){
00901               ++numLayers; 
00902             }
00903           }
00904           if(numLayers>1){
00905             emptyChambers[iE][iS][iR][iC] = false;
00906           }
00907           else{
00908             emptyChambers[iE][iS][iR][iC] = true;
00909           }
00910         }
00911       }
00912     }
00913   }
00914 
00915   //
00916   if (printalot){
00917     printf("  The size of the segment collection is %i\n", int(segments->size()));
00918     //printf("\t...start loop over segments...\n");
00919   }
00920   segmentsPerEvent->Fill(segments->size());
00921   for(CSCSegmentCollection::const_iterator it = segments->begin(); it != segments->end(); it++) {
00922     CSCDetId id  = (CSCDetId)(*it).cscDetId();
00923     StHist[id.endcap()-1][id.station()-1].segmentChi2_ndf->Fill((*it).chi2()/(*it).degreesOfFreedom());
00924     StHist[id.endcap()-1][id.station()-1].hitsInSegment->Fill((*it).nRecHits());
00925     if (printalot){ 
00926       printf("\tendcap/station/ring/chamber: %i %i %i %i\n",
00927              id.endcap(),id.station(),id.ring(),id.chamber());
00928       std::cout<<"\tposition(loc) = "<<(*it).localPosition()<<" error(loc) = "<<(*it).localPositionError()<<std::endl;
00929       std::cout<<"\t chi2/ndf = "<<(*it).chi2()/(*it).degreesOfFreedom()<<" nhits = "<<(*it).nRecHits() <<std::endl;
00930 
00931     }
00932     allSegments[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh].push_back
00933       (make_pair((*it).localPosition(), (*it).localDirection()));
00934 
00935 
00936     //---- try to get the CSC recHits that contribute to this segment.
00937     //if (printalot) printf("\tGet the recHits for this segment.\t");
00938     std::vector<CSCRecHit2D> theseRecHits = (*it).specificRecHits();
00939     int nRH = (*it).nRecHits();
00940     if (printalot){
00941       printf("\tGet the recHits for this segment.\t");
00942       printf("    nRH = %i\n",nRH);
00943     }
00944     //---- Find which of the rechits in the chamber is in the segment
00945     int layerRH = 0;
00946     for ( vector<CSCRecHit2D>::const_iterator iRH = theseRecHits.begin(); iRH != theseRecHits.end(); iRH++) {
00947       ++layerRH;
00948       CSCDetId idRH = (CSCDetId)(*iRH).cscDetId();
00949       if(printalot){ 
00950         printf("\t%i RH\tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",
00951                layerRH,idRH.endcap(),idRH.station(),idRH.ring(),idRH.chamber(),idRH.layer());
00952       }
00953       for(size_t jRH = 0; 
00954           jRH<allRechits[idRH.endcap()-1][idRH.station()-1][idRH.ring()-1][idRH.chamber()-FirstCh][idRH.layer()-1].size();
00955           ++jRH){
00956         LocalPoint lp = allRechits[idRH.endcap()-1][idRH.station()-1][idRH.ring()-1][idRH.chamber()-FirstCh][idRH.layer()-1][jRH].first;
00957         float xDiff = iRH->localPosition().x() - 
00958           allRechits[idRH.endcap()-1][idRH.station()-1][idRH.ring()-1][idRH.chamber()-FirstCh][idRH.layer()-1][jRH].first.x();
00959         float yDiff = iRH->localPosition().y() - 
00960           allRechits[idRH.endcap()-1][idRH.station()-1][idRH.ring()-1][idRH.chamber()-FirstCh][idRH.layer()-1][jRH].first.y();
00961         if(fabs(xDiff)<0.0001 && fabs(yDiff)<0.0001){
00962           std::pair <LocalPoint, bool> 
00963             recHitPos(allRechits[idRH.endcap()-1][idRH.station()-1][idRH.ring()-1][idRH.chamber()-FirstCh][idRH.layer()-1][jRH].first, true);
00964           allRechits[idRH.endcap()-1][idRH.station()-1][idRH.ring()-1][idRH.chamber()-FirstCh][idRH.layer()-1][jRH] = recHitPos;
00965           if(printalot){ 
00966             std::cout<<" number of the rechit (from zero) in the segment = "<< jRH<<std::endl;
00967           }
00968         }
00969       }
00970     }
00971   }
00972 }
00973 //
00974 
00975 void CSCEfficiency::ringCandidates(int station, float feta, std::map <std::string, bool> & chamberTypes){
00976   // yeah, hardcoded again...
00977   switch (station){
00978   case 1:
00979     if(feta>0.85 && feta<1.18){//ME13
00980       chamberTypes["ME13"] = true;
00981     }      
00982     if(feta>1.18 && feta<1.7){//ME12
00983       chamberTypes["ME12"] = true;
00984     }
00985     if(feta>1.5 && feta<2.45){//ME11
00986       chamberTypes["ME11"] = true;
00987     }
00988     break;
00989   case 2:
00990     if(feta>0.95 && feta<1.6){//ME22
00991       chamberTypes["ME22"] = true;
00992       
00993     }  
00994     if(feta>1.55 && feta<2.45){//ME21
00995       chamberTypes["ME21"] = true;
00996     }
00997     break;
00998   case 3:
00999     if(feta>1.08 && feta<1.72){//ME32
01000       chamberTypes["ME32"] = true;
01001       
01002     }  
01003     if(feta>1.69 && feta<2.45){//ME31
01004       chamberTypes["ME31"] = true;
01005     }
01006     break;
01007   case 4:
01008     if(feta>1.78 && feta<2.45){//ME41
01009       chamberTypes["ME41"] = true;
01010     }
01011     break;
01012   default:
01013     break;
01014   }
01015 }
01016 //
01017 void CSCEfficiency::chamberCandidates(int station, int ring, float phi, std::vector <int> &coupleOfChambers){
01018   coupleOfChambers.clear();
01019   // -pi< phi<+pi
01020   float phi_zero = 0.;// check! the phi at the "edge" of Ch 1
01021   float phi_const = 2.*M_PI/36.;
01022   int last_chamber = 36;
01023   int first_chamber = 1;
01024   if(1 != station && 1==ring){ // 18 chambers in the ring
01025     phi_const*=2;
01026     last_chamber /= 2;
01027   }
01028   if(phi<0.){
01029     if (printalot) std::cout<<" info: negative phi = "<<phi<<std::endl;
01030     phi += 2*M_PI;
01031   }
01032   float chamber_float = (phi - phi_zero)/phi_const;
01033   int chamber_int = int(chamber_float);
01034   if (chamber_float - float(chamber_int) -0.5 <0.){
01035     if(0!=chamber_int ){
01036       coupleOfChambers.push_back(chamber_int);
01037     }
01038     else{
01039       coupleOfChambers.push_back(last_chamber);
01040     }
01041     coupleOfChambers.push_back(chamber_int+1); 
01042     
01043   }
01044   else{
01045     coupleOfChambers.push_back(chamber_int+1);
01046     if(last_chamber!=chamber_int+1){
01047       coupleOfChambers.push_back(chamber_int+2);
01048     } 
01049     else{
01050       coupleOfChambers.push_back(first_chamber);
01051     }
01052   }
01053   if (printalot) std::cout<<" phi = "<<phi<<" phi_zero = "<<phi_zero<<" phi_const = "<<phi_const<<
01054     " candidate chambers: first ch = "<<coupleOfChambers[0]<<" second ch = "<<coupleOfChambers[1]<<std::endl;
01055 }
01056 
01057 //
01058 bool CSCEfficiency::efficienciesPerChamber(CSCDetId & id, const CSCChamber* cscChamber, FreeTrajectoryState &ftsChamber){
01059   int ec, st, rg, ch, secondRing;
01060   returnTypes(id, ec, st, rg, ch, secondRing);
01061 
01062   LocalVector localDir = cscChamber->toLocal(ftsChamber.momentum());
01063   if(printalot){
01064     std::cout<<" global dir = "<<ftsChamber.momentum()<<std::endl;
01065     std::cout<<" local dir = "<<localDir<<std::endl;
01066     std::cout<<" local theta = "<<localDir.theta()<<std::endl;
01067   }
01068   float dxdz = localDir.x()/localDir.z();
01069   float dydz = localDir.y()/localDir.z();
01070   if(2==st || 3==st){
01071     if(printalot){
01072       std::cout<<"st 3 or 4 ... flip dy/dz"<<std::endl;
01073     }
01074     dydz = - dydz;
01075   }
01076   if(printalot){
01077     std::cout<<"dy/dz = "<<dydz<<std::endl;
01078   }
01079   // Apply angle cut
01080   bool out = true;
01081   if(applyIPangleCuts){
01082     if(dydz>local_DY_DZ_Max || dydz<local_DY_DZ_Min || fabs(dxdz)>local_DX_DZ_Max){ 
01083       out = false;
01084     }
01085   }
01086 
01087   // Segments
01088   bool firstCondition = allSegments[ec][st][rg][ch].size() ? true : false;
01089   bool secondCondition = false;
01090   //---- ME1 is special as usual - ME1a and ME1b are actually one chamber
01091   if(secondRing>-1){
01092     secondCondition = allSegments[ec][st][secondRing][ch].size() ? true : false;
01093   }
01094   if(firstCondition || secondCondition){
01095     if(out){
01096       ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(1);
01097     }  
01098   }
01099   else{
01100     if(out){
01101       ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(0);
01102     } 
01103   }
01104 
01105   bool missingALCT = false;
01106   bool missingCLCT = false;
01107   if(useDigis){
01108     // ALCTs
01109     firstCondition = allALCT[ec][st][rg][ch];
01110     secondCondition = false;
01111     if(secondRing>-1){
01112       secondCondition = allALCT[ec][st][secondRing][ch];
01113     }
01114     if(firstCondition || secondCondition){
01115       if(out){
01116         ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(3);
01117       }
01118       // always apply partial angle cuts for this kind of histos 
01119       if(fabs(dxdz)<local_DX_DZ_Max){
01120         StHist[ec][st].EfficientALCT_momTheta->Fill(ftsChamber.momentum().theta());
01121         ChHist[ec][st][rg][ch].EfficientALCT_dydz->Fill(dydz);
01122       }
01123     }
01124     else{
01125       missingALCT = true; 
01126       if(out){
01127         ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(2);
01128       }
01129       if(fabs(dxdz)<local_DX_DZ_Max){ 
01130         StHist[ec][st].InefficientALCT_momTheta->Fill(ftsChamber.momentum().theta());
01131         ChHist[ec][st][rg][ch].InefficientALCT_dydz->Fill(dydz);
01132       }
01133       if(printalot){
01134         std::cout<<" missing ALCT (dy/dz = "<<dydz<<")";
01135         printf("\t\tendcap/station/ring/chamber: %i/%i/%i/%i\n",ec+1,st+1,rg+1,ch+1);
01136       }
01137     }
01138     
01139     // CLCTs
01140     firstCondition = allCLCT[ec][st][rg][ch];
01141     secondCondition = false;
01142     if(secondRing>-1){
01143       secondCondition = allCLCT[ec][st][secondRing][ch];
01144     }
01145     if(firstCondition || secondCondition){
01146       if(out){
01147         ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(5);
01148       }
01149       if(dydz<local_DY_DZ_Max && dydz>local_DY_DZ_Min){
01150         StHist[ec][st].EfficientCLCT_momPhi->Fill(ftsChamber.momentum().phi() );// - phi chamber...
01151         ChHist[ec][st][rg][ch].EfficientCLCT_dxdz->Fill(dxdz);
01152       }
01153     }
01154     else{
01155       missingCLCT = true;
01156       if(out){
01157         ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(4);
01158       }
01159       if(dydz<local_DY_DZ_Max && dydz>local_DY_DZ_Min){ 
01160         StHist[ec][st].InefficientCLCT_momPhi->Fill(ftsChamber.momentum().phi());// - phi chamber...
01161         ChHist[ec][st][rg][ch].InefficientCLCT_dxdz->Fill(dxdz);
01162       }
01163       if(printalot){
01164         std::cout<<" missing CLCT  (dx/dz = "<<dxdz<<")";
01165         printf("\t\tendcap/station/ring/chamber: %i/%i/%i/%i\n",ec+1,st+1,rg+1,ch+1);
01166       }
01167     }
01168     if(out){
01169       // CorrLCTs
01170       firstCondition = allCorrLCT[ec][st][rg][ch];
01171       secondCondition = false;
01172       if(secondRing>-1){
01173         secondCondition = allCorrLCT[ec][st][secondRing][ch];
01174       }
01175       if(firstCondition || secondCondition){
01176         ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(7);
01177       }
01178       else{
01179         ChHist[ec][st][rg][ch].digiAppearanceCount->Fill(6);
01180       }
01181     }
01182   }
01183   return out;
01184 }
01185 
01186 //
01187 bool CSCEfficiency::stripWire_Efficiencies(CSCDetId & id, FreeTrajectoryState &ftsChamber){
01188   int ec, st, rg, ch, secondRing;
01189   returnTypes(id, ec, st, rg, ch, secondRing);
01190 
01191   bool firstCondition, secondCondition;
01192   int missingLayers_s = 0;
01193   int missingLayers_wg = 0;
01194   for(int iLayer=0;iLayer<6;iLayer++){
01195  //----Strips
01196     if(printalot){ 
01197       printf("\t%i swEff: \tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",
01198              iLayer + 1,id.endcap(),id.station(),id.ring(),id.chamber(),iLayer+1);
01199       std::cout<<" size S = "<<allStrips[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh][iLayer].size()<<
01200         "size W = "<<allWG[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh][iLayer].size()<<std::endl;
01201 
01202     }
01203     firstCondition = allStrips[ec][st][rg][ch][iLayer].size() ? true : false;
01204     //allSegments[ec][st][rg][ch].size() ? true : false;
01205     secondCondition = false;
01206     if(secondRing>-1){
01207       secondCondition = allStrips[ec][st][secondRing][ch][iLayer].size() ? true : false;
01208     }
01209     if(firstCondition || secondCondition){
01210       ChHist[ec][st][rg][ch].EfficientStrips->Fill(iLayer+1);
01211     }
01212     else{
01213       if(printalot){ 
01214         std::cout<<"missing strips ";
01215         printf("\t\tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",id.endcap(),id.station(),id.ring(),id.chamber(),iLayer+1);
01216       }
01217     }
01218     // Wires
01219     firstCondition = allWG[ec][st][rg][ch][iLayer].size() ? true : false;
01220     secondCondition = false;
01221     if(secondRing>-1){
01222       secondCondition = allWG[ec][st][secondRing][ch][iLayer].size() ? true : false;
01223     }
01224     if(firstCondition || secondCondition){
01225       ChHist[ec][st][rg][ch].EfficientWireGroups->Fill(iLayer+1);
01226     }
01227     else{
01228       if(printalot){ 
01229         std::cout<<"missing wires ";
01230         printf("\t\tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",id.endcap(),id.station(),id.ring(),id.chamber(),iLayer+1);
01231       }
01232     }
01233   }
01234   // Normalization
01235   if(6!=missingLayers_s){
01236     ChHist[ec][st][rg][ch].EfficientStrips->Fill(8);
01237   }
01238   if(6!=missingLayers_wg){
01239     ChHist[ec][st][rg][ch].EfficientWireGroups->Fill(8);
01240   }
01241   ChHist[ec][st][rg][ch].EfficientStrips->Fill(9);
01242   ChHist[ec][st][rg][ch].EfficientWireGroups->Fill(9);
01243 //
01244   ChHist[ec][st][rg][ch].StripWiresCorrelations->Fill(1);
01245   if(missingLayers_s!=missingLayers_wg){
01246     ChHist[ec][st][rg][ch].StripWiresCorrelations->Fill(2);
01247     if(6==missingLayers_wg){
01248       ChHist[ec][st][rg][ch].StripWiresCorrelations->Fill(3);
01249       ChHist[ec][st][rg][ch].NoWires_momTheta->Fill(ftsChamber.momentum().theta());
01250     }
01251     if(6==missingLayers_s){
01252       ChHist[ec][st][rg][ch].StripWiresCorrelations->Fill(4);
01253       ChHist[ec][st][rg][ch].NoStrips_momPhi->Fill(ftsChamber.momentum().theta());
01254     }
01255   }
01256   else if(6==missingLayers_s){
01257     ChHist[ec][st][rg][ch].StripWiresCorrelations->Fill(5);
01258   }
01259 
01260   return true;  
01261 }
01262 //
01263 bool CSCEfficiency::recSimHitEfficiency(CSCDetId & id, FreeTrajectoryState &ftsChamber){
01264   int ec, st, rg, ch, secondRing;
01265   returnTypes(id, ec, st, rg, ch, secondRing);
01266   bool firstCondition, secondCondition;
01267   for(int iLayer=0; iLayer<6;iLayer++){
01268     firstCondition = allSimhits[ec][st][rg][ch][iLayer].size() ? true : false;
01269     secondCondition = false;
01270     int thisRing = rg;
01271     if(secondRing>-1){
01272       secondCondition = allSimhits[ec][st][secondRing][ch][iLayer].size() ? true : false;
01273       if(secondCondition){
01274         thisRing = secondRing;
01275       }
01276     }
01277     if(firstCondition || secondCondition){
01278       for(size_t iSH=0;
01279           iSH<allSimhits[ec][st][thisRing][ch][iLayer].size();
01280           ++iSH){
01281         if(13 ==
01282            fabs(allSimhits[ec][st][thisRing][ch][iLayer][iSH].second)){
01283           ChHist[ec][st][rg][ch].SimSimhits->Fill(iLayer+1);
01284           if(allRechits[ec][st][thisRing][ch][iLayer].size()){
01285             ChHist[ec][st][rg][ch].SimRechits->Fill(iLayer+1);
01286           }
01287           break;
01288         }
01289       }
01290       //---- Next is not too usefull... 
01291       /*
01292       for(unsigned int iSimHits=0;
01293           iSimHits<allSimhits[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh][iLayer].size();
01294           iSimHits++){
01295         ChHist[ec][st][rg][id.chamber()-FirstCh].SimSimhits_each->Fill(iLayer+1);
01296       }
01297       for(unsigned int iRecHits=0;
01298           iRecHits<allRechits[id.endcap()-1][id.station()-1][id.ring()-1][id.chamber()-FirstCh][iLayer].size();
01299           iRecHits++){
01300         ChHist[ec][st][rg][id.chamber()-FirstCh].SimRechits_each->Fill(iLayer+1);
01301       }
01302       */
01303       //
01304     }   
01305   }
01306   return true;
01307 }
01308 
01309 //
01310 bool CSCEfficiency::recHitSegment_Efficiencies(CSCDetId & id, const CSCChamber* cscChamber, FreeTrajectoryState &ftsChamber){
01311   int ec, st, rg, ch, secondRing;
01312   returnTypes(id, ec, st, rg, ch, secondRing);
01313   bool firstCondition, secondCondition;
01314 
01315   std::vector <bool> missingLayers_rh(6);
01316   std::vector <int> usedInSegment(6);
01317   // Rechits
01318   if(printalot) std::cout<<"RecHits eff"<<std::endl;
01319   for(int iLayer=0;iLayer<6;++iLayer){
01320     firstCondition = allRechits[ec][st][rg][ch][iLayer].size() ? true : false;
01321     secondCondition = false;
01322     int thisRing = rg;
01323     if(secondRing>-1){
01324       secondCondition = allRechits[ec][st][secondRing][ch][iLayer].size() ? true : false;
01325       if(secondCondition){
01326         thisRing = secondRing;
01327       }
01328     }
01329     if(firstCondition || secondCondition){
01330       ChHist[ec][st][rg][ch].EfficientRechits_good->Fill(iLayer+1);
01331       for(size_t iR=0;
01332           iR<allRechits[ec][st][thisRing][ch][iLayer].size();
01333           ++iR){
01334         if(allRechits[ec][st][thisRing][ch][iLayer][iR].second){
01335           usedInSegment[iLayer] = 1;
01336           break;
01337         }
01338         else{
01339           usedInSegment[iLayer] = -1;
01340         }
01341       }
01342     }
01343     else{
01344       missingLayers_rh[iLayer] = true;
01345       if(printalot){
01346         std::cout<<"missing rechits ";
01347         printf("\t\tendcap/station/ring/chamber/layer: %i/%i/%i/%i/%i\n",id.endcap(),id.station(),id.ring(),id.chamber(),iLayer+1);
01348       }
01349     }
01350   }
01351   GlobalVector globalDir;
01352   GlobalPoint globalPos;
01353  // Segments
01354   firstCondition = allSegments[ec][st][rg][ch].size() ? true : false;
01355   secondCondition = false;
01356   int secondSize = 0;
01357   int thisRing = rg;
01358   if(secondRing>-1){
01359     secondCondition = allSegments[ec][st][secondRing][ch].size() ? true : false;
01360     secondSize = allSegments[ec][st][secondRing][ch].size();
01361     if(secondCondition){
01362       thisRing = secondRing;
01363     }
01364   }
01365   if(firstCondition || secondCondition){
01366     if (printalot) std::cout<<"segments - start ec = "<<ec<<" st = "<<st<<" rg = "<<rg<<" ch = "<<ch<<std::endl;
01367     StHist[ec][st].EfficientSegments_XY->Fill(ftsChamber.position().x(),ftsChamber.position().y());
01368     if(1==allSegments[ec][st][rg][ch].size() + secondSize){
01369       globalDir = cscChamber->toGlobal(allSegments[ec][st][thisRing][ch][0].second);
01370       globalPos = cscChamber->toGlobal(allSegments[ec][st][thisRing][ch][0].first);
01371       StHist[ec][st].EfficientSegments_eta->Fill(fabs(ftsChamber.position().eta()));
01372       double residual = sqrt(pow(ftsChamber.position().x() - globalPos.x(),2)+
01373                              pow(ftsChamber.position().y() - globalPos.y(),2)+
01374                              pow(ftsChamber.position().z() - globalPos.z(),2));
01375       if (printalot) std::cout<<" fts.position() = "<<ftsChamber.position()<<" segPos = "<<globalPos<<" res = "<<residual<< std::endl;
01376       StHist[ec][st].ResidualSegments->Fill(residual);
01377     }
01378     for(int iLayer=0;iLayer<6;++iLayer){
01379       if(printalot) std::cout<<" iLayer = "<<iLayer<<" usedInSegment = "<<usedInSegment[iLayer]<<std::endl;
01380       if(0!=usedInSegment[iLayer]){
01381         if(-1==usedInSegment[iLayer]){
01382           ChHist[ec][st][rg][ch].InefficientSingleHits->Fill(iLayer+1);
01383         }
01384         ChHist[ec][st][rg][ch].AllSingleHits->Fill(iLayer+1);
01385       }
01386       firstCondition = allRechits[ec][st][rg][ch][iLayer].size() ? true : false;
01387       secondCondition = false;
01388       if(secondRing>-1){
01389         secondCondition = allRechits[ec][st][secondRing][ch][iLayer].size() ? true : false;
01390       }
01391       float stripAngle = 99999.;
01392       std::vector<float> posXY(2);
01393       bool oneSegment = false;
01394       if(1==allSegments[ec][st][rg][ch].size() + secondSize){
01395         oneSegment = true;
01396         const BoundPlane bp = cscChamber->layer(iLayer+1)->surface();
01397         linearExtrapolation(globalPos,globalDir, bp.position().z(), posXY);
01398         GlobalPoint gp_extrapol( posXY.at(0), posXY.at(1),bp.position().z());
01399         const LocalPoint lp_extrapol = cscChamber->layer(iLayer+1)->toLocal(gp_extrapol);
01400         posXY.at(0) = lp_extrapol.x();
01401         posXY.at(1) = lp_extrapol.y();
01402         int nearestStrip = cscChamber->layer(iLayer+1)->geometry()->nearestStrip(lp_extrapol);
01403         stripAngle = cscChamber->layer(iLayer+1)->geometry()->stripAngle(nearestStrip) - M_PI/2. ;
01404       }
01405       if(firstCondition || secondCondition){
01406         ChHist[ec][st][rg][ch].EfficientRechits_inSegment->Fill(iLayer+1);
01407         if(oneSegment){
01408           ChHist[ec][st][rg][ch].Y_EfficientRecHits_inSegment[iLayer]->Fill(posXY.at(1));
01409           ChHist[ec][st][rg][ch].Phi_EfficientRecHits_inSegment[iLayer]->Fill(stripAngle);
01410         }
01411       }
01412       else{
01413         if(oneSegment){
01414           ChHist[ec][st][rg][ch].Y_InefficientRecHits_inSegment[iLayer]->Fill(posXY.at(1));
01415           ChHist[ec][st][rg][ch].Phi_InefficientRecHits_inSegment[iLayer]->Fill(stripAngle);
01416         }
01417       }
01418     }
01419   }
01420   else{
01421     StHist[ec][st].InefficientSegments_XY->Fill(ftsChamber.position().x(),ftsChamber.position().y());
01422     if(printalot){
01423       std::cout<<"missing segment "<<std::endl;
01424       printf("\t\tendcap/station/ring/chamber: %i/%i/%i/%i\n",id.endcap(),id.station(),id.ring(),id.chamber());
01425       std::cout<<" fts.position() = "<<ftsChamber.position()<<std::endl;
01426     }
01427   }
01428   // Normalization
01429   ChHist[ec][st][rg][ch].EfficientRechits_good->Fill(8);
01430   if(allSegments[ec][st][rg][ch].size()+secondSize<2){
01431     StHist[ec][st].AllSegments_eta->Fill(fabs(ftsChamber.position().eta()));
01432   }
01433   ChHist[ec][st][rg][id.chamber()-FirstCh].EfficientRechits_inSegment->Fill(9);
01434 
01435   return true;
01436 }
01437 //
01438 void CSCEfficiency::returnTypes(CSCDetId & id, int &ec, int &st, int &rg, int &ch, int &secondRing){
01439   ec = id.endcap()-1;
01440   st = id.station()-1;
01441   rg = id.ring()-1;
01442   secondRing = -1;
01443   if(1==id.station() && (4==id.ring() || 1==id.ring()) ){
01444     rg = 0;
01445     secondRing = 3;
01446   }
01447   ch = id.chamber()-FirstCh;
01448 }
01449 
01450 //
01451 void CSCEfficiency::getFromFTS( const FreeTrajectoryState& fts,
01452                                 CLHEP::Hep3Vector& p3, CLHEP::Hep3Vector& r3,
01453                                 int& charge, AlgebraicSymMatrix66& cov){
01454   
01455   GlobalVector p3GV = fts.momentum();
01456   GlobalPoint r3GP = fts.position();
01457 
01458   p3.set(p3GV.x(), p3GV.y(), p3GV.z());
01459   r3.set(r3GP.x(), r3GP.y(), r3GP.z());
01460 
01461   charge = fts.charge();
01462   cov = fts.hasError() ? fts.cartesianError().matrix() : AlgebraicSymMatrix66();
01463 
01464 }
01465 
01466 FreeTrajectoryState CSCEfficiency::getFromCLHEP(const CLHEP::Hep3Vector& p3, const CLHEP::Hep3Vector& r3,
01467                                                        int charge, const AlgebraicSymMatrix66& cov,
01468                                                        const MagneticField* field){
01469 
01470   GlobalVector p3GV(p3.x(), p3.y(), p3.z());
01471   GlobalPoint r3GP(r3.x(), r3.y(), r3.z());
01472   GlobalTrajectoryParameters tPars(r3GP, p3GV, charge, field);
01473 
01474   CartesianTrajectoryError tCov(cov);
01475 
01476   return cov.kRows == 6 ? FreeTrajectoryState(tPars, tCov) : FreeTrajectoryState(tPars) ;
01477 }
01478 
01479 void CSCEfficiency::linearExtrapolation(GlobalPoint initialPosition ,GlobalVector initialDirection, 
01480                                         float zSurface, std::vector <float> &posZY){
01481   double paramLine =  lineParameter(initialPosition.z(), zSurface, initialDirection.z());
01482   double xPosition = extrapolate1D(initialPosition.x(), initialDirection.x(),paramLine);
01483   double yPosition = extrapolate1D(initialPosition.y(), initialDirection.y(),paramLine);
01484   posZY.clear();
01485   posZY.push_back(xPosition);
01486   posZY.push_back(yPosition);
01487 }
01488 //
01489 double CSCEfficiency::extrapolate1D(double initPosition, double initDirection, double parameterOfTheLine){
01490   double extrapolatedPosition = initPosition + initDirection*parameterOfTheLine;
01491   return extrapolatedPosition;
01492 }
01493 //
01494 double CSCEfficiency::lineParameter(double initZPosition, double destZPosition, double initZDirection){
01495   double paramLine = (destZPosition-initZPosition)/initZDirection;
01496   return paramLine;
01497 }
01498 //
01499 void CSCEfficiency::chooseDirection(CLHEP::Hep3Vector & innerPosition, CLHEP::Hep3Vector & outerPosition){
01500 
01501   //---- Be careful with trigger conditions too
01502   if(!isIPdata){
01503     float dy = outerPosition.y() - innerPosition.y();
01504     float dz = outerPosition.z() - innerPosition.z();
01505     if(isBeamdata){
01506       if(dz>0){
01507         alongZ = true;
01508       }
01509       else{
01510         alongZ = false;
01511       }
01512     }
01513     else{//cosmics
01514       if(dy/dz>0){
01515         alongZ = false;
01516       }
01517       else{
01518         alongZ = true;
01519       }
01520     }
01521   }
01522 }
01523 //
01524 const Propagator* CSCEfficiency::propagator(std::string propagatorName) const {
01525   return &*theService->propagator(propagatorName);
01526 }
01527   
01528 //
01529 TrajectoryStateOnSurface CSCEfficiency::propagate(FreeTrajectoryState & ftsStart, const BoundPlane &bpDest){
01530   TrajectoryStateOnSurface tSOSDest;
01531   std::string propagatorName;
01532 /*
01533 // it would work if cosmic muons had properly assigned direction...
01534   bool dzPositive = bpDest.position().z() - ftsStart.position().z() > 0 ? true : false;
01535  //---- Be careful with trigger conditions too
01536   if(!isIPdata){
01537     bool rightDirection = !(alongZ^dzPositive);
01538     if(rightDirection){
01539       if(printalot) std::cout<<" propagate along momentum"<<std::endl;
01540       propagatorName = "SteppingHelixPropagatorAlong";
01541     }
01542     else{
01543       if(printalot) std::cout<<" propagate opposite momentum"<<std::endl;
01544       propagatorName = "SteppingHelixPropagatorOpposite";
01545     }
01546   }
01547   else{
01548     if(printalot) std::cout<<" propagate any (momentum)"<<std::endl;
01549     propagatorName = "SteppingHelixPropagatorAny";
01550   }
01551 */
01552   propagatorName = "SteppingHelixPropagatorAny";
01553   tSOSDest = propagator(propagatorName)->propagate(ftsStart, bpDest);   
01554   return tSOSDest;
01555 }
01556 //
01557 bool CSCEfficiency::applyTrigger(edm::Handle<edm::TriggerResults> &hltR,
01558                                  const edm::TriggerNames & triggerNames){
01559   bool triggerPassed = true;
01560   std::vector<std::string>  hlNames=triggerNames.triggerNames();
01561   pointToTriggers.clear();
01562   for(size_t imyT = 0;imyT<myTriggers.size();++imyT){
01563     for (size_t iT=0; iT<hlNames.size(); ++iT) {
01564       //std::cout<<" iT = "<<iT<<" hlNames[iT] = "<<hlNames[iT]<<
01565       //" : wasrun = "<<hltR->wasrun(iT)<<" accept = "<<
01566       //         hltR->accept(iT)<<" !error = "<< 
01567       //        !hltR->error(iT)<<std::endl; 
01568       if(!imyT){       
01569         if(hltR->wasrun(iT) &&
01570            hltR->accept(iT) &&
01571            !hltR->error(iT) ){
01572            TriggersFired->Fill(iT); 
01573         }
01574       }
01575       if(hlNames[iT]==myTriggers[imyT]){
01576         pointToTriggers.push_back(iT);
01577         if(imyT){
01578           break;
01579         }
01580       }
01581     }
01582   }
01583   if(pointToTriggers.size()!=myTriggers.size()){
01584     pointToTriggers.clear();
01585     if(printalot){
01586       std::cout<<" Not all trigger names found - all trigger specifications will be ignored. Check your cfg file!"<<std::endl;
01587     }
01588   }
01589   else{
01590     if(pointToTriggers.size()){
01591       if(printalot){
01592         std::cout<<"The following triggers will be required in the event: "<<std::endl;
01593         for(size_t imyT =0; imyT <pointToTriggers.size();++imyT){
01594           std::cout<<"  "<<hlNames[pointToTriggers[imyT]];
01595         }
01596         std::cout<<std::endl;
01597         std::cout<<" in condition (AND/OR) : "<<!andOr<<"/"<<andOr<<std::endl;
01598       }
01599     }
01600   }
01601 
01602   if (hltR.isValid()) {
01603     if(!pointToTriggers.size()){
01604       if(printalot){
01605         std::cout<<" No triggers specified in the configuration or all ignored - no trigger information will be considered"<<std::endl;
01606       }
01607     }
01608     for(size_t imyT =0; imyT <pointToTriggers.size();++imyT){
01609       if(hltR->wasrun(pointToTriggers[imyT]) && 
01610          hltR->accept(pointToTriggers[imyT]) && 
01611          !hltR->error(pointToTriggers[imyT]) ){
01612         triggerPassed = true;
01613         if(andOr){
01614           break;
01615         }
01616       }
01617       else{
01618         triggerPassed = false;
01619         if(!andOr){
01620           triggerPassed = false;
01621           break;
01622         }
01623       }
01624     }
01625   }
01626   else{
01627     if(printalot){
01628       std::cout<<" TriggerResults handle returns invalid state?! No trigger information will be considered"<<std::endl;
01629     }
01630   }
01631   if(printalot){
01632     std::cout<<" Trigger passed: "<<triggerPassed<<std::endl;
01633   }
01634   return triggerPassed;
01635 }
01636 //
01637 
01638 // Constructor
01639 CSCEfficiency::CSCEfficiency(const ParameterSet& pset){
01640 
01641   // const float Xmin = -70;
01642   //const float Xmax = 70;
01643   //const int nXbins = int(4.*(Xmax - Xmin));
01644   const float Ymin = -165;
01645   const float Ymax = 165;
01646   const int nYbins = int((Ymax - Ymin)/2);
01647   const float Layer_min = -0.5;
01648   const float Layer_max = 9.5;
01649   const int nLayer_bins = int(Layer_max - Layer_min);
01650   //
01651 
01652   //---- Get the input parameters
01653   printout_NEvents  = pset.getUntrackedParameter<unsigned int>("printout_NEvents",0);
01654   rootFileName     = pset.getUntrackedParameter<string>("rootFileName","cscHists.root");
01655 
01656   isData  = pset.getUntrackedParameter<bool>("runOnData",true);// 
01657   isIPdata  = pset.getUntrackedParameter<bool>("IPdata",false);// 
01658   isBeamdata  = pset.getUntrackedParameter<bool>("Beamdata",false);//
01659   getAbsoluteEfficiency  = pset.getUntrackedParameter<bool>("getAbsoluteEfficiency",true);//
01660   useDigis = pset.getUntrackedParameter<bool>("useDigis", true);// 
01661   distanceFromDeadZone = pset.getUntrackedParameter<double>("distanceFromDeadZone", 10.);// 
01662   minP = pset.getUntrackedParameter<double>("minP",20.);//
01663   maxP = pset.getUntrackedParameter<double>("maxP",100.);//
01664   maxNormChi2 = pset.getUntrackedParameter<double>("maxNormChi2", 3.);//
01665   minTrackHits = pset.getUntrackedParameter<unsigned int>("minTrackHits",10);//
01666 
01667   applyIPangleCuts = pset.getUntrackedParameter<bool>("applyIPangleCuts", false);// 
01668     local_DY_DZ_Max = pset.getUntrackedParameter<double>("local_DY_DZ_Max",-0.1);//
01669       local_DY_DZ_Min = pset.getUntrackedParameter<double>("local_DY_DZ_Min",-0.8);//
01670         local_DX_DZ_Max = pset.getUntrackedParameter<double>("local_DX_DZ_Max",0.2);//
01671 
01672   alctDigiTag_  = pset.getParameter<edm::InputTag>("alctDigiTag") ;
01673   clctDigiTag_  = pset.getParameter<edm::InputTag>("clctDigiTag") ;
01674   corrlctDigiTag_  = pset.getParameter<edm::InputTag>("corrlctDigiTag") ;
01675   stripDigiTag_  = pset.getParameter<edm::InputTag>("stripDigiTag") ;
01676   wireDigiTag_ = pset.getParameter<edm::InputTag>("wireDigiTag") ;
01677   rechitDigiTag_ = pset.getParameter<edm::InputTag>("rechitDigiTag") ;
01678   segmentDigiTag_ = pset.getParameter<edm::InputTag>("segmentDigiTag") ;
01679   simHitTag     = pset.getParameter<edm::InputTag>("simHitTag");
01680   tracksTag = pset.getParameter< edm::InputTag >("tracksTag");
01681 
01682   ParameterSet serviceParameters = pset.getParameter<ParameterSet>("ServiceParameters");
01683   // maybe use the service for getting magnetic field, propagators, etc. ...
01684   theService        = new MuonServiceProxy(serviceParameters);
01685 
01686   // Trigger
01687   useTrigger =  pset.getUntrackedParameter<bool>("useTrigger", false);
01688   hlTriggerResults_ = pset.getParameter<edm::InputTag> ("HLTriggerResults");
01689   myTriggers = pset.getParameter<std::vector <std::string> >("myTriggers");
01690   andOr =  pset.getUntrackedParameter<bool>("andOr");
01691   pointToTriggers.clear();
01692 
01693 
01694   //---- set counter to zero
01695   nEventsAnalyzed = 0;
01696   //---- set presence of magnetic field
01697   magField = true;
01698   //
01699   std::string Path = "AllChambers/";
01700   std::string FullName;
01701   //---- File with output histograms 
01702   theFile = new TFile(rootFileName.c_str(), "RECREATE");
01703   theFile->cd();
01704   //---- Book histograms for the analysis
01705   char SpecName[50];
01706   
01707   sprintf(SpecName,"DataFlow"); 
01708   DataFlow =  
01709     new TH1F(SpecName,"Data flow;condition number;entries",40,-0.5,39.5);
01710   //
01711   sprintf(SpecName,"TriggersFired"); 
01712   TriggersFired =
01713     new TH1F(SpecName,"Triggers fired;trigger number;entries",140,-0.5,139.5);
01714   //
01715   int Chan = 50;
01716   float minChan = -0.5;
01717   float maxChan = 49.5;
01718   //
01719   sprintf(SpecName,"ALCTPerEvent");    
01720   ALCTPerEvent = new TH1F(SpecName,"ALCTs per event;N digis;entries",Chan,minChan,maxChan);
01721   //
01722   sprintf(SpecName,"CLCTPerEvent");    
01723   CLCTPerEvent = new TH1F(SpecName,"CLCTs per event;N digis;entries",Chan,minChan,maxChan);
01724   //
01725     sprintf(SpecName,"recHitsPerEvent");    
01726   recHitsPerEvent = new TH1F(SpecName,"RecHits per event;N digis;entries",150,-0.5,149.5);
01727   //
01728   sprintf(SpecName,"segmentsPerEvent");    
01729   segmentsPerEvent = new TH1F(SpecName,"segments per event;N digis;entries",Chan,minChan,maxChan);
01730   //
01731   //---- Book groups of histograms (for any chamber)
01732 
01733   map<std::string,bool>::iterator iter;  
01734   for(int ec = 0;ec<2;++ec){
01735     for(int st = 0;st<4;++st){
01736       theFile->cd();
01737       sprintf(SpecName,"Stations__E%d_S%d",ec+1, st+1);
01738       theFile->mkdir(SpecName);
01739       theFile->cd(SpecName);
01740 
01741       //
01742       sprintf(SpecName,"segmentChi2_ndf_St%d",st+1);
01743       StHist[ec][st].segmentChi2_ndf = 
01744         new TH1F(SpecName,"Chi2/ndf of a segment;chi2/ndf;entries",100,0.,20.);
01745       //
01746       sprintf(SpecName,"hitsInSegment_St%d",st+1);
01747       StHist[ec][st].hitsInSegment = 
01748         new TH1F(SpecName,"Number of hits in a segment;nHits;entries",7,-0.5,6.5);
01749       //
01750       Chan = 170;
01751       minChan = 0.85;
01752       maxChan = 2.55;
01753       //
01754       sprintf(SpecName,"AllSegments_eta_St%d",st+1);
01755       StHist[ec][st].AllSegments_eta = 
01756         new TH1F(SpecName,"All segments in eta;eta;entries",Chan,minChan,maxChan);
01757       //
01758       sprintf(SpecName,"EfficientSegments_eta_St%d",st+1);
01759       StHist[ec][st].EfficientSegments_eta = 
01760         new TH1F(SpecName,"Efficient segments in eta;eta;entries",Chan,minChan,maxChan);
01761       //
01762       sprintf(SpecName,"ResidualSegments_St%d",st+1);
01763       StHist[ec][st].ResidualSegments = 
01764         new TH1F(SpecName,"Residual (segments);residual,cm;entries",75,0.,15.);
01765       //
01766       Chan = 200;
01767       minChan = -800.;
01768       maxChan = 800.;
01769       int Chan2 = 200;
01770       float minChan2 = -800.;
01771       float maxChan2 = 800.;
01772       
01773       sprintf(SpecName,"EfficientSegments_XY_St%d",st+1);
01774       StHist[ec][st].EfficientSegments_XY = new TH2F(SpecName,"Efficient segments in XY;X;Y",
01775                                                      Chan,minChan,maxChan,Chan2,minChan2,maxChan2);
01776       sprintf(SpecName,"InefficientSegments_XY_St%d",st+1);
01777       StHist[ec][st].InefficientSegments_XY = new TH2F(SpecName,"Inefficient segments in XY;X;Y",
01778                                                        Chan,minChan,maxChan,Chan2,minChan2,maxChan2);
01779       //
01780       Chan = 80;
01781       minChan = 0;
01782       maxChan = 3.2;
01783       sprintf(SpecName,"EfficientALCT_momTheta_St%d",st+1);
01784       StHist[ec][st].EfficientALCT_momTheta = new TH1F(SpecName,"Efficient ALCT in theta (momentum);theta, rad;entries",
01785                                                        Chan,minChan,maxChan);
01786       //
01787       sprintf(SpecName,"InefficientALCT_momTheta_St%d",st+1);
01788       StHist[ec][st].InefficientALCT_momTheta = new TH1F(SpecName,"Inefficient ALCT in theta (momentum);theta, rad;entries",
01789                                                          Chan,minChan,maxChan);
01790       //
01791       Chan = 160;
01792       minChan = -3.2;
01793       maxChan = 3.2;
01794       sprintf(SpecName,"EfficientCLCT_momPhi_St%d",st+1);
01795       StHist[ec][st].EfficientCLCT_momPhi = new TH1F(SpecName,"Efficient CLCT in phi (momentum);phi, rad;entries",
01796                                                      Chan,minChan,maxChan);
01797       //
01798       sprintf(SpecName,"InefficientCLCT_momPhi_St%d",st+1);
01799       StHist[ec][st].InefficientCLCT_momPhi = new TH1F(SpecName,"Inefficient CLCT in phi (momentum);phi, rad;entries",
01800                                                        Chan,minChan,maxChan);
01801       //
01802       theFile->cd();
01803       for(int rg = 0;rg<3;++rg){
01804         if(0!=st && rg>1){
01805           continue;
01806         }
01807         else if(1==rg && 3==st){
01808           continue;
01809         }
01810         for(int iChamber=FirstCh;iChamber<FirstCh+NumCh;iChamber++){
01811           if(0!=st && 0==rg && iChamber >18){
01812             continue;
01813           }
01814           theFile->cd();
01815           sprintf(SpecName,"Chambers__E%d_S%d_R%d_Chamber_%d",ec+1, st+1, rg+1,iChamber);
01816           theFile->mkdir(SpecName);
01817           theFile->cd(SpecName);
01818           //
01819 
01820           sprintf(SpecName,"EfficientRechits_inSegment_Ch%d",iChamber);
01821           ChHist[ec][st][rg][iChamber-FirstCh].EfficientRechits_inSegment = 
01822             new TH1F(SpecName,"Existing RecHit given a segment;layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01823           //
01824           sprintf(SpecName,"InefficientSingleHits_Ch%d",iChamber);
01825           ChHist[ec][st][rg][iChamber-FirstCh].InefficientSingleHits = 
01826             new TH1F(SpecName,"Single RecHits not in the segment;layers (1-6);entries ",nLayer_bins,Layer_min,Layer_max);
01827           //
01828           sprintf(SpecName,"AllSingleHits_Ch%d",iChamber);
01829           ChHist[ec][st][rg][iChamber-FirstCh].AllSingleHits = 
01830             new TH1F(SpecName,"Single RecHits given a segment; layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01831           //
01832           sprintf(SpecName,"digiAppearanceCount_Ch%d",iChamber);
01833           ChHist[ec][st][rg][iChamber-FirstCh].digiAppearanceCount = 
01834             new TH1F(SpecName,"Digi appearance (no-yes): segment(0,1), ALCT(2,3), CLCT(4,5), CorrLCT(6,7); digi type;entries",
01835                      8,-0.5,7.5);
01836           //
01837           Chan = 100;
01838           minChan = -1.1;
01839           maxChan = 0.9;
01840           sprintf(SpecName,"EfficientALCT_dydz_Ch%d",iChamber);
01841           ChHist[ec][st][rg][iChamber-FirstCh].EfficientALCT_dydz = 
01842             new TH1F(SpecName,"Efficient ALCT; local dy/dz (ME 3 and 4 flipped);entries",
01843                      Chan, minChan, maxChan);
01844           //
01845           sprintf(SpecName,"InefficientALCT_dydz_Ch%d",iChamber);
01846           ChHist[ec][st][rg][iChamber-FirstCh].InefficientALCT_dydz = 
01847             new TH1F(SpecName,"Inefficient ALCT; local dy/dz (ME 3 and 4 flipped);entries",
01848                      Chan, minChan, maxChan);
01849           //
01850           Chan = 100;
01851           minChan = -1.;
01852           maxChan = 1.0;
01853           sprintf(SpecName,"EfficientCLCT_dxdz_Ch%d",iChamber);
01854           ChHist[ec][st][rg][iChamber-FirstCh].EfficientCLCT_dxdz = 
01855             new TH1F(SpecName,"Efficient CLCT; local dxdz;entries",
01856                      Chan, minChan, maxChan);
01857           //
01858           sprintf(SpecName,"InefficientCLCT_dxdz_Ch%d",iChamber);
01859           ChHist[ec][st][rg][iChamber-FirstCh].InefficientCLCT_dxdz = 
01860             new TH1F(SpecName,"Inefficient CLCT; local dxdz;entries",
01861                      Chan, minChan, maxChan);
01862           //
01863           sprintf(SpecName,"EfficientRechits_good_Ch%d",iChamber);
01864           ChHist[ec][st][rg][iChamber-FirstCh].EfficientRechits_good = 
01865             new TH1F(SpecName,"Existing RecHit - sensitive area only;layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01866           //
01867           sprintf(SpecName,"EfficientStrips_Ch%d",iChamber);
01868           ChHist[ec][st][rg][iChamber-FirstCh].EfficientStrips = 
01869             new TH1F(SpecName,"Existing strip;layer (1-6); entries",nLayer_bins,Layer_min,Layer_max);
01870           //
01871           sprintf(SpecName,"EfficientWireGroups_Ch%d",iChamber);
01872           ChHist[ec][st][rg][iChamber-FirstCh].EfficientWireGroups = 
01873             new TH1F(SpecName,"Existing WireGroups;layer (1-6); entries ",nLayer_bins,Layer_min,Layer_max);
01874           //
01875           sprintf(SpecName,"StripWiresCorrelations_Ch%d",iChamber);
01876           ChHist[ec][st][rg][iChamber-FirstCh].StripWiresCorrelations = 
01877             new TH1F(SpecName,"StripWire correlations;; entries ",5,0.5,5.5);
01878           //
01879           Chan = 80;
01880           minChan = 0;
01881           maxChan = 3.2;
01882           sprintf(SpecName,"NoWires_momTheta_Ch%d",iChamber);
01883           ChHist[ec][st][rg][iChamber-FirstCh].NoWires_momTheta = 
01884             new TH1F(SpecName,"No wires (all strips present) - in theta (momentum);theta, rad;entries",
01885                      Chan,minChan,maxChan);
01886           //
01887           Chan = 160;
01888           minChan = -3.2;
01889           maxChan = 3.2;
01890           sprintf(SpecName,"NoStrips_momPhi_Ch%d",iChamber);
01891           ChHist[ec][st][rg][iChamber-FirstCh].NoStrips_momPhi = 
01892             new TH1F(SpecName,"No strips (all wires present) - in phi (momentum);phi, rad;entries",
01893                      Chan,minChan,maxChan);
01894           //
01895           for(int iLayer=0; iLayer<6;iLayer++){
01896             sprintf(SpecName,"Y_InefficientRecHits_inSegment_Ch%d_L%d",iChamber,iLayer);
01897             ChHist[ec][st][rg][iChamber-FirstCh].Y_InefficientRecHits_inSegment.push_back
01898               (new TH1F(SpecName,"Missing RecHit/layer in a segment (local system, whole chamber);Y, cm; entries",
01899                         nYbins,Ymin, Ymax));
01900             //
01901             sprintf(SpecName,"Y_EfficientRecHits_inSegment_Ch%d_L%d",iChamber,iLayer);
01902             ChHist[ec][st][rg][iChamber-FirstCh].Y_EfficientRecHits_inSegment.push_back
01903               (new TH1F(SpecName,"Efficient (extrapolated from the segment) RecHit/layer in a segment (local system, whole chamber);Y, cm; entries",
01904                         nYbins,Ymin, Ymax));
01905             //
01906             Chan = 200;
01907             minChan = -0.2;
01908             maxChan = 0.2;
01909             sprintf(SpecName,"Phi_InefficientRecHits_inSegment_Ch%d_L%d",iChamber,iLayer);
01910             ChHist[ec][st][rg][iChamber-FirstCh].Phi_InefficientRecHits_inSegment.push_back
01911               (new TH1F(SpecName,"Missing RecHit/layer in a segment (local system, whole chamber);Phi, rad; entries",
01912                         Chan, minChan, maxChan));
01913             //
01914             sprintf(SpecName,"Phi_EfficientRecHits_inSegment_Ch%d_L%d",iChamber,iLayer);
01915             ChHist[ec][st][rg][iChamber-FirstCh].Phi_EfficientRecHits_inSegment.push_back
01916               (new TH1F(SpecName,"Efficient (extrapolated from the segment) in a segment (local system, whole chamber);Phi, rad; entries",
01917                         Chan, minChan, maxChan));
01918             
01919           }
01920           //
01921           sprintf(SpecName,"Sim_Rechits_Ch%d",iChamber);
01922           ChHist[ec][st][rg][iChamber-FirstCh].SimRechits = 
01923             new TH1F(SpecName,"Existing RecHit (Sim);layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01924           //
01925           sprintf(SpecName,"Sim_Simhits_Ch%d",iChamber);
01926           ChHist[ec][st][rg][iChamber-FirstCh].SimSimhits = 
01927             new TH1F(SpecName,"Existing SimHit (Sim);layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01928           //
01929           /*
01930           sprintf(SpecName,"Sim_Rechits_each_Ch%d",iChamber);
01931           ChHist[ec][st][rg][iChamber-FirstCh].SimRechits_each = 
01932             new TH1F(SpecName,"Existing RecHit (Sim), each;layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01933           //
01934           sprintf(SpecName,"Sim_Simhits_each_Ch%d",iChamber);
01935           ChHist[ec][st][rg][iChamber-FirstCh].SimSimhits_each = 
01936             new TH1F(SpecName,"Existing SimHit (Sim), each;layers (1-6);entries",nLayer_bins,Layer_min,Layer_max);
01937           */
01938           theFile->cd();
01939         }
01940       }
01941     }
01942   }
01943 }
01944 
01945 // Destructor
01946 CSCEfficiency::~CSCEfficiency(){
01947   if (theService) delete theService; 
01948   // Write the histos to a file
01949   theFile->cd();
01950   //
01951   char SpecName[20];
01952   std::vector<float> bins, Efficiency, EffError;
01953   std::vector<float> eff(2);
01954 
01955   //---- loop over chambers
01956   std::map <std::string, bool> chamberTypes;
01957   chamberTypes["ME11"] = false;
01958   chamberTypes["ME12"] = false;
01959   chamberTypes["ME13"] = false;
01960   chamberTypes["ME21"] = false;
01961   chamberTypes["ME22"] = false;
01962   chamberTypes["ME31"] = false;
01963   chamberTypes["ME32"] = false;
01964   chamberTypes["ME41"] = false;
01965   
01966   map<std::string,bool>::iterator iter;  
01967   std::cout<<" Writing proper histogram structure (patience)..."<<std::endl;
01968   for(int ec = 0;ec<2;++ec){
01969     for(int st = 0;st<4;++st){
01970       sprintf(SpecName,"Stations__E%d_S%d",ec+1, st+1);
01971       theFile->cd(SpecName);
01972       StHist[ec][st].segmentChi2_ndf->Write();
01973       StHist[ec][st].hitsInSegment->Write();
01974       StHist[ec][st].AllSegments_eta->Write();
01975       StHist[ec][st].EfficientSegments_eta->Write();
01976       StHist[ec][st].ResidualSegments->Write();
01977       StHist[ec][st].EfficientSegments_XY->Write();
01978       StHist[ec][st].InefficientSegments_XY->Write();
01979       StHist[ec][st].EfficientALCT_momTheta->Write();
01980       StHist[ec][st].InefficientALCT_momTheta->Write();
01981       StHist[ec][st].EfficientCLCT_momPhi->Write();
01982       StHist[ec][st].InefficientCLCT_momPhi->Write();
01983       for(int rg = 0;rg<3;++rg){
01984         if(0!=st && rg>1){
01985           continue;
01986         }
01987         else if(1==rg && 3==st){
01988           continue;
01989         }
01990         for(int iChamber=FirstCh;iChamber<FirstCh+NumCh;iChamber++){
01991           if(0!=st && 0==rg && iChamber >18){
01992             continue;
01993           }
01994           sprintf(SpecName,"Chambers__E%d_S%d_R%d_Chamber_%d",ec+1, st+1, rg+1,iChamber);
01995           theFile->cd(SpecName);
01996           
01997           ChHist[ec][st][rg][iChamber-FirstCh].EfficientRechits_inSegment->Write();
01998           ChHist[ec][st][rg][iChamber-FirstCh].AllSingleHits->Write();
01999           ChHist[ec][st][rg][iChamber-FirstCh].digiAppearanceCount->Write();
02000           ChHist[ec][st][rg][iChamber-FirstCh].EfficientALCT_dydz->Write();
02001           ChHist[ec][st][rg][iChamber-FirstCh].InefficientALCT_dydz->Write();
02002           ChHist[ec][st][rg][iChamber-FirstCh].EfficientCLCT_dxdz->Write();
02003           ChHist[ec][st][rg][iChamber-FirstCh].InefficientCLCT_dxdz->Write();
02004           ChHist[ec][st][rg][iChamber-FirstCh].InefficientSingleHits->Write();
02005           ChHist[ec][st][rg][iChamber-FirstCh].EfficientRechits_good->Write();
02006           ChHist[ec][st][rg][iChamber-FirstCh].EfficientStrips->Write();
02007           ChHist[ec][st][rg][iChamber-FirstCh].StripWiresCorrelations->Write();
02008           ChHist[ec][st][rg][iChamber-FirstCh].NoWires_momTheta->Write();
02009           ChHist[ec][st][rg][iChamber-FirstCh].NoStrips_momPhi->Write();
02010           ChHist[ec][st][rg][iChamber-FirstCh].EfficientWireGroups->Write();
02011           for(unsigned int iLayer = 0; iLayer< 6; iLayer++){
02012             ChHist[ec][st][rg][iChamber-FirstCh].Y_InefficientRecHits_inSegment[iLayer]->Write();
02013             ChHist[ec][st][rg][iChamber-FirstCh].Y_EfficientRecHits_inSegment[iLayer]->Write();
02014             ChHist[ec][st][rg][iChamber-FirstCh].Phi_InefficientRecHits_inSegment[iLayer]->Write();
02015             ChHist[ec][st][rg][iChamber-FirstCh].Phi_EfficientRecHits_inSegment[iLayer]->Write();
02016           }
02017           ChHist[ec][st][rg][iChamber-FirstCh].SimRechits->Write();
02018           ChHist[ec][st][rg][iChamber-FirstCh].SimSimhits->Write();
02019           /*
02020           ChHist[ec][st][rg][iChamber-FirstCh].SimRechits_each->Write();
02021           ChHist[ec][st][rg][iChamber-FirstCh].SimSimhits_each->Write();
02022           */
02023           //
02024           theFile->cd(SpecName);
02025           theFile->cd();
02026         }
02027       }
02028     }
02029   }
02030   //
02031   sprintf(SpecName,"AllChambers");
02032   theFile->mkdir(SpecName);
02033   theFile->cd(SpecName);
02034   DataFlow->Write(); 
02035   TriggersFired->Write();
02036   ALCTPerEvent->Write();
02037   CLCTPerEvent->Write();
02038   recHitsPerEvent->Write();
02039   segmentsPerEvent->Write();
02040   //
02041   theFile->cd(SpecName);
02042   //---- Close the file
02043   theFile->Close();
02044 }
02045 
02046 // ------------ method called once each job just before starting event loop  ------------
02047 void
02048 CSCEfficiency::beginJob()
02049 {
02050 }
02051 
02052 // ------------ method called once each job just after ending the event loop  ------------
02053 void
02054 CSCEfficiency::endJob() {
02055 }
02056 
02057 DEFINE_FWK_MODULE(CSCEfficiency);
02058