CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibTracker/SiStripHitEfficiency/plugins/SiStripHitEffFromCalibTree.cc

Go to the documentation of this file.
00001 //Original Author:  Christopher Edelmaier
00002 //        Created:  Feb. 11, 2010
00003 #include <memory>
00004 #include <string>
00005 #include <iostream>
00006 
00007 #include "FWCore/Framework/interface/Frameworkfwd.h"
00008 #include "FWCore/Framework/interface/EDAnalyzer.h"
00009 #include "FWCore/Framework/interface/Event.h"
00010 #include "FWCore/Framework/interface/MakerMacros.h"
00011 #include "FWCore/Framework/interface/ESHandle.h"
00012 #include "FWCore/Framework/interface/EventSetup.h"
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 #include "FWCore/ParameterSet/interface/FileInPath.h"
00015 
00016 #include "CalibTracker/SiStripHitEfficiency/interface/HitEff.h"
00017 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00018 #include "DataFormats/Common/interface/Handle.h"
00019 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00020 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00021 #include "DataFormats/GeometryVector/interface/LocalVector.h"
00022 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00023 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00024 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
00025 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00026 #include "DataFormats/TrackReco/interface/Track.h"
00027 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00028 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00029 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00030 #include "TrackingTools/Records/interface/TransientRecHitRecord.h" 
00031 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00032 #include "CalibTracker/SiStripHitEfficiency/interface/TrajectoryAtInvalidHit.h"
00033 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00034 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00035 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00036 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00037 #include "RecoLocalTracker/ClusterParameterEstimator/interface/StripClusterParameterEstimator.h"
00038 #include "TrackingTools/GeomPropagators/interface/AnalyticalPropagator.h"
00039 #include "DataFormats/TrackReco/interface/DeDxData.h"
00040 #include "DataFormats/DetId/interface/DetIdCollection.h"
00041 #include "TrackingTools/DetLayers/interface/DetLayer.h"
00042 #include "RecoTracker/MeasurementDet/interface/MeasurementTracker.h"
00043 
00044 #include "RecoTracker/Record/interface/CkfComponentsRecord.h"
00045 #include "AnalysisDataFormats/SiStripClusterInfo/interface/SiStripClusterInfo.h"
00046 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
00047 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00048 #include "CalibTracker/Records/interface/SiStripQualityRcd.h"
00049 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
00050 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
00051 #include "DataFormats/SiStripDetId/interface/SiStripSubStructure.h"
00052 #include "Geometry/TrackerGeometryBuilder/interface/GluedGeomDet.h"
00053 #include "DataFormats/Common/interface/DetSetVector.h"
00054 #include "DataFormats/Common/interface/DetSetVectorNew.h"
00055 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h" 
00056 
00057 #include "DataFormats/MuonReco/interface/Muon.h"
00058 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00059 
00060 #include "FWCore/ServiceRegistry/interface/Service.h"
00061 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00062 #include "CommonTools/TrackerMap/interface/TrackerMap.h"
00063 #include "CommonTools/ConditionDBWriter/interface/ConditionDBWriter.h"
00064 
00065 #include "TFile.h"
00066 #include "TCanvas.h"
00067 #include "TObjString.h"
00068 #include "TString.h"
00069 #include "TH1F.h"
00070 #include "TH2F.h"
00071 #include "TProfile.h"
00072 #include "TF1.h"
00073 #include "TROOT.h"
00074 #include "TTree.h"
00075 #include "TChain.h"
00076 #include "TStyle.h"
00077 #include "TLeaf.h"
00078 #include "TGaxis.h"
00079 #include "TGraphAsymmErrors.h"
00080 #include "TLatex.h"
00081 #include "TLegend.h"
00082 
00083 using namespace edm;
00084 using namespace reco;
00085 using namespace std;
00086 
00087 struct hit{
00088   double x;
00089   double y;
00090   double z;
00091   unsigned int id;
00092 };
00093 
00094 class SiStripHitEffFromCalibTree : public ConditionDBWriter<SiStripBadStrip> {
00095   public:
00096     explicit SiStripHitEffFromCalibTree(const edm::ParameterSet&);
00097     ~SiStripHitEffFromCalibTree();
00098 
00099   private:
00100     virtual void algoBeginJob();
00101     virtual void algoEndJob();
00102     virtual void algoAnalyze(const edm::Event& e, const edm::EventSetup& c);
00103     void SetBadComponents(int i, int component,SiStripQuality::BadComponent& BC, std::stringstream ssV[4][19], int NBadComponent[4][19][4]);
00104     void makeTKMap();
00105     void makeHotColdMaps();
00106     void makeSQLite();
00107     void totalStatistics();
00108     void makeSummary();
00109     float calcPhi(float x, float y);
00110 
00111     edm::Service<TFileService> fs;
00112     SiStripDetInfoFileReader* reader;
00113     edm::FileInPath FileInPath_;
00114     SiStripQuality* quality_;
00115     SiStripBadStrip* getNewObject();
00116     
00117     TFile* CalibTreeFile;
00118     TTree* CalibTree;
00119     TString CalibTreeFilename; 
00120     float threshold;
00121     unsigned int nModsMin;
00122     unsigned int doSummary;
00123     float _ResXSig;
00124     unsigned int _bunchx;
00125     vector<hit> hits[23];
00126     vector<TH2F*> HotColdMaps;
00127     map< unsigned int, pair< unsigned int, unsigned int> > modCounter[23];
00128     TrackerMap *tkmap;
00129     TrackerMap *tkmapbad;
00130     int layerfound[23];
00131     int layertotal[23];
00132     int goodlayertotal[35];
00133     int goodlayerfound[35];
00134     int alllayertotal[35];
00135     int alllayerfound[35];
00136     map< unsigned int, double > BadModules;
00137 };
00138 
00139 SiStripHitEffFromCalibTree::SiStripHitEffFromCalibTree(const edm::ParameterSet& conf) :
00140   ConditionDBWriter<SiStripBadStrip>(conf),
00141   FileInPath_("CalibTracker/SiStripCommon/data/SiStripDetInfo.dat")
00142 {
00143   CalibTreeFilename = conf.getParameter<std::string>("CalibTreeFilename"); 
00144   threshold = conf.getParameter<double>("Threshold");
00145   nModsMin = conf.getParameter<int>("nModsMin");
00146   doSummary = conf.getParameter<int>("doSummary");
00147   _ResXSig = conf.getUntrackedParameter<double>("ResXSig",-1);
00148   _bunchx = conf.getUntrackedParameter<int>("BunchCrossing",0);
00149   reader = new SiStripDetInfoFileReader(FileInPath_.fullPath());
00150   
00151   quality_ = new SiStripQuality;
00152 }
00153 
00154 SiStripHitEffFromCalibTree::~SiStripHitEffFromCalibTree() { }
00155 
00156 void SiStripHitEffFromCalibTree::algoBeginJob() {
00157   //I have no idea what goes here
00158   //fs->make<TTree>("HitEffHistos","Tree of the inefficient hit histograms");
00159 }
00160 
00161 void SiStripHitEffFromCalibTree::algoEndJob() {
00162   //Still have no idea what goes here
00163 
00164 }
00165 
00166 void SiStripHitEffFromCalibTree::algoAnalyze(const edm::Event& e, const edm::EventSetup& c) {
00167   //Open the ROOT Calib Tree
00168   CalibTreeFile = TFile::Open(CalibTreeFilename,"READ");
00169   CalibTreeFile->cd("anEff"); 
00170   CalibTree = (TTree*)(gDirectory->Get("traj")) ;
00171   TLeaf* BadLf = CalibTree->GetLeaf("ModIsBad");
00172   TLeaf* sistripLf = CalibTree->GetLeaf("SiStripQualBad");
00173   TLeaf* idLf = CalibTree->GetLeaf("Id");
00174   TLeaf* acceptLf = CalibTree->GetLeaf("withinAcceptance");
00175   TLeaf* layerLf = CalibTree->GetLeaf("layer");
00176   TLeaf* nHitsLf = CalibTree->GetLeaf("nHits");
00177   TLeaf* xLf = CalibTree->GetLeaf("TrajGlbX");
00178   TLeaf* yLf = CalibTree->GetLeaf("TrajGlbY");
00179   TLeaf* zLf = CalibTree->GetLeaf("TrajGlbZ");
00180   TLeaf* ResXSigLf = CalibTree->GetLeaf("ResXSig");
00181   TLeaf* BunchLf(0);
00182   for(int l=0; l < 35; l++) {
00183     goodlayertotal[l] = 0;
00184     goodlayerfound[l] = 0;
00185     alllayertotal[l] = 0;
00186     alllayerfound[l] = 0;
00187   }
00188   if(_bunchx != 0) {
00189     BunchLf = CalibTree->GetLeaf("bunchx");
00190   }
00191   int nevents = CalibTree->GetEntries();
00192   cout << "Successfully loaded analyze function with " << nevents << " events!\n";
00193   cout << "A module is bad if efficiency < " << threshold << " and has at least " << nModsMin << " nModsMin." << endl;
00194 
00195   //Loop through all of the events
00196   for(int j =0; j < nevents; j++) {
00197     CalibTree->GetEvent(j);
00198     unsigned int isBad = (unsigned int)BadLf->GetValue();
00199     unsigned int quality = (unsigned int)sistripLf->GetValue();
00200     unsigned int id = (unsigned int)idLf->GetValue();
00201     unsigned int accept = (unsigned int)acceptLf->GetValue();
00202     unsigned int layer = (unsigned int)layerLf->GetValue();
00203     unsigned int nHits = (unsigned int)nHitsLf->GetValue();
00204     double x = xLf->GetValue();
00205     double y = yLf->GetValue();
00206     double z = zLf->GetValue();
00207     double resxsig = ResXSigLf->GetValue();
00208     bool badquality = false;
00209     if(_bunchx != 0) {
00210       if(_bunchx != BunchLf->GetValue()) continue;
00211     }
00212     //We have two things we want to do, both an XY color plot, and the efficiency measurement
00213     //First, ignore anything that isn't in acceptance and isn't good quality
00214     
00215     //if(quality == 1 || accept != 1 || nHits < 8) continue;
00216     if(accept != 1 || nHits < 8) continue;
00217     if(quality == 1) badquality = true;
00218     
00219     //Now that we have a good event, we need to look at if we expected it or not, and the location
00220     //if we didn't
00221     //Fill the missing hit information first
00222     bool badflag = false;
00223     if(_ResXSig < 0) {
00224       if(isBad == 1) badflag = true;
00225     }
00226     else {
00227       if(isBad == 1 || resxsig > _ResXSig) badflag = true;
00228     }
00229     if(badflag && !badquality) {   
00230       hit temphit;         
00231       temphit.x = x;
00232       temphit.y = y;
00233       temphit.z = z;
00234       temphit.id = id;
00235       hits[layer].push_back(temphit);
00236     } 
00237     pair<unsigned int, unsigned int> newgoodpair (1,1);
00238     pair<unsigned int, unsigned int> newbadpair (1,0);
00239     //First, figure out if the module already exists in the map of maps
00240     map< unsigned int, pair< unsigned int, unsigned int> >::iterator it = modCounter[layer].find(id);
00241     if(!badquality) {
00242       if(it == modCounter[layer].end()) {
00243         if(badflag) modCounter[layer][id] = newbadpair;
00244         else modCounter[layer][id] = newgoodpair;
00245       }
00246       else {
00247         ((*it).second.first)++;
00248         if(!badflag) ((*it).second.second)++;
00249       }
00250       //Have to do the decoding for which side to go on (ugh)
00251       if(layer <= 10) {
00252         if(!badflag) goodlayerfound[layer]++;
00253         goodlayertotal[layer]++;
00254       }
00255       else if(layer > 10 && layer < 14) {
00256         if( ((id>>13)&0x3) == 1) {
00257           if(!badflag) goodlayerfound[layer]++;
00258           goodlayertotal[layer]++;
00259         }
00260         else if( ((id>>13)&0x3) == 2) {
00261           if(!badflag) goodlayerfound[layer+3]++;
00262           goodlayertotal[layer+3]++;
00263         }
00264       }
00265       else if(layer > 13 && layer <= 22) {
00266         if( ((id>>18)&0x3) == 1) {
00267           if(!badflag) goodlayerfound[layer+3]++;
00268           goodlayertotal[layer+3]++;
00269         }
00270         else if( ((id>>18)&0x3) == 2) {
00271           if(!badflag) goodlayerfound[layer+11]++;
00272           goodlayertotal[layer+11]++;
00273         }
00274       } 
00275     }
00276     //Do the one where we don't exclude bad modules!
00277     if(layer <= 10) {
00278       if(!badflag) alllayerfound[layer]++;
00279       alllayertotal[layer]++;
00280     }
00281     else if(layer > 10 && layer < 14) {
00282       if( ((id>>13)&0x3) == 1) {
00283         if(!badflag) alllayerfound[layer]++;
00284         alllayertotal[layer]++;
00285       }
00286       else if( ((id>>13)&0x3) == 2) {
00287         if(!badflag) alllayerfound[layer+3]++;
00288         alllayertotal[layer+3]++;
00289       }
00290     }
00291     else if(layer > 13 && layer <= 22) {
00292       if( ((id>>18)&0x3) == 1) {
00293         if(!badflag) alllayerfound[layer+3]++;
00294         alllayertotal[layer+3]++;
00295       }
00296       else if( ((id>>18)&0x3) == 2) {
00297         if(!badflag) alllayerfound[layer+12]++;
00298         alllayertotal[layer+12]++;
00299       }
00300     }  
00301     //At this point, both of our maps are loaded with the correct information
00302   }
00303   //CalibTreeFile->Close();
00304   makeHotColdMaps();
00305   makeTKMap();
00306   makeSQLite();
00307   totalStatistics();
00308   makeSummary();
00309   
00311   //try to write out what's in the quality record
00313   int NTkBadComponent[4]; //k: 0=BadModule, 1=BadFiber, 2=BadApv, 3=BadStrips
00314   int NBadComponent[4][19][4];  
00315   //legend: NBadComponent[i][j][k]= SubSystem i, layer/disk/wheel j, BadModule/Fiber/Apv k
00316   //     i: 0=TIB, 1=TID, 2=TOB, 3=TEC
00317   //     k: 0=BadModule, 1=BadFiber, 2=BadApv, 3=BadStrips
00318   std::stringstream ssV[4][19];
00319   
00320   for(int i=0;i<4;++i){
00321     NTkBadComponent[i]=0;
00322     for(int j=0;j<19;++j){
00323       ssV[i][j].str("");
00324       for(int k=0;k<4;++k)
00325         NBadComponent[i][j][k]=0;
00326     }
00327   }
00328  
00329  
00330   std::vector<SiStripQuality::BadComponent> BC = quality_->getBadComponentList();
00331    
00332   for (size_t i=0;i<BC.size();++i){
00333      
00334     //&&&&&&&&&&&&&
00335     //Full Tk
00336     //&&&&&&&&&&&&&
00337  
00338     if (BC[i].BadModule) 
00339       NTkBadComponent[0]++;
00340     if (BC[i].BadFibers) 
00341       NTkBadComponent[1]+= ( (BC[i].BadFibers>>2)&0x1 )+ ( (BC[i].BadFibers>>1)&0x1 ) + ( (BC[i].BadFibers)&0x1 );
00342     if (BC[i].BadApvs)
00343       NTkBadComponent[2]+= ( (BC[i].BadApvs>>5)&0x1 )+ ( (BC[i].BadApvs>>4)&0x1 ) + ( (BC[i].BadApvs>>3)&0x1 ) + 
00344         ( (BC[i].BadApvs>>2)&0x1 )+ ( (BC[i].BadApvs>>1)&0x1 ) + ( (BC[i].BadApvs)&0x1 );
00345  
00346     //&&&&&&&&&&&&&&&&&
00347     //Single SubSystem
00348     //&&&&&&&&&&&&&&&&&
00349  
00350     int component;
00351     SiStripDetId a(BC[i].detid);
00352     if ( a.subdetId() == SiStripDetId::TIB ){
00353       //&&&&&&&&&&&&&&&&&
00354       //TIB
00355       //&&&&&&&&&&&&&&&&&
00356        
00357       component=TIBDetId(BC[i].detid).layer();
00358       SetBadComponents(0, component, BC[i], ssV, NBadComponent);              
00359  
00360     } else if ( a.subdetId() == SiStripDetId::TID ) {
00361       //&&&&&&&&&&&&&&&&&
00362       //TID
00363       //&&&&&&&&&&&&&&&&&
00364  
00365       component=TIDDetId(BC[i].detid).side()==2?TIDDetId(BC[i].detid).wheel():TIDDetId(BC[i].detid).wheel()+3;
00366       SetBadComponents(1, component, BC[i], ssV, NBadComponent);              
00367  
00368     } else if ( a.subdetId() == SiStripDetId::TOB ) {
00369       //&&&&&&&&&&&&&&&&&
00370       //TOB
00371       //&&&&&&&&&&&&&&&&&
00372  
00373       component=TOBDetId(BC[i].detid).layer();
00374       SetBadComponents(2, component, BC[i], ssV, NBadComponent);              
00375  
00376     } else if ( a.subdetId() == SiStripDetId::TEC ) {
00377       //&&&&&&&&&&&&&&&&&
00378       //TEC
00379       //&&&&&&&&&&&&&&&&&
00380  
00381       component=TECDetId(BC[i].detid).side()==2?TECDetId(BC[i].detid).wheel():TECDetId(BC[i].detid).wheel()+9;
00382       SetBadComponents(3, component, BC[i], ssV, NBadComponent);              
00383  
00384     }    
00385   }
00386  
00387   //&&&&&&&&&&&&&&&&&&
00388   // Single Strip Info
00389   //&&&&&&&&&&&&&&&&&&
00390   float percentage=0;
00391  
00392   SiStripQuality::RegistryIterator rbegin = quality_->getRegistryVectorBegin();
00393   SiStripQuality::RegistryIterator rend   = quality_->getRegistryVectorEnd();
00394    
00395   for (SiStripBadStrip::RegistryIterator rp=rbegin; rp != rend; ++rp) {
00396     unsigned int detid=rp->detid;
00397  
00398     int subdet=-999; int component=-999;
00399     SiStripDetId a(detid);
00400     if ( a.subdetId() == 3 ){
00401       subdet=0;
00402       component=TIBDetId(detid).layer();
00403     } else if ( a.subdetId() == 4 ) {
00404       subdet=1;
00405       component=TIDDetId(detid).side()==2?TIDDetId(detid).wheel():TIDDetId(detid).wheel()+3;
00406     } else if ( a.subdetId() == 5 ) {
00407       subdet=2;
00408       component=TOBDetId(detid).layer();
00409     } else if ( a.subdetId() == 6 ) {
00410       subdet=3;
00411       component=TECDetId(detid).side()==2?TECDetId(detid).wheel():TECDetId(detid).wheel()+9;
00412     } 
00413  
00414     SiStripQuality::Range sqrange = SiStripQuality::Range( quality_->getDataVectorBegin()+rp->ibegin , quality_->getDataVectorBegin()+rp->iend );
00415          
00416     percentage=0;
00417     for(int it=0;it<sqrange.second-sqrange.first;it++){
00418       unsigned int range=quality_->decode( *(sqrange.first+it) ).range;
00419       NTkBadComponent[3]+=range;
00420       NBadComponent[subdet][0][3]+=range;
00421       NBadComponent[subdet][component][3]+=range;
00422       percentage+=range;
00423     }
00424     if(percentage!=0)
00425       percentage/=128.*reader->getNumberOfApvsAndStripLength(detid).first;
00426     if(percentage>1)
00427       edm::LogError("SiStripQualityStatistics") <<  "PROBLEM detid " << detid << " value " << percentage<< std::endl; 
00428   }
00429   //&&&&&&&&&&&&&&&&&&
00430   // printout
00431   //&&&&&&&&&&&&&&&&&&
00432  
00433   cout << "\n-----------------\nNew IOV starting from run " <<   e.id().run() << " event " << e.id().event() << " lumiBlock " << e.luminosityBlock() << " time " << e.time().value()  << "\n-----------------\n";
00434   cout << "\n-----------------\nGlobal Info\n-----------------";
00435   cout << "\nBadComponent \t    Modules \tFibers \tApvs\tStrips\n----------------------------------------------------------------";
00436   cout << "\nTracker:\t\t"<<NTkBadComponent[0]<<"\t"<<NTkBadComponent[1]<<"\t"<<NTkBadComponent[2]<<"\t"<<NTkBadComponent[3];
00437   cout << endl;
00438   cout << "\nTIB:\t\t\t"<<NBadComponent[0][0][0]<<"\t"<<NBadComponent[0][0][1]<<"\t"<<NBadComponent[0][0][2]<<"\t"<<NBadComponent[0][0][3];
00439   cout << "\nTID:\t\t\t"<<NBadComponent[1][0][0]<<"\t"<<NBadComponent[1][0][1]<<"\t"<<NBadComponent[1][0][2]<<"\t"<<NBadComponent[1][0][3];
00440   cout << "\nTOB:\t\t\t"<<NBadComponent[2][0][0]<<"\t"<<NBadComponent[2][0][1]<<"\t"<<NBadComponent[2][0][2]<<"\t"<<NBadComponent[2][0][3];
00441   cout << "\nTEC:\t\t\t"<<NBadComponent[3][0][0]<<"\t"<<NBadComponent[3][0][1]<<"\t"<<NBadComponent[3][0][2]<<"\t"<<NBadComponent[3][0][3];
00442   cout << "\n";
00443  
00444   for (int i=1;i<5;++i)
00445     cout << "\nTIB Layer " << i   << " :\t\t"<<NBadComponent[0][i][0]<<"\t"<<NBadComponent[0][i][1]<<"\t"<<NBadComponent[0][i][2]<<"\t"<<NBadComponent[0][i][3];
00446   cout << "\n";
00447   for (int i=1;i<4;++i)
00448     cout << "\nTID+ Disk " << i   << " :\t\t"<<NBadComponent[1][i][0]<<"\t"<<NBadComponent[1][i][1]<<"\t"<<NBadComponent[1][i][2]<<"\t"<<NBadComponent[1][i][3];
00449   for (int i=4;i<7;++i)
00450     cout << "\nTID- Disk " << i-3 << " :\t\t"<<NBadComponent[1][i][0]<<"\t"<<NBadComponent[1][i][1]<<"\t"<<NBadComponent[1][i][2]<<"\t"<<NBadComponent[1][i][3];
00451   cout << "\n";
00452   for (int i=1;i<7;++i)
00453     cout << "\nTOB Layer " << i   << " :\t\t"<<NBadComponent[2][i][0]<<"\t"<<NBadComponent[2][i][1]<<"\t"<<NBadComponent[2][i][2]<<"\t"<<NBadComponent[2][i][3];
00454   cout << "\n";
00455   for (int i=1;i<10;++i)
00456     cout << "\nTEC+ Disk " << i   << " :\t\t"<<NBadComponent[3][i][0]<<"\t"<<NBadComponent[3][i][1]<<"\t"<<NBadComponent[3][i][2]<<"\t"<<NBadComponent[3][i][3];
00457   for (int i=10;i<19;++i)
00458     cout << "\nTEC- Disk " << i-9 << " :\t\t"<<NBadComponent[3][i][0]<<"\t"<<NBadComponent[3][i][1]<<"\t"<<NBadComponent[3][i][2]<<"\t"<<NBadComponent[3][i][3];
00459   cout << "\n";
00460  
00461   cout << "\n----------------------------------------------------------------\n\t\t   Detid  \tModules Fibers Apvs\n----------------------------------------------------------------";
00462   for (int i=1;i<5;++i)
00463     cout << "\nTIB Layer " << i << " :" << ssV[0][i].str();
00464   cout << "\n";
00465   for (int i=1;i<4;++i)
00466     cout << "\nTID+ Disk " << i << " :" << ssV[1][i].str();
00467   for (int i=4;i<7;++i)
00468     cout << "\nTID- Disk " << i-3 << " :" << ssV[1][i].str();
00469   cout << "\n";
00470   for (int i=1;i<7;++i)
00471     cout << "\nTOB Layer " << i << " :" << ssV[2][i].str();
00472   cout << "\n";
00473   for (int i=1;i<10;++i)
00474     cout << "\nTEC+ Disk " << i << " :" << ssV[3][i].str();
00475   for (int i=10;i<19;++i)
00476     cout << "\nTEC- Disk " << i-9 << " :" << ssV[3][i].str();
00477 
00478 }
00479 
00480 void SiStripHitEffFromCalibTree::makeHotColdMaps() {
00481   cout << "Entering hot cold map generation!\n";
00482   TStyle* gStyle = new TStyle("gStyle","myStyle");
00483   gStyle->cd();
00484   gStyle->SetPalette(1);
00485   gStyle->SetCanvasColor(kWhite);
00486   gStyle->SetOptStat(0);
00487   //Here we make the hot/cold color maps that we love so very much
00488   //Already have access to the data as a private variable
00489   //Create all of the histograms in the TFileService 
00490   TH2F *temph2;
00491   for(Long_t maplayer = 1; maplayer <=22; maplayer++) {                                                                                   
00492     //Initialize all of the histograms                                                                                                    
00493     if(maplayer > 0 && maplayer <= 4) {                                                                                                   
00494         //We are in the TIB                                                                                                               
00495         temph2 = fs->make<TH2F>(Form("%s%i","TIB",(int)(maplayer)),"TIB",100,-1,361,100,-100,100);                                        
00496         temph2->GetXaxis()->SetTitle("Phi");
00497         temph2->GetXaxis()->SetBinLabel(1,TString("360"));
00498         temph2->GetXaxis()->SetBinLabel(50,TString("180"));
00499         temph2->GetXaxis()->SetBinLabel(100,TString("0"));                                                                                                
00500         temph2->GetYaxis()->SetTitle("Global Z");
00501         temph2->SetOption("colz");                                                                                        
00502         HotColdMaps.push_back(temph2);                                                                                                    
00503       }                                                                                                                                   
00504       else if(maplayer > 4 && maplayer <= 10) {                                                                                           
00505         //We are in the TOB                                                                                                               
00506         temph2 = fs->make<TH2F>(Form("%s%i","TOB",(int)(maplayer-4)),"TOB",100,-1,361,100,-120,120);                              
00507         temph2->GetXaxis()->SetTitle("Phi");
00508         temph2->GetXaxis()->SetBinLabel(1,TString("360"));
00509         temph2->GetXaxis()->SetBinLabel(50,TString("180"));
00510         temph2->GetXaxis()->SetBinLabel(100,TString("0"));                                                                                                
00511         temph2->GetYaxis()->SetTitle("Global Z");
00512         temph2->SetOption("colz");                                                                                                
00513         HotColdMaps.push_back(temph2);                                                                                                    
00514       }                                                                                                                                   
00515       else if(maplayer > 10 && maplayer <= 13) {                                                                                          
00516         //We are in the TID                                                                                                               
00517         //Split by +/-                                                                                                                    
00518         temph2 = fs->make<TH2F>(Form("%s%i","TID-",(int)(maplayer-10)),"TID-",100,-100,100,100,-100,100);                         
00519         temph2->GetXaxis()->SetTitle("Global Y");
00520         temph2->GetXaxis()->SetBinLabel(1,TString("+Y"));
00521         temph2->GetXaxis()->SetBinLabel(50,TString("0"));
00522         temph2->GetXaxis()->SetBinLabel(100,TString("-Y"));                                                                                               
00523         temph2->GetYaxis()->SetTitle("Global X");
00524         temph2->GetYaxis()->SetBinLabel(1,TString("-X"));
00525         temph2->GetYaxis()->SetBinLabel(50,TString("0"));
00526         temph2->GetYaxis()->SetBinLabel(100,TString("+X"));
00527         temph2->SetOption("colz");                                                                                                
00528         HotColdMaps.push_back(temph2);                                                                                                    
00529         temph2 = fs->make<TH2F>(Form("%s%i","TID+",(int)(maplayer-10)),"TID+",100,-100,100,100,-100,100);                         
00530         temph2->GetXaxis()->SetTitle("Global Y");
00531         temph2->GetXaxis()->SetBinLabel(1,TString("+Y"));
00532         temph2->GetXaxis()->SetBinLabel(50,TString("0"));
00533         temph2->GetXaxis()->SetBinLabel(100,TString("-Y"));                                                                                               
00534         temph2->GetYaxis()->SetTitle("Global X");
00535         temph2->GetYaxis()->SetBinLabel(1,TString("-X"));
00536         temph2->GetYaxis()->SetBinLabel(50,TString("0"));
00537         temph2->GetYaxis()->SetBinLabel(100,TString("+X"));                                                                                               
00538         temph2->SetOption("colz");
00539         HotColdMaps.push_back(temph2);                                                                                                    
00540       }                                                                                                                                   
00541       else if(maplayer > 13) {
00542         //We are in the TEC
00543         //Split by +/-
00544         temph2 = fs->make<TH2F>(Form("%s%i","TEC-",(int)(maplayer-13)),"TEC-",100,-120,120,100,-120,120);
00545         temph2->GetXaxis()->SetTitle("Global Y");
00546         temph2->GetXaxis()->SetBinLabel(1,TString("+Y"));
00547         temph2->GetXaxis()->SetBinLabel(50,TString("0"));
00548         temph2->GetXaxis()->SetBinLabel(100,TString("-Y"));                                                                                               
00549         temph2->GetYaxis()->SetTitle("Global X");
00550         temph2->GetYaxis()->SetBinLabel(1,TString("-X"));
00551         temph2->GetYaxis()->SetBinLabel(50,TString("0"));
00552         temph2->GetYaxis()->SetBinLabel(100,TString("+X"));
00553         temph2->SetOption("colz");
00554         HotColdMaps.push_back(temph2);
00555         temph2 = fs->make<TH2F>(Form("%s%i","TEC+",(int)(maplayer-13)),"TEC+",100,-120,120,100,-120,120);
00556         temph2->GetXaxis()->SetTitle("Global Y");
00557         temph2->GetXaxis()->SetBinLabel(1,TString("+Y"));
00558         temph2->GetXaxis()->SetBinLabel(50,TString("0"));
00559         temph2->GetXaxis()->SetBinLabel(100,TString("-Y"));                                                                                               
00560         temph2->GetYaxis()->SetTitle("Global X");
00561         temph2->GetYaxis()->SetBinLabel(1,TString("-X"));
00562         temph2->GetYaxis()->SetBinLabel(50,TString("0"));
00563         temph2->GetYaxis()->SetBinLabel(100,TString("+X"));
00564         temph2->SetOption("colz");
00565         HotColdMaps.push_back(temph2);
00566     }
00567   }
00568   for(Long_t mylayer = 1; mylayer <= 22; mylayer++) {
00569     //Determine what kind of plot we want to write out
00570     //Loop through the entirety of each layer
00571     //Create an array of the histograms
00572     vector<hit>::const_iterator iter;
00573     for(iter = hits[mylayer].begin(); iter != hits[mylayer].end(); iter++) {
00574       //Looping over the particular layer
00575       //Fill by 360-x to get the proper location to compare with TKMaps of phi
00576       //Also global xy is messed up
00577       if(mylayer > 0 && mylayer <= 4) {
00578         //We are in the TIB
00579         float phi = calcPhi(iter->x, iter->y);
00580         HotColdMaps[mylayer - 1]->Fill(360.-phi,iter->z,1.); 
00581       }
00582       else if(mylayer > 4 && mylayer <= 10) {
00583         //We are in the TOB
00584         float phi = calcPhi(iter->x,iter->y);
00585         HotColdMaps[mylayer - 1]->Fill(360.-phi,iter->z,1.);
00586       }
00587       else if(mylayer > 10 && mylayer <= 13) {
00588         //We are in the TID
00589         //There are 2 different maps here
00590         int side = (((iter->id)>>13) & 0x3);
00591         if(side == 1) HotColdMaps[(mylayer - 1) + (mylayer - 11)]->Fill(-iter->y,iter->x,1.); 
00592         else if(side == 2) HotColdMaps[(mylayer - 1) + (mylayer - 10)]->Fill(-iter->y,iter->x,1.);
00593         //if(side == 1) HotColdMaps[(mylayer - 1) + (mylayer - 11)]->Fill(iter->x,iter->y,1.); 
00594         //else if(side == 2) HotColdMaps[(mylayer - 1) + (mylayer - 10)]->Fill(iter->x,iter->y,1.);
00595       }
00596       else if(mylayer > 13) {
00597         //We are in the TEC
00598         //There are 2 different maps here
00599         int side = (((iter->id)>>18) & 0x3);
00600         if(side == 1) HotColdMaps[(mylayer + 2) + (mylayer - 14)]->Fill(-iter->y,iter->x,1.);
00601         else if(side == 2) HotColdMaps[(mylayer + 2) + (mylayer - 13)]->Fill(-iter->y,iter->x,1.);
00602         //if(side == 1) HotColdMaps[(mylayer + 2) + (mylayer - 14)]->Fill(iter->x,iter->y,1.);
00603         //else if(side == 2) HotColdMaps[(mylayer + 2) + (mylayer - 13)]->Fill(iter->x,iter->y,1.);
00604       }
00605     }
00606   }
00607   cout << "Finished HotCold Map Generation\n";
00608 }
00609 
00610 void SiStripHitEffFromCalibTree::makeTKMap() {
00611   cout << "Entering TKMap generation!\n";
00612   tkmap = new TrackerMap("  Detector Inefficiency  ");
00613   tkmapbad = new TrackerMap("  Inefficient Modules  ");
00614   for(Long_t i = 1; i <= 22; i++) {
00615     layertotal[i] = 0;
00616     layerfound[i] = 0;
00617     //Loop over every layer, extracting the information from
00618     //the map of the efficiencies
00619     map<unsigned int, pair<unsigned int, unsigned int> >::const_iterator ih;
00620     for( ih = modCounter[i].begin(); ih != modCounter[i].end(); ih++) {
00621       //We should be in the layer in question, and looping over all of the modules in said layer
00622       //Generate the list for the TKmap, and the bad module list
00623       double myeff = (double)(((*ih).second).second)/(((*ih).second).first);
00624       if ( ((((*ih).second).first) >= nModsMin) && (myeff < threshold) ) {
00625         //We have a bad module, put it in the list!
00626         BadModules[(*ih).first] = myeff;
00627         tkmapbad->fillc((*ih).first,255,0,0);
00628         cout << "Layer " << i << " module " << (*ih).first << " efficiency " << myeff << " " << (((*ih).second).second) << "/" << (((*ih).second).first) << endl;
00629       }
00630       else {
00631         //Fill the bad list with empty results for every module
00632         tkmapbad->fillc((*ih).first,255,255,255);
00633       }
00634       if((((*ih).second).first) < 100 ) {
00635         cout << "Module " << (*ih).first << " layer " << i << " is under occupancy at " << (((*ih).second).first) << endl;
00636       }
00637       //Put any module into the TKMap
00638       //Should call module ID, and then 1- efficiency for that module
00639       //if((*ih).first == 369137820) {
00640       //  cout << "Module 369137820 has 1-eff of " << 1.-myeff << endl;
00641         //cout << "Which is " << ((*ih).second).second << "/" << ((*ih).second).first << endl;
00642       //}
00643       tkmap->fill((*ih).first,1.-myeff);
00644       //Find the total number of hits in the module
00645       layertotal[i] += int(((*ih).second).first);
00646       layerfound[i] += int(((*ih).second).second);
00647     }
00648   }
00649   tkmap->save(true, 0, 0, "SiStripHitEffTKMap.png");
00650   tkmapbad->save(true, 0, 0, "SiStripHitEffTKMapBad.png");
00651   cout << "Finished TKMap Generation\n";
00652 }
00653 
00654 void SiStripHitEffFromCalibTree::makeSQLite() {
00655   //Generate the SQLite file for use in the Database of the bad modules!
00656   cout << "Entering SQLite file generation!\n";
00657   std::vector<unsigned int> BadStripList;
00658   unsigned short NStrips;
00659   unsigned int id1;
00660   SiStripQuality* pQuality = new SiStripQuality;
00661   //This is the list of the bad strips, use to mask out entire APVs
00662   //Now simply go through the bad hit list and mask out things that
00663   //are bad!
00664   map< unsigned int, double >::const_iterator it;
00665   for(it = BadModules.begin(); it != BadModules.end(); it++) {
00666     //We need to figure out how many strips are in this particular module
00667     //To Mask correctly!
00668     NStrips=reader->getNumberOfApvsAndStripLength((*it).first).first*128;
00669     cout << "Number of strips module " << (*it).first << " is " << NStrips << endl;
00670     BadStripList.push_back(pQuality->encode(0,NStrips,0));
00671     //Now compact into a single bad module
00672     id1=(unsigned int)(*it).first;
00673     cout << "ID1 shoudl match list of modules above " << id1 << endl;
00674     quality_->compact(id1,BadStripList);
00675     SiStripQuality::Range range(BadStripList.begin(),BadStripList.end());
00676     quality_->put(id1,range);
00677     BadStripList.clear();
00678   }
00679   //Fill all the bad components now
00680   quality_->fillBadComponents();
00681 }
00682 
00683 void SiStripHitEffFromCalibTree::totalStatistics() {
00684   //Calculate the statistics by layer
00685   int totalfound = 0;
00686   int totaltotal = 0;
00687   double layereff;
00688   for(Long_t i=1; i<=22; i++) {
00689     layereff = double(layerfound[i])/double(layertotal[i]);
00690     cout << "Layer " << i << " has total efficiency " << layereff << " " << layerfound[i] << "/" << layertotal[i] << endl;
00691     totalfound += layerfound[i];
00692     totaltotal += layertotal[i];
00693   }
00694   cout << "The total efficiency is " << double(totalfound)/double(totaltotal) << endl;
00695 }
00696 
00697 void SiStripHitEffFromCalibTree::makeSummary() {
00698   //setTDRStyle();
00699   
00700   int nLayers = 34;
00701   
00702   TH1F *found = fs->make<TH1F>("found","found",nLayers+1,0,nLayers+1);
00703   TH1F *all = fs->make<TH1F>("all","all",nLayers+1,0,nLayers+1);
00704   TH1F *found2 = fs->make<TH1F>("found2","found2",nLayers+1,0,nLayers+1);
00705   TH1F *all2 = fs->make<TH1F>("all2","all2",nLayers+1,0,nLayers+1);
00706   // first bin only to keep real data off the y axis so set to -1
00707   found->SetBinContent(0,-1);
00708   all->SetBinContent(0,1);
00709   
00710   TCanvas *c7 =new TCanvas("c7"," test ",10,10,800,600);
00711   c7->SetFillColor(0);
00712   c7->SetGrid();
00713 
00714   for (Long_t i=1; i< nLayers+1; ++i) {
00715     if (i==10) i++;
00716     if (i==25) i++;
00717     if (i==34) break;
00718 
00719     cout << "Fill only good modules layer " << i << ":  S = " << goodlayerfound[i] << "    B = " << goodlayertotal[i] << endl;
00720     if (goodlayertotal[i] > 5) {
00721       found->SetBinContent(i,goodlayerfound[i]);
00722       all->SetBinContent(i,goodlayertotal[i]);
00723     } else {
00724       found->SetBinContent(i,0);
00725       all->SetBinContent(i,10);
00726     }
00727     
00728     cout << "Filling all modules layer " << i << ":  S = " << alllayerfound[i] << "    B = " << alllayertotal[i] << endl;
00729     if (alllayertotal[i] > 5) {
00730       found2->SetBinContent(i,alllayerfound[i]);
00731       all2->SetBinContent(i,alllayertotal[i]);
00732     } else {
00733       found2->SetBinContent(i,0);
00734       all2->SetBinContent(i,10);
00735     }
00736 
00737   }
00738 
00739   found->Sumw2();
00740   all->Sumw2();
00741 
00742   found2->Sumw2();
00743   all2->Sumw2();
00744 
00745   TGraphAsymmErrors *gr = new TGraphAsymmErrors(nLayers+1);
00746   gr->BayesDivide(found,all); 
00747 
00748   TGraphAsymmErrors *gr2 = new TGraphAsymmErrors(nLayers+1);
00749   gr2->BayesDivide(found2,all2);
00750 
00751   for(int j = 0; j<nLayers+1; j++){
00752     gr->SetPointError(j, 0., 0., gr->GetErrorYlow(j),gr->GetErrorYhigh(j) );
00753     gr2->SetPointError(j, 0., 0., gr2->GetErrorYlow(j),gr2->GetErrorYhigh(j) );
00754   }
00755 
00756   gr->GetXaxis()->SetLimits(0,nLayers);
00757   gr->SetMarkerColor(2);
00758   gr->SetMarkerSize(1.2);
00759   gr->SetLineColor(2);
00760   gr->SetLineWidth(4);
00761   gr->SetMarkerStyle(20);
00762   gr->SetMinimum(0.90);
00763   gr->SetMaximum(1.001);
00764   gr->GetYaxis()->SetTitle("Efficiency");
00765 
00766   gr2->GetXaxis()->SetLimits(0,nLayers);
00767   gr2->SetMarkerColor(1);
00768   gr2->SetMarkerSize(1.2);
00769   gr2->SetLineColor(1);
00770   gr2->SetLineWidth(4);
00771   gr2->SetMarkerStyle(21);
00772   gr2->SetMinimum(0.90);
00773   gr2->SetMaximum(1.001);
00774   gr2->GetYaxis()->SetTitle("Efficiency");
00775   //cout << "starting labels" << endl;
00776   //for ( int k=1; k<nLayers+1; k++) {
00777   for ( Long_t k=1; k<nLayers+1; k++) {
00778     if (k==10) k++;
00779     if (k==25) k++;
00780     if (k==34) break;
00781     TString label;
00782     if (k<5) {
00783       label = TString("TIB  ") + k;
00784     } else if (k>4&&k<11) {
00785       label = TString("TOB  ")+(k-4);
00786     } else if (k>10&&k<14) {
00787       label = TString("TID- ")+(k-10);
00788     } else if (k>13&&k<17) {
00789       label = TString("TID+ ")+(k-13);
00790     } else if (k>16&&k<26) {
00791       label = TString("TEC- ")+(k-16);
00792     } else if (k>25) {
00793       label = TString("TEC+ ")+(k-25);
00794     }
00795     gr->GetXaxis()->SetBinLabel(((k+1)*100)/(nLayers)-2,label);
00796     gr2->GetXaxis()->SetBinLabel(((k+1)*100)/(nLayers)-2,label);
00797   }
00798   
00799   gr->Draw("AP");
00800   gr->GetXaxis()->SetNdivisions(36);
00801 
00802   c7->cd();
00803   TPad *overlay = new TPad("overlay","",0,0,1,1);
00804   overlay->SetFillStyle(4000);
00805   overlay->SetFillColor(0);
00806   overlay->SetFrameFillStyle(4000);
00807   overlay->Draw("same");
00808   overlay->cd();
00809   gr2->Draw("AP");
00810 
00811   TLegend *leg = new TLegend(0.70,0.20,0.92,0.39);
00812   leg->AddEntry(gr,"Good Modules","p");
00813   leg->AddEntry(gr2,"All Modules","p");
00814   leg->SetTextSize(0.020);
00815   leg->SetFillColor(0);
00816   leg->Draw("same");
00817   
00818   c7->SaveAs("Summary.png");
00819 }
00820 
00821 SiStripBadStrip* SiStripHitEffFromCalibTree::getNewObject() {
00822   //Need this for a Condition DB Writer
00823   //Initialize a return variable
00824   SiStripBadStrip* obj=new SiStripBadStrip();
00825   
00826   SiStripBadStrip::RegistryIterator rIter=quality_->getRegistryVectorBegin();
00827   SiStripBadStrip::RegistryIterator rIterEnd=quality_->getRegistryVectorEnd();
00828   
00829   for(;rIter!=rIterEnd;++rIter){
00830     SiStripBadStrip::Range range(quality_->getDataVectorBegin()+rIter->ibegin,quality_->getDataVectorBegin()+rIter->iend);
00831     if ( ! obj->put(rIter->detid,range) )
00832     edm::LogError("SiStripHitEffFromCalibTree")<<"[SiStripHitEffFromCalibTree::getNewObject] detid already exists"<<std::endl;
00833   }
00834   
00835   return obj;
00836 }
00837 
00838 float SiStripHitEffFromCalibTree::calcPhi(float x, float y) {
00839   float phi = 0;
00840   float Pi = 3.14159;
00841   if((x>=0)&&(y>=0)) phi = atan(y/x);
00842   else if((x>=0)&&(y<=0)) phi = atan(y/x) + 2*Pi;
00843   else if((x<=0)&&(y>=0)) phi = atan(y/x) + Pi;
00844   else phi = atan(y/x) + Pi;
00845   phi = phi*180.0/Pi;
00846 
00847   return phi;
00848 } 
00849 
00850 void SiStripHitEffFromCalibTree::SetBadComponents(int i, int component,SiStripQuality::BadComponent& BC, std::stringstream ssV[4][19], int NBadComponent[4][19][4]){
00851  
00852   int napv=reader->getNumberOfApvsAndStripLength(BC.detid).first;
00853  
00854   ssV[i][component] << "\n\t\t " 
00855                     << BC.detid 
00856                     << " \t " << BC.BadModule << " \t " 
00857                     << ( (BC.BadFibers)&0x1 ) << " ";
00858   if (napv==4)
00859     ssV[i][component] << "x " <<( (BC.BadFibers>>1)&0x1 );
00860    
00861   if (napv==6)
00862     ssV[i][component] << ( (BC.BadFibers>>1)&0x1 ) << " "
00863                       << ( (BC.BadFibers>>2)&0x1 );
00864     ssV[i][component] << " \t " 
00865                       << ( (BC.BadApvs)&0x1 ) << " " 
00866                       << ( (BC.BadApvs>>1)&0x1 ) << " ";
00867   if (napv==4) 
00868     ssV[i][component] << "x x " << ( (BC.BadApvs>>2)&0x1 ) << " " 
00869                       << ( (BC.BadApvs>>3)&0x1 );
00870   if (napv==6) 
00871     ssV[i][component] << ( (BC.BadApvs>>2)&0x1 ) << " " 
00872                       << ( (BC.BadApvs>>3)&0x1 ) << " " 
00873                       << ( (BC.BadApvs>>4)&0x1 ) << " " 
00874                       << ( (BC.BadApvs>>5)&0x1 ) << " "; 
00875  
00876   if (BC.BadApvs){
00877     NBadComponent[i][0][2]+= ( (BC.BadApvs>>5)&0x1 )+ ( (BC.BadApvs>>4)&0x1 ) + ( (BC.BadApvs>>3)&0x1 ) + 
00878       ( (BC.BadApvs>>2)&0x1 )+ ( (BC.BadApvs>>1)&0x1 ) + ( (BC.BadApvs)&0x1 );
00879     NBadComponent[i][component][2]+= ( (BC.BadApvs>>5)&0x1 )+ ( (BC.BadApvs>>4)&0x1 ) + ( (BC.BadApvs>>3)&0x1 ) + 
00880       ( (BC.BadApvs>>2)&0x1 )+ ( (BC.BadApvs>>1)&0x1 ) + ( (BC.BadApvs)&0x1 );
00881   }
00882   if (BC.BadFibers){ 
00883     NBadComponent[i][0][1]+= ( (BC.BadFibers>>2)&0x1 )+ ( (BC.BadFibers>>1)&0x1 ) + ( (BC.BadFibers)&0x1 );
00884     NBadComponent[i][component][1]+= ( (BC.BadFibers>>2)&0x1 )+ ( (BC.BadFibers>>1)&0x1 ) + ( (BC.BadFibers)&0x1 );
00885   }   
00886   if (BC.BadModule){
00887     NBadComponent[i][0][0]++;
00888     NBadComponent[i][component][0]++;
00889   }
00890 }
00891 
00892 DEFINE_FWK_MODULE(SiStripHitEffFromCalibTree);