CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Validation/RecoVertex/src/PrimaryVertexAnalyzer4PU.cc

Go to the documentation of this file.
00001 #include "Validation/RecoVertex/interface/PrimaryVertexAnalyzer4PU.h"
00002 
00003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include "FWCore/Version/interface/GetReleaseVersion.h"
00006 #include "MagneticField/Engine/interface/MagneticField.h"
00007 
00008 // reco track and vertex 
00009 #include "DataFormats/TrackReco/interface/Track.h"
00010 #include "DataFormats/VertexReco/interface/Vertex.h"
00011 #include "DataFormats/Math/interface/Point3D.h"
00012 #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
00013 
00014 // simulated vertices,..., add <use name=SimDataFormats/Vertex> and <../Track>
00015 #include <SimDataFormats/Vertex/interface/SimVertex.h>
00016 #include <SimDataFormats/Vertex/interface/SimVertexContainer.h>
00017 #include <SimDataFormats/Track/interface/SimTrack.h>
00018 #include <SimDataFormats/Track/interface/SimTrackContainer.h>
00019 
00020 #include "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00021 #include "SimDataFormats/CrossingFrame/interface/CrossingFramePlaybackInfo.h"
00022 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00023 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00024 
00025 //generator level + CLHEP
00026 #include "HepMC/GenEvent.h"
00027 #include "HepMC/GenVertex.h"
00028 
00029 
00030 // TrackingParticle
00031 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
00032 #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertexContainer.h"
00033 //associator
00034 #include "SimTracker/Records/interface/TrackAssociatorRecord.h"
00035 
00036 
00037 // fit
00038 #include "RecoVertex/KalmanVertexFit/interface/KalmanVertexFitter.h"
00039 #include "RecoVertex/AdaptiveVertexFit/interface/AdaptiveVertexFitter.h"
00040 
00041 #include "CommonTools/Statistics/interface/ChiSquaredProbability.h"
00042 
00043 // Root
00044 #include <TH1.h>
00045 #include <TH2.h>
00046 #include <TFile.h>
00047 #include <TProfile.h>
00048  
00049 #include <cmath>
00050 #include <gsl/gsl_math.h>
00051 #include <gsl/gsl_eigen.h>
00052 
00053 
00054 // cluster stufff
00055 //#include "DataFormats/TrackRecoTrack.h"
00056 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
00057 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"
00058 
00059 
00060 using namespace edm;
00061 using namespace reco;
00062 using namespace std;
00063 //
00064 // constants, enums and typedefs
00065 //
00066 typedef reco::Vertex::trackRef_iterator trackit_t;
00067 //
00068 // static data member definitions
00069 //
00070 
00071 //
00072 // constructors and destructor
00073 //
00074 PrimaryVertexAnalyzer4PU::PrimaryVertexAnalyzer4PU(const ParameterSet& iConfig):theTrackFilter(iConfig.getParameter<edm::ParameterSet>("TkFilterParameters"))
00075 {
00076    //now do what ever initialization is needed
00077   simG4_=iConfig.getParameter<edm::InputTag>( "simG4" );
00078   recoTrackProducer_= iConfig.getUntrackedParameter<std::string>("recoTrackProducer");
00079   // open output file to store histograms}
00080   outputFile_  = iConfig.getUntrackedParameter<std::string>("outputFile");
00081 
00082   rootFile_ = TFile::Open(outputFile_.c_str(),"RECREATE");
00083   verbose_= iConfig.getUntrackedParameter<bool>("verbose", false);
00084   doMatching_= iConfig.getUntrackedParameter<bool>("matching", false);
00085   printXBS_= iConfig.getUntrackedParameter<bool>("XBS", false);
00086   simUnit_= 1.0;  // starting with CMSSW_1_2_x ??
00087   if ( (edm::getReleaseVersion()).find("CMSSW_1_1_",0)!=std::string::npos){
00088     simUnit_=0.1;  // for use in  CMSSW_1_1_1 tutorial
00089   }
00090   
00091   dumpPUcandidates_=iConfig.getUntrackedParameter<bool>("dumpPUcandidates", false);
00092 
00093   zmatch_=iConfig.getUntrackedParameter<double>("zmatch", 0.0500);
00094   cout << "PrimaryVertexAnalyzer4PU: zmatch=" << zmatch_ << endl;
00095   eventcounter_=0;
00096   dumpcounter_=0;
00097   ndump_=10;
00098   DEBUG_=false;
00099   //DEBUG_=true;
00100 }
00101 
00102 
00103 
00104 
00105 PrimaryVertexAnalyzer4PU::~PrimaryVertexAnalyzer4PU()
00106 {
00107     // do anything here that needs to be done at desctruction time
00108    // (e.g. close files, deallocate resources etc.)
00109   delete rootFile_;
00110 }
00111 
00112 
00113 
00114 //
00115 // member functions
00116 //
00117 
00118 
00119 std::map<std::string, TH1*>  PrimaryVertexAnalyzer4PU::bookVertexHistograms(){
00120   std::map<std::string, TH1*> h;
00121 
00122   // release validation histograms used in DoCompare.C
00123   h["nbtksinvtx"]   = new TH1F("nbtksinvtx","reconstructed tracks in vertex",40,-0.5,39.5); 
00124   h["nbtksinvtxPU"] = new TH1F("nbtksinvtxPU","reconstructed tracks in vertex",40,-0.5,39.5); 
00125   h["nbtksinvtxTag"]= new TH1F("nbtksinvtxTag","reconstructed tracks in vertex",40,-0.5,39.5); 
00126   h["resx"]         = new TH1F("resx","residual x",100,-0.04,0.04);
00127   h["resy"]         = new TH1F("resy","residual y",100,-0.04,0.04);
00128   h["resz"]         = new TH1F("resz","residual z",100,-0.1,0.1);
00129   h["resz10"]       = new TH1F("resz10","residual z",100,-1.0,1.);
00130   h["pullx"]        = new TH1F("pullx","pull x",100,-25.,25.);
00131   h["pully"]        = new TH1F("pully","pull y",100,-25.,25.);
00132   h["pullz"]        = new TH1F("pullz","pull z",100,-25.,25.);
00133   h["vtxchi2"]      = new TH1F("vtxchi2","chi squared",100,0.,100.);
00134   h["vtxndf"]       = new TH1F("vtxndf","degrees of freedom",500,0.,100.);
00135   h["vtxndfc"]       = new TH1F("vtxndfc","expected 2nd highest ndof",500,0.,100.);
00136 
00137   h["vtxndfvsntk"]  = new TH2F("vtxndfvsntk","ndof vs #tracks",20,0.,100, 20, 0., 200.);
00138   h["vtxndfoverntk"]= new TH1F("vtxndfoverntk","ndof / #tracks",40,0.,2.);
00139   h["vtxndf2overntk"]= new TH1F("vtxndf2overntk","(ndof+2) / #tracks",40,0.,2.);
00140   h["tklinks"]      = new TH1F("tklinks","Usable track links",2,-0.5,1.5);
00141   h["nans"]         = new TH1F("nans","Illegal values for x,y,z,xx,xy,xz,yy,yz,zz",9,0.5,9.5);
00142 
00143 
00144   // raw
00145   add(h, new TH1F("szRecVtx","size of recvtx collection",20, -0.5, 19.5));
00146   add(h, new TH1F("isFake","fake vertex",2, -0.5, 1.5));
00147   add(h, new TH1F("isFake1","fake vertex or ndof<0",2, -0.5, 1.5));
00148   add(h, new TH1F("bunchCrossing","bunchCrossing",4000, 0., 4000.));
00149   add(h, new TH2F("bunchCrossingLogNtk","bunchCrossingLogNtk",4000, 0., 4000.,5,0., 5.));
00150   add(h, new TH1F("highpurityTrackFraction","fraction of high purity tracks",20, 0., 1.));
00151   add(h, new TH2F("trkchi2vsndof","vertices chi2 vs ndof",50, 0., 100., 50, 0., 200.));
00152   add(h, new TH1F("trkchi2overndof","vertices chi2 / ndof",50, 0., 5.));
00153   // two track vertices
00154   add(h,new TH2F("2trkchi2vsndof","two-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00155   add(h,new TH1F("2trkmassSS","two-track vertices mass (same sign)",100, 0., 2.));
00156   add(h,new TH1F("2trkmassOS","two-track vertices mass (opposite sign)",100, 0., 2.));
00157   add(h,new TH1F("2trkdphi","two-track vertices delta-phi",360, 0, 2*M_PI));
00158   add(h,new TH1F("2trkseta","two-track vertices sum-eta",50, -2., 2.));
00159   add(h,new TH1F("2trkdphicurl","two-track vertices delta-phi (sum eta<0.1)",360, 0, 2*M_PI));
00160   add(h,new TH1F("2trksetacurl","two-track vertices sum-eta (delta-phi<0.1)",50, -2., 2.));
00161   add(h,new TH1F("2trkdetaOS","two-track vertices delta-eta (same sign)",50, -0.5, 0.5));
00162   add(h,new TH1F("2trkdetaSS","two-track vertices delta-eta (opposite sign)",50, -0.5, 0.5));
00163   // two track PU vertices
00164   add(h,new TH1F("2trkmassSSPU","two-track vertices mass (same sign)",100, 0., 2.));
00165   add(h,new TH1F("2trkmassOSPU","two-track vertices mass (opposite sign)",100, 0., 2.));
00166   add(h,new TH1F("2trkdphiPU","two-track vertices delta-phi",360, 0, 2*M_PI));
00167   add(h,new TH1F("2trksetaPU","two-track vertices sum-eta",50, -2., 2.));
00168   add(h,new TH1F("2trkdphicurlPU","two-track vertices delta-phi (sum eta<0.1)",360, 0, 2*M_PI));
00169   add(h,new TH1F("2trksetacurlPU","two-track vertices sum-eta (delta-phi<0.1)",50, -2., 2.));
00170   add(h,new TH1F("2trkdetaOSPU","two-track vertices delta-eta (same sign)",50, -0.5, 0.5));
00171   add(h,new TH1F("2trkdetaSSPU","two-track vertices delta-eta (opposite sign)",50, -0.5, 0.5));
00172   // three track vertices
00173   add(h,new TH2F("2trkchi2vsndof","two-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00174   add(h,new TH2F("3trkchi2vsndof","three-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00175   add(h,new TH2F("4trkchi2vsndof","four-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00176   add(h,new TH2F("5trkchi2vsndof","five-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00177   // same for fakes
00178   add(h,new TH2F("fake2trkchi2vsndof","fake two-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00179   add(h,new TH2F("fake3trkchi2vsndof","fake three-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00180   add(h,new TH2F("fake4trkchi2vsndof","fake four-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00181   add(h,new TH2F("fake5trkchi2vsndof","fake five-track vertices chi2 vs ndof",40, 0., 10., 20, 0., 20.));
00182   h["resxr"]        = new TH1F("resxr","relative residual x",100,-0.04,0.04);
00183   h["resyr"]        = new TH1F("resyr","relative residual y",100,-0.04,0.04);
00184   h["reszr"]        = new TH1F("reszr","relative residual z",100,-0.1,0.1);
00185   h["pullxr"]       = new TH1F("pullxr","relative pull x",100,-25.,25.);
00186   h["pullyr"]       = new TH1F("pullyr","relative pull y",100,-25.,25.);
00187   h["pullzr"]       = new TH1F("pullzr","relative pull z",100,-25.,25.);
00188   h["vtxprob"]      = new TH1F("vtxprob","chisquared probability",100,0.,1.);
00189   h["eff"]          = new TH1F("eff","efficiency",2, -0.5, 1.5);
00190   h["efftag"]       = new TH1F("efftag","efficiency tagged vertex",2, -0.5, 1.5);
00191   h["zdistancetag"] = new TH1F("zdistancetag","z-distance between tagged and generated",100, -0.1, 0.1);
00192   h["abszdistancetag"] = new TH1F("abszdistancetag","z-distance between tagged and generated",1000, 0., 1.0);
00193   h["abszdistancetagcum"] = new TH1F("abszdistancetagcum","z-distance between tagged and generated",1000, 0., 1.0);
00194   h["puritytag"]    = new TH1F("puritytag","purity of primary vertex tags",2, -0.5, 1.5);
00195   h["effvsptsq"]    = new TProfile("effvsptsq","efficiency vs ptsq",20, 0., 10000., 0, 1.);
00196   h["effvsnsimtrk"] = new TProfile("effvsnsimtrk","efficiency vs # simtracks",50, 0., 50., 0, 1.);
00197   h["effvsnrectrk"] = new TProfile("effvsnrectrk","efficiency vs # rectracks",50, 0., 50., 0, 1.);
00198   h["effvsnseltrk"] = new TProfile("effvsnseltrk","efficiency vs # selected tracks",50, 0., 50., 0, 1.);
00199   h["effvsz"]       = new TProfile("effvsz","efficiency vs z",20, -20., 20., 0, 1.);
00200   h["effvsz2"]      = new TProfile("effvsz2","efficiency vs z (2mm)",20, -20., 20., 0, 1.);
00201   h["effvsr"]       = new TProfile("effvsr","efficiency vs r",20, 0., 1., 0, 1.);
00202   h["xresvsntrk"] = new TProfile("xresvsntrk","xresolution vs # vertex tracks",40, 0., 200., 0, 0.01);
00203   h["yresvsntrk"] = new TProfile("yresvsntrk","yresolution vs # vertex tracks",40, 0., 200., 0, 0.01);
00204   h["zresvsntrk"] = new TProfile("zresvsntrk","zresolution vs # vertex tracks",40, 0., 200., 0, 0.01);
00205   h["cpuvsntrk"] = new TProfile("cpuvsntrk","cpu time vs # of fitted tracks",40, 0., 200., 0, 200.);
00206   h["cpucluvsntrk"] = new TProfile("cpucluvsntrk","clustering cpu time # of tracks",40, 0., 200., 0, 10.);
00207   h["cpufit"]    = new TH1F("cpufit","cpu time for fitting",100, 0., 200.);
00208   h["cpuclu"]    = new TH1F("cpuclu","cpu time for clustering",100, 0., 200.);
00209   h["nbtksinvtx2"]   = new TH1F("nbtksinvtx2","reconstructed tracks in vertex",40,0.,200.); 
00210   h["nbtksinvtxPU2"]   = new TH1F("nbtksinvtxPU2","reconstructed tracks in vertex",40,0.,200.); 
00211   h["nbtksinvtxTag2"]   = new TH1F("nbtksinvtxTag2","reconstructed tracks in vertex",40,0.,200.); 
00212 
00213   h["xrec"]         = new TH1F("xrec","reconstructed x",100,-0.1,0.1);
00214   h["yrec"]         = new TH1F("yrec","reconstructed y",100,-0.1,0.1);
00215   h["zrec"]         = new TH1F("zrec","reconstructed z",100,-20.,20.);
00216   h["err1"]         = new TH1F("err1","error 1",100,0.,0.1);
00217   h["err2"]         = new TH1F("err2","error 2",100,0.,0.1);
00218   h["errx"]         = new TH1F("errx","error x",100,0.,0.1);
00219   h["erry"]         = new TH1F("erry","error y",100,0.,0.1);
00220   h["errz"]         = new TH1F("errz","error z",100,0.,2.0);
00221   h["errz1"]        = new TH1F("errz1","error z",100,0.,0.2);
00222 
00223   h["zrecNt100"]         = new TH1F("zrecNt100","reconstructed z for high multiplicity events",80,-40.,40.);
00224   add(h, new TH2F("zrecvsnt","reconstructed z vs number of tracks",100,-50., 50., 20, 0., 200.));
00225   add(h, new TH2F("xyrec","reconstructed xy",100, -4., 4., 100, -4., 4.));
00226   h["xrecBeam"]     = new TH1F("xrecBeam","reconstructed x - beam x",100,-0.1,0.1);
00227   h["yrecBeam"]     = new TH1F("yrecBeam","reconstructed y - beam y",100,-0.1,0.1);
00228   h["zrecBeam"]     = new TH1F("zrecBeam","reconstructed z - beam z",100,-20.,20.);
00229   h["xrecBeamvsz"] = new TH2F("xrecBeamvsz","reconstructed x - beam x vs z", 20, -20., 20.,100,-0.1,0.1);
00230   h["yrecBeamvsz"] = new TH2F("yrecBeamvsz","reconstructed y - beam y vs z", 20, -20., 20.,100,-0.1,0.1);
00231   h["xrecBeamvszprof"] = new TProfile("xrecBeamvszprof","reconstructed x - beam x vs z-z0", 20, -20., 20.,-0.1,0.1);
00232   h["yrecBeamvszprof"] = new TProfile("yrecBeamvszprof","reconstructed y - beam y vs z-z0", 20, -20., 20.,-0.1,0.1);
00233   add(h, new TH2F("xrecBeamvsdxXBS","reconstructed x - beam x vs resolution",10,0., 0.02, 100, -0.1,0.1)); // just a test
00234   add(h, new TH2F("yrecBeamvsdyXBS","reconstructed z - beam z vs resolution",10,0., 0.02, 100, -0.1,0.1));
00235   add(h, new TH2F("xrecBeamvsdx","reconstructed x - beam x vs resolution",10,0., 0.02, 100, -0.1,0.1));
00236   add(h, new TH2F("yrecBeamvsdy","reconstructed z - beam z vs resolution",10,0., 0.02, 100, -0.1,0.1));
00237   add(h, new TH2F("xrecBeamvsdxR2","reconstructed x - beam x vs resolution",20,0., 0.04, 100, -0.1,0.1));
00238   add(h, new TH2F("yrecBeamvsdyR2","reconstructed z - beam z vs resolution",20,0., 0.04, 100, -0.1,0.1));
00239   //  add(h, new TH2F("xrecBeamvsdx","reconstructed x - beam x vs resolution",100,-0.1,0.1, 10, 0., 0.04));
00240   //  add(h, new TH2F("yrecBeamvsdy","reconstructed y - beam y vs resolution",100,-0.1,0.1, 10, 0., 0.04));
00241   h["xrecBeamvsdxprof"] = new TProfile("xrecBeamvsdxprof","reconstructed x - beam x vs resolution",10, 0., 0.04,-0.1,0.1 );
00242   h["yrecBeamvsdyprof"] = new TProfile("yrecBeamvsdyprof","reconstructed y - beam y vs resolution",10, 0., 0.04,-0.1,0.1 );
00243   add(h, new TProfile("xrecBeam2vsdx2prof","reconstructed x - beam x vs resolution",10,0., 0.002, 0., 0.01));
00244   add(h, new TProfile("yrecBeam2vsdy2prof","reconstructed y - beam y vs resolution",10,0., 0.002, 0., 0.01));
00245   add(h,new TH2F("xrecBeamvsdx2","reconstructed x - beam x vs resolution",10,0., 0.002, 100, -0.01, 0.01));
00246   add(h,new TH2F("yrecBeamvsdy2","reconstructed y - beam y vs resolution",10,0., 0.002, 100, -0.01, 0.01));
00247   h["xrecb"]        = new TH1F("xrecb","reconstructed x - beam x",100,-0.01,0.01);
00248   h["yrecb"]        = new TH1F("yrecb","reconstructed y - beam y",100,-0.01,0.01);
00249   h["zrecb"]        = new TH1F("zrecb","reconstructed z - beam z",100,-20.,20.);
00250   h["xrec1"]        = new TH1F("xrec1","reconstructed x",100,-4,4);
00251   h["yrec1"]        = new TH1F("yrec1","reconstructed y",100,-4,4);  // should match the sim histos
00252   h["zrec1"]        = new TH1F("zrec1","reconstructed z",100,-80.,80.);
00253   h["xrec2"]        = new TH1F("xrec2","reconstructed x",100,-1,1);
00254   h["yrec2"]        = new TH1F("yrec2","reconstructed y",100,-1,1);
00255   h["zrec2"]        = new TH1F("zrec2","reconstructed z",100,-40.,40.);
00256   h["xrec3"]        = new TH1F("xrec3","reconstructed x",100,-0.1,0.1);
00257   h["yrec3"]        = new TH1F("yrec3","reconstructed y",100,-0.1,0.1);
00258   h["zrec3"]        = new TH1F("zrec3","reconstructed z",100,-20.,20.);
00259   add(h, new TH1F("xrecBeamPull","normalized residuals reconstructed x - beam x",100,-20,20));
00260   add(h, new TH1F("yrecBeamPull","normalized residuals reconstructed y - beam y",100,-20,20));
00261   add(h, new TH1F("zdiffrec","z-distance between vertices",200,-20., 20.));
00262   add(h, new TH1F("zdiffrec2","z-distance between ndof>2 vertices",200,-20., 20.));
00263   add(h, new TH1F("zdiffrec4","z-distance between ndof>4 vertices",200,-20., 20.));
00264   add(h, new TH1F("zdiffrec8","z-distance between ndof>8 vertices",200,-20., 20.));
00265   add(h, new TH2F("zvszrec2","z positions of multiple vertices",200,-20., 20., 200,-20., 20.));
00266   add(h, new TH2F("pzvspz2","prob(z) of multiple vertices",100, 0.,1.,100,0., 1.));
00267   add(h, new TH2F("zvszrec4","z positions of multiple vertices",100,-20., 20., 100,-20., 20.));
00268   add(h, new TH2F("pzvspz4","prob(z) of multiple vertices",100, 0.,1.,100,0., 1.));
00269   add(h, new TH2F("zdiffvsz","z-distance vs z",100,-10.,10.,30,-15.,15.));
00270   add(h, new TH2F("zdiffvsz4","z-distance vs z (ndof>4)",100,-10.,10.,30,-15.,15.));
00271   add(h, new TProfile("eff0vsntrec","efficiency vs # reconstructed tracks",50, 0., 50., 0, 1.));
00272   add(h, new TProfile("eff0vsntsel","efficiency vs # selected tracks",50, 0., 50., 0, 1.));
00273   add(h, new TProfile("eff0ndof0vsntsel","efficiency (ndof>0) vs # selected tracks",50, 0., 50., 0, 1.));
00274   add(h, new TProfile("eff0ndof8vsntsel","efficiency (ndof>8) vs # selected tracks",50, 0., 50., 0, 1.));
00275   add(h, new TProfile("eff0ndof2vsntsel","efficiency (ndof>2) vs # selected tracks",50, 0., 50., 0, 1.));
00276   add(h, new TProfile("eff0ndof4vsntsel","efficiency (ndof>4) vs # selected tracks",50, 0., 50., 0, 1.));
00277   add(h, new TH1F("xbeamPUcand","x - beam of PU candidates (ndof>4)",100,-1., 1.));
00278   add(h, new TH1F("ybeamPUcand","y - beam of PU candidates (ndof>4)",100,-1., 1.));
00279   add(h, new TH1F("xbeamPullPUcand","x - beam pull of PU candidates (ndof>4)",100,-20., 20.));
00280   add(h, new TH1F("ybeamPullPUcand","y - beam pull of PU candidates (ndof>4)",100,-20., 20.));
00281   add(h, new TH1F("ndofOverNtk","ndof / ntk of ndidates (ndof>4)",100,0., 2.));
00282   //add(h, new TH1F("sumwOverNtk","sumw / ntk of ndidates (ndof>4)",100,0., 2.));
00283   add(h, new TH1F("ndofOverNtkPUcand","ndof / ntk of ndidates (ndof>4)",100,0., 2.));
00284   //add(h, new TH1F("sumwOverNtkPUcand","sumw / ntk of ndidates (ndof>4)",100,0., 2.));
00285   add(h, new TH1F("zPUcand","z positions of PU candidates (all)",100,-20., 20.));
00286   add(h, new TH1F("zPUcand2","z positions of PU candidates (ndof>2)",100,-20., 20.));
00287   add(h, new TH1F("zPUcand4","z positions of PU candidates (ndof>4)",100,-20., 20.));
00288   add(h, new TH1F("ndofPUcand","ndof of PU candidates (all)",50,0., 100.));
00289   add(h, new TH1F("ndofPUcand2","ndof of PU candidates (ndof>2)",50,0., 100.));
00290   add(h, new TH1F("ndofPUcand4","ndof of PU candidates (ndof>4)",50,0., 100.));
00291   add(h, new TH1F("ndofnr2","second highest ndof",500,0., 100.));
00292   add(h, new TH1F("ndofnr2d1cm","second highest ndof (dz>1cm)",500,0., 100.));
00293   add(h, new TH1F("ndofnr2d2cm","second highest ndof (dz>2cm)",500,0., 100.));
00294 
00295   h["nrecvtx"]      = new TH1F("nrecvtx","# of reconstructed vertices", 50, -0.5, 49.5);
00296   h["nrecvtx8"]      = new TH1F("nrecvtx8","# of reconstructed vertices with ndof>8", 50, -0.5, 49.5);
00297   h["nrecvtx2"]      = new TH1F("nrecvtx2","# of reconstructed vertices with ndof>2", 50, -0.5, 49.5);
00298   h["nrecvtx4"]      = new TH1F("nrecvtx4","# of reconstructed vertices with ndof>4", 50, -0.5, 49.5);
00299   //  h["nsimvtx"]      = new TH1F("nsimvtx","# of simulated vertices", 50, -0.5, 49.5);
00300   h["nrectrk"]      = new TH1F("nrectrk","# of reconstructed tracks", 100, -0.5, 99.5);
00301   add(h, new TH1F("nsimtrk","# of simulated tracks", 100, -0.5, 99.5));
00302   add(h, new TH1F("nsimtrkSignal","# of simulated tracks (Signal)", 100, -0.5, 99.5));
00303   add(h, new TH1F("nsimtrkPU","# of simulated tracks (PU)", 100, -0.5, 99.5));
00304   h["nsimtrk"]->StatOverflows(kTRUE);
00305   h["nsimtrkPU"]->StatOverflows(kTRUE);
00306   h["nsimtrkSignal"]->StatOverflows(kTRUE);
00307   h["xrectag"]      = new TH1F("xrectag","reconstructed x, signal vtx",100,-0.05,0.05);
00308   h["yrectag"]      = new TH1F("yrectag","reconstructed y, signal vtx",100,-0.05,0.05);
00309   h["zrectag"]      = new TH1F("zrectag","reconstructed z, signal vtx",100,-20.,20.);
00310   h["nrectrk0vtx"] = new TH1F("nrectrk0vtx","# rec tracks no vertex ",100,-0.5, 99.5);
00311   h["nseltrk0vtx"] = new TH1F("nseltrk0vtx","# rec tracks no vertex ",100,-0.5, 99.5);
00312   h["nrecsimtrk"] = new TH1F("nrecsimtrk","# rec tracks matched to sim tracks in vertex",100,-0.5, 99.5);
00313   h["nrecnosimtrk"] = new TH1F("nrecsimtrk","# rec tracks not matched to sim tracks in vertex",100,-0.5, 99.5);
00314   h["trackAssEffvsPt"] =  new TProfile("trackAssEffvsPt","track association efficiency vs pt",20, 0., 100., 0, 1.);
00315 
00316   // cluster stuff
00317   h["nseltrk"]         = new TH1F("nseltrk","# of reconstructed tracks selected for PV", 100, -0.5, 99.5);
00318   h["nclutrkall"]      = new TH1F("nclutrkall","# of reconstructed tracks in clusters", 100, -0.5, 99.5);
00319   h["nclutrkvtx"]      = new TH1F("nclutrkvtx","# of reconstructed tracks in clusters of reconstructed vertices", 100, -0.5, 99.5);
00320   h["nclu"]            = new TH1F("nclu","# of clusters", 100, -0.5, 99.5);
00321   h["nclu0vtx"]        = new TH1F("nclu0vtx","# of clusters in events with no PV", 100, -0.5, 99.5);
00322   h["zlost1"]           = new TH1F("zlost1","z of lost vertices (bad z)", 100, -20., 20.);
00323   h["zlost2"]           = new TH1F("zlost2","z of lost vertices (no matching cluster)", 100, -20., 20.);
00324   h["zlost3"]           = new TH1F("zlost3","z of lost vertices (vertex too far from beam)", 100, -20., 20.);
00325   h["zlost4"]           = new TH1F("zlost4","z of lost vertices (invalid vertex)", 100, -20., 20.);
00326   h["selstat"]     = new TH1F("selstat","selstat", 5, -2.5, 2.5);
00327   
00328 
00329   // properties of fake vertices  (MC only)_
00330   add(h, new TH1F("fakeVtxZNdofgt05","z of fake vertices with ndof>0.5", 100, -20., 20.));
00331   add(h, new TH1F("fakeVtxZNdofgt2","z of fake vertices with ndof>2", 100, -20., 20.));
00332   add(h, new TH1F("fakeVtxZ","z of fake vertices", 100, -20., 20.));
00333   add(h, new TH1F("fakeVtxNdof","ndof of fake vertices", 500,0., 100.));
00334   add(h,new TH1F("fakeVtxNtrk","number of tracks in fake vertex",20,-0.5, 19.5));
00335   add(h,new TH1F("matchedVtxNdof","ndof of matched vertices", 500,0., 100.));
00336 
00337 
00338   //  histograms of track quality (Data and MC)
00339   string types[] = {"all","sel","bachelor","sellost","wgt05","wlt05","M","tagged","untagged","ndof4","PUcand","PUfake"};
00340   for(int t=0; t<12; t++){
00341     add(h, new TH1F(("rapidity_"+types[t]).c_str(),"rapidity ",100,-3., 3.));
00342     h["z0_"+types[t]] = new TH1F(("z0_"+types[t]).c_str(),"z0 ",80,-40., 40.);
00343     h["phi_"+types[t]] = new TH1F(("phi_"+types[t]).c_str(),"phi ",80,-3.14159, 3.14159);
00344     h["eta_"+types[t]] = new TH1F(("eta_"+types[t]).c_str(),"eta ",80,-4., 4.);
00345     h["pt_"+types[t]] = new TH1F(("pt_"+types[t]).c_str(),"pt ",100,0., 20.);
00346     h["found_"+types[t]]     = new TH1F(("found_"+types[t]).c_str(),"found hits",20, 0., 20.);
00347     h["lost_"+types[t]]      = new TH1F(("lost_"+types[t]).c_str(),"lost hits",20, 0., 20.);
00348     h["nchi2_"+types[t]]     = new TH1F(("nchi2_"+types[t]).c_str(),"normalized track chi2",100, 0., 20.);
00349     h["rstart_"+types[t]]    = new TH1F(("rstart_"+types[t]).c_str(),"start radius",100, 0., 20.);
00350     h["tfom_"+types[t]]      = new TH1F(("tfom_"+types[t]).c_str(),"track figure of merit",100, 0., 100.);
00351     h["expectedInner_"+types[t]]      = new TH1F(("expectedInner_"+types[t]).c_str(),"expected inner hits ",10, 0., 10.);
00352     h["expectedOuter_"+types[t]]      = new TH1F(("expectedOuter_"+types[t]).c_str(),"expected outer hits ",10, 0., 10.);
00353     h["logtresxy_"+types[t]] = new TH1F(("logtresxy_"+types[t]).c_str(),"log10(track r-phi resolution/um)",100, 0., 5.);
00354     h["logtresz_"+types[t]] = new TH1F(("logtresz_"+types[t]).c_str(),"log10(track z resolution/um)",100, 0., 5.);
00355     h["tpullxy_"+types[t]]   = new TH1F(("tpullxy_"+types[t]).c_str(),"track r-phi pull",100, -10., 10.);
00356     add(h, new TH2F( ("lvseta_"+types[t]).c_str(),"cluster length vs eta",60,-3., 3., 20, 0., 20));
00357     add(h, new TH2F( ("lvstanlambda_"+types[t]).c_str(),"cluster length vs tan lambda",60,-6., 6., 20, 0., 20));
00358     add(h, new TH1F( ("restrkz_"+types[t]).c_str(),"z-residuals (track vs vertex)", 200, -5., 5.));
00359     add(h, new TH2F( ("restrkzvsphi_"+types[t]).c_str(),"z-residuals (track - vertex)", 12,-3.14159,3.14159,100, -5., 5.));
00360     add(h, new TH2F( ("restrkzvseta_"+types[t]).c_str(),"z-residuals (track - vertex)", 12,-3.,3.,200, -5., 5.));
00361     add(h, new TH2F( ("pulltrkzvsphi_"+types[t]).c_str(),"normalized z-residuals (track - vertex)", 12,-3.14159,3.14159,100, -5., 5.));
00362     add(h, new TH2F( ("pulltrkzvseta_"+types[t]).c_str(),"normalized z-residuals (track - vertex)", 12,-3.,3.,100, -5., 5.));
00363     add(h, new TH1F( ("pulltrkz_"+types[t]).c_str(),"normalized z-residuals (track vs vertex)", 100, -5., 5.));
00364     add(h, new TH1F( ("sigmatrkz0_"+types[t]).c_str(),"z-resolution (excluding beam)", 100, 0., 5.));
00365     add(h, new TH1F( ("sigmatrkz_"+types[t]).c_str(),"z-resolution (including beam)", 100,0., 5.));
00366     add(h, new TH1F( ("nbarrelhits_"+types[t]).c_str(),"number of pixel barrel hits", 10, 0., 10.));
00367     add(h, new TH1F( ("nbarrelLayers_"+types[t]).c_str(),"number of pixel barrel layers", 10, 0., 10.));
00368     add(h, new TH1F( ("nPxLayers_"+types[t]).c_str(),"number of pixel layers (barrel+endcap)", 10, 0., 10.));
00369     add(h, new TH1F( ("nSiLayers_"+types[t]).c_str(),"number of Tracker layers ", 20, 0., 20.));
00370     add(h, new TH1F( ("trackAlgo_"+types[t]).c_str(),"track algorithm ", 30, 0., 30.));
00371     add(h, new TH1F( ("trackQuality_"+types[t]).c_str(),"track quality ", 7, -1., 6.));
00372   }
00373   add(h, new TH1F("trackWt","track weight in vertex",100,0., 1.));
00374    
00375 
00376   h["nrectrk"]->StatOverflows(kTRUE);
00377   h["nrectrk"]->StatOverflows(kTRUE);
00378   h["nrectrk0vtx"]->StatOverflows(kTRUE);
00379   h["nseltrk0vtx"]->StatOverflows(kTRUE);
00380   h["nrecsimtrk"]->StatOverflows(kTRUE);
00381   h["nrecnosimtrk"]->StatOverflows(kTRUE);
00382   h["nseltrk"]->StatOverflows(kTRUE);
00383   h["nbtksinvtx"]->StatOverflows(kTRUE);
00384   h["nbtksinvtxPU"]->StatOverflows(kTRUE);
00385   h["nbtksinvtxTag"]->StatOverflows(kTRUE);
00386   h["nbtksinvtx2"]->StatOverflows(kTRUE);
00387   h["nbtksinvtxPU2"]->StatOverflows(kTRUE);
00388   h["nbtksinvtxTag2"]->StatOverflows(kTRUE);
00389 
00390   // pile-up and track assignment related histograms (MC)
00391   h["npu0"]       =new TH1F("npu0","Number of simulated vertices",40,0.,40.);
00392   h["npu1"]       =new TH1F("npu1","Number of simulated vertices with >0 track",40,0.,40.);
00393   h["npu2"]       =new TH1F("npu2","Number of simulated vertices with >1 track",40,0.,40.);
00394   h["nrecv"]      =new TH1F("nrecv","# of reconstructed vertices", 40, 0, 40);
00395   add(h,new TH2F("nrecvsnpu","#rec vertices vs number of sim vertices with >0 tracks", 40,  0., 40, 40,  0, 40));
00396   add(h,new TH2F("nrecvsnpu2","#rec vertices vs number of sim vertices with >1 tracks", 40,  0., 40, 40,  0, 40));
00397   add(h,new TH1F("sumpt","sumpt of simulated tracks",100,0.,100.));
00398   add(h,new TH1F("sumptSignal","sumpt of simulated tracks in Signal events",100,0.,200.));
00399   add(h,new TH1F("sumptPU","sumpt of simulated tracks in PU events",100,0.,200.));
00400   add(h,new TH1F("sumpt2rec","sumpt2 of reconstructed and matched tracks",100,0.,100.));
00401   add(h,new TH1F("sumpt2","sumpt2 of simulated tracks",100,0.,100.));
00402   add(h,new TH1F("sumpt2Signal","sumpt2 of simulated tracks in Signal events",100,0.,200.));
00403   add(h,new TH1F("sumpt2PU","sumpt2 of simulated tracks in PU events",100,0.,200.));
00404   add(h,new TH1F("sumpt2rec","sumpt2 of reconstructed and matched tracks",100,0.,100.));
00405   add(h,new TH1F("sumpt2recSignal","sumpt2 of reconstructed and matched tracks in Signal events",100,0.,200.));
00406   add(h,new TH1F("sumpt2recPU","sumpt2 of reconstructed and matched tracks in PU events",100,0.,200.));
00407   add(h,new TH1F("nRecTrkInSimVtx","number of reco tracks matched to sim-vertex", 101, 0., 100));
00408   add(h,new TH1F("nRecTrkInSimVtxSignal","number of reco tracks matched to signal sim-vertex", 101, 0., 100));
00409   add(h,new TH1F("nRecTrkInSimVtxPU","number of reco tracks matched to PU-vertex", 101, 0., 100));
00410   add(h,new TH1F("nPrimRecTrkInSimVtx","number of reco primary tracks matched to sim-vertex", 101, 0., 100));
00411   add(h,new TH1F("nPrimRecTrkInSimVtxSignal","number of reco primary tracks matched to signal sim-vertex", 101, 0., 100));
00412   add(h,new TH1F("nPrimRecTrkInSimVtxPU","number of reco primary tracks matched to PU-vertex", 101, 0., 100));
00413   // obsolete, scheduled for removal
00414   add(h,new TH1F("recPurity","track purity of reconstructed vertices", 101, 0., 1.01));
00415   add(h,new TH1F("recPuritySignal","track purity of reconstructed Signal vertices", 101, 0., 1.01));
00416   add(h,new TH1F("recPurityPU","track purity of reconstructed PU vertices", 101, 0., 1.01));
00417   // end of obsolete
00418   add(h,new TH1F("recmatchPurity","track purity of all vertices", 101, 0., 1.01));
00419   add(h,new TH1F("recmatchPurityTag","track purity of tagged vertices", 101, 0., 1.01));
00420   add(h,new TH1F("recmatchPurityTagSignal","track purity of tagged Signal vertices", 101, 0., 1.01));
00421   add(h,new TH1F("recmatchPurityTagPU","track purity of tagged PU vertices", 101, 0., 1.01));
00422   add(h,new TH1F("recmatchPuritynoTag","track purity of untagged vertices", 101, 0., 1.01));
00423   add(h,new TH1F("recmatchPuritynoTagSignal","track purity of untagged Signal vertices", 101, 0., 1.01));
00424   add(h,new TH1F("recmatchPuritynoTagPU","track purity of untagged PU vertices", 101, 0., 1.01));
00425   add(h,new TH1F("recmatchvtxs","number of sim vertices contributing to recvtx", 10, 0., 10.));
00426   add(h,new TH1F("recmatchvtxsTag","number of sim vertices contributing to recvtx", 10, 0., 10.));
00427   add(h,new TH1F("recmatchvtxsnoTag","number of sim vertices contributing to recvtx", 10, 0., 10.));
00428   //
00429   add(h,new TH1F("trkAssignmentEfficiency", "track to vertex assignment efficiency", 101, 0., 1.01) );
00430   add(h,new TH1F("trkAssignmentEfficiencySignal", "track to signal vertex assignment efficiency", 101, 0., 1.01) );
00431   add(h,new TH1F("trkAssignmentEfficiencyPU", "track to PU vertex assignment efficiency", 101, 0., 1.01) );
00432   add(h,new TH1F("primtrkAssignmentEfficiency", "track to vertex assignment efficiency", 101, 0., 1.01) );
00433   add(h,new TH1F("primtrkAssignmentEfficiencySignal", "track to signal vertex assignment efficiency", 101, 0., 1.01) );
00434   add(h,new TH1F("primtrkAssignmentEfficiencyPU", "track to PU vertex assignment efficiency", 101, 0., 1.01) );
00435   add(h,new TH1F("vtxMultiplicity", "number of rec vertices containg tracks from one true vertex", 10, 0., 10.) );
00436   add(h,new TH1F("vtxMultiplicitySignal", "number of rec vertices containg tracks from the Signal Vertex", 10, 0., 10.) );
00437   add(h,new TH1F("vtxMultiplicityPU", "number of rec vertices containg tracks from a PU Vertex", 10, 0., 10.) );
00438   
00439   add(h,new TProfile("vtxFindingEfficiencyVsNtrk","finding efficiency vs number of associated rec tracks",100, 0., 100., 0., 1.) );
00440   add(h,new TProfile("vtxFindingEfficiencyVsNtrkSignal","Signal vertex finding efficiency vs number of associated rec tracks",100, 0., 100., 0., 1.) );
00441   add(h,new TProfile("vtxFindingEfficiencyVsNtrkPU","PU vertex finding efficiency vs number of associated rec tracks",100, 0., 100., 0., 1.) );
00442 
00443   add(h,new TH1F("TagVtxTrkPurity","TagVtxTrkPurity",100,0.,1.01));
00444   add(h,new TH1F("TagVtxTrkEfficiency","TagVtxTrkEfficiency",100,0.,1.01));
00445   
00446   add(h,new TH1F("matchVtxFraction","fraction of sim vertex tracks found in a recvertex",101,0,1.01));
00447   add(h,new TH1F("matchVtxFractionSignal","fraction of sim vertex tracks found in a recvertex",101,0,1.01));
00448   add(h,new TH1F("matchVtxFractionPU","fraction of sim vertex tracks found in a recvertex",101,0,1.01));
00449   add(h,new TH1F("matchVtxFractionCum","fraction of sim vertex tracks found in a recvertex",101,0,1.01));
00450   add(h,new TH1F("matchVtxFractionCumSignal","fraction of sim vertexs track found in a recvertex",101,0,1.01));
00451   add(h,new TH1F("matchVtxFractionCumPU","fraction of sim vertex tracks found in a recvertex",101,0,1.01));
00452   add(h,new TH1F("matchVtxEfficiency","efficiency for finding matching rec vertex",2,-0.5,1.5));
00453   add(h,new TH1F("matchVtxEfficiencySignal","efficiency for finding matching rec vertex",2,-0.5,1.5));
00454   add(h,new TH1F("matchVtxEfficiencyPU","efficiency for finding matching rec vertex",2,-0.5,1.5));
00455   add(h,new TH1F("matchVtxEfficiency2","efficiency for finding matching rec vertex (nt>1)",2,-0.5,1.5));
00456   add(h,new TH1F("matchVtxEfficiency2Signal","efficiency for finding matching rec vertex (nt>1)",2,-0.5,1.5));
00457   add(h,new TH1F("matchVtxEfficiency2PU","efficiency for finding matching rec vertex (nt>1)",2,-0.5,1.5));
00458   add(h,new TH1F("matchVtxEfficiency5","efficiency for finding matching rec vertex (purity>0.5)",2,-0.5,1.5));
00459   add(h,new TH1F("matchVtxEfficiency5Signal","efficiency for finding matching rec vertex (purity>0.5)",2,-0.5,1.5));
00460   add(h,new TH1F("matchVtxEfficiency5PU","efficiency for finding matching rec vertex (purity>0.5)",2,-0.5,1.5));
00461 
00462 
00463   add(h,new TH1F("matchVtxEfficiencyZ","efficiency for finding matching rec vertex within 1 mm",2,-0.5,1.5));
00464   add(h,new TH1F("matchVtxEfficiencyZSignal","efficiency for finding matching rec vertex within 1 mm",2,-0.5,1.5));
00465   add(h,new TH1F("matchVtxEfficiencyZPU","efficiency for finding matching rec vertex within 1 mm",2,-0.5,1.5));
00466 
00467   add(h,new TH1F("matchVtxEfficiencyZ1","efficiency for finding matching rec vertex within 1 mm (nt>0)",2,-0.5,1.5));
00468   add(h,new TH1F("matchVtxEfficiencyZ1Signal","efficiency for finding matching rec vertex within 1 mm (nt>0)",2,-0.5,1.5));
00469   add(h,new TH1F("matchVtxEfficiencyZ1PU","efficiency for finding matching rec vertex within 1 mm (nt>0)",2,-0.5,1.5));
00470 
00471   add(h,new TH1F("matchVtxEfficiencyZ2","efficiency for finding matching rec vertex within 1 mm (nt>1)",2,-0.5,1.5));
00472   add(h,new TH1F("matchVtxEfficiencyZ2Signal","efficiency for finding matching rec vertex within 1 mm (nt>1)",2,-0.5,1.5));
00473   add(h,new TH1F("matchVtxEfficiencyZ2PU","efficiency for finding matching rec vertex within 1 mm (nt>1)",2,-0.5,1.5));
00474 
00475   add(h,new TH1F("matchVtxZ","z distance to matched recvtx",100, -0.1, 0.1));
00476   add(h,new TH1F("matchVtxZPU","z distance to matched recvtx",100, -0.1, 0.1));
00477   add(h,new TH1F("matchVtxZSignal","z distance to matched recvtx",100, -0.1, 0.1));
00478 
00479   add(h,new TH1F("matchVtxZCum","z distance to matched recvtx",1001,0, 1.01));
00480   add(h,new TH1F("matchVtxZCumSignal","z distance to matched recvtx",1001,0, 1.01));
00481   add(h,new TH1F("matchVtxZCumPU","z distance to matched recvtx",1001,0, 1.01));
00482 
00483   add(h, new TH1F("unmatchedVtx","number of unmatched rec vertices (fakes)",10,0.,10.));
00484   add(h, new TH1F("unmatchedVtxFrac","fraction of unmatched rec vertices (fakes)",1000,0.,1.0));
00485   add(h, new TH1F("unmatchedVtxZ","z of unmached rec  vertices (fakes)",100,-20., 20.));
00486   add(h, new TH1F("unmatchedVtxNdof","ndof of unmatched rec vertices (fakes)", 500,0., 100.));
00487   add(h,new TH2F("correctlyassigned","pt and eta of correctly assigned tracks", 60,  -3., 3., 100, 0, 10.));
00488   add(h,new TH2F("misassigned","pt and eta of mis assigned tracks", 60,  -3., 3., 100, 0, 10.));
00489 
00490   add(h,new TH1F("ptcat","pt of correctly assigned tracks", 100, 0, 10.));
00491   add(h,new TH1F("etacat","eta of correctly assigned tracks", 60, -3., 3.));
00492   add(h,new TH1F("phicat","phi of correctly assigned tracks", 100, -3.14159, 3.14159));
00493   add(h,new TH1F("dzcat","dz of correctly assigned tracks", 100, 0., 1.));
00494 
00495   add(h,new TH1F("ptmis","pt of mis-assigned tracks", 100, 0, 10.));
00496   add(h,new TH1F("etamis","eta of mis-assigned tracks", 60, -3., 3.));
00497   add(h,new TH1F("phimis","phi of mis-assigned tracks",100, -3.14159, 3.14159));
00498   add(h,new TH1F("dzmis","dz of mis-assigned tracks", 100, 0., 1.));
00499 
00500 
00501   add(h,new TH1F("Tc","Tc computed with Truth matched Reco Tracks",100,0.,20.));
00502   add(h,new TH1F("TcSignal","Tc of signal vertices computed with Truth matched Reco Tracks",100,0.,20.));
00503   add(h,new TH1F("TcPU","Tc of PU vertices computed with Truth matched Reco Tracks",100,0.,20.));
00504 
00505   add(h,new TH1F("logTc","log Tc computed with Truth matched Reco Tracks",100,-2.,8.));
00506   add(h,new TH1F("logTcSignal","log Tc of signal vertices computed with Truth matched Reco Tracks",100,-2.,8.));
00507   add(h,new TH1F("logTcPU","log Tc of PU vertices computed with Truth matched Reco Tracks",100,-2.,8.));
00508 
00509   add(h,new TH1F("xTc","Tc of merged clusters",100,0.,20.));
00510   add(h,new TH1F("xTcSignal","Tc of signal vertices merged with PU",100,0.,20.));
00511   add(h,new TH1F("xTcPU","Tc of merged PU vertices",100,0.,20.));
00512 
00513   add(h,new TH1F("logxTc","log Tc merged vertices",100,-2.,8.));
00514   add(h,new TH1F("logxTcSignal","log Tc of signal vertices merged with PU",100,-2.,8.));
00515   add(h,new TH1F("logxTcPU","log Tc of merged PU vertices ",100,-2.,8.));
00516 
00517   add(h,new TH1F("logChisq","Chisq/ntrk computed with Truth matched Reco Tracks",100,-2.,8.));
00518   add(h,new TH1F("logChisqSignal","Chisq/ntrk of signal vertices computed with Truth matched Reco Tracks",100,-2.,8.));
00519   add(h,new TH1F("logChisqPU","Chisq/ntrk of PU vertices computed with Truth matched Reco Tracks",100,-2.,8.));
00520 
00521   add(h,new TH1F("logxChisq","Chisq/ntrk of merged clusters",100,-2.,8.));
00522   add(h,new TH1F("logxChisqSignal","Chisq/ntrk of signal vertices merged with PU",100,-2.,8.));
00523   add(h,new TH1F("logxChisqPU","Chisq/ntrk of merged PU vertices",100,-2.,8.));
00524 
00525   add(h,new TH1F("Chisq","Chisq/ntrk computed with Truth matched Reco Tracks",100,0.,20.));
00526   add(h,new TH1F("ChisqSignal","Chisq/ntrk of signal vertices computed with Truth matched Reco Tracks",100,0.,20.));
00527   add(h,new TH1F("ChisqPU","Chisq/ntrk of PU vertices computed with Truth matched Reco Tracks",100,0.,20.));
00528 
00529   add(h,new TH1F("xChisq","Chisq/ntrk of merged clusters",100,0.,20.));
00530   add(h,new TH1F("xChisqSignal","Chisq/ntrk of signal vertices merged with PU",100,0.,20.));
00531   add(h,new TH1F("xChisqPU","Chisq/ntrk of merged PU vertices",100,0.,20.));
00532 
00533   add(h,new TH1F("dzmax","dzmax computed with Truth matched Reco Tracks",100,0.,2.));
00534   add(h,new TH1F("dzmaxSignal","dzmax of signal vertices computed with Truth matched Reco Tracks",100,0.,2.));
00535   add(h,new TH1F("dzmaxPU","dzmax of PU vertices computed with Truth matched Reco Tracks",100,0.,2.));
00536 
00537   add(h,new TH1F("xdzmax","dzmax of merged clusters",100,0.,2.));
00538   add(h,new TH1F("xdzmaxSignal","dzmax of signal vertices merged with PU",100,0.,2.));
00539   add(h,new TH1F("xdzmaxPU","dzmax of merged PU vertices",100,0.,2.));
00540 
00541   add(h,new TH1F("dztrim","dzmax computed with Truth matched Reco Tracks",100,0.,2.));
00542   add(h,new TH1F("dztrimSignal","dzmax of signal vertices computed with Truth matched Reco Tracks",100,0.,2.));
00543   add(h,new TH1F("dztrimPU","dzmax of PU vertices computed with Truth matched Reco Tracks",100,0.,2.));
00544 
00545   add(h,new TH1F("xdztrim","dzmax of merged clusters",100,0.,2.));
00546   add(h,new TH1F("xdztrimSignal","dzmax of signal vertices merged with PU",100,0.,2.));
00547   add(h,new TH1F("xdztrimPU","dzmax of merged PU vertices",100,0.,2.));
00548 
00549   add(h,new TH1F("m4m2","m4m2 computed with Truth matched Reco Tracks",100,0.,100.));
00550   add(h,new TH1F("m4m2Signal","m4m2 of signal vertices computed with Truth matched Reco Tracks",100,0.,100.));
00551   add(h,new TH1F("m4m2PU","m4m2 of PU vertices computed with Truth matched Reco Tracks",100,0.,100.));
00552 
00553   add(h,new TH1F("xm4m2","m4m2 of merged clusters",100,0.,100.));
00554   add(h,new TH1F("xm4m2Signal","m4m2 of signal vertices merged with PU",100,0.,100.));
00555   add(h,new TH1F("xm4m2PU","m4m2 of merged PU vertices",100,0.,100.));
00556 
00557   return h;
00558 }
00559 
00560 
00561 //
00562 // member functions
00563 //
00564 void PrimaryVertexAnalyzer4PU::beginJob(){
00565   std::cout << " PrimaryVertexAnalyzer4PU::beginJob  conversion from sim units to rec units is " << simUnit_ << std::endl;
00566 
00567 
00568   rootFile_->cd();
00569 
00570   TDirectory *noBS = rootFile_->mkdir("noBS");
00571   noBS->cd();
00572   hnoBS=bookVertexHistograms();
00573   for(std::map<std::string,TH1*>::const_iterator hist=hnoBS.begin(); hist!=hnoBS.end(); hist++){
00574     hist->second->SetDirectory(noBS);
00575   }
00576 
00577   TDirectory *withBS = rootFile_->mkdir("BS");
00578   withBS->cd();
00579   hBS=bookVertexHistograms();
00580   for(std::map<std::string,TH1*>::const_iterator hist=hBS.begin(); hist!=hBS.end(); hist++){
00581     hist->second->SetDirectory(withBS);
00582   }
00583 
00584   TDirectory *DA = rootFile_->mkdir("DA");
00585   DA->cd();
00586   hDA=bookVertexHistograms();
00587   for(std::map<std::string,TH1*>::const_iterator hist=hDA.begin(); hist!=hDA.end(); hist++){
00588     hist->second->SetDirectory(DA);
00589   }
00590 
00591 //   TDirectory *PIX = rootFile_->mkdir("PIX");
00592 //   PIX->cd();
00593 //   hPIX=bookVertexHistograms();
00594 //   for(std::map<std::string,TH1*>::const_iterator hist=hPIX.begin(); hist!=hPIX.end(); hist++){
00595 //     hist->second->SetDirectory(PIX);
00596 //   }
00597 
00598 //   TDirectory *MVF = rootFile_->mkdir("MVF");
00599 //   MVF->cd();
00600 //   hMVF=bookVertexHistograms();
00601 //   for(std::map<std::string,TH1*>::const_iterator hist=hMVF.begin(); hist!=hMVF.end(); hist++){
00602 //     hist->second->SetDirectory(MVF);
00603 //   }
00604 
00605   rootFile_->cd();
00606   hsimPV["rapidity"] = new TH1F("rapidity","rapidity ",100,-10., 10.);
00607   hsimPV["chRapidity"] = new TH1F("chRapidity","charged rapidity ",100,-10., 10.);
00608   hsimPV["recRapidity"] = new TH1F("recRapidity","reconstructed rapidity ",100,-10., 10.);
00609   hsimPV["pt"] = new TH1F("pt","pt ",100,0., 20.);
00610 
00611   hsimPV["xsim"]         = new TH1F("xsim","simulated x",100,-0.01,0.01); // 0.01cm = 100 um
00612   hsimPV["ysim"]         = new TH1F("ysim","simulated y",100,-0.01,0.01);
00613   hsimPV["zsim"]         = new TH1F("zsim","simulated z",100,-20.,20.);
00614 
00615   hsimPV["xsim1"]        = new TH1F("xsim1","simulated x",100,-4.,4.);
00616   hsimPV["ysim1"]        = new TH1F("ysim1","simulated y",100,-4.,4.);
00617   hsimPV["zsim1"]        = new TH1F("zsim1","simulated z",100,-40.,40.);
00618 
00619   add(hsimPV, new TH1F("xsim2PU","simulated x (Pile-up)",100,-1.,1.));
00620   add(hsimPV, new TH1F("ysim2PU","simulated y (Pile-up)",100,-1.,1.)); 
00621   add(hsimPV, new TH1F("zsim2PU","simulated z (Pile-up)",100,-20.,20.)); 
00622   add(hsimPV, new TH1F("xsim2Signal","simulated x (Signal)",100,-1.,1.));
00623   add(hsimPV, new TH1F("ysim2Signal","simulated y (Signal)",100,-1.,1.));
00624   add(hsimPV, new TH1F("zsim2Signal","simulated z (Signal)",100,-20.,20.));
00625 
00626   hsimPV["xsim2"]        = new TH1F("xsim2","simulated x",100,-1,1); // 0.01cm = 100 um
00627   hsimPV["ysim2"]        = new TH1F("ysim2","simulated y",100,-1,1);
00628   hsimPV["zsim2"]        = new TH1F("zsim2","simulated z",100,-20.,20.);
00629   hsimPV["xsim3"]        = new TH1F("xsim3","simulated x",100,-0.1,0.1); // 0.01cm = 100 um
00630   hsimPV["ysim3"]        = new TH1F("ysim3","simulated y",100,-0.1,0.1);
00631   hsimPV["zsim3"]        = new TH1F("zsim3","simulated z",100,-20.,20.);
00632   hsimPV["xsimb"]        = new TH1F("xsimb","simulated x",100,-0.01,0.01); // 0.01cm = 100 um
00633   hsimPV["ysimb"]        = new TH1F("ysimb","simulated y",100,-0.01,0.01);
00634   hsimPV["zsimb"]        = new TH1F("zsimb","simulated z",100,-20.,20.);
00635   hsimPV["xsimb1"]        = new TH1F("xsimb1","simulated x",100,-0.1,0.1); // 0.01cm = 100 um
00636   hsimPV["ysimb1"]        = new TH1F("ysimb1","simulated y",100,-0.1,0.1);
00637   hsimPV["zsimb1"]        = new TH1F("zsimb1","simulated z",100,-20.,20.);
00638   add(hsimPV, new TH1F("xbeam","beamspot x",100,-1.,1.));
00639   add(hsimPV, new TH1F("ybeam","beamspot y",100,-1.,1.));
00640   add(hsimPV, new TH1F("zbeam","beamspot z",100,-1.,1.));
00641   add(hsimPV, new TH1F("wxbeam","beamspot sigma x",100,-1.,1.));
00642   add(hsimPV, new TH1F("wybeam","beamspot sigma y",100,-1.,1.));
00643   add(hsimPV, new TH1F("wzbeam","beamspot sigma z",100,-1.,1.));
00644   hsimPV["xsim2"]->StatOverflows(kTRUE);
00645   hsimPV["ysim2"]->StatOverflows(kTRUE);
00646   hsimPV["zsim2"]->StatOverflows(kTRUE);
00647   hsimPV["xsimb"]->StatOverflows(kTRUE);
00648   hsimPV["ysimb"]->StatOverflows(kTRUE);
00649   hsimPV["zsimb"]->StatOverflows(kTRUE);
00650   hsimPV["nsimvtx"]      = new TH1F("nsimvtx","# of simulated vertices", 50, -0.5, 49.5);
00651   //  hsimPV["nsimtrk"]      = new TH1F("nsimtrk","# of simulated tracks", 100, -0.5, 99.5); //  not filled right now, also exists in hBS..
00652   //  hsimPV["nsimtrk"]->StatOverflows(kTRUE);
00653   hsimPV["nbsimtksinvtx"]= new TH1F("nbsimtksinvtx","simulated tracks in vertex",100,-0.5,99.5); 
00654   hsimPV["nbsimtksinvtx"]->StatOverflows(kTRUE);
00655 
00656 }
00657 
00658 
00659 void PrimaryVertexAnalyzer4PU::endJob() {
00660   std::cout << "this is void PrimaryVertexAnalyzer4PU::endJob() " << std::endl;
00661   //cumulate some histos
00662   double sumDA=0,sumBS=0,sumnoBS=0, sumPIX=0,sumMVF=0;
00663   for(int i=101; i>0; i--){
00664     sumDA+=hDA["matchVtxFractionSignal"]->GetBinContent(i)/hDA["matchVtxFractionSignal"]->Integral();
00665     hDA["matchVtxFractionCumSignal"]->SetBinContent(i,sumDA);
00666     sumBS+=hBS["matchVtxFractionSignal"]->GetBinContent(i)/hBS["matchVtxFractionSignal"]->Integral();
00667     hBS["matchVtxFractionCumSignal"]->SetBinContent(i,sumBS);
00668     sumnoBS+=hnoBS["matchVtxFractionSignal"]->GetBinContent(i)/hnoBS["matchVtxFractionSignal"]->Integral();
00669     hnoBS["matchVtxFractionCumSignal"]->SetBinContent(i,sumnoBS);
00670 //     sumPIX+=hPIX["matchVtxFractionSignal"]->GetBinContent(i)/hPIX["matchVtxFractionSignal"]->Integral();
00671 //     hPIX["matchVtxFractionCumSignal"]->SetBinContent(i,sumPIX);
00672 //     sumMVF+=hMVF["matchVtxFractionSignal"]->GetBinContent(i)/hMVF["matchVtxFractionSignal"]->Integral();
00673 //     hMVF["matchVtxFractionCumSignal"]->SetBinContent(i,sumMVF);
00674   }
00675   sumDA=0,sumBS=0,sumnoBS=0,sumPIX=0,sumMVF=0;
00676   for(int i=1; i<1001; i++){
00677     sumDA+=hDA["abszdistancetag"]->GetBinContent(i);
00678     hDA["abszdistancetagcum"]->SetBinContent(i,sumDA/float(hDA["abszdistancetag"]->GetEntries()));
00679     sumBS+=hBS["abszdistancetag"]->GetBinContent(i);
00680     hBS["abszdistancetagcum"]->SetBinContent(i,sumBS/float(hBS["abszdistancetag"]->GetEntries()));
00681     sumnoBS+=hnoBS["abszdistancetag"]->GetBinContent(i);
00682     hnoBS["abszdistancetagcum"]->SetBinContent(i,sumnoBS/float(hnoBS["abszdistancetag"]->GetEntries()));
00683 //     sumPIX+=hPIX["abszdistancetag"]->GetBinContent(i);
00684 //     hPIX["abszdistancetagcum"]->SetBinContent(i,sumPIX/float(hPIX["abszdistancetag"]->GetEntries()));
00685 //     sumMVF+=hMVF["abszdistancetag"]->GetBinContent(i);
00686 //     hMVF["abszdistancetagcum"]->SetBinContent(i,sumMVF/float(hMVF["abszdistancetag"]->GetEntries()));
00687   }
00688 
00689   Cumulate(hBS["matchVtxZCum"]);   Cumulate(hBS["matchVtxZCumSignal"]);   Cumulate(hBS["matchVtxZCumPU"]); 
00690   Cumulate(hnoBS["matchVtxZCum"]);   Cumulate(hnoBS["matchVtxZCumSignal"]);   Cumulate(hnoBS["matchVtxZCumPU"]); 
00691   Cumulate(hDA["matchVtxZCum"]);   Cumulate(hDA["matchVtxZCumSignal"]);   Cumulate(hDA["matchVtxZCumPU"]); 
00692   /*
00693    h->ComputeIntegral();
00694    Double_t *integral = h->GetIntegral();
00695    h->SetContent(integral);
00696   */
00697 
00698   // make a reference for ndofnr2
00699   //hDA["vtxndof"]->ComputeIntegral();
00700   //Double_t *integral = hDA["vtxndf"]->GetIntegral();
00701   //   h->SetContent(integral);
00702   double p;
00703   for(int i=1; i<501; i++){
00704     if(hDA["vtxndf"]->GetEntries()>0){
00705       p=  hDA["vtxndf"]->Integral(i,501)/hDA["vtxndf"]->GetEntries();    hDA["vtxndfc"]->SetBinContent(i,p*hDA["vtxndf"]->GetBinContent(i));
00706     }
00707     if(hBS["vtxndf"]->GetEntries()>0){
00708       p=  hBS["vtxndf"]->Integral(i,501)/hBS["vtxndf"]->GetEntries();    hBS["vtxndfc"]->SetBinContent(i,p*hBS["vtxndf"]->GetBinContent(i));
00709     }
00710     if(hnoBS["vtxndf"]->GetEntries()>0){
00711       p=hnoBS["vtxndf"]->Integral(i,501)/hnoBS["vtxndf"]->GetEntries();  hnoBS["vtxndfc"]->SetBinContent(i,p*hnoBS["vtxndf"]->GetBinContent(i));
00712     }
00713 //     if(hPIX["vtxndf"]->GetEntries()>0){
00714 //       p=hPIX["vtxndf"]->Integral(i,501)/hPIX["vtxndf"]->GetEntries();  hPIX["vtxndfc"]->SetBinContent(i,p*hPIX["vtxndf"]->GetBinContent(i));
00715 //     }
00716   }
00717   
00718   rootFile_->cd();
00719   for(std::map<std::string,TH1*>::const_iterator hist=hsimPV.begin(); hist!=hsimPV.end(); hist++){
00720     std::cout << "writing " << hist->first << std::endl;
00721     hist->second->Write();
00722   }
00723   rootFile_->Write();
00724   std::cout << "PrimaryVertexAnalyzer4PU::endJob: done" << std::endl;
00725 }
00726 
00727 
00728 
00729 
00730 // helper functions
00731 std::vector<PrimaryVertexAnalyzer4PU::SimPart> PrimaryVertexAnalyzer4PU::getSimTrkParameters(
00732                                                                                              edm::Handle<edm::SimTrackContainer> & simTrks,
00733                                                                                              edm::Handle<edm::SimVertexContainer> & simVtcs,
00734                                                                                              double simUnit)
00735 {
00736    std::vector<SimPart > tsim;
00737    if(simVtcs->begin()==simVtcs->end()){
00738      if(verbose_){
00739        cout << "  PrimaryVertexAnalyzer4PU::getSimTrkParameters  no simvtcs" << endl;
00740      }
00741      return tsim;
00742    }
00743    if(verbose_){
00744      cout << "  PrimaryVertexAnalyzer4PU::getSimTrkParameters simVtcs n=" << simVtcs->size() << endl;
00745      cout << "  PrimaryVertexAnalyzer4PU::getSimTrkParameters 1st position" << setw(8) << setprecision(4) << simVtcs->begin()->position() << endl;
00746    }
00747    double t0=simVtcs->begin()->position().e();
00748 
00749    for(edm::SimTrackContainer::const_iterator t=simTrks->begin();
00750        t!=simTrks->end(); ++t){
00751      if (t->noVertex()){
00752        std::cout << "simtrk  has no vertex" << std::endl;
00753      }else{
00754        // get the vertex position
00755        //HepLorentzVector v=(*simVtcs)[t->vertIndex()].position();
00756        math::XYZTLorentzVectorD v((*simVtcs)[t->vertIndex()].position().x(),
00757                           (*simVtcs)[t->vertIndex()].position().y(),
00758                           (*simVtcs)[t->vertIndex()].position().z(),
00759                           (*simVtcs)[t->vertIndex()].position().e());
00760        int pdgCode=t->type();
00761 
00762        if( pdgCode==-99 ){
00763          // such entries cause crashes, no idea what they are
00764          std::cout << "funny particle skipped  , code="  << pdgCode << std::endl;
00765        }else{
00766          double Q=0; //double Q=HepPDT::theTable().getParticleData(pdgCode)->charge();
00767          if ((pdgCode==11)||(pdgCode==13)||(pdgCode==15)||(pdgCode==-211)||(pdgCode==-2212)||(pdgCode==-321)||(pdgCode==-3222)){Q=-1;}
00768          else if((pdgCode==-11)||(pdgCode==-13)||(pdgCode==-15)||(pdgCode==211)||(pdgCode==2212)||(pdgCode==321)||(pdgCode==3222)){Q=1;}
00769          else {
00770            //std::cout << pdgCode << " " <<std::endl;
00771          }
00772          math::XYZTLorentzVectorD p(t->momentum().x(),t->momentum().y(),t->momentum().z(),t->momentum().e());
00773          if ( (Q != 0) && (p.pt()>0.1)  && (fabs(t->momentum().eta())<3.0)
00774               && fabs(v.z()*simUnit<20) && (sqrt(v.x()*v.x()+v.y()*v.y())<10.)){
00775            double x0=v.x()*simUnit;
00776            double y0=v.y()*simUnit;
00777            double z0=v.z()*simUnit;
00778            double kappa=-Q*0.002998*fBfield_/p.pt();
00779            double D0=x0*sin(p.phi())-y0*cos(p.phi())-0.5*kappa*(x0*x0+y0*y0);
00780            double q=sqrt(1.-2.*kappa*D0);
00781            double s0=(x0*cos(p.phi())+y0*sin(p.phi()))/q;
00782            double s1;
00783            if (fabs(kappa*s0)>0.001){
00784              s1=asin(kappa*s0)/kappa;
00785            }else{
00786              double ks02=(kappa*s0)*(kappa*s0);
00787              s1=s0*(1.+ks02/6.+3./40.*ks02*ks02+5./112.*pow(ks02,3));
00788            }
00789            SimPart sp;//ParameterVector par;
00790            sp.par[reco::TrackBase::i_qoverp] = Q/p.P();
00791            sp.par[reco::TrackBase::i_lambda] = M_PI/2.-p.theta();
00792            sp.par[reco::TrackBase::i_phi] = p.phi()-asin(kappa*s0);
00793            sp.par[reco::TrackBase::i_dxy] = -2.*D0/(1.+q);
00794            sp.par[reco::TrackBase::i_dsz] = z0*sin(p.theta())-s1*cos(p.theta());
00795 
00796            sp.pdg=pdgCode;
00797            if (v.t()-t0<1e-15){
00798              sp.type=0;  // primary
00799            }else{
00800              sp.type=1;  //secondary
00801            }
00802 
00803            // now get zpca  (get perigee wrt beam)
00804            double x1=x0-0.033; double y1=y0-0.; // FIXME how do we get the simulated beam position?
00805            D0=x1*sin(p.phi())-y1*cos(p.phi())-0.5*kappa*(x1*x1+y1*y1);
00806            q=sqrt(1.-2.*kappa*D0);
00807            s0=(x1*cos(p.phi())+y1*sin(p.phi()))/q;
00808            if (fabs(kappa*s0)>0.001){
00809              s1=asin(kappa*s0)/kappa;
00810            }else{
00811              double ks02=(kappa*s0)*(kappa*s0);
00812              s1=s0*(1.+ks02/6.+3./40.*ks02*ks02+5./112.*pow(ks02,3));
00813            }
00814            sp.ddcap=-2.*D0/(1.+q);
00815            sp.zdcap=z0-s1/tan(p.theta());
00816            sp.zvtx=z0;
00817            sp.xvtx=x0;
00818            sp.yvtx=y0;
00819 
00820            tsim.push_back(sp);
00821          }
00822        }
00823      }// has vertex
00824    }//for loop
00825    return tsim;
00826 }
00827 
00828 
00829 int*  PrimaryVertexAnalyzer4PU::supf(std::vector<SimPart>& simtrks, const reco::TrackCollection & trks){
00830   int nsim=simtrks.size();
00831   int nrec=trks.size();
00832   int *rectosim=new int[nrec]; // pointer to associated simtrk
00833   double** pij=new double*[nrec];
00834   double mu=100.; // initial chi^2 cut-off  (5 dofs !)
00835   int nmatch=0;
00836   int i=0;
00837   for(reco::TrackCollection::const_iterator t=trks.begin(); t!=trks.end(); ++t){
00838     pij[i]=new double[nsim];
00839     rectosim[i]=-1;
00840     ParameterVector  par = t->parameters();
00841     //reco::TrackBase::CovarianceMatrix V = t->covariance();
00842     reco::TrackBase::CovarianceMatrix S = t->covariance();
00843     S.Invert();
00844     for(int j=0; j<nsim; j++){
00845       simtrks[j].rec=-1;
00846       SimPart s=simtrks[j];
00847       double c=0;
00848       for(int k=0; k<5; k++){
00849         for(int l=0; l<5; l++){
00850           c+=(par(k)-s.par[k])*(par(l)-s.par[l])*S(k,l);
00851         }
00852       }
00853       pij[i][j]=exp(-0.5*c);
00854 
00855 //       double c0=pow((par[0]-s.par[0])/t->qoverpError(),2)*0.1
00856 //      +pow((par[1]-s.par[1])/t->lambdaError(),2)
00857 //      +pow((par[2]-s.par[2])/t->phiError(),2)
00858 //      +pow((par[3]-s.par[3])/t->dxyError(),2)*0.1;
00859 //         +pow((par[4]-s.par[4])/t->dszError(),2)*0.1;
00860 //       pij[i][j]=exp(-0.5*c0);
00861 
00862 //       if( c0 <100 ){
00863 //       cout << setw(3) << i << " rec " << setw(6) << par << endl;
00864 //       cout << setw(3) << j << " sim " << setw(6) << s.par << " ---> C=" << c << endl;
00865 //       cout <<  "       "  << setw(6)
00866 //         << (par[0]-s.par[0])<< ","
00867 //         << (par[1]-s.par[1])<< ","
00868 //         << (par[2]-s.par[2])<< ","
00869 //         << (par[3]-s.par[3])<< ","
00870 //         << (par[4]-s.par[4])
00871 //         << " match=" << match(simtrks[j].par, trks.at(i).parameters())
00872 //         << endl;
00873 //       cout <<  "       "  << setw(6)
00874 //         << (par[0]-s.par[0])/t->qoverpError() << ","
00875 //         << (par[1]-s.par[1])/t->lambdaError() << ","
00876 //         << (par[2]-s.par[2])/t->phiError() << ","
00877 //         << (par[3]-s.par[3])/t->dxyError() << ","
00878 //         << (par[4]-s.par[4])/t->dszError() << " c0=" << c0
00879 //         << endl <<endl;
00880 //       }
00881 
00882     }
00883     i++;
00884   }
00885 
00886   for(int k=0; k<nrec; k++){
00887     int imatch=-1; int jmatch=-1;
00888     double pmatch=0;
00889     for(int j=0; j<nsim; j++){
00890       if ((simtrks[j].rec)<0){
00891         double psum=exp(-0.5*mu); //cutoff
00892         for(int i=0; i<nrec; i++){
00893           if (rectosim[i]<0){ psum+=pij[i][j];}
00894         }
00895         for(int i=0; i<nrec; i++){
00896           if ((rectosim[i]<0)&&(pij[i][j]/psum>pmatch)){
00897             pmatch=pij[i][j]/psum;
00898             imatch=i; jmatch=j;
00899           }
00900         }
00901       }
00902     }// sim loop
00903    if((jmatch>=0)||(imatch>=0)){
00904      //std::cout << pmatch << "    " << pij[imatch][jmatch] << "  match=" <<
00905      // match(simtrks[jmatch].par, trks.at(imatch).parameters()) <<std::endl;
00906       if (pmatch>0.01){
00907         rectosim[imatch]=jmatch;
00908         simtrks[jmatch].rec=imatch;
00909         nmatch++;
00910       }else if (match(simtrks[jmatch].par, trks.at(imatch).parameters())){
00911         // accept it anyway if it matches crudely and relax the cut-off
00912         rectosim[imatch]=jmatch;
00913         simtrks[jmatch].rec=imatch;
00914         nmatch++;
00915         mu=mu*2;
00916       }
00917     }
00918   }
00919 
00920 //   std::cout << ">>>>>>>>>>>>>>>--------------supf----------------------" << std::endl;
00921 //   std::cout <<"nsim=" << nsim   << "   nrec=" << nrec << "    nmatch=" << nmatch << std::endl;
00922 //   std::cout << "rec to sim " << std::endl;
00923 //   for(int i=0; i<nrec; i++){
00924 //     std::cout << i << " ---> " << rectosim[i] << std::endl;
00925 //   }
00926 //   std::cout << "sim to rec " << std::endl;
00927 //   for(int j=0; j<nsim; j++){
00928 //     std::cout << j << " ---> " << simtrks[j].rec << std::endl;
00929 //   }
00930 
00931    std::cout << "unmatched sim " << std::endl;
00932    for(int j=0; j<nsim; j++){
00933      if(simtrks[j].rec<0){
00934        double pt= 1./simtrks[j].par[0]/tan(simtrks[j].par[1]);
00935        if((fabs(pt))>1.){
00936          std::cout << setw(3) << j << setw(8) << simtrks[j].pdg 
00937                    << setw(8) << setprecision(4) << "  (" << simtrks[j].xvtx << "," << simtrks[j].yvtx <<  "," << simtrks[j].zvtx << ")" 
00938                    << " pt= " <<  pt
00939                    << " phi=" << simtrks[j].par[2] 
00940                    << " eta= " <<  -log(tan(0.5*(M_PI/2-simtrks[j].par[1]))) 
00941                    << std::endl; 
00942        }
00943      }
00944    }
00945 //   std::cout << "<<<<<<<<<<<<<<<--------------supf----------------------" << std::endl;
00946 
00947   //delete rectosim; // or return it?
00948   for(int i=0; i<nrec; i++){delete pij[i];}
00949   delete pij;
00950   return rectosim;  // caller must delete it
00951 }
00952 
00953 
00954 
00955 
00956 
00957 
00958 
00959 
00960 bool PrimaryVertexAnalyzer4PU::match(const ParameterVector  &a,
00961                                      const ParameterVector  &b){
00962   double dqoverp =a(0)-b(0);
00963   double dlambda =a(1)-b(1);
00964   double dphi    =a(2)-b(2);
00965   double dsz     =a(4)-b(4);
00966   if (dphi>M_PI){ dphi-=M_2_PI; }else if(dphi<-M_PI){dphi+=M_2_PI;}
00967   //  return ( (fabs(dqoverp)<0.2) && (fabs(dlambda)<0.02) && (fabs(dphi)<0.04) && (fabs(dsz)<0.1) );
00968   return ( (fabs(dqoverp)<0.2) && (fabs(dlambda)<0.02) && (fabs(dphi)<0.04) && (fabs(dsz)<1.0) );
00969 }
00970 
00971 
00972 bool PrimaryVertexAnalyzer4PU::matchVertex(const simPrimaryVertex  &vsim, 
00973                                        const reco::Vertex       &vrec){
00974   return (fabs(vsim.z*simUnit_-vrec.z())<zmatch_);
00975 }
00976 
00977 bool PrimaryVertexAnalyzer4PU::isResonance(const HepMC::GenParticle * p){
00978   double ctau=(pdt_->particle( abs(p->pdg_id()) ))->lifetime();
00979   //std::cout << "isResonance   " << p->pdg_id() << " " << ctau << std::endl;
00980   return  ctau >0 && ctau <1e-6;
00981 }
00982 
00983 bool PrimaryVertexAnalyzer4PU::isFinalstateParticle(const HepMC::GenParticle * p){
00984   return ( !p->end_vertex() && p->status()==1 );
00985 }
00986 
00987 
00988 bool PrimaryVertexAnalyzer4PU::isCharged(const HepMC::GenParticle * p){
00989   const ParticleData * part = pdt_->particle( p->pdg_id() );
00990   if (part){
00991     return part->charge()!=0;
00992   }else{
00993     // the new/improved particle table doesn't know anti-particles
00994     return  pdt_->particle( -p->pdg_id() )!=0;
00995   }
00996 }
00997 
00998 
00999 
01000 
01001 void PrimaryVertexAnalyzer4PU::fillTrackHistos(std::map<std::string, TH1*> & h, const std::string & ttype, const reco::Track & t, const reco::Vertex * v){
01002     Fill(h,"rapidity_"+ttype,t.eta());
01003     Fill(h,"z0_"+ttype,t.vz());
01004     Fill(h,"phi_"+ttype,t.phi());
01005     Fill(h,"eta_"+ttype,t.eta());
01006     Fill(h,"pt_"+ttype,t.pt());
01007     Fill(h,"found_"+ttype,t.found());
01008     Fill(h,"lost_"+ttype,t.lost());
01009     Fill(h,"nchi2_"+ttype,t.normalizedChi2());
01010     Fill(h,"rstart_"+ttype,(t.innerPosition()).Rho());
01011 
01012     double d0Error=t.d0Error();
01013     double d0=t.dxy(vertexBeamSpot_.position());
01014     if (d0Error>0){ 
01015       Fill(h,"logtresxy_"+ttype,log(d0Error/0.0001)/log(10.));
01016       Fill(h,"tpullxy_"+ttype,d0/d0Error);
01017     }
01018     //double z0=t.vz();
01019     double dzError=t.dzError();
01020     if(dzError>0){
01021       Fill(h,"logtresz_"+ttype,log(dzError/0.0001)/log(10.));
01022     }
01023 
01024     //
01025     Fill(h,"sigmatrkz_"+ttype,sqrt(pow(t.dzError(),2)+wxy2_/pow(tan(t.theta()),2)));
01026     Fill(h,"sigmatrkz0_"+ttype,t.dzError());
01027 
01028     // track vs vertex 
01029     if((! (v==NULL)) && (v->ndof()>10.)) {
01030       // emulate clusterizer input
01031       //const TransientTrack & tt = theB_->build(&t);    wrong !!!!
01032       TransientTrack tt = theB_->build(&t);    tt.setBeamSpot(vertexBeamSpot_); // need the setBeamSpot !
01033       double z=(tt.stateAtBeamLine().trackStateAtPCA()).position().z();
01034       double tantheta=tan((tt.stateAtBeamLine().trackStateAtPCA()).momentum().theta());
01035       double dz2= pow(tt.track().dzError(),2)+wxy2_/pow(tantheta,2);
01036       
01037       Fill(h,"restrkz_"+ttype,z-v->position().z());
01038       Fill(h,"restrkzvsphi_"+ttype,t.phi(), z-v->position().z());
01039       Fill(h,"restrkzvseta_"+ttype,t.eta(), z-v->position().z());
01040       Fill(h,"pulltrkzvsphi_"+ttype,t.phi(), (z-v->position().z())/sqrt(dz2));
01041       Fill(h,"pulltrkzvseta_"+ttype,t.eta(), (z-v->position().z())/sqrt(dz2));
01042 
01043       Fill(h,"pulltrkz_"+ttype,(z-v->position().z())/sqrt(dz2));
01044 
01045       double x1=t.vx()-vertexBeamSpot_.x0(); double y1=t.vy()-vertexBeamSpot_.y0();
01046       double kappa=-0.002998*fBfield_*t.qoverp()/cos(t.theta());
01047       double D0=x1*sin(t.phi())-y1*cos(t.phi())-0.5*kappa*(x1*x1+y1*y1);
01048       double q=sqrt(1.-2.*kappa*D0);
01049       double s0=(x1*cos(t.phi())+y1*sin(t.phi()))/q; 
01050       double s1;
01051       if (fabs(kappa*s0)>0.001){
01052         s1=asin(kappa*s0)/kappa;
01053       }else{
01054         double ks02=(kappa*s0)*(kappa*s0);
01055         s1=s0*(1.+ks02/6.+3./40.*ks02*ks02+5./112.*pow(ks02,3));
01056       }
01057       //     sp.ddcap=-2.*D0/(1.+q);
01058       //double zdcap=t.vz()-s1/tan(t.theta());
01059 
01060     }
01061     //
01062     
01063     // collect some info on hits and clusters
01064     Fill(h,"nbarrelLayers_"+ttype,t.hitPattern().pixelBarrelLayersWithMeasurement());
01065     Fill(h,"nPxLayers_"+ttype,t.hitPattern().pixelLayersWithMeasurement());
01066     Fill(h,"nSiLayers_"+ttype,t.hitPattern().trackerLayersWithMeasurement());
01067     Fill(h,"expectedInner_"+ttype,t.trackerExpectedHitsInner().numberOfHits());
01068     Fill(h,"expectedOuter_"+ttype,t.trackerExpectedHitsOuter().numberOfHits());
01069     Fill(h,"trackAlgo_"+ttype,t.algo());
01070     Fill(h,"trackQuality_"+ttype,t.qualityMask());
01071 
01072     //
01073     int longesthit=0, nbarrel=0;
01074     for(trackingRecHit_iterator hit=t.recHitsBegin(); hit!=t.recHitsEnd(); hit++){
01075       if ((**hit).isValid()   && (**hit).geographicalId().det() == DetId::Tracker ){
01076        bool barrel = DetId((**hit).geographicalId()).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel);
01077        //bool endcap = DetId::DetId((**hit).geographicalId()).subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap);
01078        if (barrel){
01079          const SiPixelRecHit *pixhit = dynamic_cast<const SiPixelRecHit*>( &(**hit));
01080          edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster> const& clust = (*pixhit).cluster();
01081          if (clust.isNonnull()) {
01082            nbarrel++;
01083            if (clust->sizeY()>longesthit) longesthit=clust->sizeY();
01084            if (clust->sizeY()>20.){
01085              Fill(h,"lvseta_"+ttype,t.eta(), 19.9);
01086              Fill(h,"lvstanlambda_"+ttype,tan(t.lambda()), 19.9);
01087            }else{
01088              Fill(h,"lvseta_"+ttype,t.eta(), float(clust->sizeY()));
01089              Fill(h,"lvstanlambda_"+ttype,tan(t.lambda()), float(clust->sizeY()));
01090            }
01091          }
01092        }
01093       }
01094     }
01095     Fill(h,"nbarrelhits_"+ttype,float(nbarrel));
01096     //-------------------------------------------------------------------
01097 
01098 }
01099 
01100 void PrimaryVertexAnalyzer4PU::dumpHitInfo(const reco::Track & t){
01101     // collect some info on hits and clusters
01102   int longesthit=0, nbarrel=0;
01103   cout << Form("%5.2f  %5.2f  : ",t.pt(),t.eta());
01104   for(trackingRecHit_iterator hit=t.recHitsBegin(); hit!=t.recHitsEnd(); hit++){
01105     if ((**hit).isValid()   && (**hit).geographicalId().det() == DetId::Tracker ){
01106       bool barrel = DetId((**hit).geographicalId()).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel);
01107       //bool endcap = DetId::DetId((**hit).geographicalId()).subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap);
01108       if (barrel){
01109         nbarrel++;
01110         const SiPixelRecHit *pixhit = dynamic_cast<const SiPixelRecHit*>( &(**hit));
01111         edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster> const& clust = (*pixhit).cluster();
01112         if (clust.isNonnull()) {
01113           cout << Form("%4d",clust->sizeY());
01114           if (clust->sizeY()>longesthit) longesthit=clust->sizeY();
01115         }
01116       }
01117     }
01118   }
01119   //cout << endl;
01120 }
01121 
01122 void PrimaryVertexAnalyzer4PU::printRecVtxs(const Handle<reco::VertexCollection> recVtxs, std::string title){
01123     int ivtx=0;
01124     std::cout << std::endl << title << std::endl;
01125     for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
01126         v!=recVtxs->end(); ++v){
01127       string vtype=" recvtx  ";
01128       if( v->isFake()){
01129         vtype=" fake   ";
01130       }else if (v->ndof()==-5){
01131         vtype=" cluster "; // pos=selector[iclu],cputime[iclu],clusterz[iclu]
01132       }else if(v->ndof()==-3){
01133         vtype=" event   ";
01134       }
01135       std::cout << "vtx "<< std::setw(3) << std::setfill(' ')<<ivtx++
01136                 << vtype
01137                 << " #trk " << std::fixed << std::setprecision(4) << std::setw(3) << v->tracksSize() 
01138                 << " chi2 " << std::fixed << std::setw(4) << std::setprecision(1) << v->chi2() 
01139                 << " ndof " << std::fixed << std::setw(5) << std::setprecision(2) << v->ndof()
01140                 << " x "  << std::setw(8) <<std::fixed << std::setprecision(4) << v->x() 
01141                 << " dx " << std::setw(8) << v->xError()// <<  std::endl 
01142                 << " y "  << std::setw(8) << v->y() 
01143                 << " dy " << std::setw(8) << v->yError()//<< std::endl
01144                 << " z "  << std::setw(8) << v->z() 
01145                 << " dz " << std::setw(8) << v->zError()
01146                 << std::endl;
01147     }
01148 }
01149 
01150 
01151 void PrimaryVertexAnalyzer4PU::printSimVtxs(const Handle<SimVertexContainer> simVtxs){
01152     int i=0;
01153     for(SimVertexContainer::const_iterator vsim=simVtxs->begin();
01154         vsim!=simVtxs->end(); ++vsim){
01155       if ( vsim->position().x()*vsim->position().x()+vsim->position().y()*vsim->position().y() < 1.){
01156         std::cout << i++ << ")" << std::scientific
01157                   << " evtid=" << vsim->eventId().event()  << ","  << vsim->eventId().bunchCrossing()
01158                   << " sim x=" << vsim->position().x()*simUnit_
01159                   << " sim y=" << vsim->position().y()*simUnit_
01160                   << " sim z=" << vsim->position().z()*simUnit_
01161                   << " sim t=" << vsim->position().t()
01162                   << " parent=" << vsim->parentIndex() 
01163                   << std::endl;
01164       }
01165     }
01166 }
01167 
01168 
01169 
01170 
01171 
01172 
01173 
01174 void PrimaryVertexAnalyzer4PU::printSimTrks(const Handle<SimTrackContainer> simTrks){
01175   std::cout <<  " simTrks   type, (momentum), vertIndex, genpartIndex"  << std::endl;
01176   int i=1;
01177   for(SimTrackContainer::const_iterator t=simTrks->begin();
01178       t!=simTrks->end(); ++t){
01179     //HepMC::GenParticle* gp=evtMC->GetEvent()->particle( (*t).genpartIndex() );
01180     std::cout << i++ << ")" 
01181               << t->eventId().event()  << ","  << t->eventId().bunchCrossing()
01182               << (*t)
01183               << " index="
01184               << (*t).genpartIndex();
01185     //if (gp) {
01186     //  HepMC::GenVertex *gv=gp->production_vertex();
01187     //  std::cout  <<  " genvertex =" << (*gv);
01188     //}
01189     std::cout << std::endl;
01190   }
01191 }
01192 
01193 
01194 
01195 void PrimaryVertexAnalyzer4PU::printRecTrks(const Handle<reco::TrackCollection> &recTrks  ){
01196 
01197   cout << "printRecTrks" << endl;
01198   int i =1;
01199   for(reco::TrackCollection::const_iterator t=recTrks->begin(); t!=recTrks->end(); ++t){
01200     //    reco::TrackBase::ParameterVector  par = t->parameters();
01201     
01202     cout << endl;
01203     cout << "Track "<<i << " " ; i++;
01204     //enum TrackQuality { undefQuality=-1, loose=0, tight=1, highPurity=2, confirmed=3, goodIterative=4, qualitySize=5};
01205     if( t->quality(reco::TrackBase::loose)){ cout << "loose ";};
01206     if( t->quality(reco::TrackBase::tight)){ cout << "tight ";};
01207     if( t->quality(reco::TrackBase::highPurity)){ cout << "highPurity ";};
01208     if( t->quality(reco::TrackBase::confirmed)){ cout << "confirmed  ";};
01209     if( t->quality(reco::TrackBase::goodIterative)){ cout << "goodIterative  ";};
01210     cout  << endl;
01211 
01212     TransientTrack  tk = theB_->build(&(*t)); tk.setBeamSpot(vertexBeamSpot_);   
01213     double ipsig=0;
01214     if (tk.stateAtBeamLine().isValid()){
01215       ipsig= tk.stateAtBeamLine().transverseImpactParameter().significance();
01216     }else{
01217       ipsig=-1;
01218     }
01219 
01220     cout << Form("pt=%8.3f phi=%6.3f eta=%6.3f z=%8.4f  dz=%6.4f, ipsig=%6.1f",t->pt(), t->phi(), t->eta(), t->vz(), t->dzError(),ipsig) << endl;
01221 
01222 
01223     cout << Form(" found=%6d  lost=%6d   chi2/ndof=%10.3f ",t->found(), t->lost(),t->normalizedChi2())<<endl;
01224     const reco::HitPattern & p= t->hitPattern();
01225     cout << "subdet layers valid lost" << endl;
01226     cout << Form(" barrel  %2d  %2d  %2d",p.pixelBarrelLayersWithMeasurement(),p.numberOfValidPixelBarrelHits(), p.numberOfLostPixelBarrelHits()) << endl;
01227     cout << Form(" fwd     %2d  %2d  %2d",p.pixelEndcapLayersWithMeasurement(),p.numberOfValidPixelEndcapHits(), p.numberOfLostPixelEndcapHits()) << endl;
01228     cout << Form(" pixel   %2d  %2d  %2d",p.pixelLayersWithMeasurement(), p.numberOfValidPixelHits(), p.numberOfLostPixelHits()) << endl;
01229     cout << Form(" tracker %2d  %2d  %2d",p.trackerLayersWithMeasurement(), p.numberOfValidTrackerHits(), p.numberOfLostTrackerHits()) << endl;
01230     cout << endl;
01231     const reco::HitPattern & pinner= t->trackerExpectedHitsInner();
01232     const reco::HitPattern & pouter= t->trackerExpectedHitsOuter();
01233     int ninner=pinner.numberOfHits();
01234     int nouter=pouter.numberOfHits();
01235 
01236     //    double d0Error=t.d0Error();
01237     //    double d0=t.dxy(myBeamSpot);
01238     
01239     //
01240     for(trackingRecHit_iterator hit=t->recHitsBegin(); hit!=t->recHitsEnd(); hit++){
01241       if ((**hit).isValid()   && (**hit).geographicalId().det() == DetId::Tracker ){
01242        bool barrel = DetId((**hit).geographicalId()).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel);
01243        bool endcap = DetId((**hit).geographicalId()).subdetId() == static_cast<int>(PixelSubdetector::PixelEndcap);
01244        if (barrel){
01245          const SiPixelRecHit *pixhit = dynamic_cast<const SiPixelRecHit*>( &(**hit));
01246          edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster> const& clust = (*pixhit).cluster();
01247          if (clust.isNonnull()) {
01248            cout << Form(" barrel cluster size=%2d   charge=%6.1f wx=%2d  wy=%2d, expected=%3.1f",clust->size(),clust->charge(),clust->sizeX(),clust->sizeY(),1.+2./fabs(tan(t->theta()))) << endl;
01249          }
01250        }else if(endcap){
01251          const SiPixelRecHit *pixhit = dynamic_cast<const SiPixelRecHit*>( &(**hit));
01252          edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster> const& clust = (*pixhit).cluster();
01253          if (clust.isNonnull()) {
01254            cout << Form(" endcap cluster size=%2d   charge=%6.1f wx=%2d  wy=%2d",clust->size(),clust->charge(),clust->sizeX(),clust->sizeY()) << endl;
01255          }
01256        }
01257       }
01258     }
01259     cout << "hitpattern" << endl;
01260     for(int i=0; i<p.numberOfHits(); i++){      p.printHitPattern(i,std::cout);    }
01261     cout << "expected inner " << ninner << endl;
01262     for(int i=0; i<pinner.numberOfHits(); i++){      pinner.printHitPattern(i,std::cout);    }
01263     cout << "expected outer " << nouter << endl;
01264     for(int i=0; i<pouter.numberOfHits(); i++){      pouter.printHitPattern(i,std::cout);    }
01265   }
01266 }
01267 
01268 namespace {
01269 
01270   bool recTrackLessZ(const reco::TransientTrack & tk1,
01271                      const reco::TransientTrack & tk2)
01272   {
01273     return tk1.stateAtBeamLine().trackStateAtPCA().position().z() < tk2.stateAtBeamLine().trackStateAtPCA().position().z();
01274   }
01275 
01276 }
01277 
01278 
01279 
01280 
01281 void PrimaryVertexAnalyzer4PU::printPVTrks(const Handle<reco::TrackCollection> &recTrks, 
01282                                            const Handle<reco::VertexCollection> &recVtxs,  
01283                                            std::vector<SimPart>& tsim,
01284                                            std::vector<SimEvent>& simEvt,
01285                                            bool selectedOnly){
01286   // make a printout of the tracks selected for PV reconstructions, show matching MC tracks, too
01287 
01288   vector<TransientTrack>  selTrks;
01289   for(reco::TrackCollection::const_iterator t=recTrks->begin();
01290       t!=recTrks->end(); ++t){
01291     TransientTrack  tt = theB_->build(&(*t));  tt.setBeamSpot(vertexBeamSpot_);
01292     if( (!selectedOnly) || (selectedOnly && theTrackFilter(tt))){     selTrks.push_back(tt);    }
01293   }
01294   if (selTrks.size()==0) return;
01295   stable_sort(selTrks.begin(), selTrks.end(), recTrackLessZ);
01296 
01297   // select tracks like for PV reconstruction and match them to sim tracks
01298   reco::TrackCollection selRecTrks;
01299 
01300   for(unsigned int i=0; i<selTrks.size(); i++){ selRecTrks.push_back(selTrks[i].track());} 
01301   int* rectosim=supf(tsim, selRecTrks);
01302 
01303 
01304 
01305   // now dump in a format similar to the clusterizer
01306   cout << "printPVTrks" << endl;
01307   cout << "----          z +/- dz     1bet-m     ip +/-dip         pt   phi   eta";
01308   if((tsim.size()>0)||(simEvt.size()>0)) {cout << " type     pdg    zvtx    zdca      dca    zvtx   zdca    dsz";}
01309   cout << endl;
01310 
01311   cout.precision(4);
01312   int isel=0;
01313   for(unsigned int i=0; i<selTrks.size(); i++){
01314     if  (selectedOnly || (theTrackFilter(selTrks[i]))) {
01315           cout <<  setw (3)<< isel;
01316           isel++;
01317     }else{
01318       cout <<  "   ";
01319     }
01320 
01321 
01322     // is this track in the tracklist of a recvtx ?
01323     int vmatch=-1;
01324     int iv=0;
01325     for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
01326         v!=recVtxs->end(); ++v){
01327       if ( (v->ndof()<-1) || (v->chi2()<=0) ) continue;  // skip clusters 
01328       for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
01329         const reco::Track & RTv=*(tv->get());  
01330         if(selTrks[i].track().vz()==RTv.vz()) {vmatch=iv;}
01331       }
01332       iv++;
01333     }
01334 
01335     double tz=(selTrks[i].stateAtBeamLine().trackStateAtPCA()).position().z();
01336     double tantheta=tan((selTrks[i].stateAtBeamLine().trackStateAtPCA()).momentum().theta());
01337     double tdz0= selTrks[i].track().dzError();
01338     double tdz2= pow(selTrks[i].track().dzError(),2)+ (pow(vertexBeamSpot_.BeamWidthX(),2)+pow(vertexBeamSpot_.BeamWidthY(),2))/pow(tantheta,2);
01339     
01340     if(vmatch>-1){
01341       cout << "["<<setw(2)<<vmatch<<"]";
01342     }else{
01343       //int status=theTrackFilter.status(selTrks[i]);
01344       int status=0;
01345       if(status==0){
01346         cout <<"    ";
01347       }else{
01348         if(status&0x1){cout << "i";}else{cout << ".";};
01349         if(status&0x2){cout << "c";}else{cout << ".";};
01350         if(status&0x4){cout << "h";}else{cout << ".";};
01351         if(status&0x8){cout << "q";}else{cout << ".";};
01352       }
01353     }
01354     cout  <<  setw (8) << fixed << setprecision(4)<<  tz << " +/-" <<  setw (6)<< sqrt(tdz2);
01355     
01356 
01357     // track quality and hit information, see DataFormats/TrackReco/interface/HitPattern.h
01358     if(selTrks[i].track().quality(reco::TrackBase::highPurity)){ cout << " *";}else{cout <<"  ";}
01359     if(selTrks[i].track().hitPattern().hasValidHitInFirstPixelBarrel()){cout <<"+";}else{cout << "-";}
01360     cout << setw(1) << selTrks[i].track().hitPattern().pixelBarrelLayersWithMeasurement();
01361     cout << setw(1) << selTrks[i].track().hitPattern().pixelEndcapLayersWithMeasurement(); 
01362     cout << setw(1) << hex << selTrks[i].track().hitPattern().trackerLayersWithMeasurement()-selTrks[i].track().hitPattern().pixelLayersWithMeasurement()<<dec;
01363     cout << "-" << setw(1)<<hex <<selTrks[i].track().trackerExpectedHitsOuter().numberOfHits() << dec;
01364 
01365     
01366     Measurement1D IP=selTrks[i].stateAtBeamLine().transverseImpactParameter();
01367     cout << setw (8) << IP.value() << "+/-" << setw (6) << IP.error();
01368     if(selTrks[i].track().ptError()<1){
01369       cout << " " << setw(8) << setprecision(2)  << selTrks[i].track().pt()*selTrks[i].track().charge();
01370     }else{
01371       cout << " " << setw(7) << setprecision(1)  << selTrks[i].track().pt()*selTrks[i].track().charge() << "*";
01372       //cout << "+/-" << setw(6)<< setprecision(2) << selTrks[i].track().ptError();
01373     }
01374     cout << " " << setw(5) << setprecision(2) << selTrks[i].track().phi()
01375          << " " << setw(5) << setprecision(2) << selTrks[i].track().eta() ;
01376 
01377 
01378 
01379     // print MC info, if available
01380     if(simEvt.size()>0){
01381       reco::Track t=selTrks[i].track();
01382       try{
01383         TrackingParticleRef tpr = z2tp_[t.vz()];
01384         const TrackingVertex *parentVertex= tpr->parentVertex().get();
01385         //double vx=parentVertex->position().x(); // problems with tpr->vz()
01386         //double vy=parentVertex->position().y(); work in progress
01387         double vz=parentVertex->position().z();
01388         cout << " " << tpr->eventId().event();
01389         cout << " " << setw(5) << tpr->pdgId();
01390         cout << " " << setw(8) << setprecision(4) << vz;
01391       }catch (...){
01392         cout << " not matched";
01393       }
01394     }else{
01395       //    cout << "  " << rectosim[i];
01396       if(rectosim[i]>0){
01397         if(tsim[rectosim[i]].type==0){  cout << " prim " ;}else{cout << " sec  ";}
01398         cout << " " << setw(5) << tsim[rectosim[i]].pdg;
01399         cout << " " << setw(8) << setprecision(4) << tsim[rectosim[i]].zvtx;
01400         cout << " " << setw(8) << setprecision(4) << tsim[rectosim[i]].zdcap;
01401         cout << " " << setw(8) << setprecision(4) << tsim[rectosim[i]].ddcap;
01402         double zvtxpull=(tz-tsim[rectosim[i]].zvtx)/sqrt(tdz2);
01403         cout << setw(6) << setprecision(1) << zvtxpull;
01404         double zdcapull=(tz-tsim[rectosim[i]].zdcap)/tdz0;
01405         cout << setw(6) << setprecision(1) << zdcapull;
01406         double dszpull=(selTrks[i].track().dsz()-tsim[rectosim[i]].par[4])/selTrks[i].track().dszError();
01407         cout << setw(6) << setprecision(1) << dszpull;
01408       }
01409     }
01410     cout << endl;
01411   }
01412   delete rectosim;
01413 }
01414 
01415 
01416 void PrimaryVertexAnalyzer4PU::matchRecTracksToVertex(simPrimaryVertex & pv, 
01417                                                    const std::vector<SimPart > & tsim,
01418                                                    const edm::Handle<reco::TrackCollection> & recTrks)
01419 {
01420   // find all recTracks that belong to this simulated vertex (not just the ones that are used in
01421   // matching recVertex)
01422 
01423   std::cout << "dump rec tracks: " << std::endl;
01424   int irec=0;
01425   for(reco::TrackCollection::const_iterator t=recTrks->begin();
01426       t!=recTrks->end(); ++t){
01427     reco::TrackBase::ParameterVector  p = t->parameters();
01428     std::cout  << irec++ << ") " << p <<  std::endl;
01429   }
01430 
01431   std::cout << "match sim tracks: " << std::endl;
01432   pv.matchedRecTrackIndex.clear();
01433   pv.nMatchedTracks=0;
01434   int isim=0;
01435   for(std::vector<SimPart>::const_iterator s=tsim.begin();
01436       s!=tsim.end(); ++s){
01437     std::cout  << isim++ << " " << s->par;// << std::endl;
01438     int imatch=-1;
01439     int irec=0;
01440     for(reco::TrackCollection::const_iterator t=recTrks->begin();
01441         t!=recTrks->end(); ++t){
01442       reco::TrackBase::ParameterVector  p = t->parameters();
01443       if (match(s->par,p)){     imatch=irec; }
01444       irec++;
01445     }
01446     pv.matchedRecTrackIndex.push_back(imatch);
01447     if(imatch>-1){ 
01448       pv.nMatchedTracks++; 
01449       std::cout << " matched to rec trk" << imatch << std::endl;
01450     }else{
01451       std::cout << " not matched" << std::endl;
01452     }
01453   }
01454 }
01455 /********************************************************************************************************/
01456 
01457 
01458 
01459 
01460 
01461 
01462 /********************************************************************************************************/
01463 
01464 void PrimaryVertexAnalyzer4PU::getTc(const std::vector<reco::TransientTrack>& tracks, 
01465                                double & Tc, double & chsq, double & dzmax, double & dztrim, double & m4m2){
01466   if (tracks.size()<2){ Tc=-1; chsq=-1; dzmax=-1; dztrim=-1; m4m2=-1; return;}
01467 
01468   double sumw=0, sumwz=0, sumww=0,sumwwz=0,sumwwzz=0;
01469   double zmin=1e10, zmin1=1e10, zmax1=-1e10, zmax=-1e10;
01470   double m4=0,m3=0,m2=0,m1=0,m0=0;
01471   for(vector<reco::TransientTrack>::const_iterator it=tracks.begin(); it!=tracks.end(); it++){
01472      double tantheta=tan(((*it).stateAtBeamLine().trackStateAtPCA()).momentum().theta());
01473      reco::BeamSpot beamspot=(it->stateAtBeamLine()).beamSpot();
01474      double z=((*it).stateAtBeamLine().trackStateAtPCA()).position().z();
01475      double dz2= pow((*it).track().dzError(),2)+pow(beamspot.BeamWidthX()/tantheta,2);
01476    //    t.dz2= pow((*it).track().dzError(),2) + pow(wxy0/tantheta,2) +  1./(1.+exp(pow(t.ip/t.dip,2)-pow(2.)))*pow(ip/tantheta,2);
01477      double w=1./dz2;  // take p=1
01478      sumw    += w;
01479      sumwz   += w*z;
01480      sumwwz  += w*w*z;;
01481      sumwwzz += w*w*z*z;
01482      sumww   += w*w;
01483      m0      += w;
01484      m1      += w*z;
01485      m2      += w*z*z;
01486      m3      += w*z*z*z;
01487      m4      += w*z*z*z*z;
01488      if(dz2<pow(0.1,2)){
01489        if(z<zmin1){zmin1=z;}    if(z<zmin){zmin1=zmin; zmin=z;}
01490        if(z>zmax1){zmax1=z;}    if(z>zmax){zmax1=zmax; zmax=z;}
01491      }
01492   }
01493   double z=sumwz/sumw;
01494   double a=sumwwzz-2*z*sumwwz+z*z*sumww;
01495   double b=sumw;
01496   if(tracks.size()>1){
01497     chsq=(m2-m0*z*z)/(tracks.size()-1);
01498     Tc=2.*a/b;
01499     m4m2=sqrt((m4-4*m3*z+6*m2*z*z-3*m1*z*z*z+m0*z*z*z*z)/(m2-2*m1*z+z*z*m0));
01500   }else{
01501     chsq=0;
01502     Tc=0;
01503     m4m2=0;
01504   }
01505   dzmax=zmax-zmin;
01506   dztrim=zmax1-zmin1;// truncated
01507 }
01508 /********************************************************************************************************/
01509 
01510 
01511 
01512 
01513 
01514 /********************************************************************************************************/
01515 bool PrimaryVertexAnalyzer4PU::truthMatchedTrack( edm::RefToBase<reco::Track> track, TrackingParticleRef & tpr)
01516 
01517 /********************************************************************************************************/
01518 // for a reco track select the matching tracking particle, always use this function to make sure we
01519 // are consistent
01520 // to get the TrackingParticle form the TrackingParticleRef, use ->get();
01521 {
01522   double f=0;
01523   try{
01524     std::vector<std::pair<TrackingParticleRef, double> > tp = r2s_[track];
01525     for (std::vector<std::pair<TrackingParticleRef, double> >::const_iterator it = tp.begin(); 
01526          it != tp.end(); ++it) {
01527       
01528       if (it->second>f){
01529         tpr=it->first;
01530         f=it->second;
01531       }
01532     }
01533   } catch (Exception event) {
01534     // silly way of testing whether track is in r2s_
01535   }
01536   
01537   // sanity check on track parameters?
01538   
01539   return (f>0.5);
01540 }
01541 /********************************************************************************************************/
01542 
01543 
01544 
01545 
01546 
01547 
01548 /********************************************************************************************************/
01549 std::vector< edm::RefToBase<reco::Track> >  PrimaryVertexAnalyzer4PU::getTruthMatchedVertexTracks(
01550                                        const reco::Vertex& v
01551                                        )
01552 // for vertex v get a list of tracks for which truth matching is available 
01553 /********************************************************************************************************/
01554 {
01555   std::vector<  edm::RefToBase<reco::Track> > b;
01556   TrackingParticleRef tpr;
01557 
01558   for(trackit_t tv=v.tracks_begin(); tv!=v.tracks_end(); tv++){
01559     edm::RefToBase<reco::Track> track=*tv;
01560     if (truthMatchedTrack(track, tpr)){
01561       b.push_back(*tv);
01562     }
01563   }
01564 
01565 
01566   return b;
01567 }
01568 /********************************************************************************************************/
01569 
01570 
01571 
01572 
01573 
01574 /********************************************************************************************************/
01575 std::vector<PrimaryVertexAnalyzer4PU::SimEvent> PrimaryVertexAnalyzer4PU::getSimEvents
01576 (
01577  // const Event& iEvent, const EventSetup& iSetup,
01578  edm::Handle<TrackingParticleCollection>  TPCollectionH,
01579  edm::Handle<TrackingVertexCollection>  TVCollectionH,
01580  edm::Handle<View<Track> > trackCollectionH
01581  ){
01582 
01583   const TrackingParticleCollection* simTracks = TPCollectionH.product();
01584   const View<Track>  tC = *(trackCollectionH.product());
01585 
01586 
01587   vector<SimEvent> simEvt;
01588   map<EncodedEventId, unsigned int> eventIdToEventMap;
01589   map<EncodedEventId, unsigned int>::iterator id;
01590   bool dumpTP=false;
01591   for(TrackingParticleCollection::const_iterator it=simTracks->begin(); it!=simTracks->end(); it++){
01592     
01593     if( fabs(it->parentVertex().get()->position().z())>100.) continue; // skip funny entries @ -13900
01594 
01595     unsigned int event=0;  //note, this is no longer the same as eventId().event()
01596     id=eventIdToEventMap.find(it->eventId());
01597     if (id==eventIdToEventMap.end()){
01598 
01599       // new event here
01600       SimEvent e;
01601       e.eventId=it->eventId();
01602       event=simEvt.size();
01603       const TrackingVertex *parentVertex= it->parentVertex().get();
01604       if(DEBUG_){
01605         cout << "getSimEvents: ";
01606         cout << it->eventId().bunchCrossing() << "," <<  it->eventId().event() 
01607              << " z="<< it->vz() << " " 
01608              << parentVertex->eventId().bunchCrossing() << ","  <<parentVertex->eventId().event() 
01609              << " z=" << parentVertex->position().z() << endl;
01610       }
01611       if (it->eventId()==parentVertex->eventId()){
01612         //e.x=it->vx(); e.y=it->vy(); e.z=it->vz();// wrong ???
01613         e.x=parentVertex->position().x();
01614         e.y=parentVertex->position().y();
01615         e.z=parentVertex->position().z();
01616         if(e.z<-100){
01617           dumpTP=true;
01618         }
01619       }else{
01620         e.x=0; e.y=0; e.z=-88.;
01621       }
01622       simEvt.push_back(e);
01623       eventIdToEventMap[e.eventId]=event;
01624     }else{
01625       event=id->second;
01626     }
01627       
01628 
01629     simEvt[event].tp.push_back(&(*it));
01630     if( (abs(it->eta())<2.5) && (it->charge()!=0) ){
01631       simEvt[event].sumpt2+=pow(it->pt(),2); // should keep track of decays ?
01632       simEvt[event].sumpt+=it->pt(); 
01633     }
01634   }
01635 
01636   if(dumpTP){
01637     for(TrackingParticleCollection::const_iterator it=simTracks->begin(); it!=simTracks->end(); it++){
01638       std::cout << *it << std::endl;
01639     } 
01640   }
01641 
01642 
01643   for(View<Track>::size_type i=0; i<tC.size(); ++i) {
01644     RefToBase<Track> track(trackCollectionH, i);
01645     TrackingParticleRef tpr;
01646     if( truthMatchedTrack(track,tpr)){
01647 
01648       if( eventIdToEventMap.find(tpr->eventId())==eventIdToEventMap.end() ){ cout << "Bug in getSimEvents" << endl; break; }
01649 
01650       z2tp_[track.get()->vz()]=tpr;
01651 
01652       unsigned int event=eventIdToEventMap[tpr->eventId()];
01653       double ipsig=0,ipdist=0;
01654       const TrackingVertex *parentVertex= tpr->parentVertex().get();
01655       double vx=parentVertex->position().x(); // problems with tpr->vz()
01656       double vy=parentVertex->position().y();
01657       double vz=parentVertex->position().z();
01658       double d=sqrt(pow(simEvt[event].x-vx,2)+pow(simEvt[event].y-vy,2)+pow(simEvt[event].z-vz,2))*1.e4;
01659       ipdist=d;
01660       double dxy=track->dxy(vertexBeamSpot_.position());
01661       ipsig=dxy/track->dxyError();
01662 
01663 
01664       TransientTrack  t = theB_->build(tC[i]); 
01665       t.setBeamSpot(vertexBeamSpot_);   
01666       if (theTrackFilter(t)){
01667         simEvt[event].tk.push_back(t);
01668         if(ipdist<5){simEvt[event].tkprim.push_back(t);}
01669         if(ipsig<5){simEvt[event].tkprimsel.push_back(t);}
01670       }
01671       
01672     }
01673   }
01674 
01675 
01676   
01677   AdaptiveVertexFitter theFitter;
01678   cout << "SimEvents " << simEvt.size()  <<  endl;
01679   for(unsigned int i=0; i<simEvt.size(); i++){
01680 
01681     if(simEvt[i].tkprim.size()>0){
01682 
01683       getTc(simEvt[i].tkprimsel, simEvt[i].Tc, simEvt[i].chisq, simEvt[i].dzmax, simEvt[i].dztrim, simEvt[i].m4m2);
01684       TransientVertex v = theFitter.vertex(simEvt[i].tkprim, vertexBeamSpot_);
01685       if (v.isValid()){
01686         simEvt[i].xfit=v.position().x();
01687         simEvt[i].yfit=v.position().y();
01688         simEvt[i].zfit=v.position().z();
01689         //      if (simEvt[i].z<-80.){simEvt[i].z=v.position().z();} // for now
01690       }
01691     }
01692 
01693 
01694     if(DEBUG_){
01695       cout << i <<"  )   nTP="  << simEvt[i].tp.size()
01696            << "   z=" <<  simEvt[i].z
01697            << "    recTrks="  << simEvt[i].tk.size() 
01698            << "    recTrksPrim="  << simEvt[i].tkprim.size() 
01699            << "   zfit=" << simEvt[i].zfit
01700            << endl;
01701     }
01702   }
01703  
01704   return simEvt;
01705 }
01706 
01707 
01708 std::vector<PrimaryVertexAnalyzer4PU::simPrimaryVertex> PrimaryVertexAnalyzer4PU::getSimPVs(
01709                                       const Handle<HepMCProduct> evtMC)
01710 {
01711   if(DEBUG_){std::cout << "getSimPVs HepMC " << std::endl;}
01712 
01713   std::vector<PrimaryVertexAnalyzer4PU::simPrimaryVertex> simpv;
01714   const HepMC::GenEvent* evt=evtMC->GetEvent();
01715   if (evt) {
01716 //     std::cout << "process id " <<evt->signal_process_id()<<std::endl;
01717 //     std::cout <<"signal process vertex "<< ( evt->signal_process_vertex() ?
01718 //                                           evt->signal_process_vertex()->barcode() : 0 )   <<std::endl;
01719 //     std::cout <<"number of vertices " << evt->vertices_size() << std::endl;
01720 
01721 
01722     //int idx=0;
01723     for(HepMC::GenEvent::vertex_const_iterator vitr= evt->vertices_begin();
01724         vitr != evt->vertices_end(); ++vitr ) 
01725       { // loop for vertex ...
01726 
01727         HepMC::FourVector pos = (*vitr)->position();
01728         //      if (pos.t()>0) { continue;} // skip secondary vertices, doesn't work for some samples
01729 
01730         if (fabs(pos.z())>1000) continue;  // skip funny junk vertices
01731 
01732         bool hasMotherVertex=false;
01733         //std::cout << "mothers" << std::endl;
01734         for ( HepMC::GenVertex::particle_iterator
01735               mother  = (*vitr)->particles_begin(HepMC::parents);
01736               mother != (*vitr)->particles_end(HepMC::parents);
01737               ++mother ) {
01738           HepMC::GenVertex * mv=(*mother)->production_vertex();
01739           if (mv) {hasMotherVertex=true;}
01740           //std::cout << "\t"; (*mother)->print();
01741         }
01742         if(hasMotherVertex) {continue;}
01743 
01744 
01745         // could be a new vertex, check  all primaries found so far to avoid multiple entries
01746         const double mm=0.1;  
01747         simPrimaryVertex sv(pos.x()*mm,pos.y()*mm,pos.z()*mm);
01748         simPrimaryVertex *vp=NULL;  // will become non-NULL if a vertex is found and then point to it
01749         for(std::vector<simPrimaryVertex>::iterator v0=simpv.begin();
01750             v0!=simpv.end(); v0++){
01751           if( (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
01752             vp=&(*v0);
01753             break;
01754           }
01755         }
01756         if(!vp){
01757           // this is a new vertex
01758           //std::cout << "this is a new vertex" << sv.x << " " << sv.y << " " << sv.z <<std::endl;
01759           simpv.push_back(sv);
01760           vp=&simpv.back();
01761 //      }else{
01762 //        std::cout << "this is not a new vertex" << std::endl;
01763         }
01764 
01765         
01766         // store the gen vertex barcode with this simpv
01767         vp->genVertex.push_back((*vitr)->barcode());
01768 
01769 
01770         // collect final state descendants and sum up momenta etc
01771         for ( HepMC::GenVertex::particle_iterator
01772               daughter  = (*vitr)->particles_begin(HepMC::descendants);
01773               daughter != (*vitr)->particles_end(HepMC::descendants);
01774               ++daughter ) {
01775           //std::cout << "checking daughter  type " << (*daughter)->pdg_id() << " final :" <<isFinalstateParticle(*daughter) << std::endl;
01776           if (isFinalstateParticle(*daughter)){ 
01777             if ( find(vp->finalstateParticles.begin(), vp->finalstateParticles.end(),(*daughter)->barcode())
01778                  == vp->finalstateParticles.end()){
01779               vp->finalstateParticles.push_back((*daughter)->barcode());
01780               HepMC::FourVector m=(*daughter)->momentum();
01781               //std::cout << "adding particle to primary " << m.px()<< " "  << m.py() << " "  << m.pz() << std::endl; 
01782               vp->ptot.setPx(vp->ptot.px()+m.px());
01783               vp->ptot.setPy(vp->ptot.py()+m.py());
01784               vp->ptot.setPz(vp->ptot.pz()+m.pz());
01785               vp->ptot.setE(vp->ptot.e()+m.e());
01786               vp->ptsq+=(m.perp())*(m.perp());
01787               // count relevant particles
01788               if ( (m.perp()>0.2) && (fabs(m.pseudoRapidity())<2.5) && isCharged( *daughter ) ){
01789                 vp->nGenTrk++;
01790               }
01791               
01792               hsimPV["rapidity"]->Fill(m.pseudoRapidity());
01793               if( (m.perp()>0.8) &&  isCharged( *daughter ) ){
01794                 hsimPV["chRapidity"]->Fill(m.pseudoRapidity());
01795               }
01796               hsimPV["pt"]->Fill(m.perp());
01797             }//new final state particle for this vertex
01798           }//daughter is a final state particle
01799         }
01800 
01801 
01802         //idx++;
01803       }
01804   }
01805   if(verbose_){
01806     cout << "------- PrimaryVertexAnalyzer4PU simPVs -------" <<  endl;
01807     for(std::vector<simPrimaryVertex>::iterator v0=simpv.begin();
01808         v0!=simpv.end(); v0++){
01809       cout << "z=" << v0->z 
01810            << "  px=" << v0->ptot.px()
01811            << "  py=" << v0->ptot.py()
01812            << "  pz=" << v0->ptot.pz() 
01813            << "  pt2="<< v0->ptsq 
01814            << endl;
01815     }
01816     cout << "-----------------------------------------------" << endl;
01817   }
01818   return simpv;
01819 }
01820 
01821 
01822 
01823 
01824 
01825 
01826 
01827 
01828 /* get sim pv from TrackingParticles/TrackingVertex */
01829 std::vector<PrimaryVertexAnalyzer4PU::simPrimaryVertex> PrimaryVertexAnalyzer4PU::getSimPVs(
01830                                                                                           const edm::Handle<TrackingVertexCollection> tVC
01831                                                                                           )
01832 {
01833   std::vector<PrimaryVertexAnalyzer4PU::simPrimaryVertex> simpv;
01834   //std::cout <<"number of vertices " << tVC->size() << std::endl;
01835 
01836   if(DEBUG_){std::cout << "getSimPVs TrackingVertexCollection " << std::endl;}
01837 
01838   for (TrackingVertexCollection::const_iterator v = tVC -> begin(); v != tVC -> end(); ++v) {
01839 
01840     if(DEBUG_){
01841       std::cout << (v->eventId()).event() << v -> position() << v->g4Vertices().size() <<" "  <<v->genVertices().size() <<  "   t=" <<v->position().t()*1.e12 <<"    ==0:" <<(v->position().t()>0) << std::endl;
01842       for( TrackingVertex::g4v_iterator gv=v->g4Vertices_begin(); gv!=v->g4Vertices_end(); gv++){
01843         std::cout << *gv << std::endl;
01844       }
01845       std::cout << "----------" << std::endl;
01846     }
01847  
01848     //    bool hasMotherVertex=false;
01849     if ((unsigned int) v->eventId().event()<simpv.size()) continue;
01850         //if (v->position().t()>0) { continue;} // skip secondary vertices (obsolete + doesn't work)
01851     if (fabs(v->position().z())>1000) continue;  // skip funny junk vertices
01852     
01853     // could be a new vertex, check  all primaries found so far to avoid multiple entries
01854     const double mm=1.0; // for tracking vertices
01855     simPrimaryVertex sv(v->position().x()*mm,v->position().y()*mm,v->position().z()*mm);
01856 
01857     //cout << "sv: " << (v->eventId()).event() << endl;
01858     sv.eventId=v->eventId();
01859 
01860     for (TrackingParticleRefVector::iterator iTrack = v->daughterTracks_begin(); iTrack != v->daughterTracks_end(); ++iTrack){
01861       //cout <<((**iTrack).eventId()).event() << " ";  // an iterator of Refs, dereference twice. Cool eyh?
01862       sv.eventId=(**iTrack).eventId();
01863     }
01864     //cout <<endl;
01865     simPrimaryVertex *vp=NULL;  // will become non-NULL if a vertex is found and then point to it
01866     for(std::vector<simPrimaryVertex>::iterator v0=simpv.begin();
01867         v0!=simpv.end(); v0++){
01868       if( (sv.eventId==v0->eventId) && (fabs(sv.x-v0->x)<1e-5) && (fabs(sv.y-v0->y)<1e-5) && (fabs(sv.z-v0->z)<1e-5)){
01869         vp=&(*v0);
01870         break;
01871       }
01872     }
01873     if(!vp){
01874       // this is a new vertex
01875       if(DEBUG_){std::cout << "this is a new vertex " << sv.eventId.event() << "   "  << sv.x << " " << sv.y << " " << sv.z <<std::endl;}
01876       simpv.push_back(sv);
01877       vp=&simpv.back();
01878     }else{
01879       if(DEBUG_){std::cout << "this is not a new vertex"  << sv.x << " " << sv.y << " " << sv.z <<std::endl;}
01880     }
01881 
01882 
01883     // Loop over daughter track(s)
01884     if(DEBUG_){
01885       for (TrackingVertex::tp_iterator iTP = v -> daughterTracks_begin(); iTP != v -> daughterTracks_end(); ++iTP) {
01886         std::cout << "  Daughter momentum:      " << (*(*iTP)).momentum();
01887         std::cout << "  Daughter type     " << (*(*iTP)).pdgId();
01888         std::cout << std::endl;
01889       }
01890     }
01891   }
01892   if(DEBUG_){  
01893     cout << "------- PrimaryVertexAnalyzer4PU simPVs from TrackingVertices -------" <<  endl; 
01894     for(std::vector<simPrimaryVertex>::iterator v0=simpv.begin();
01895         v0!=simpv.end(); v0++){
01896       cout << "z=" << v0->z << "  event=" << v0->eventId.event() << endl;
01897     }
01898     cout << "-----------------------------------------------" << endl;
01899   }
01900   return simpv;
01901 }
01902 
01903 
01904 
01905 
01906 
01907 
01908 // ------------ method called to produce the data  ------------
01909 void
01910 PrimaryVertexAnalyzer4PU::analyze(const Event& iEvent, const EventSetup& iSetup)
01911 {
01912   
01913   bool MC=false;
01914   std::vector<simPrimaryVertex> simpv;  //  a list of primary MC vertices
01915   std::vector<SimPart> tsim;
01916   std::string mcproduct="generator";  // starting with 3_1_0 pre something
01917 
01918   eventcounter_++;
01919   run_             = iEvent.id().run();
01920   luminosityBlock_ = iEvent.luminosityBlock();
01921   event_           = iEvent.id().event();
01922   bunchCrossing_   = iEvent.bunchCrossing();
01923   orbitNumber_     = iEvent.orbitNumber();
01924 
01925   dumpThisEvent_ = false;
01926 
01927 
01928 
01929   if(verbose_){
01930     std::cout << endl 
01931               << "PrimaryVertexAnalyzer4PU::analyze   event counter=" << eventcounter_
01932               << " Run=" << run_ << "  LumiBlock " << luminosityBlock_ << "  event  " << event_
01933               << " bx=" << bunchCrossing_ <<  " orbit=" << orbitNumber_ 
01934       //<< " selected = " << good
01935               << std::endl;
01936   }
01937 
01938 
01939    try{
01940     iSetup.getData(pdt_);
01941   }catch(const Exception&){
01942     std::cout << "Some problem occurred with the particle data table. This may not work !" <<std::endl;
01943   }
01944 
01945   Handle<reco::VertexCollection> recVtxs;
01946   bool bnoBS=iEvent.getByLabel("offlinePrimaryVertices", recVtxs);
01947   
01948   Handle<reco::VertexCollection> recVtxsBS;
01949   bool bBS=iEvent.getByLabel("offlinePrimaryVerticesWithBS", recVtxsBS);
01950   
01951   Handle<reco::VertexCollection> recVtxsDA;
01952   bool bDA=iEvent.getByLabel("offlinePrimaryVerticesDA", recVtxsDA);
01953 
01954 //   Handle<reco::VertexCollection> recVtxsPIX;
01955 //   bool bPIX=iEvent.getByLabel("pixelVertices", recVtxsPIX);
01956 //   bPIX=false;
01957 
01958 //   Handle<reco::VertexCollection> recVtxsMVF;
01959 //   bool bMVF=iEvent.getByLabel("offlinePrimaryVerticesMVF", recVtxsMVF);
01960 
01961   Handle<reco::TrackCollection> recTrks;
01962   iEvent.getByLabel(recoTrackProducer_, recTrks);
01963 
01964 
01965   int nhighpurity=0, ntot=0;
01966   for(reco::TrackCollection::const_iterator t=recTrks->begin(); t!=recTrks->end(); ++t){  
01967     ntot++;
01968     if(t->quality(reco::TrackBase::highPurity)) nhighpurity++;
01969   } 
01970   if(ntot>10)  hnoBS["highpurityTrackFraction"]->Fill(float(nhighpurity)/float(ntot));
01971   if((recoTrackProducer_=="generalTracks")&&(nhighpurity<0.25*ntot)){
01972     if(verbose_){ cout << "rejected, " << nhighpurity << " highpurity  out of  " << ntot << "  total tracks "<< endl<< endl;}
01973     return;
01974   }
01975 
01976 
01977 
01978   
01979 
01980   if(iEvent.getByType(recoBeamSpotHandle_)){
01981     vertexBeamSpot_= *recoBeamSpotHandle_;
01982     wxy2_=pow(vertexBeamSpot_.BeamWidthX(),2)+pow(vertexBeamSpot_.BeamWidthY(),2);
01983     Fill(hsimPV, "xbeam",vertexBeamSpot_.x0()); Fill(hsimPV, "wxbeam",vertexBeamSpot_.BeamWidthX());
01984     Fill(hsimPV, "ybeam",vertexBeamSpot_.y0()); Fill(hsimPV, "wybeam",vertexBeamSpot_.BeamWidthY());
01985     Fill(hsimPV, "zbeam",vertexBeamSpot_.z0());// Fill("wzbeam",vertexBeamSpot_.BeamWidthZ());
01986   }else{
01987     cout << " beamspot not found, using dummy " << endl;
01988     vertexBeamSpot_=reco::BeamSpot();// dummy
01989   }
01990 
01991 
01992   if(bnoBS){
01993     if((recVtxs->begin()->isValid())&&(recVtxs->begin()->ndof()>1)&&(recVtxs->begin()->ndof()>(0.0*recVtxs->begin()->tracksSize()))){  // was 5 and 0.2
01994       Fill(hnoBS,"xrecBeamvsdxXBS",recVtxs->begin()->xError(),recVtxs->begin()->x()-vertexBeamSpot_.x0());
01995       Fill(hnoBS,"yrecBeamvsdyXBS",recVtxs->begin()->yError(),recVtxs->begin()->y()-vertexBeamSpot_.y0());
01996 
01997       if(printXBS_) {
01998         cout << Form("XBS %10d %5d %10d  %4d   %lu %5.2f    %8f %8f       %8f %8f      %8f %8f",
01999                       run_,luminosityBlock_,event_,bunchCrossing_,
02000                       (unsigned long)(recVtxs->begin()->tracksSize()), recVtxs->begin()->ndof(),
02001                       recVtxs->begin()->x(),               recVtxs->begin()->xError(), 
02002                       recVtxs->begin()->y(),               recVtxs->begin()->yError(), 
02003                       recVtxs->begin()->z(),               recVtxs->begin()->zError()
02004                      ) << endl; 
02005       }
02006 
02007     }
02008   }
02009 
02010  
02011   // for the associator
02012   Handle<View<Track> > trackCollectionH;
02013   iEvent.getByLabel(recoTrackProducer_,trackCollectionH);
02014 
02015   Handle<HepMCProduct> evtMC;
02016 
02017   Handle<SimVertexContainer> simVtxs;
02018   iEvent.getByLabel( simG4_, simVtxs);
02019   
02020   Handle<SimTrackContainer> simTrks;
02021   iEvent.getByLabel( simG4_, simTrks);
02022 
02023 
02024 
02025 
02026 
02027   edm::Handle<TrackingParticleCollection>  TPCollectionH ;
02028   edm::Handle<TrackingVertexCollection>    TVCollectionH ;
02029   bool gotTP=iEvent.getByLabel("mergedtruth","MergedTrackTruth",TPCollectionH);
02030   bool gotTV=iEvent.getByLabel("mergedtruth","MergedTrackTruth",TVCollectionH);
02031 
02032 
02033   iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",theB_);
02034   fBfield_=((*theB_).field()->inTesla(GlobalPoint(0.,0.,0.))).z();
02035 
02036  
02037   vector<SimEvent> simEvt;
02038   if (gotTP && gotTV ){
02039 
02040     edm::ESHandle<TrackAssociatorBase> theHitsAssociator;
02041     iSetup.get<TrackAssociatorRecord>().get("TrackAssociatorByHits",theHitsAssociator);
02042     associatorByHits_ = (TrackAssociatorBase *) theHitsAssociator.product();
02043     r2s_ =   associatorByHits_->associateRecoToSim (trackCollectionH,TPCollectionH, &iEvent ); 
02044     //simEvt=getSimEvents(iEvent, TPCollectionH, TVCollectionH, trackCollectionH);
02045     simEvt=getSimEvents(TPCollectionH, TVCollectionH, trackCollectionH);
02046 
02047     if (simEvt.size()==0){
02048       cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
02049       cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
02050       cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
02051       cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
02052       cout << " !!!!!!!!!!!!!!!!!!!!!!   got TrackingParticles but no simEvt   !!!!!!!!!!!!!!!!!" << endl;
02053       cout << " dumping Tracking particles " << endl;
02054       const TrackingParticleCollection* simTracks = TPCollectionH.product();
02055       for(TrackingParticleCollection::const_iterator it=simTracks->begin(); it!=simTracks->end(); it++){
02056         cout << *it << endl;
02057       }
02058       cout << " dumping Tracking Vertices " << endl;
02059       const TrackingVertexCollection* tpVtxs = TVCollectionH.product();
02060       for(TrackingVertexCollection::const_iterator iv=tpVtxs->begin(); iv!=tpVtxs->end(); iv++){
02061         cout << *iv << endl;
02062       }
02063       if(iEvent.getByLabel(mcproduct,evtMC)){
02064         cout << "dumping simTracks" << endl;
02065         for(SimTrackContainer::const_iterator t=simTrks->begin();  t!=simTrks->end(); ++t){
02066           cout << *t << endl;
02067         }
02068         cout << "dumping simVertexes" << endl;
02069         for(SimVertexContainer::const_iterator vv=simVtxs->begin();  
02070             vv!=simVtxs->end(); 
02071             ++vv){
02072           cout << *vv << endl;
02073         }
02074       }else{
02075         cout << "no hepMC" << endl;
02076       }
02077       cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
02078 
02079       const HepMC::GenEvent* evt=evtMC->GetEvent();
02080       if(evt){
02081         std::cout << "process id " <<evt->signal_process_id()<<std::endl;
02082         std::cout <<"signal process vertex "<< ( evt->signal_process_vertex() ?
02083                                                  evt->signal_process_vertex()->barcode() : 0 )   <<std::endl;
02084         std::cout <<"number of vertices " << evt->vertices_size() << std::endl;
02085         evt->print();
02086       }else{
02087         cout << "no event in HepMC" << endl;
02088       }
02089       cout << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
02090 
02091     }
02092   }
02093 
02094 
02095   
02096 
02097   if(gotTV){
02098 
02099     MC=true;
02100     if(verbose_){
02101       cout << "Found Tracking Vertices " << endl;
02102     }
02103     simpv=getSimPVs(TVCollectionH);
02104     
02105 
02106   }else if(iEvent.getByLabel(mcproduct,evtMC)){
02107 
02108     MC=true;
02109     simpv=getSimPVs(evtMC);
02110 
02111     if(verbose_){
02112       cout << "Using HepMCProduct " << endl;
02113       std::cout << "simtrks " << simTrks->size() << std::endl;
02114     }
02115     tsim = PrimaryVertexAnalyzer4PU::getSimTrkParameters(simTrks, simVtxs, simUnit_);
02116     
02117   }else{
02118     MC=false;
02119     // if(verbose_({cout << "No MC info at all" << endl;}
02120   }
02121 
02122 
02123 
02124 
02125   // get the beam spot from the appropriate dummy vertex, if available
02126   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02127       v!=recVtxs->end(); ++v){
02128     if ((v->ndof()==-3) && (v->chi2()==0) ){ 
02129       myBeamSpot=math::XYZPoint(v->x(), v->y(), v->z());
02130     }
02131   }
02132 
02133   
02134 
02135 
02136   hsimPV["nsimvtx"]->Fill(simpv.size());
02137   for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
02138        vsim!=simpv.end(); vsim++){
02139     if(doMatching_){  
02140       matchRecTracksToVertex(*vsim, tsim, recTrks); // hepmc, based on track parameters
02141      }
02142 
02143      hsimPV["nbsimtksinvtx"]->Fill(vsim->nGenTrk);
02144      hsimPV["xsim"]->Fill(vsim->x*simUnit_);
02145      hsimPV["ysim"]->Fill(vsim->y*simUnit_);
02146      hsimPV["zsim"]->Fill(vsim->z*simUnit_);
02147      hsimPV["xsim1"]->Fill(vsim->x*simUnit_);
02148      hsimPV["ysim1"]->Fill(vsim->y*simUnit_);
02149      hsimPV["zsim1"]->Fill(vsim->z*simUnit_);
02150      Fill(hsimPV,"xsim2",vsim->x*simUnit_,vsim==simpv.begin());
02151      Fill(hsimPV,"ysim2",vsim->y*simUnit_,vsim==simpv.begin());
02152      Fill(hsimPV,"zsim2",vsim->z*simUnit_,vsim==simpv.begin());
02153      hsimPV["xsim2"]->Fill(vsim->x*simUnit_);
02154      hsimPV["ysim2"]->Fill(vsim->y*simUnit_);
02155      hsimPV["zsim2"]->Fill(vsim->z*simUnit_);
02156      hsimPV["xsim3"]->Fill(vsim->x*simUnit_);
02157      hsimPV["ysim3"]->Fill(vsim->y*simUnit_);
02158      hsimPV["zsim3"]->Fill(vsim->z*simUnit_);
02159      hsimPV["xsimb"]->Fill(vsim->x*simUnit_-vertexBeamSpot_.x0());
02160      hsimPV["ysimb"]->Fill(vsim->y*simUnit_-vertexBeamSpot_.y0());
02161      hsimPV["zsimb"]->Fill(vsim->z*simUnit_-vertexBeamSpot_.z0());
02162      hsimPV["xsimb1"]->Fill(vsim->x*simUnit_-vertexBeamSpot_.x0());
02163      hsimPV["ysimb1"]->Fill(vsim->y*simUnit_-vertexBeamSpot_.y0());
02164      hsimPV["zsimb1"]->Fill(vsim->z*simUnit_-vertexBeamSpot_.z0());
02165   }
02166 
02167 
02168 
02169 
02170    
02171    if(bnoBS){
02172      analyzeVertexCollection(hnoBS, recVtxs, recTrks, simpv,"noBS");
02173      analyzeVertexCollectionTP(hnoBS, recVtxs, recTrks, simEvt,"noBS");
02174    }
02175    if(bBS){
02176      analyzeVertexCollection(hBS, recVtxsBS, recTrks, simpv,"BS");
02177      analyzeVertexCollectionTP(hBS, recVtxsBS, recTrks, simEvt,"BS");
02178    }
02179    if(bDA){
02180      analyzeVertexCollection(hDA, recVtxsDA, recTrks, simpv,"DA");
02181      analyzeVertexCollectionTP(hDA, recVtxsDA, recTrks, simEvt,"DA");
02182    }
02183 //    if(bPIX){
02184 //      analyzeVertexCollection(hPIX, recVtxsPIX, recTrks, simpv,"PIX");
02185 //      analyzeVertexCollectionTP(hPIX, recVtxsPIX, recTrks, simEvt,"PIX");
02186 //    }
02187 
02188 
02189 
02190    if((dumpThisEvent_&& (dumpcounter_<100)) ||(verbose_ && (eventcounter_<ndump_))){
02191      cout << endl << "Event dump" << endl
02192           << "event counter=" << eventcounter_
02193           << " Run=" << run_ << "  LumiBlock " << luminosityBlock_ << "  event  " << event_
02194           << " bx=" << bunchCrossing_ <<  " orbit=" << orbitNumber_ 
02195           << std::endl;
02196      dumpcounter_++;
02197 
02198      //evtMC->GetEvent()->print();
02199      //printRecTrks(recTrks);  // very verbose !!
02200      
02201 //      if (bPIX) printRecVtxs(recVtxsPIX,"pixel vertices");
02202      if (bnoBS) {printRecVtxs(recVtxs,"Offline without Beamspot");}
02203      if (bnoBS && (!bDA)){ printPVTrks(recTrks, recVtxs, tsim, simEvt, false);}
02204      if (bBS) printRecVtxs(recVtxsBS,"Offline with Beamspot");
02205      if (bDA) {
02206        printRecVtxs(recVtxsDA,"Offline DA");
02207        printPVTrks(recTrks, recVtxsDA, tsim, simEvt, false);
02208      }
02209      if (dumpcounter_<2){cout << "beamspot " << vertexBeamSpot_ << endl;}
02210    }
02211 
02212   if(verbose_){
02213     std::cout << std::endl;
02214   }
02215 }
02216 
02217 namespace {
02218 bool lt(const std::pair<double,unsigned int>& a,const std::pair<double,unsigned int>& b ){
02219   return a.first<b.first;
02220 }
02221 }
02222 
02223 /***************************************************************************************/
02224 void PrimaryVertexAnalyzer4PU::printEventSummary(std::map<std::string, TH1*> & h,
02225                                                  const edm::Handle<reco::VertexCollection> recVtxs,
02226                                                  const edm::Handle<reco::TrackCollection> recTrks, 
02227                                                  vector<SimEvent> & simEvt,
02228                                                  const string message){
02229   // make a readable summary of the vertex finding if the TrackingParticles are availabe
02230   if (simEvt.size()==0){return;}
02231 
02232 
02233   // sort vertices in z ... for nicer printout
02234 
02235   vector< pair<double,unsigned int> >  zrecv;
02236   for(unsigned int idx=0; idx<recVtxs->size(); idx++){
02237     if ( (recVtxs->at(idx).ndof()<0) || (recVtxs->at(idx).chi2()<=0) ) continue;  // skip clusters 
02238     zrecv.push_back( make_pair(recVtxs->at(idx).z(),idx) );
02239   }
02240   stable_sort(zrecv.begin(),zrecv.end(),lt);
02241 
02242   // same for simulated vertices
02243   vector< pair<double,unsigned int> >  zsimv;
02244   for(unsigned int idx=0; idx<simEvt.size(); idx++){
02245     zsimv.push_back(make_pair(simEvt[idx].z, idx));
02246   }
02247   stable_sort(zsimv.begin(), zsimv.end(),lt);
02248 
02249 
02250 
02251 
02252   cout << "---------------------------" << endl;
02253   cout << "event counter = " << eventcounter_ << "   " << message << endl;
02254   cout << "---------------------------" << endl;
02255   cout << " z[cm]       rec -->    ";
02256   cout.precision(4);
02257   for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02258     cout << setw(7) << fixed << itrec->first;
02259     if (itrec->second==0){cout << "*" ;}else{cout << " " ;}
02260   }
02261   cout << endl;
02262   cout << "                        ";
02263   for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02264     cout << setw(7) << fixed << recVtxs->at(itrec->second).tracksSize();
02265     if (itrec->second==0){cout << "*" ;}else{cout << " " ;}
02266   }
02267   cout << "   rec tracks" << endl;
02268   cout << "                        ";
02269   map<unsigned int, int> truthMatchedVertexTracks;
02270   for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02271     truthMatchedVertexTracks[itrec->second]=getTruthMatchedVertexTracks(recVtxs->at(itrec->second)).size();
02272     cout << setw(7) << fixed << truthMatchedVertexTracks[itrec->second];
02273     if (itrec->second==0){cout << "*" ;}else{cout << " " ;}
02274   }
02275   cout << "   truth matched " << endl;
02276 
02277   cout << "sim ------- trk  prim ----" << endl;
02278 
02279 
02280 
02281   map<unsigned int, unsigned int> rvmatch; // reco vertex matched to sim vertex  (sim to rec)
02282   map<unsigned int, double > nmatch;  // highest number of truth-matched tracks of ev found in a recvtx
02283   map<unsigned int, double > purity;  // highest purity of a rec vtx (i.e. highest number of tracks from the same simvtx)
02284   map<unsigned int, double > wpurity;  // same for the sum of weights
02285 
02286   for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02287     purity[itrec->second]=0.;
02288     wpurity[itrec->second]=0.;
02289   }
02290 
02291   for(vector< pair<double,unsigned int> >::iterator itsim=zsimv.begin(); itsim!=zsimv.end(); itsim++){
02292     SimEvent* ev =&(simEvt[itsim->second]);
02293 
02294 
02295     cout.precision(4);
02296     if (itsim->second==0){
02297       cout << setw(8) << fixed << ev->z << ")*" << setw(5) << ev->tk.size() << setw(5) << ev->tkprim.size() << "  | ";
02298     }else{
02299       cout << setw(8) << fixed << ev->z << ") " << setw(5) << ev->tk.size() << setw(5) << ev->tkprim.size() << "  | ";
02300     }
02301 
02302     nmatch[itsim->second]=0;  // highest number of truth-matched tracks of ev found in a recvtx
02303     double matchpurity=0,matchwpurity=0;
02304 
02305     for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02306       const reco::Vertex *v = &(recVtxs->at(itrec->second));
02307 
02308       // count tracks found in both, sim and rec
02309       double n=0,wt=0;
02310       for(vector<TransientTrack>::iterator te=ev->tk.begin(); te!=ev->tk.end(); te++){
02311         const reco::Track&  RTe=te->track();
02312          for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
02313            const reco::Track & RTv=*(tv->get());  
02314            if(RTe.vz()==RTv.vz()) {n++; wt+=v->trackWeight(*tv);}
02315         }
02316       }
02317       cout << setw(7) << int(n)<< " ";
02318 
02319       if (n > nmatch[itsim->second]){
02320         nmatch[itsim->second]=n;
02321         rvmatch[itsim->second]=itrec->second;
02322         matchpurity=n/truthMatchedVertexTracks[itrec->second];
02323         matchwpurity=wt/truthMatchedVertexTracks[itrec->second];
02324       }
02325 
02326       if(n > purity[itrec->second]){
02327         purity[itrec->second]=n;
02328       }
02329 
02330       if(wt > wpurity[itrec->second]){
02331         wpurity[itrec->second]=wt;
02332       }
02333 
02334     }// end of reco vertex loop
02335 
02336     cout << "  | ";
02337     if  (nmatch[itsim->second]>0 ){
02338       if(matchpurity>0.5){
02339         cout << "found  ";
02340       }else{
02341         cout << "merged ";
02342       } 
02343       cout << "  max eff. = "  << setw(8) << nmatch[itsim->second]/ev->tk.size() << " p=" << matchpurity << " w=" << matchwpurity <<  endl;
02344     }else{
02345       if(ev->tk.size()==0){
02346         cout  << " invisible" << endl;
02347       }else if (ev->tk.size()==1){
02348         cout << "single track " << endl;
02349       }else{
02350         cout << "lost " << endl;
02351       }
02352     }
02353   }
02354   cout << "---------------------------" << endl;
02355 
02356   //  the purity of the reconstructed vertex
02357   cout << "               purity   ";
02358   for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02359     cout << setw(7) << fixed << purity[itrec->second]/truthMatchedVertexTracks[itrec->second];
02360     if (itrec->second==0){cout << "*" ;}else{cout << " " ;}
02361   }
02362   cout << endl;
02363 
02364 //   //  classification of reconstructed vertex fake/real
02365 //   cout << "                        ";
02366 //   for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02367 //     cout << setw(7) << fixed << purity[itrec->second]/truthMatchedVertexTracks[itrec->second];
02368 //     if (itrec->second==0){cout << "*" ;}else{cout << " " ;}
02369 //   }
02370 //   cout << endl;
02371   cout << "---------------------------" << endl;
02372 
02373 
02374 
02375 
02376   // list problematic tracks
02377   for(vector< pair<double,unsigned int> >::iterator itsim=zsimv.begin(); itsim!=zsimv.end(); itsim++){
02378     SimEvent* ev =&(simEvt[itsim->second]);
02379 
02380     for(vector<TransientTrack>::iterator te=ev->tk.begin(); te!=ev->tk.end(); te++){
02381       const reco::Track&  RTe=te->track();
02382       
02383       int ivassign=-1;  // will become the index of the vertex to which a track was assigned
02384       
02385       for(vector< pair<double,unsigned int> >::iterator itrec=zrecv.begin(); itrec!=zrecv.end(); itrec++){
02386         const reco::Vertex *v = &(recVtxs->at(itrec->second));
02387 
02388         for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
02389           const reco::Track & RTv=*(tv->get());  
02390           if(RTe.vz()==RTv.vz()) {ivassign=itrec->second;}
02391         }
02392       }
02393       double tantheta=tan((te->stateAtBeamLine().trackStateAtPCA()).momentum().theta());
02394       reco::BeamSpot beamspot=(te->stateAtBeamLine()).beamSpot();
02395       //double z=(te->stateAtBeamLine().trackStateAtPCA()).position().z();
02396       double dz2= pow(RTe.dzError(),2)+pow(beamspot.BeamWidthX()/tantheta,2);
02397       
02398       if(ivassign==(int)rvmatch[itsim->second]){
02399         Fill(h,"correctlyassigned",RTe.eta(),RTe.pt());
02400         Fill(h,"ptcat",RTe.pt());
02401         Fill(h,"etacat",RTe.eta());
02402         Fill(h,"phicat",RTe.phi());
02403         Fill(h,"dzcat",sqrt(dz2));
02404       }else{
02405         Fill(h,"misassigned",RTe.eta(),RTe.pt());
02406         Fill(h,"ptmis",RTe.pt());
02407         Fill(h,"etamis",RTe.eta());
02408         Fill(h,"phimis",RTe.phi());
02409         Fill(h,"dzmis",sqrt(dz2));
02410         cout << "vertex " << setw(8) << fixed << ev->z;
02411 
02412         if (ivassign<0){
02413           cout << " track lost                ";
02414           // for some clusterizers there shouldn't be any lost tracks,
02415           // are there differences in the track selection?
02416         }else{
02417           cout << " track misassigned " << setw(8) << fixed << recVtxs->at(ivassign).z();
02418         }
02419 
02420         cout << "  track z=" << setw(8) << fixed  << RTe.vz() << "+/-" << RTe.dzError() << "  pt=" <<  setw(8) << fixed<< RTe.pt() << "  eta=" << setw(8) << fixed << RTe.eta()<< " sel=" <<theTrackFilter(*te);
02421 
02422         //
02423         //cout << " ztrack=" << te->track().vz();
02424         TrackingParticleRef tpr = z2tp_[te->track().vz()];
02425         double zparent=tpr->parentVertex().get()->position().z();
02426         if(zparent==ev->z) {
02427           cout << " prim";
02428         }else{
02429           cout << " sec ";
02430         }
02431         cout << "  id=" << tpr->pdgId();
02432         cout << endl;
02433 
02434         //
02435       }
02436     }// next simvertex-track
02437 
02438   }//next simvertex
02439 
02440   cout << "---------------------------" << endl;
02441 
02442 }
02443 /***************************************************************************************/
02444 
02445 
02446 
02447 
02448 /***************************************************************************************/
02449 void PrimaryVertexAnalyzer4PU::analyzeVertexCollectionTP(std::map<std::string, TH1*> & h,
02450                                const edm::Handle<reco::VertexCollection> recVtxs,
02451                                const edm::Handle<reco::TrackCollection> recTrks, 
02452                                                         vector<SimEvent> & simEvt,
02453                                                         const string message){
02454   
02455   //  cout <<"PrimaryVertexAnalyzer4PU::analyzeVertexCollectionTP size=" << simEvt.size() << endl;
02456   if(simEvt.size()==0)return;
02457 
02458   printEventSummary(h, recVtxs,recTrks,simEvt,message);
02459 
02460   //const int iSignal=0;  
02461   EncodedEventId iSignal=simEvt[0].eventId;
02462   Fill(h,"npu0",simEvt.size());
02463 
02464 
02465   for(vector<SimEvent>::iterator ev=simEvt.begin(); ev!=simEvt.end(); ev++){
02466     Fill(h,"Tc",    ev->Tc,    ev==simEvt.begin());
02467     Fill(h,"Chisq", ev->chisq, ev==simEvt.begin());
02468     if(ev->chisq>0)Fill(h,"logChisq", log(ev->chisq), ev==simEvt.begin());
02469     Fill(h,"dzmax", ev->dzmax, ev==simEvt.begin());
02470     Fill(h,"dztrim",ev->dztrim,ev==simEvt.begin());
02471     Fill(h,"m4m2",  ev->m4m2,  ev==simEvt.begin());
02472     if(ev->Tc>0){  Fill(h,"logTc",log(ev->Tc)/log(10.),ev==simEvt.begin());}
02473     
02474 
02475     for(vector<SimEvent>::iterator ev2=ev+1; ev2!=simEvt.end(); ev2++){
02476       vector<TransientTrack> xt;
02477       if((ev->tkprimsel.size()>0)&&(ev2->tkprimsel.size()>0)&&(ev->tkprimsel.size()+ev2->tkprimsel.size())>1){
02478         xt.insert (xt.end() ,ev->tkprimsel.begin(),ev->tkprimsel.end());
02479         xt.insert (xt.end() ,ev2->tkprimsel.begin(),ev2->tkprimsel.end());
02480         double xTc,xChsq,xDzmax,xDztrim,xm4m2;
02481         getTc(xt, xTc, xChsq, xDzmax, xDztrim,xm4m2);
02482         if(xTc>0){
02483           Fill(h,"xTc",xTc,ev==simEvt.begin());
02484           Fill(h,"logxTc",   log(xTc)/log(10),ev==simEvt.begin());
02485           Fill(h,"xChisq",   xChsq,ev==simEvt.begin());
02486           if(xChsq>0){Fill(h,"logxChisq",   log(xChsq),ev==simEvt.begin());};
02487           Fill(h,"xdzmax",   xDzmax,ev==simEvt.begin());
02488           Fill(h,"xdztrim",  xDztrim,ev==simEvt.begin());
02489           Fill(h,"xm4m2",    xm4m2,ev==simEvt.begin());
02490           
02491         }
02492       }
02493     }
02494   }
02495   
02496   // --------------------------------------- count actual rec vtxs ----------------------
02497   int nrecvtxs=0;//, nrecvtxs1=0, nrecvtxs2=0;
02498   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02499       v!=recVtxs->end(); ++v){
02500     if ( (v->isFake()) || (v->ndof()<0) || (v->chi2()<=0) ) continue;  // skip clusters 
02501     nrecvtxs++;
02502   }
02503 
02504   // --------------------------------------- fill the track assignment matrix ----------------------
02505   for(vector<SimEvent>::iterator ev=simEvt.begin(); ev!=simEvt.end(); ev++){
02506     ev->ntInRecVz.clear();  // just in case
02507     ev->zmatch=-99.;
02508     ev->nmatch=0;
02509     for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02510         v!=recVtxs->end(); ++v){
02511       double n=0, wt=0;
02512       for(vector<TransientTrack>::iterator te=ev->tk.begin(); te!=ev->tk.end(); te++){
02513         const reco::Track&  RTe=te->track();
02514         for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
02515           const reco::Track & RTv=*(tv->get());  
02516           if(RTe.vz()==RTv.vz()) {n++; wt+=v->trackWeight(*tv);}
02517         }
02518       }
02519       ev->ntInRecVz[v->z()]=n;
02520       if (n > ev->nmatch){ ev->nmatch=n; ev->zmatch=v->z(); ev->zmatch=v->z(); }
02521     }
02522   }
02523   
02524     // call a vertex a fake if for every sim vertex there is another recvertex containing more tracks
02525     // from that sim vertex than the current recvertex
02526   double nfake=0;
02527   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02528       v!=recVtxs->end(); ++v){
02529     double zmatch=-99; bool matched=false;
02530     for(vector<SimEvent>::iterator ev=simEvt.begin(); ev!=simEvt.end(); ev++){
02531       if ((ev->nmatch>0)&&(ev->zmatch==v->z())){
02532         matched=true; zmatch=ev->z;
02533       }
02534     }
02535     if(!matched && !v->isFake()) {
02536       nfake++;
02537       cout << " fake rec vertex at z=" << v->z() << endl;
02538       // some histograms of fake vertex properties here
02539       Fill(h,"unmatchedVtxZ",v->z());
02540       Fill(h,"unmatchedVtxNdof",v->ndof());
02541     }
02542   }
02543   if(nrecvtxs>0){
02544     Fill(h,"unmatchedVtx",nfake);
02545     Fill(h,"unmatchedVtxFrac",nfake/nrecvtxs);
02546   }
02547 
02548   // --------------------------------------- match rec to sim ---------------------------------------
02549   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02550       v!=recVtxs->end(); ++v){
02551 
02552     if ( (v->ndof()<0) || (v->chi2()<=0) ) continue;  // skip clusters 
02553     double  nmatch=-1;      // highest number of tracks in recvtx v found in any event
02554     EncodedEventId evmatch;
02555     bool matchFound=false;
02556     double nmatchvtx=0;     // number of simvtcs contributing to recvtx v
02557 
02558     for(vector<SimEvent>::iterator ev=simEvt.begin(); ev!=simEvt.end(); ev++){
02559 
02560       double n=0;  // number of tracks that are in both, the recvtx v and the event ev
02561       for(vector<TransientTrack>::iterator te=ev->tk.begin(); te!=ev->tk.end(); te++){
02562 
02563         const reco::Track&  RTe=te->track();
02564          for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
02565            const reco::Track & RTv=*(tv->get());  
02566            if(RTe.vz()==RTv.vz()){ n++;}
02567         }
02568       }     
02569 
02570       // find the best match in terms of the highest number of tracks 
02571       // from a simvertex in this rec vertex
02572       if (n > nmatch){
02573         nmatch=n;
02574         evmatch=ev->eventId;
02575         matchFound=true;
02576       }
02577       if(n>0){
02578         nmatchvtx++;
02579       }
02580     }
02581 
02582     double nmatchany=getTruthMatchedVertexTracks(*v).size();  
02583     if (matchFound && (nmatchany>0)){
02584       //           highest number of tracks in recvtx matched to (the same) sim vertex
02585       // purity := -----------------------------------------------------------------
02586       //                  number of truth matched tracks in this recvtx
02587       double purity =nmatch/nmatchany; 
02588       Fill(h,"recmatchPurity",purity);
02589       if(v==recVtxs->begin()){
02590         Fill(h,"recmatchPurityTag",purity, (bool)(evmatch==iSignal));
02591       }else{
02592         Fill(h,"recmatchPuritynoTag",purity,(bool)(evmatch==iSignal));
02593       }
02594     }
02595     Fill(h,"recmatchvtxs",nmatchvtx);
02596     if(v==recVtxs->begin()){
02597       Fill(h,"recmatchvtxsTag",nmatchvtx);
02598     }else{
02599       Fill(h,"recmatchvtxsnoTag",nmatchvtx);
02600     }
02601 
02602 
02603      
02604   } // recvtx loop
02605   Fill(h,"nrecv",nrecvtxs);
02606 
02607 
02608   // --------------------------------------- match sim to rec  ---------------------------------------
02609 
02610   int npu1=0, npu2=0;
02611 
02612   for(vector<SimEvent>::iterator ev=simEvt.begin(); ev!=simEvt.end(); ev++){
02613 
02614     if(ev->tk.size()>0) npu1++;
02615     if(ev->tk.size()>1) npu2++;
02616 
02617     bool isSignal= ev->eventId==iSignal;
02618     
02619     Fill(h,"nRecTrkInSimVtx",(double) ev->tk.size(),isSignal);
02620     Fill(h,"nPrimRecTrkInSimVtx",(double) ev->tkprim.size(),isSignal);
02621     Fill(h,"sumpt2rec",sqrt(ev->sumpt2rec),isSignal);
02622     Fill(h,"sumpt2",sqrt(ev->sumpt2),isSignal);
02623     Fill(h,"sumpt",sqrt(ev->sumpt),isSignal);
02624 
02625     double nRecVWithTrk=0;  // vertices with tracks from this simvertex
02626     double  nmatch=0, ntmatch=0, zmatch=-99;
02627 
02628     for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02629         v!=recVtxs->end(); ++v){
02630       if ( (v->ndof()<-1) || (v->chi2()<=0) ) continue;  // skip clusters 
02631       // count tracks found in both, sim and rec
02632       double n=0, wt=0;
02633       for(vector<TransientTrack>::iterator te=ev->tk.begin(); te!=ev->tk.end(); te++){
02634         const reco::Track&  RTe=te->track();
02635         for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
02636            const reco::Track & RTv=*(tv->get());  
02637            if(RTe.vz()==RTv.vz()) {n++; wt+=v->trackWeight(*tv);}
02638         }
02639       }
02640 
02641       if(n>0){  nRecVWithTrk++; }
02642       if (n > nmatch){
02643         nmatch=n; ntmatch=v->tracksSize(); zmatch=v->position().z();
02644       }
02645       
02646     }// end of reco vertex loop
02647 
02648 
02649     // nmatch is the highest number of tracks from this sim vertex found in a single reco-vertex
02650     if(ev->tk.size()>0){ Fill(h,"trkAssignmentEfficiency", nmatch/ev->tk.size(), isSignal); };
02651     if(ev->tkprim.size()>0){ Fill(h,"primtrkAssignmentEfficiency", nmatch/ev->tkprim.size(), isSignal); };
02652 
02653     // matched efficiency = efficiency for finding a reco vertex with > 50% of the simvertexs reconstructed tracks
02654 
02655     double ntsim=ev->tk.size(); // may be better to use the number of primary tracks here ?
02656     double matchpurity=nmatch/ntmatch;
02657 
02658     if(ntsim>0){
02659 
02660       Fill(h,"matchVtxFraction",nmatch/ntsim,isSignal);
02661       if(nmatch/ntsim>=0.5){
02662         Fill(h,"matchVtxEfficiency",1.,isSignal);
02663         if(ntsim>1){Fill(h,"matchVtxEfficiency2",1.,isSignal);}
02664         if(matchpurity>0.5){Fill(h,"matchVtxEfficiency5",1.,isSignal);}
02665       }else{
02666         Fill(h,"matchVtxEfficiency",0.,isSignal);
02667         if(ntsim>1){Fill(h,"matchVtxEfficiency2",0.,isSignal);}
02668         Fill(h,"matchVtxEfficiency5",0.,isSignal);  // no (matchpurity>5) here !!
02669         if(isSignal){
02670         cout << "Signal vertex not matched " <<  message << "  event=" << eventcounter_ << " nmatch=" << nmatch << "  ntsim=" << ntsim << endl;
02671         }
02672       }
02673     } // ntsim >0
02674 
02675 
02676     if(zmatch>-99){
02677       Fill(h,"matchVtxZ",zmatch-ev->z);
02678       Fill(h,"matchVtxZ",zmatch-ev->z,isSignal);
02679       Fill(h,"matchVtxZCum",fabs(zmatch-ev->z));
02680       Fill(h,"matchVtxZCum",fabs(zmatch-ev->z),isSignal);
02681     }else{
02682       Fill(h,"matchVtxZCum",1.0);
02683       Fill(h,"matchVtxZCum",1.0,isSignal);
02684     }
02685     if(fabs(zmatch-ev->z)<zmatch_){
02686       Fill(h,"matchVtxEfficiencyZ",1.,isSignal);
02687     }else{
02688       Fill(h,"matchVtxEfficiencyZ",0.,isSignal);
02689     }   
02690     
02691     if(ntsim>0) Fill(h, "matchVtxEfficiencyZ1", fabs(zmatch-ev->z)<zmatch_ , isSignal);
02692     if(ntsim>1) Fill(h, "matchVtxEfficiencyZ2", fabs(zmatch-ev->z)<zmatch_ , isSignal);
02693 
02694 
02695     Fill(h,"vtxMultiplicity",nRecVWithTrk,isSignal);
02696 
02697     // efficiency vs number of tracks, use your favorite definition of efficiency here
02698     //if(nmatch>=0.5*ntmatch){  // purity
02699     if(fabs(zmatch-ev->z)<zmatch_){  //  zmatch
02700       Fill(h,"vtxFindingEfficiencyVsNtrk",(double) ev->tk.size(),1.);
02701        if(isSignal){
02702          Fill(h,"vtxFindingEfficiencyVsNtrkSignal",ev->tk.size(),1.);
02703       }else{
02704         Fill(h,"vtxFindingEfficiencyVsNtrkPU",(double) ev->tk.size(),1.);
02705       }
02706     }else{
02707       Fill(h,"vtxFindingEfficiencyVsNtrk",(double) ev->tk.size(),0.);
02708       if(isSignal){
02709         Fill(h,"vtxFindingEfficiencyVsNtrkSignal",(double) ev->tk.size(),1.);
02710       }else{
02711         Fill(h,"vtxFindingEfficiencyVsNtrkPU",(double) ev->tk.size(),1.);
02712       }
02713     }
02714 
02715     
02716   }
02717   
02718   Fill(h,"npu1",npu1);
02719   Fill(h,"npu2",npu2);
02720 
02721   Fill(h,"nrecvsnpu",npu1,float(nrecvtxs));
02722   Fill(h,"nrecvsnpu2",npu2,float(nrecvtxs));
02723 
02724   // ---------------------------------------  sim-signal vs rec-tag  ---------------------------------------
02725   SimEvent* ev=&(simEvt[0]);
02726   const reco::Vertex* v=&(*recVtxs->begin());
02727 
02728   double n=0;
02729   for(vector<TransientTrack>::iterator te=ev->tk.begin(); te!=ev->tk.end(); te++){
02730     const reco::Track&  RTe=te->track();
02731     for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++){
02732       const reco::Track & RTv=*(tv->get());  
02733       if(RTe.vz()==RTv.vz()) {n++;}
02734     }
02735   }
02736   
02737   cout << "Number of tracks in reco tagvtx " << v->tracksSize() << endl;
02738   cout << "Number of selected tracks in sim event vtx " << ev->tk.size() << "    (prim=" << ev->tkprim.size() << ")"<<endl;
02739   cout << "Number of tracks in both         " << n << endl;
02740   double ntruthmatched=getTruthMatchedVertexTracks(*v).size();
02741   if (ntruthmatched>0){
02742     cout << "TrackPurity = "<< n/ntruthmatched <<endl;
02743     Fill(h,"TagVtxTrkPurity",n/ntruthmatched);
02744   }
02745   if (ev->tk.size()>0){
02746     cout << "TrackEfficiency = "<< n/ev->tk.size() <<endl;
02747     Fill(h,"TagVtxTrkEfficiency",n/ev->tk.size());
02748   }
02749 }
02750 
02751 /***************************************************************************************/
02752 
02753 
02754 
02755 
02756 
02757 
02758 /***************************************************************************************/
02759 
02760 void PrimaryVertexAnalyzer4PU::analyzeVertexCollection(std::map<std::string, TH1*> & h,
02761                                                  const Handle<reco::VertexCollection> recVtxs,
02762                                                  const Handle<reco::TrackCollection> recTrks, 
02763                                                  std::vector<simPrimaryVertex> & simpv,
02764                                                        const std::string message)
02765 {
02766   //cout <<"PrimaryVertexAnalyzer4PU::analyzeVertexCollection (HepMC), simpvs=" << simpv.size() << endl;
02767   int nrectrks=recTrks->size();
02768   int nrecvtxs=recVtxs->size();
02769   int nseltrks=-1; 
02770   reco::TrackCollection selTrks;   // selected tracks
02771   reco::TrackCollection lostTrks;  // selected but lost tracks (part of dropped clusters)
02772 
02773   // extract dummy vertices representing clusters
02774   reco::VertexCollection clusters;
02775   reco::Vertex allSelected;
02776   double cpufit=0;
02777   double cpuclu=0;
02778   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
02779       v!=recVtxs->end(); ++v){
02780     if ( (fabs(v->ndof()+3.)<0.0001) && (v->chi2()<=0) ){ 
02781       // this dummy vertex is for the full event
02782       allSelected=(*v);
02783       nseltrks=(allSelected.tracksSize());
02784       nrecvtxs--;
02785       cpuclu=-v->chi2();
02786       continue;
02787     }else if( (fabs(v->ndof()+2.)<0.0001) && (v->chi2()==0) ){
02788       // this is a cluster, not a vertex
02789       clusters.push_back(*v);
02790       Fill(h,"cpuvsntrk",(double) v->tracksSize(),fabs(v->y()));
02791       cpufit+=fabs(v->y());
02792       Fill(h,"nclutrkall",(double) v->tracksSize());
02793       Fill(h,"selstat",v->x());
02794       //Fill(h,"nclutrkvtx",);// see below
02795       nrecvtxs--;
02796     }
02797   }
02798   Fill(h,"cpuclu",cpuclu);
02799   Fill(h,"cpufit",cpufit);
02800   Fill(h,"cpucluvsntrk",nrectrks, cpuclu);
02801   
02802 
02803   
02804   if(simpv.size()>0){//this is mc
02805     double dsimrecx=0.;
02806     double dsimrecy=0.;//0.0011;
02807     double dsimrecz=0.;//0.0012;
02808     
02809     // vertex matching and efficiency bookkeeping
02810     int nsimtrk=0;
02811     for(std::vector<simPrimaryVertex>::iterator vsim=simpv.begin();
02812         vsim!=simpv.end(); vsim++){
02813       
02814       
02815       nsimtrk+=vsim->nGenTrk;
02816       // look for a matching reconstructed vertex
02817       vsim->recVtx=NULL;
02818       vsim->cluster=-1;
02819       
02820       for(reco::VertexCollection::const_iterator vrec=recVtxs->begin();  vrec!=recVtxs->end(); ++vrec){
02821 
02822         if( vrec->isFake() ) {
02823           continue;  // skip fake vertices (=beamspot)
02824           cout << "fake vertex" << endl;
02825         }
02826 
02827         if( vrec->ndof()<0. )continue;  // skip dummy clusters, if any
02828         //        if ( matchVertex(*vsim,*vrec) ){
02829 
02830         // if the matching critera are fulfilled, accept the rec-vertex that is closest in z
02831         if(    ((vsim->recVtx) && (fabs(vsim->recVtx->position().z()-vsim->z-dsimrecz)>fabs(vrec->z()-vsim->z-dsimrecz)))
02832                || (!vsim->recVtx) )
02833           {
02834             vsim->recVtx=&(*vrec);
02835 
02836             // find the corresponding cluster
02837             for(unsigned int iclu=0; iclu<clusters.size(); iclu++){
02838               if( fabs(clusters[iclu].position().z()-vrec->position().z()) < 0.001 ){
02839                 vsim->cluster=iclu;
02840                 vsim->nclutrk=clusters[iclu].position().y();
02841               }
02842             }
02843           }
02844 
02845         // the following only works in MC samples without pile-up
02846         if ((simpv.size()==1) && ( fabs(vsim->recVtx->position().z()-vsim->z-dsimrecz)>zmatch_ )){
02847             // now we have a recvertex without a matching simvertex, I would call this fake 
02848             // however, the G4 info does not contain pile-up
02849             Fill(h,"fakeVtxZ",vrec->z());
02850             if (vrec->ndof()>=0.5) Fill(h,"fakeVtxZNdofgt05",vrec->z());
02851             if (vrec->ndof()>=2.0) Fill(h,"fakeVtxZNdofgt2",vrec->z());
02852             Fill(h,"fakeVtxNdof",vrec->ndof());
02853             //Fill(h,"fakeVtxNdof1",vrec->ndof());
02854             Fill(h,"fakeVtxNtrk",vrec->tracksSize());
02855             if(vrec->tracksSize()==2){  Fill(h,"fake2trkchi2vsndof",vrec->ndof(),vrec->chi2());   }
02856             if(vrec->tracksSize()==3){  Fill(h,"fake3trkchi2vsndof",vrec->ndof(),vrec->chi2());   }
02857             if(vrec->tracksSize()==4){  Fill(h,"fake4trkchi2vsndof",vrec->ndof(),vrec->chi2());   }
02858             if(vrec->tracksSize()==5){  Fill(h,"fake5trkchi2vsndof",vrec->ndof(),vrec->chi2());   }
02859           }
02860       }
02861       
02862 
02863       Fill(h,"nsimtrk",float(nsimtrk));
02864       Fill(h,"nsimtrk",float(nsimtrk),vsim==simpv.begin());
02865       Fill(h,"nrecsimtrk",float(vsim->nMatchedTracks));
02866       Fill(h,"nrecnosimtrk",float(nsimtrk-vsim->nMatchedTracks));
02867       
02868       // histogram properties of matched vertices
02869       if (vsim->recVtx && ( fabs(vsim->recVtx->z()-vsim->z*simUnit_)<zmatch_ )){
02870         
02871         if(verbose_){std::cout <<"primary matched " << message << " " << setw(8) << setprecision(4) << vsim->x << " " << vsim->y << " " << vsim->z << std:: endl;}
02872         Fill(h,"matchedVtxNdof", vsim->recVtx->ndof());
02873         // residuals an pulls with respect to simulated vertex
02874         Fill(h,"resx", vsim->recVtx->x()-vsim->x*simUnit_ );
02875         Fill(h,"resy", vsim->recVtx->y()-vsim->y*simUnit_ );
02876         Fill(h,"resz", vsim->recVtx->z()-vsim->z*simUnit_ );
02877         Fill(h,"resz10", vsim->recVtx->z()-vsim->z*simUnit_ );
02878         Fill(h,"pullx", (vsim->recVtx->x()-vsim->x*simUnit_)/vsim->recVtx->xError() );
02879         Fill(h,"pully", (vsim->recVtx->y()-vsim->y*simUnit_)/vsim->recVtx->yError() );
02880         Fill(h,"pullz", (vsim->recVtx->z()-vsim->z*simUnit_)/vsim->recVtx->zError() );
02881         Fill(h,"resxr", vsim->recVtx->x()-vsim->x*simUnit_-dsimrecx);
02882         Fill(h,"resyr", vsim->recVtx->y()-vsim->y*simUnit_-dsimrecy );
02883         Fill(h,"reszr", vsim->recVtx->z()-vsim->z*simUnit_-dsimrecz);
02884         Fill(h,"pullxr", (vsim->recVtx->x()-vsim->x*simUnit_-dsimrecx)/vsim->recVtx->xError() );
02885         Fill(h,"pullyr", (vsim->recVtx->y()-vsim->y*simUnit_-dsimrecy)/vsim->recVtx->yError() );
02886         Fill(h,"pullzr", (vsim->recVtx->z()-vsim->z*simUnit_-dsimrecz)/vsim->recVtx->zError() );
02887 
02888 
02889 
02890         // efficiency with zmatch within 500 um (or whatever zmatch is)
02891         Fill(h,"eff", 1.);
02892         if(simpv.size()==1){
02893           if (vsim->recVtx==&(*recVtxs->begin())){
02894             Fill(h,"efftag", 1.); 
02895           }else{
02896             Fill(h,"efftag", 0.); 
02897             cout << "signal vertex not tagged " << message << " " << eventcounter_ << endl;
02898             // call XXClusterizerInZ.vertices(seltrks,3)
02899             
02900           }
02901         }
02902         
02903         Fill(h,"effvsptsq",vsim->ptsq,1.);
02904         Fill(h,"effvsnsimtrk",vsim->nGenTrk,1.);
02905         Fill(h,"effvsnrectrk",nrectrks,1.);
02906         Fill(h,"effvsnseltrk",nseltrks,1.);
02907         Fill(h,"effvsz",vsim->z*simUnit_,1.);
02908         Fill(h,"effvsz2",vsim->z*simUnit_,1.);
02909         Fill(h,"effvsr",sqrt(vsim->x*vsim->x+vsim->y*vsim->y)*simUnit_,1.);
02910         
02911 
02912       }else{  // no matching rec vertex found for this simvertex
02913         
02914         bool plapper=verbose_ && vsim->nGenTrk;
02915         if(plapper){
02916           // be quiet about invisble vertices
02917           std::cout << "primary not found "  << message << " " << eventcounter_ << "  x=" <<vsim->x*simUnit_ << "  y=" << vsim->y*simUnit_  << " z=" << vsim->z*simUnit_  << " nGenTrk=" << vsim->nGenTrk << std::endl;
02918         }
02919         int mistype=0;
02920         if (vsim->recVtx){
02921           if(plapper){
02922             std::cout << "nearest recvertex at " << vsim->recVtx->z() << "   dz=" << vsim->recVtx->z()-vsim->z*simUnit_ << std::endl;
02923           }
02924           
02925           if (fabs(vsim->recVtx->z()-vsim->z*simUnit_)<0.2 ){
02926             Fill(h,"effvsz2",vsim->z*simUnit_,1.);
02927           }
02928           
02929           if (fabs(vsim->recVtx->z()-vsim->z*simUnit_)<0.5 ){
02930             if(verbose_){std::cout << "type 1, lousy z vertex" << std::endl;}
02931             Fill(h,"zlost1", vsim->z*simUnit_,1.);
02932             mistype=1;
02933           }else{
02934             if(plapper){std::cout << "type 2a no vertex anywhere near" << std::endl;}
02935             mistype=2;
02936           }
02937         }else{// no recVtx at all
02938           mistype=2;
02939           if(plapper){std::cout << "type 2b, no vertex at all" << std::endl;}
02940         }
02941         
02942         if(mistype==2){
02943           int selstat=-3;
02944           // no matching vertex found, is there a cluster?
02945           for(unsigned int iclu=0; iclu<clusters.size(); iclu++){
02946             if( fabs(clusters[iclu].position().z()-vsim->z*simUnit_) < 0.1 ){
02947               selstat=int(clusters[iclu].position().x()+0.1);
02948               if(verbose_){std::cout << "matching cluster found with selstat=" << clusters[iclu].position().x() << std::endl;}
02949             }
02950           }
02951           if (selstat==0){
02952             if(plapper){std::cout << "vertex rejected (distance to beam)" << std::endl;}
02953             Fill(h,"zlost3", vsim->z*simUnit_,1.);
02954           }else if(selstat==-1){
02955             if(plapper) {std::cout << "vertex invalid" << std::endl;}
02956             Fill(h,"zlost4", vsim->z*simUnit_,1.);
02957           }else if(selstat==1){
02958             if(plapper){std::cout << "vertex accepted, this cannot be right!!!!!!!!!!" << std::endl;}
02959           }else if(selstat==-2){
02960             if(plapper){std::cout << "dont know what this means !!!!!!!!!!" << std::endl;}
02961           }else if(selstat==-3){
02962             if(plapper){std::cout << "no matching cluster found " << std::endl;}
02963             Fill(h,"zlost2", vsim->z*simUnit_,1.);
02964           }else{
02965             if(plapper){std::cout << "dont know what this means either !!!!!!!!!!" << selstat << std::endl;}
02966           }
02967         }//
02968         
02969         
02970         Fill(h,"eff", 0.);
02971         if(simpv.size()==1){ Fill(h,"efftag", 0.); }
02972         
02973         Fill(h,"effvsptsq",vsim->ptsq,0.);
02974         Fill(h,"effvsnsimtrk",float(vsim->nGenTrk),0.);
02975         Fill(h,"effvsnrectrk",nrectrks,0.);
02976         Fill(h,"effvsnseltrk",nseltrks,0.);
02977         Fill(h,"effvsz",vsim->z*simUnit_,0.);
02978         Fill(h,"effvsr",sqrt(vsim->x*vsim->x+vsim->y*vsim->y)*simUnit_,0.);
02979         
02980       } // no recvertex for this simvertex
02981 
02982     }
02983 
02984     // end of sim/rec matching
02985    
02986      
02987    // purity of event vertex tags
02988     if (recVtxs->size()>0){
02989       Double_t dz=(*recVtxs->begin()).z() - (*simpv.begin()).z*simUnit_;
02990       Fill(h,"zdistancetag",dz);
02991       Fill(h,"abszdistancetag",fabs(dz));
02992       if( fabs(dz)<zmatch_){
02993         Fill(h,"puritytag",1.);
02994       }else{
02995         // bad tag: the true primary was more than 500 um (or zmatch) away from the tagged primary
02996         Fill(h,"puritytag",0.);
02997       }
02998     }
02999     
03000   }else{
03001     //if(verbose_) cout << "PrimaryVertexAnalyzer4PU::analyzeVertexCollection:  simPV is empty!" << endl;
03002   }
03003 
03004 
03005   //******* the following code does not require MC and will/should work for data **********
03006 
03007 
03008   Fill(h,"bunchCrossing",bunchCrossing_);
03009   if(recTrks->size()>0)  Fill(h,"bunchCrossingLogNtk",bunchCrossing_,log(recTrks->size())/log(10.));
03010   
03011   // -----------------  reconstructed tracks  ------------------------
03012   // the list of selected tracks can only be correct if the selection parameterset  and track collection
03013   // is the same that was used for the reconstruction
03014 
03015   int nt=0;
03016   for(reco::TrackCollection::const_iterator t=recTrks->begin();
03017       t!=recTrks->end(); ++t){
03018     if((recVtxs->size()>0) && (recVtxs->begin()->isValid())){
03019       fillTrackHistos(h,"all",*t,&(*recVtxs->begin()));
03020     }else{
03021       fillTrackHistos(h,"all",*t);
03022     }
03023     if(recTrks->size()>100)    fillTrackHistos(h,"M",*t);
03024 
03025 
03026 
03027     TransientTrack  tt = theB_->build(&(*t));  tt.setBeamSpot(vertexBeamSpot_);
03028     if  (theTrackFilter(tt)){
03029       selTrks.push_back(*t);
03030       fillTrackHistos(h,"sel",*t);
03031       int foundinvtx=0;
03032       int nvtemp=-1;
03033       for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
03034           v!=recVtxs->end(); ++v){
03035         nvtemp++;
03036         if(( v->isFake()) || (v->ndof()<-2) ) break;
03037         for(trackit_t tv=v->tracks_begin(); tv!=v->tracks_end(); tv++ ){
03038           if( ((**tv).vz()==t->vz()&&((**tv).phi()==t->phi())) ) {
03039             foundinvtx++;
03040           }
03041         }
03042         
03043       }
03044       if(foundinvtx==0){
03045         fillTrackHistos(h,"sellost",*t);
03046       }else if(foundinvtx>1){
03047         cout << "hmmmm " << foundinvtx << endl;
03048       }
03049     }
03050     nt++;
03051   }
03052 
03053 
03054   if (nseltrks<0){
03055     nseltrks=selTrks.size();
03056   }else if( ! (nseltrks==(int)selTrks.size()) ){
03057     std::cout << "Warning: inconsistent track selection !" << std::endl;
03058   }
03059 
03060 
03061 
03062   // fill track histograms of vertex tracks
03063   int nrec=0,  nrec0=0, nrec8=0, nrec2=0, nrec4=0;
03064   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
03065       v!=recVtxs->end(); ++v){
03066     
03067     if (! (v->isFake()) && v->ndof()>0 && v->chi2()>0 ){
03068       nrec++;
03069       if (v->ndof()>0) nrec0++;
03070       if (v->ndof()>8) nrec8++;
03071       if (v->ndof()>2) nrec2++;
03072       if (v->ndof()>4) nrec4++;
03073       for(trackit_t t=v->tracks_begin(); t!=v->tracks_end(); t++){
03074         if(v==recVtxs->begin()){
03075           fillTrackHistos(h,"tagged",**t,  &(*v));
03076         }else{
03077           fillTrackHistos(h,"untagged",**t,  &(*v));
03078         }
03079 
03080         Float_t wt=v->trackWeight(*t);
03081         //dumpHitInfo(**t); cout << "  w=" << wt << endl;
03082         Fill(h,"trackWt",wt);
03083         if(wt>0.5){
03084           fillTrackHistos(h,"wgt05",**t, &(*v));
03085           if(v->ndof()>4) fillTrackHistos(h,"ndof4",**t, &(*v));
03086         }else{
03087           fillTrackHistos(h,"wlt05",**t, &(*v));
03088         }
03089       }
03090     }
03091   }
03092 
03093 
03094   // bachelor tracks (only available through clusters right now)
03095   for(unsigned int iclu=0; iclu<clusters.size(); iclu++){
03096     if (clusters[iclu].tracksSize()==1){
03097       for(trackit_t t = clusters[iclu].tracks_begin(); 
03098           t!=clusters[iclu].tracks_end(); t++){
03099         fillTrackHistos(h,"bachelor",**t);
03100       }
03101     }
03102   }
03103 
03104 
03105   // -----------------  reconstructed vertices  ------------------------
03106 
03107   // event 
03108   Fill(h,"szRecVtx",recVtxs->size());
03109   Fill(h,"nclu",clusters.size());
03110   Fill(h,"nseltrk",nseltrks);
03111   Fill(h,"nrectrk",nrectrks);
03112   Fill(h,"nrecvtx",nrec);
03113   Fill(h,"nrecvtx2",nrec2);
03114   Fill(h,"nrecvtx4",nrec4);
03115   Fill(h,"nrecvtx8",nrec8);
03116 
03117   if(nrec>0){
03118     Fill(h,"eff0vsntrec",nrectrks,1.);
03119     Fill(h,"eff0vsntsel",nseltrks,1.);
03120   }else{
03121     Fill(h,"eff0vsntrec",nrectrks,0.);
03122     Fill(h,"eff0vsntsel",nseltrks,0.);
03123     if((nseltrks>1)&&(verbose_)){
03124       cout << Form("PrimaryVertexAnalyzer4PU: %s may have lost a vertex  %10d  %10d     %4d / %4d ",message.c_str(),run_, event_, nrectrks,nseltrks) << endl;
03125       dumpThisEvent_=true;
03126     }
03127   }
03128   if(nrec0>0) { Fill(h,"eff0ndof0vsntsel",nseltrks,1.);}else{ Fill(h,"eff0ndof0vsntsel",nseltrks,0.);}
03129   if(nrec2>0) { Fill(h,"eff0ndof2vsntsel",nseltrks,1.);}else{ Fill(h,"eff0ndof2vsntsel",nseltrks,0.);}
03130   if(nrec4>0) { Fill(h,"eff0ndof4vsntsel",nseltrks,1.);}else{ Fill(h,"eff0ndof4vsntsel",nseltrks,0.);}
03131   if(nrec8>0) { Fill(h,"eff0ndof8vsntsel",nseltrks,1.);}else{ Fill(h,"eff0ndof8vsntsel",nseltrks,0.);}
03132 
03133   if((nrec>1)&&(DEBUG_)) {
03134     cout << "multivertex event" << endl;
03135     dumpThisEvent_=true;
03136   }
03137 
03138   if((nrectrks>10)&&(nseltrks<3)){
03139     cout << "small fraction of selected tracks "  << endl;
03140     dumpThisEvent_=true;
03141   }
03142 
03143   // properties of events without a vertex
03144   if((nrec==0)||(recVtxs->begin()->isFake())){
03145     Fill(h,"nrectrk0vtx",nrectrks);
03146     Fill(h,"nseltrk0vtx",nseltrks);
03147     Fill(h,"nclu0vtx",clusters.size());
03148   }
03149 
03150 
03151   //  properties of (valid) vertices
03152   double ndof2=-10,ndof1=-10, zndof1=0, zndof2=0;
03153   for(reco::VertexCollection::const_iterator v=recVtxs->begin(); 
03154       v!=recVtxs->end(); ++v){
03155     if(v->isFake()){ Fill(h,"isFake",1.);}else{ Fill(h,"isFake",0.);}
03156     if(v->isFake()||((v->ndof()<0)&&(v->ndof()>-3))){ Fill(h,"isFake1",1.);}else{ Fill(h,"isFake1",0.);}
03157 
03158     if((v->isFake())||(v->ndof()<0)) continue;
03159     if(v->ndof()>ndof1){ ndof2=ndof1; zndof2=zndof1; ndof1=v->ndof(); zndof1=v->position().z();}
03160     else if(v->ndof()>ndof2){ ndof2=v->ndof(); zndof2=v->position().z();}
03161 
03162 
03163     // some special histogram for two track vertices
03164     if(v->tracksSize()==2){
03165       const TrackBaseRef& t1= *(v->tracks_begin());
03166       const TrackBaseRef& t2=*(v->tracks_begin()+1);
03167       bool os=(t1->charge()*t2->charge()<0);
03168       double dphi=t1->phi()-t2->phi(); if (dphi<0) dphi+=2*M_PI;
03169       double m12=sqrt(pow( sqrt(pow(0.139,2)+pow( t1->p(),2)) +sqrt(pow(0.139,2)+pow( t2->p(),2)) ,2)
03170                              -pow(t1->px()+t2->px(),2)
03171                              -pow(t1->py()+t2->py(),2)
03172                              -pow(t1->pz()+t2->pz(),2)
03173                     );
03174       if(os){
03175         Fill(h,"2trkdetaOS",t1->eta()-t2->eta());
03176         Fill(h,"2trkmassOS",m12);
03177       }else{
03178         Fill(h,"2trkdetaSS",t1->eta()-t2->eta());
03179         Fill(h,"2trkmassSS",m12);
03180       }
03181       Fill(h,"2trkdphi",dphi);
03182       Fill(h,"2trkseta",t1->eta()+t2->eta());
03183       if(fabs(dphi-M_PI)<0.1)      Fill(h,"2trksetacurl",t1->eta()+t2->eta());
03184       if(fabs(t1->eta()+t2->eta())<0.1) Fill(h,"2trkdphicurl",dphi);
03185       // fill separately for extra vertices
03186       if(v!=recVtxs->begin()){
03187         if(os){
03188           Fill(h,"2trkdetaOSPU",t1->eta()-t2->eta());
03189           Fill(h,"2trkmassOSPU",m12);
03190         }else{
03191           Fill(h,"2trkdetaSSPU",t1->eta()-t2->eta());
03192           Fill(h,"2trkmassSSPU",m12);
03193         }
03194         Fill(h,"2trkdphiPU",dphi);
03195         Fill(h,"2trksetaPU",t1->eta()+t2->eta());
03196         if(fabs(dphi-M_PI)<0.1)      Fill(h,"2trksetacurlPU",t1->eta()+t2->eta());
03197         if(fabs(t1->eta()+t2->eta())<0.1) Fill(h,"2trkdphicurlPU",dphi);
03198       }
03199     }
03200 
03201 
03202     Fill(h,"trkchi2vsndof",v->ndof(),v->chi2());
03203     if(v->ndof()>0){    Fill(h,"trkchi2overndof",v->chi2()/v->ndof()); }
03204     if(v->tracksSize()==2){  Fill(h,"2trkchi2vsndof",v->ndof(),v->chi2());   }
03205     if(v->tracksSize()==3){  Fill(h,"3trkchi2vsndof",v->ndof(),v->chi2());   }
03206     if(v->tracksSize()==4){  Fill(h,"4trkchi2vsndof",v->ndof(),v->chi2());   }
03207     if(v->tracksSize()==5){  Fill(h,"5trkchi2vsndof",v->ndof(),v->chi2());   }
03208 
03209     Fill(h,"nbtksinvtx",v->tracksSize());
03210     Fill(h,"nbtksinvtx2",v->tracksSize());
03211     Fill(h,"vtxchi2",v->chi2());
03212     Fill(h,"vtxndf",v->ndof());
03213     Fill(h,"vtxprob",ChiSquaredProbability(v->chi2() ,v->ndof()));
03214     Fill(h,"vtxndfvsntk",v->tracksSize(), v->ndof());
03215     Fill(h,"vtxndfoverntk",v->ndof()/v->tracksSize());
03216     Fill(h,"vtxndf2overntk",(v->ndof()+2)/v->tracksSize());
03217     Fill(h,"zrecvsnt",v->position().z(),float(nrectrks));
03218     if(nrectrks>100){
03219       Fill(h,"zrecNt100",v->position().z());
03220     }
03221 
03222     if(v->ndof()>2.0){  // enter only vertices that really contain tracks
03223       Fill(h,"xrec",v->position().x());
03224       Fill(h,"yrec",v->position().y());
03225       Fill(h,"zrec",v->position().z());
03226       Fill(h,"xrec1",v->position().x());
03227       Fill(h,"yrec1",v->position().y());
03228       Fill(h,"zrec1",v->position().z());
03229       Fill(h,"xrec2",v->position().x());
03230       Fill(h,"yrec2",v->position().y());
03231       Fill(h,"zrec2",v->position().z());
03232       Fill(h,"xrec3",v->position().x());
03233       Fill(h,"yrec3",v->position().y());
03234       Fill(h,"zrec3",v->position().z());
03235       Fill(h,"xrecb",v->position().x()-vertexBeamSpot_.x0());
03236       Fill(h,"yrecb",v->position().y()-vertexBeamSpot_.y0());
03237       Fill(h,"zrecb",v->position().z()-vertexBeamSpot_.z0());
03238       Fill(h,"xrecBeam",v->position().x()-vertexBeamSpot_.x0());
03239       Fill(h,"yrecBeam",v->position().y()-vertexBeamSpot_.y0());
03240       Fill(h,"zrecBeam",v->position().z()-vertexBeamSpot_.z0());
03241       Fill(h,"xrecBeamPull",(v->position().x()-vertexBeamSpot_.x0())/sqrt(pow(v->xError(),2)+pow(vertexBeamSpot_.BeamWidthX(),2)));
03242       Fill(h,"yrecBeamPull",(v->position().y()-vertexBeamSpot_.y0())/sqrt(pow(v->yError(),2)+pow(vertexBeamSpot_.BeamWidthY(),2)));
03243       
03244       Fill(h,"xrecBeamvsdx",v->xError(),v->position().x()-vertexBeamSpot_.x0());
03245       Fill(h,"yrecBeamvsdy",v->yError(),v->position().y()-vertexBeamSpot_.y0());
03246       Fill(h,"xrecBeamvsdxR2",v->position().x()-vertexBeamSpot_.x0(),v->xError());
03247       Fill(h,"yrecBeamvsdyR2",v->position().y()-vertexBeamSpot_.y0(),v->yError());
03248       Fill(h,"xrecBeam2vsdx2prof",pow(v->xError(),2),pow(v->position().x()-vertexBeamSpot_.x0(),2));
03249       Fill(h,"yrecBeam2vsdy2prof",pow(v->yError(),2),pow(v->position().y()-vertexBeamSpot_.y0(),2));
03250       Fill(h,"xrecBeamvsdx2",pow(v->xError(),2),pow(v->position().x()-vertexBeamSpot_.x0(),2));
03251       Fill(h,"yrecBeamvsdy2",pow(v->yError(),2),pow(v->position().y()-vertexBeamSpot_.y0(),2));
03252       Fill(h,"xrecBeamvsz",v->position().z(),v->position().x()-vertexBeamSpot_.x0());
03253       Fill(h,"yrecBeamvsz",v->position().z(),v->position().y()-vertexBeamSpot_.y0());
03254       Fill(h,"xrecBeamvszprof",v->position().z(),v->position().x()-vertexBeamSpot_.x0());
03255       Fill(h,"yrecBeamvszprof",v->position().z(),v->position().y()-vertexBeamSpot_.y0());
03256       Fill(h,"xrecBeamvsdxprof",v->xError(),v->position().x()-vertexBeamSpot_.x0());
03257       Fill(h,"yrecBeamvsdyprof",v->yError(),v->position().y()-vertexBeamSpot_.y0());
03258       
03259       
03260       Fill(h,"errx",v->xError());
03261       Fill(h,"erry",v->yError());
03262       Fill(h,"errz",v->zError());
03263       double vxx=v->covariance(0,0);
03264       double vyy=v->covariance(1,1);
03265       double vxy=v->covariance(1,0);
03266       double dv=0.25*(vxx+vyy)*(vxx+vyy-(vxx*vyy-vxy*vxy));
03267       if(dv>0){
03268         double l1=0.5*(vxx+vyy)+sqrt(dv);
03269         Fill(h,"err1",sqrt(l1));
03270         double l2=sqrt(0.5*(vxx+vyy)-sqrt(dv));
03271         if(l2>0) Fill(h,"err2",sqrt(l2));
03272       }
03273       
03274       
03275       // look at the tagged vertex separately
03276       if (v==recVtxs->begin()){
03277         Fill(h,"nbtksinvtxTag",v->tracksSize());
03278         Fill(h,"nbtksinvtxTag2",v->tracksSize());
03279         Fill(h,"xrectag",v->position().x());
03280         Fill(h,"yrectag",v->position().y());
03281         Fill(h,"zrectag",v->position().z());
03282       }else{
03283         Fill(h,"nbtksinvtxPU",v->tracksSize());
03284         Fill(h,"nbtksinvtxPU2",v->tracksSize());
03285       }
03286         
03287       // vertex resolution vs number of tracks
03288       Fill(h,"xresvsntrk",v->tracksSize(),v->xError());
03289       Fill(h,"yresvsntrk",v->tracksSize(),v->yError());
03290       Fill(h,"zresvsntrk",v->tracksSize(),v->zError());
03291       
03292     }
03293     
03294     // cluster properties (if available)
03295     for(unsigned int iclu=0; iclu<clusters.size(); iclu++){
03296       if( fabs(clusters[iclu].position().z()-v->position().z()) < 0.0001 ){
03297         Fill(h,"nclutrkvtx",clusters[iclu].tracksSize());
03298       }
03299     }
03300     
03301 
03302         
03303     //  properties of (valid) neighbour vertices
03304     reco::VertexCollection::const_iterator v1=v;     v1++;
03305     for(; v1!=recVtxs->end(); ++v1){
03306       if((v1->isFake())||(v1->ndof()<0)) continue;
03307       Fill(h,"zdiffrec",v->position().z()-v1->position().z());
03308 //       if(fabs(v->position().z()-v1->position().z())>1){
03309 //      cout << message << " zdiffrec=" << v->position().z()-v1->position().z() << " " << v->ndof() << " " << v1->ndof() << endl;
03310 //      dumpThisEvent_=true;
03311 //       }
03312 
03313       double z0=v->position().z()-vertexBeamSpot_.z0();
03314       double z1=v1->position().z()-vertexBeamSpot_.z0();
03315       Fill(h,"zPUcand",z0);     Fill(h,"zPUcand",z1);
03316       Fill(h,"ndofPUcand",v->ndof()); Fill(h,"ndofPUcand",v1->ndof());
03317       
03318       Fill(h,"zdiffvsz",z1-z0,0.5*(z1+z0));
03319 
03320       if ((v->ndof()>2) && (v1->ndof()>2)){
03321         Fill(h,"zdiffrec2",v->position().z()-v1->position().z());
03322         Fill(h,"zPUcand2",z0);
03323         Fill(h,"zPUcand2",z1);
03324         Fill(h,"ndofPUcand2",v->ndof());
03325         Fill(h,"ndofPUcand2",v1->ndof());
03326         Fill(h,"zvszrec2",z0, z1);
03327         Fill(h,"pzvspz2",TMath::Freq(z0/2.16),TMath::Freq(z1/2.16) );
03328       }
03329       
03330       if ((v->ndof()>4) && (v1->ndof()>4)){
03331         Fill(h,"zdiffvsz4",z1-z0,0.5*(z1+z0));
03332         Fill(h,"zdiffrec4",v->position().z()-v1->position().z());
03333         Fill(h,"zvszrec4",z0, z1);
03334         Fill(h,"pzvspz4",TMath::Freq(z0/2.16),TMath::Freq(z1/2.16) );
03335         //cout << "ndof4 pu-candidate " << run_ << " " << event_ << endl ;
03336         if(fabs(z0-z1)>1.0){
03337           Fill(h,"xbeamPUcand",v->position().x()-vertexBeamSpot_.x0());
03338           Fill(h,"ybeamPUcand",v->position().y()-vertexBeamSpot_.y0());
03339           Fill(h,"xbeamPullPUcand",(v->position().x()-vertexBeamSpot_.x0())/v->xError());
03340           Fill(h,"ybeamPullPUcand",(v->position().y()-vertexBeamSpot_.y0())/v->yError());
03341           //Fill(h,"sumwOverNtkPUcand",sumw/v->tracksSize());
03342           //Fill(h,"sumwOverNtkPUcand",sumw/v1->tracksSize());
03343           Fill(h,"ndofOverNtkPUcand",v->ndof()/v->tracksSize());
03344           Fill(h,"ndofOverNtkPUcand",v1->ndof()/v1->tracksSize());
03345           Fill(h,"xbeamPUcand",v1->position().x()-vertexBeamSpot_.x0());
03346           Fill(h,"ybeamPUcand",v1->position().y()-vertexBeamSpot_.y0());
03347           Fill(h,"xbeamPullPUcand",(v1->position().x()-vertexBeamSpot_.x0())/v1->xError());
03348           Fill(h,"ybeamPullPUcand",(v1->position().y()-vertexBeamSpot_.y0())/v1->yError());
03349           Fill(h,"zPUcand4",z0);
03350           Fill(h,"zPUcand4",z1);
03351           Fill(h,"ndofPUcand4",v->ndof());
03352           Fill(h,"ndofPUcand4",v1->ndof());
03353           for(trackit_t t=v->tracks_begin(); t!=v->tracks_end(); t++){ if(v->trackWeight(*t)>0.5) fillTrackHistos(h,"PUcand",**t, &(*v));  }
03354           for(trackit_t t=v1->tracks_begin(); t!=v1->tracks_end(); t++){ if(v1->trackWeight(*t)>0.5) fillTrackHistos(h,"PUcand",**t, &(*v1));   }
03355         }
03356         }
03357       
03358       if ((v->ndof()>4) && (v1->ndof()>2) && (v1->ndof()<4)){
03359         for(trackit_t t=v1->tracks_begin(); t!=v1->tracks_end(); t++){ if(v1->trackWeight(*t)>0.5) fillTrackHistos(h,"PUfake",**t, &(*v1));   }
03360       }
03361       
03362       if ((v->ndof()>8) && (v1->ndof()>8)){
03363         Fill(h,"zdiffrec8",v->position().z()-v1->position().z());
03364         if(dumpPUcandidates_ && fabs(z0-z1)>1.0){
03365           cout << "PU candidate " << run_ << " " << event_ << " " << message <<  " zdiff=" <<z0-z1 << endl;
03366 //      cerr << "PU candidate " << run_ << " "<<  event_ << " " << message <<  endl;
03367           dumpThisEvent_=true;
03368         }
03369       }
03370 
03371     }
03372     
03373     // test track links, use reconstructed vertices
03374       bool problem = false;
03375       Fill(h,"nans",1.,isnan(v->position().x())*1.);
03376       Fill(h,"nans",2.,isnan(v->position().y())*1.);
03377       Fill(h,"nans",3.,isnan(v->position().z())*1.);
03378       
03379       int index = 3;
03380       for (int i = 0; i != 3; i++) {
03381         for (int j = i; j != 3; j++) {
03382           index++;
03383           Fill(h,"nans",index*1., isnan(v->covariance(i, j))*1.);
03384           if (isnan(v->covariance(i, j))) problem = true;
03385           // in addition, diagonal element must be positive
03386           if (j == i && v->covariance(i, j) < 0) {
03387             Fill(h,"nans",index*1., 1.);
03388             problem = true;
03389           }
03390         }
03391       }
03392       
03393       try {
03394         for(trackit_t t = v->tracks_begin(); 
03395             t!=v->tracks_end(); t++) {
03396           // illegal charge
03397           if ( (**t).charge() < -1 || (**t).charge() > 1 ) {
03398             Fill(h,"tklinks",0.);
03399           }
03400           else {
03401             Fill(h,"tklinks",1.);
03402           }
03403         }
03404       } catch (...) {
03405         // exception thrown when trying to use linked track
03406         Fill(h,"tklinks",0.);
03407       }
03408 
03409       if (problem) {
03410         // analyze track parameter covariance definiteness
03411         double data[25];
03412         try {
03413           int itk = 0;
03414           for(trackit_t t = v->tracks_begin(); 
03415               t!=v->tracks_end(); t++) {
03416             std::cout << "Track " << itk++ << std::endl;
03417             int i2 = 0;
03418             for (int i = 0; i != 5; i++) {
03419               for (int j = 0; j != 5; j++) {
03420                 data[i2] = (**t).covariance(i, j);
03421                 std::cout << std:: scientific << data[i2] << " ";
03422                 i2++;
03423               }
03424               std::cout << std::endl;
03425             }
03426             gsl_matrix_view m 
03427               = gsl_matrix_view_array (data, 5, 5);
03428             
03429             gsl_vector *eval = gsl_vector_alloc (5);
03430             gsl_matrix *evec = gsl_matrix_alloc (5, 5);
03431             
03432             gsl_eigen_symmv_workspace * w = 
03433               gsl_eigen_symmv_alloc (5);
03434             
03435             gsl_eigen_symmv (&m.matrix, eval, evec, w);
03436             
03437             gsl_eigen_symmv_free (w);
03438             
03439             gsl_eigen_symmv_sort (eval, evec, 
03440                                   GSL_EIGEN_SORT_ABS_ASC);
03441             
03442             // print sorted eigenvalues
03443           {
03444             int i;
03445             for (i = 0; i < 5; i++) {
03446               double eval_i 
03447                 = gsl_vector_get (eval, i);
03448               gsl_vector_view evec_i 
03449                 = gsl_matrix_column (evec, i);
03450               
03451               printf ("eigenvalue = %g\n", eval_i);
03452               //              printf ("eigenvector = \n");
03453               //              gsl_vector_fprintf (stdout, 
03454               //                                  &evec_i.vector, "%g");
03455             }
03456           }
03457           }
03458         }
03459       catch (...) {
03460         // exception thrown when trying to use linked track
03461         break;
03462       }// catch()
03463       }// if (problem)
03464 
03465 
03466     
03467   }  // vertex loop (v)
03468 
03469 
03470   // 2nd highest ndof
03471   if (ndof2>0){
03472       Fill(h,"ndofnr2",ndof2); 
03473       if(fabs(zndof1-zndof2)>1) Fill(h,"ndofnr2d1cm",ndof2); 
03474       if(fabs(zndof1-zndof2)>2) Fill(h,"ndofnr2d2cm",ndof2); 
03475   }
03476 
03477 
03478 }
03479