00001 #include "Validation/RecoTrack/interface/TrackerSeedValidator.h"
00002 #include "DQMServices/ClientConfig/interface/FitSlicesYTool.h"
00003
00004 #include "FWCore/Framework/interface/MakerMacros.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006
00007 #include "DataFormats/TrackReco/interface/Track.h"
00008 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00009 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00010 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00011 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00012 #include "SimTracker/TrackAssociation/interface/TrackAssociatorByChi2.h"
00013 #include "SimTracker/TrackAssociation/interface/QuickTrackAssociatorByHits.h"
00014 #include "SimTracker/TrackerHitAssociation/interface/TrackerHitAssociator.h"
00015 #include "SimTracker/Records/interface/TrackAssociatorRecord.h"
00016 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
00017 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00018 #include "TrackingTools/PatternTools/interface/TSCBLBuilderNoMaterial.h"
00019 #include "TrackingTools/TrajectoryState/interface/PerigeeConversions.h"
00020 #include "TrackingTools/Records/interface/TransientRecHitRecord.h"
00021 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00022 #include "Validation/RecoTrack/interface/MTVHistoProducerAlgoFactory.h"
00023 #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertex.h"
00024 #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertexContainer.h"
00025 #include "SimDataFormats/PileupSummaryInfo/interface/PileupSummaryInfo.h"
00026 #include "SimDataFormats/EncodedEventId/interface/EncodedEventId.h"
00027 #include "SimTracker/TrackAssociation/plugins/ParametersDefinerForTPESProducer.h"
00028
00029 #include <TF1.h>
00030
00031 using namespace std;
00032 using namespace edm;
00033
00034 typedef edm::Ref<edm::HepMCProduct, HepMC::GenParticle > GenParticleRef;
00035
00036 TrackerSeedValidator::TrackerSeedValidator(const edm::ParameterSet& pset):MultiTrackValidatorBase(pset){
00037
00038
00039 ParameterSet psetForHistoProducerAlgo = pset.getParameter<ParameterSet>("histoProducerAlgoBlock");
00040 string histoProducerAlgoName = psetForHistoProducerAlgo.getParameter<string>("ComponentName");
00041 histoProducerAlgo_ = MTVHistoProducerAlgoFactory::get()->create(histoProducerAlgoName ,psetForHistoProducerAlgo);
00042 histoProducerAlgo_->setDQMStore(dbe_);
00043
00044 dirName_ = pset.getParameter<std::string>("dirName");
00045
00046 tpSelector = TrackingParticleSelector(pset.getParameter<double>("ptMinTP"),
00047 pset.getParameter<double>("minRapidityTP"),
00048 pset.getParameter<double>("maxRapidityTP"),
00049 pset.getParameter<double>("tipTP"),
00050 pset.getParameter<double>("lipTP"),
00051 pset.getParameter<int>("minHitTP"),
00052 pset.getParameter<bool>("signalOnlyTP"),
00053 pset.getParameter<bool>("chargedOnlyTP"),
00054 pset.getParameter<bool>("stableOnlyTP"),
00055 pset.getParameter<std::vector<int> >("pdgIdTP"));
00056
00057 runStandalone = pset.getParameter<bool>("runStandalone");
00058
00059 builderName = pset.getParameter<std::string>("TTRHBuilder");
00060 }
00061
00062 TrackerSeedValidator::~TrackerSeedValidator(){delete histoProducerAlgo_;}
00063
00064 void TrackerSeedValidator::beginRun(edm::Run const&, edm::EventSetup const& setup) {
00065 setup.get<IdealMagneticFieldRecord>().get(theMF);
00066 setup.get<TransientRecHitRecord>().get(builderName,theTTRHBuilder);
00067
00068 for (unsigned int ww=0;ww<associators.size();ww++){
00069 for (unsigned int www=0;www<label.size();www++){
00070
00071 dbe_->cd();
00072 InputTag algo = label[www];
00073 string dirName=dirName_;
00074 if (algo.process()!="")
00075 dirName+=algo.process()+"_";
00076 if(algo.label()!="")
00077 dirName+=algo.label()+"_";
00078 if(algo.instance()!="")
00079 dirName+=algo.instance()+"_";
00080
00081
00082
00083 string assoc= associators[ww];
00084 if (assoc.find("Track")<assoc.length()){
00085 assoc.replace(assoc.find("Track"),5,"");
00086 }
00087 dirName+=assoc;
00088 std::replace(dirName.begin(), dirName.end(), ':', '_');
00089
00090 dbe_->setCurrentFolder(dirName.c_str());
00091
00092
00093
00094 histoProducerAlgo_->initialize();
00095
00096 dbe_->goUp();
00097 string subDirName = dirName + "/simulation";
00098 dbe_->setCurrentFolder(subDirName.c_str());
00099
00100
00101 histoProducerAlgo_->bookSimHistos();
00102
00103 dbe_->cd();
00104 dbe_->setCurrentFolder(dirName.c_str());
00105
00106
00107 histoProducerAlgo_->bookRecoHistos();
00108 if (runStandalone) histoProducerAlgo_->bookRecoHistosForStandaloneRunning();
00109 }
00110 edm::ESHandle<TrackAssociatorBase> theAssociator;
00111 for (unsigned int w=0;w<associators.size();w++) {
00112 setup.get<TrackAssociatorRecord>().get(associators[w],theAssociator);
00113 associator.push_back( theAssociator.product() );
00114 }
00115 }
00116 }
00117
00118 void TrackerSeedValidator::analyze(const edm::Event& event, const edm::EventSetup& setup){
00119
00120 edm::LogInfo("TrackValidator") << "\n====================================================" << "\n"
00121 << "Analyzing new event" << "\n"
00122 << "====================================================\n" << "\n";
00123
00124 edm::ESHandle<ParametersDefinerForTP> parametersDefinerTP;
00125 setup.get<TrackAssociatorRecord>().get(parametersDefiner,parametersDefinerTP);
00126
00127 edm::Handle<TrackingParticleCollection> TPCollectionHeff ;
00128 event.getByLabel(label_tp_effic,TPCollectionHeff);
00129 const TrackingParticleCollection tPCeff = *(TPCollectionHeff.product());
00130
00131 edm::Handle<TrackingParticleCollection> TPCollectionHfake ;
00132 event.getByLabel(label_tp_fake,TPCollectionHfake);
00133 const TrackingParticleCollection tPCfake = *(TPCollectionHfake.product());
00134
00135 if (tPCeff.size()==0) {edm::LogInfo("TrackValidator") << "TP Collection for efficiency studies has size = 0! Skipping Event." ; return;}
00136 if (tPCfake.size()==0) {edm::LogInfo("TrackValidator") << "TP Collection for fake rate studies has size = 0! Skipping Event." ; return;}
00137
00138 edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00139 event.getByLabel(bsSrc,recoBeamSpotHandle);
00140 reco::BeamSpot bs = *recoBeamSpotHandle;
00141
00142 edm::Handle< vector<PileupSummaryInfo> > puinfoH;
00143 event.getByLabel(label_pileupinfo,puinfoH);
00144 PileupSummaryInfo puinfo;
00145
00146 for (unsigned int puinfo_ite=0;puinfo_ite<(*puinfoH).size();++puinfo_ite){
00147 if ((*puinfoH)[puinfo_ite].getBunchCrossing()==0){
00148 puinfo=(*puinfoH)[puinfo_ite];
00149 break;
00150 }
00151 }
00152
00153 edm::Handle<TrackingVertexCollection> tvH;
00154 event.getByLabel(label_tv,tvH);
00155 TrackingVertexCollection tv = *tvH;
00156
00157 int w=0;
00158 for (unsigned int ww=0;ww<associators.size();ww++){
00159 for (unsigned int www=0;www<label.size();www++){
00160 edm::LogVerbatim("TrackValidator") << "Analyzing "
00161 << label[www].process()<<":"
00162 << label[www].label()<<":"
00163 << label[www].instance()<<" with "
00164 << associators[ww].c_str() <<"\n";
00165
00166
00167
00168 edm::Handle<edm::View<TrajectorySeed> > seedCollection;
00169 event.getByLabel(label[www], seedCollection);
00170 if (seedCollection->size()==0) {
00171 edm::LogInfo("TrackValidator") << "SeedCollection size = 0!" ;
00172 continue;
00173 }
00174
00175
00176 LogTrace("TrackValidator") << "Calling associateRecoToSim method" << "\n";
00177 reco::RecoToSimCollectionSeed recSimColl=associator[ww]->associateRecoToSim(seedCollection,
00178 TPCollectionHfake,
00179 &event,&setup);
00180 LogTrace("TrackValidator") << "Calling associateSimToReco method" << "\n";
00181 reco::SimToRecoCollectionSeed simRecColl=associator[ww]->associateSimToReco(seedCollection,
00182 TPCollectionHeff,
00183 &event,&setup);
00184
00185
00186
00187
00188
00189 edm::LogVerbatim("TrackValidator") << "\n# of TrackingParticles: " << tPCeff.size() << "\n";
00190 int ats(0);
00191 int st(0);
00192 unsigned sts(0);
00193 unsigned asts(0);
00194 for (TrackingParticleCollection::size_type i=0; i<tPCeff.size(); i++){
00195 TrackingParticleRef tp(TPCollectionHeff, i);
00196
00197 if (tp->charge()==0) continue;
00198
00199 if(! tpSelector(*tp)) continue;
00200
00201 TrackingParticle::Vector momentumTP = tp->momentum();
00202 TrackingParticle::Point vertexTP = tp->vertex();
00203
00204 TrackingParticle::Vector momentum = parametersDefinerTP->momentum(event,setup,tp);
00205 TrackingParticle::Point vertex = parametersDefinerTP->vertex(event,setup,tp);
00206 double dxySim = (-vertex.x()*sin(momentum.phi())+vertex.y()*cos(momentum.phi()));
00207 double dzSim = vertex.z() - (vertex.x()*momentum.x()+vertex.y()*momentum.y())/sqrt(momentum.perp2())
00208 * momentum.z()/sqrt(momentum.perp2());
00209
00210 st++;
00211
00212 histoProducerAlgo_->fill_generic_simTrack_histos(w,momentumTP,vertexTP, tp->eventId().bunchCrossing());
00213
00214 const TrajectorySeed* matchedSeedPointer=0;
00215 std::vector<std::pair<edm::RefToBase<TrajectorySeed>, double> > rt;
00216 if(simRecColl.find(tp) != simRecColl.end()){
00217 rt = simRecColl[tp];
00218 if (rt.size()!=0) {
00219 ats++;
00220 matchedSeedPointer = rt.begin()->first.get();
00221 edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st
00222 << " with pt=" << sqrt(tp->momentum().perp2())
00223 << " associated with quality:" << rt.begin()->second <<"\n";
00224 }
00225 }else{
00226 edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st
00227 << " with pt=" << sqrt(tp->momentum().perp2())
00228 << " NOT associated to any TrajectorySeed" << "\n";
00229 }
00230
00231 int nSimHits = tp->numberOfTrackerHits();
00232
00233 double vtx_z_PU = tp->vertex().z();
00234 for (size_t j = 0; j < tv.size(); j++) {
00235 if (tp->eventId().event() == tv[j].eventId().event()) {
00236 vtx_z_PU = tv[j].position().z();
00237 break;
00238 }
00239 }
00240
00241
00242
00243 reco::Track* matchedTrackPointer = 0;
00244 if (matchedSeedPointer) {
00245 TSCBLBuilderNoMaterial tscblBuilder;
00246 TransientTrackingRecHit::RecHitPointer recHit = theTTRHBuilder->build(&*(matchedSeedPointer->recHits().second-1));
00247 TrajectoryStateOnSurface state = trajectoryStateTransform::transientState( matchedSeedPointer->startingState(), recHit->surface(), theMF.product());
00248 TrajectoryStateClosestToBeamLine tsAtClosestApproachSeed = tscblBuilder(*state.freeState(),bs);
00249 if(!(tsAtClosestApproachSeed.isValid())){
00250 edm::LogVerbatim("SeedValidator")<<"TrajectoryStateClosestToBeamLine not valid";
00251 continue;
00252 }
00253 const reco::TrackBase::Point vSeed1(tsAtClosestApproachSeed.trackStateAtPCA().position().x(),
00254 tsAtClosestApproachSeed.trackStateAtPCA().position().y(),
00255 tsAtClosestApproachSeed.trackStateAtPCA().position().z());
00256 const reco::TrackBase::Vector pSeed(tsAtClosestApproachSeed.trackStateAtPCA().momentum().x(),
00257 tsAtClosestApproachSeed.trackStateAtPCA().momentum().y(),
00258 tsAtClosestApproachSeed.trackStateAtPCA().momentum().z());
00259
00260 PerigeeTrajectoryError seedPerigeeErrors = PerigeeConversions::ftsToPerigeeError(tsAtClosestApproachSeed.trackStateAtPCA());
00261 matchedTrackPointer = new reco::Track(0.,0., vSeed1, pSeed, 1, seedPerigeeErrors.covarianceMatrix());
00262 matchedTrackPointer->setHitPattern(matchedSeedPointer->recHits().first,matchedSeedPointer->recHits().second);
00263 }
00264
00265 histoProducerAlgo_->fill_recoAssociated_simTrack_histos(w,*tp,tp->momentum(),tp->vertex(),dxySim,dzSim,nSimHits,
00266 matchedTrackPointer,puinfo.getPU_NumInteractions(), vtx_z_PU);
00267
00268 sts++;
00269 if (matchedTrackPointer) asts++;
00270
00271 }
00272
00273
00274
00275
00276 edm::LogVerbatim("TrackValidator") << "\n# of TrajectorySeeds with "
00277 << label[www].process()<<":"
00278 << label[www].label()<<":"
00279 << label[www].instance()
00280 << ": " << seedCollection->size() << "\n";
00281 int sat(0);
00282 int at(0);
00283 int rT(0);
00284
00285 TSCBLBuilderNoMaterial tscblBuilder;
00286 for(TrajectorySeedCollection::size_type i=0; i<seedCollection->size(); ++i){
00287 edm::RefToBase<TrajectorySeed> seed(seedCollection, i);
00288 rT++;
00289
00290
00291 TransientTrackingRecHit::RecHitPointer recHit = theTTRHBuilder->build(&*(seed->recHits().second-1));
00292 TrajectoryStateOnSurface state = trajectoryStateTransform::transientState( seed->startingState(), recHit->surface(), theMF.product());
00293 TrajectoryStateClosestToBeamLine tsAtClosestApproachSeed = tscblBuilder(*state.freeState(),bs);
00294 if(!(tsAtClosestApproachSeed.isValid())){
00295 edm::LogVerbatim("SeedValidator")<<"TrajectoryStateClosestToBeamLine not valid";
00296 continue;
00297 }
00298 const reco::TrackBase::Point vSeed1(tsAtClosestApproachSeed.trackStateAtPCA().position().x(),
00299 tsAtClosestApproachSeed.trackStateAtPCA().position().y(),
00300 tsAtClosestApproachSeed.trackStateAtPCA().position().z());
00301 const reco::TrackBase::Vector pSeed(tsAtClosestApproachSeed.trackStateAtPCA().momentum().x(),
00302 tsAtClosestApproachSeed.trackStateAtPCA().momentum().y(),
00303 tsAtClosestApproachSeed.trackStateAtPCA().momentum().z());
00304
00305 PerigeeTrajectoryError seedPerigeeErrors = PerigeeConversions::ftsToPerigeeError(tsAtClosestApproachSeed.trackStateAtPCA());
00306
00307
00308 reco::Track* trackFromSeed = new reco::Track(0.,0., vSeed1, pSeed, 1, seedPerigeeErrors.covarianceMatrix());
00309 trackFromSeed->setHitPattern(seed->recHits().first,seed->recHits().second);
00310
00311 bool isSigSimMatched(false);
00312 bool isSimMatched(false);
00313 bool isChargeMatched(true);
00314 int numAssocSeeds = 0;
00315 int tpbx = 0;
00316 int nSimHits = 0;
00317 double sharedFraction = 0.;
00318 std::vector<std::pair<TrackingParticleRef, double> > tp;
00319 if(recSimColl.find(seed) != recSimColl.end()) {
00320 tp = recSimColl[seed];
00321 if (tp.size()!=0) {
00322
00323 nSimHits = tp[0].first->numberOfTrackerHits();
00324 sharedFraction = tp[0].second;
00325 isSimMatched = true;
00326 if (tp[0].first->charge() != seed->startingState().parameters().charge()) isChargeMatched = false;
00327 if(simRecColl.find(tp[0].first) != simRecColl.end()) numAssocSeeds = simRecColl[tp[0].first].size();
00328
00329 tpbx = tp[0].first->eventId().bunchCrossing();
00330
00331 at++;
00332
00333 for (unsigned int tp_ite=0;tp_ite<tp.size();++tp_ite){
00334 TrackingParticle trackpart = *(tp[tp_ite].first);
00335 if ((trackpart.eventId().event() == 0) && (trackpart.eventId().bunchCrossing() == 0)){
00336 isSigSimMatched = true;
00337 sat++;
00338 break;
00339 }
00340 }
00341
00342
00343 edm::LogVerbatim("SeedValidator") << "TrajectorySeed #" << rT << " associated with quality:" << tp.begin()->second <<"\n";
00344 }
00345 } else {
00346 edm::LogVerbatim("SeedValidator") << "TrajectorySeed #" << rT << " NOT associated to any TrackingParticle" << "\n";
00347 }
00348
00349 histoProducerAlgo_->fill_generic_recoTrack_histos(w,*trackFromSeed,bs.position(),isSimMatched,isSigSimMatched,
00350 isChargeMatched, numAssocSeeds, puinfo.getPU_NumInteractions(),
00351 tpbx, nSimHits, sharedFraction);
00352
00353
00354 try{
00355 if (tp.size()==0) continue;
00356
00357 histoProducerAlgo_->fill_simAssociated_recoTrack_histos(w,*trackFromSeed);
00358
00359 TrackingParticleRef tpr = tp.begin()->first;
00360
00361
00362 TrackingParticle::Vector momentumTP = parametersDefinerTP->momentum(event,setup,tpr);
00363 TrackingParticle::Point vertexTP = parametersDefinerTP->vertex(event,setup,tpr);
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 histoProducerAlgo_->fill_ResoAndPull_recoTrack_histos(w,momentumTP,vertexTP,tpr->charge(),
00398 *trackFromSeed,bs.position());
00399
00400
00401 } catch (cms::Exception e){
00402 LogTrace("SeedValidator") << "exception found: " << e.what() << "\n";
00403 }
00404 }
00405
00406 histoProducerAlgo_->fill_trackBased_histos(w,at,rT,st);
00407
00408 edm::LogVerbatim("SeedValidator") << "Total Simulated: " << st << "\n"
00409 << "Total Associated (simToReco): " << ats << "\n"
00410 << "Total Reconstructed: " << rT << "\n"
00411 << "Total Associated (recoToSim): " << at << "\n"
00412 << "Total Fakes: " << rT-at << "\n";
00413 w++;
00414 }
00415 }
00416 }
00417
00418 void TrackerSeedValidator::endRun(edm::Run const&, edm::EventSetup const&) {
00419 LogTrace("SeedValidator") << "TrackerSeedValidator::endRun()";
00420 int w=0;
00421 for (unsigned int ww=0;ww<associators.size();ww++){
00422 for (unsigned int www=0;www<label.size();www++){
00423
00424 if (runStandalone) histoProducerAlgo_->finalHistoFits(w);
00425 if (runStandalone) histoProducerAlgo_->fillProfileHistosFromVectors(w);
00426 histoProducerAlgo_->fillHistosFromVectors(w);
00427
00428 w++;
00429 }
00430 }
00431 if ( out.size() != 0 && dbe_ ) dbe_->save(out);
00432 }
00433
00434
00435