Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-21 23:14:28

0001 #include "TTreeValidation.h"
0002 #include "Event.h"
0003 #include "RecoTracker/MkFitCore/interface/Config.h"
0004 #include "RecoTracker/MkFitCore/standalone/ConfigStandalone.h"
0005 #include "RecoTracker/MkFitCore/interface/IterationConfig.h"
0006 
0007 #ifndef NO_ROOT
0008 
0009 namespace mkfit {
0010 
0011   TTreeValidation::TTreeValidation(std::string fileName, const TrackerInfo* trk_info) {
0012     std::lock_guard<std::mutex> locker(glock_);
0013     gROOT->ProcessLine("#include <vector>");
0014 
0015     ntotallayers_fit_ = trk_info->n_layers();
0016 
0017     // KPM via DSR's ROOT wizardry: ROOT's context management implicitly assumes that a file is opened and
0018     // closed on the same thread. To avoid the problem, we declare a local
0019     // TContext object; when it goes out of scope, its destructor unregisters
0020     // the context, guaranteeing the context is unregistered in the same thread
0021     // it was registered in. (do this for tfiles and trees
0022     TDirectory::TContext contextEraser;
0023     f_ = std::unique_ptr<TFile>(TFile::Open(fileName.c_str(), "recreate"));
0024 
0025     if (Config::sim_val_for_cmssw || Config::sim_val) {
0026       TTreeValidation::initializeEfficiencyTree();
0027       TTreeValidation::initializeFakeRateTree();
0028     }
0029     if (Config::cmssw_val) {
0030       TTreeValidation::initializeCMSSWEfficiencyTree();
0031       TTreeValidation::initializeCMSSWFakeRateTree();
0032     }
0033     if (Config::fit_val) {
0034       for (int i = 0; i < nfvs_; ++i)
0035         fvs_[i].resize(ntotallayers_fit_);
0036       TTreeValidation::initializeFitTree();
0037     }
0038     TTreeValidation::initializeConfigTree();
0039   }
0040 
0041   void TTreeValidation::initializeEfficiencyTree() {
0042     // efficiency validation
0043     efftree_ = std::make_unique<TTree>("efftree", "efftree");
0044     efftree_->SetDirectory(0);
0045 
0046     efftree_->Branch("evtID", &evtID_eff_);
0047     efftree_->Branch("mcID", &mcID_eff_);
0048 
0049     efftree_->Branch("nHits_mc", &nHits_mc_eff_);
0050     efftree_->Branch("nLayers_mc", &nLayers_mc_eff_);
0051     efftree_->Branch("lastlyr_mc", &lastlyr_mc_eff_);
0052 
0053     efftree_->Branch("seedID_seed", &seedID_seed_eff_);
0054     efftree_->Branch("seedID_build", &seedID_build_eff_);
0055     efftree_->Branch("seedID_fit", &seedID_fit_eff_);
0056 
0057     efftree_->Branch("x_mc_gen", &x_mc_gen_eff_);
0058     efftree_->Branch("y_mc_gen", &y_mc_gen_eff_);
0059     efftree_->Branch("z_mc_gen", &z_mc_gen_eff_);
0060 
0061     efftree_->Branch("pt_mc_gen", &pt_mc_gen_eff_);
0062     efftree_->Branch("phi_mc_gen", &phi_mc_gen_eff_);
0063     efftree_->Branch("eta_mc_gen", &eta_mc_gen_eff_);
0064 
0065     efftree_->Branch("mcmask_seed", &mcmask_seed_eff_);
0066     efftree_->Branch("mcmask_build", &mcmask_build_eff_);
0067     efftree_->Branch("mcmask_fit", &mcmask_fit_eff_);
0068 
0069     efftree_->Branch("mcTSmask_seed", &mcTSmask_seed_eff_);
0070     efftree_->Branch("mcTSmask_build", &mcTSmask_build_eff_);
0071     efftree_->Branch("mcTSmask_fit", &mcTSmask_fit_eff_);
0072 
0073     efftree_->Branch("xhit_seed", &xhit_seed_eff_);
0074     efftree_->Branch("xhit_build", &xhit_build_eff_);
0075     efftree_->Branch("xhit_fit", &xhit_fit_eff_);
0076 
0077     efftree_->Branch("yhit_seed", &yhit_seed_eff_);
0078     efftree_->Branch("yhit_build", &yhit_build_eff_);
0079     efftree_->Branch("yhit_fit", &yhit_fit_eff_);
0080 
0081     efftree_->Branch("zhit_seed", &zhit_seed_eff_);
0082     efftree_->Branch("zhit_build", &zhit_build_eff_);
0083     efftree_->Branch("zhit_fit", &zhit_fit_eff_);
0084 
0085     efftree_->Branch("pt_mc_seed", &pt_mc_seed_eff_);
0086     efftree_->Branch("pt_seed", &pt_seed_eff_);
0087     efftree_->Branch("ept_seed", &ept_seed_eff_);
0088     efftree_->Branch("pt_mc_build", &pt_mc_build_eff_);
0089     efftree_->Branch("pt_build", &pt_build_eff_);
0090     efftree_->Branch("ept_build", &ept_build_eff_);
0091     efftree_->Branch("pt_mc_fit", &pt_mc_fit_eff_);
0092     efftree_->Branch("pt_fit", &pt_fit_eff_);
0093     efftree_->Branch("ept_fit", &ept_fit_eff_);
0094 
0095     efftree_->Branch("phi_mc_seed", &phi_mc_seed_eff_);
0096     efftree_->Branch("phi_seed", &phi_seed_eff_);
0097     efftree_->Branch("ephi_seed", &ephi_seed_eff_);
0098     efftree_->Branch("phi_mc_build", &phi_mc_build_eff_);
0099     efftree_->Branch("phi_build", &phi_build_eff_);
0100     efftree_->Branch("ephi_build", &ephi_build_eff_);
0101     efftree_->Branch("phi_mc_fit", &phi_mc_fit_eff_);
0102     efftree_->Branch("phi_fit", &phi_fit_eff_);
0103     efftree_->Branch("ephi_fit", &ephi_fit_eff_);
0104 
0105     efftree_->Branch("eta_mc_seed", &eta_mc_seed_eff_);
0106     efftree_->Branch("eta_seed", &eta_seed_eff_);
0107     efftree_->Branch("eeta_seed", &eeta_seed_eff_);
0108     efftree_->Branch("eta_mc_build", &eta_mc_build_eff_);
0109     efftree_->Branch("eta_build", &eta_build_eff_);
0110     efftree_->Branch("eeta_build", &eeta_build_eff_);
0111     efftree_->Branch("eta_mc_fit", &eta_mc_fit_eff_);
0112     efftree_->Branch("eta_fit", &eta_fit_eff_);
0113     efftree_->Branch("eeta_fit", &eeta_fit_eff_);
0114 
0115     efftree_->Branch("nHits_seed", &nHits_seed_eff_);
0116     efftree_->Branch("nHits_build", &nHits_build_eff_);
0117     efftree_->Branch("nHits_fit", &nHits_fit_eff_);
0118 
0119     efftree_->Branch("nLayers_seed", &nLayers_seed_eff_);
0120     efftree_->Branch("nLayers_build", &nLayers_build_eff_);
0121     efftree_->Branch("nLayers_fit", &nLayers_fit_eff_);
0122 
0123     efftree_->Branch("nHitsMatched_seed", &nHitsMatched_seed_eff_);
0124     efftree_->Branch("nHitsMatched_build", &nHitsMatched_build_eff_);
0125     efftree_->Branch("nHitsMatched_fit", &nHitsMatched_fit_eff_);
0126 
0127     efftree_->Branch("fracHitsMatched_seed", &fracHitsMatched_seed_eff_);
0128     efftree_->Branch("fracHitsMatched_build", &fracHitsMatched_build_eff_);
0129     efftree_->Branch("fracHitsMatched_fit", &fracHitsMatched_fit_eff_);
0130 
0131     efftree_->Branch("lastlyr_seed", &lastlyr_seed_eff_);
0132     efftree_->Branch("lastlyr_build", &lastlyr_build_eff_);
0133     efftree_->Branch("lastlyr_fit", &lastlyr_fit_eff_);
0134 
0135     efftree_->Branch("dphi_seed", &dphi_seed_eff_);
0136     efftree_->Branch("dphi_build", &dphi_build_eff_);
0137     efftree_->Branch("dphi_fit", &dphi_fit_eff_);
0138 
0139     efftree_->Branch("hitchi2_seed", &hitchi2_seed_eff_);
0140     efftree_->Branch("hitchi2_build", &hitchi2_build_eff_);
0141     efftree_->Branch("hitchi2_fit", &hitchi2_fit_eff_);
0142 
0143     efftree_->Branch("score_seed", &score_seed_eff_);
0144     efftree_->Branch("score_build", &score_build_eff_);
0145     efftree_->Branch("score_fit", &score_fit_eff_);
0146 
0147     efftree_->Branch("helixchi2_seed", &helixchi2_seed_eff_);
0148     efftree_->Branch("helixchi2_build", &helixchi2_build_eff_);
0149     efftree_->Branch("helixchi2_fit", &helixchi2_fit_eff_);
0150 
0151     efftree_->Branch("duplmask_seed", &duplmask_seed_eff_);
0152     efftree_->Branch("duplmask_build", &duplmask_build_eff_);
0153     efftree_->Branch("duplmask_fit", &duplmask_fit_eff_);
0154 
0155     efftree_->Branch("nTkMatches_seed", &nTkMatches_seed_eff_);
0156     efftree_->Branch("nTkMatches_build", &nTkMatches_build_eff_);
0157     efftree_->Branch("nTkMatches_fit", &nTkMatches_fit_eff_);
0158 
0159     efftree_->Branch("itermask_seed", &itermask_seed_eff_);
0160     efftree_->Branch("itermask_build", &itermask_build_eff_);
0161     efftree_->Branch("itermask_fit", &itermask_fit_eff_);
0162     efftree_->Branch("iterduplmask_seed", &iterduplmask_seed_eff_);
0163     efftree_->Branch("iterduplmask_build", &iterduplmask_build_eff_);
0164     efftree_->Branch("iterduplmask_fit", &iterduplmask_fit_eff_);
0165     efftree_->Branch("algo_seed", &algo_seed_eff_);
0166 
0167     if (Config::keepHitInfo) {
0168       efftree_->Branch("hitlyrs_mc", &hitlyrs_mc_eff_);
0169       efftree_->Branch("hitlyrs_seed", &hitlyrs_seed_eff_);
0170       efftree_->Branch("hitlyrs_build", &hitlyrs_build_eff_);
0171       efftree_->Branch("hitlyrs_fit", &hitlyrs_fit_eff_);
0172 
0173       efftree_->Branch("hitidxs_mc", &hitidxs_mc_eff_);
0174       efftree_->Branch("hitidxs_seed", &hitidxs_seed_eff_);
0175       efftree_->Branch("hitidxs_build", &hitidxs_build_eff_);
0176       efftree_->Branch("hitidxs_fit", &hitidxs_fit_eff_);
0177 
0178       efftree_->Branch("hitmcTkIDs_mc", &hitmcTkIDs_mc_eff_);
0179       efftree_->Branch("hitmcTkIDs_seed", &hitmcTkIDs_seed_eff_);
0180       efftree_->Branch("hitmcTkIDs_build", &hitmcTkIDs_build_eff_);
0181       efftree_->Branch("hitmcTkIDs_fit", &hitmcTkIDs_fit_eff_);
0182 
0183       efftree_->Branch("hitxs_mc", &hitxs_mc_eff_);
0184       efftree_->Branch("hitxs_seed", &hitxs_seed_eff_);
0185       efftree_->Branch("hitxs_build", &hitxs_build_eff_);
0186       efftree_->Branch("hitxs_fit", &hitxs_fit_eff_);
0187 
0188       efftree_->Branch("hitys_mc", &hitys_mc_eff_);
0189       efftree_->Branch("hitys_seed", &hitys_seed_eff_);
0190       efftree_->Branch("hitys_build", &hitys_build_eff_);
0191       efftree_->Branch("hitys_fit", &hitys_fit_eff_);
0192 
0193       efftree_->Branch("hitzs_mc", &hitzs_mc_eff_);
0194       efftree_->Branch("hitzs_seed", &hitzs_seed_eff_);
0195       efftree_->Branch("hitzs_build", &hitzs_build_eff_);
0196       efftree_->Branch("hitzs_fit", &hitzs_fit_eff_);
0197     }
0198   }
0199 
0200   void TTreeValidation::initializeFakeRateTree() {
0201     // fake rate validation
0202     frtree_ = std::make_unique<TTree>("frtree", "frtree");
0203     frtree_->SetDirectory(0);
0204 
0205     frtree_->Branch("evtID", &evtID_FR_);
0206     frtree_->Branch("seedID", &seedID_FR_);
0207 
0208     frtree_->Branch("seedmask_seed", &seedmask_seed_FR_);
0209     frtree_->Branch("seedmask_build", &seedmask_build_FR_);
0210     frtree_->Branch("seedmask_fit", &seedmask_fit_FR_);
0211 
0212     frtree_->Branch("xhit_seed", &xhit_seed_FR_);
0213     frtree_->Branch("xhit_build", &xhit_build_FR_);
0214     frtree_->Branch("xhit_fit", &xhit_fit_FR_);
0215 
0216     frtree_->Branch("yhit_seed", &yhit_seed_FR_);
0217     frtree_->Branch("yhit_build", &yhit_build_FR_);
0218     frtree_->Branch("yhit_fit", &yhit_fit_FR_);
0219 
0220     frtree_->Branch("zhit_seed", &zhit_seed_FR_);
0221     frtree_->Branch("zhit_build", &zhit_build_FR_);
0222     frtree_->Branch("zhit_fit", &zhit_fit_FR_);
0223 
0224     frtree_->Branch("pt_seed", &pt_seed_FR_);
0225     frtree_->Branch("ept_seed", &ept_seed_FR_);
0226     frtree_->Branch("pt_build", &pt_build_FR_);
0227     frtree_->Branch("ept_build", &ept_build_FR_);
0228     frtree_->Branch("pt_fit", &pt_fit_FR_);
0229     frtree_->Branch("ept_fit", &ept_fit_FR_);
0230 
0231     frtree_->Branch("phi_seed", &phi_seed_FR_);
0232     frtree_->Branch("ephi_seed", &ephi_seed_FR_);
0233     frtree_->Branch("phi_build", &phi_build_FR_);
0234     frtree_->Branch("ephi_build", &ephi_build_FR_);
0235     frtree_->Branch("phi_fit", &phi_fit_FR_);
0236     frtree_->Branch("ephi_fit", &ephi_fit_FR_);
0237 
0238     frtree_->Branch("eta_seed", &eta_seed_FR_);
0239     frtree_->Branch("eeta_seed", &eeta_seed_FR_);
0240     frtree_->Branch("eta_build", &eta_build_FR_);
0241     frtree_->Branch("eeta_build", &eeta_build_FR_);
0242     frtree_->Branch("eta_fit", &eta_fit_FR_);
0243     frtree_->Branch("eeta_fit", &eeta_fit_FR_);
0244 
0245     frtree_->Branch("nHits_seed", &nHits_seed_FR_);
0246     frtree_->Branch("nHits_build", &nHits_build_FR_);
0247     frtree_->Branch("nHits_fit", &nHits_fit_FR_);
0248 
0249     frtree_->Branch("nLayers_seed", &nLayers_seed_FR_);
0250     frtree_->Branch("nLayers_build", &nLayers_build_FR_);
0251     frtree_->Branch("nLayers_fit", &nLayers_fit_FR_);
0252 
0253     frtree_->Branch("nHitsMatched_seed", &nHitsMatched_seed_FR_);
0254     frtree_->Branch("nHitsMatched_build", &nHitsMatched_build_FR_);
0255     frtree_->Branch("nHitsMatched_fit", &nHitsMatched_fit_FR_);
0256 
0257     frtree_->Branch("fracHitsMatched_seed", &fracHitsMatched_seed_FR_);
0258     frtree_->Branch("fracHitsMatched_build", &fracHitsMatched_build_FR_);
0259     frtree_->Branch("fracHitsMatched_fit", &fracHitsMatched_fit_FR_);
0260 
0261     frtree_->Branch("lastlyr_seed", &lastlyr_seed_FR_);
0262     frtree_->Branch("lastlyr_build", &lastlyr_build_FR_);
0263     frtree_->Branch("lastlyr_fit", &lastlyr_fit_FR_);
0264 
0265     frtree_->Branch("dphi_seed", &dphi_seed_FR_);
0266     frtree_->Branch("dphi_build", &dphi_build_FR_);
0267     frtree_->Branch("dphi_fit", &dphi_fit_FR_);
0268 
0269     frtree_->Branch("hitchi2_seed", &hitchi2_seed_FR_);
0270     frtree_->Branch("hitchi2_build", &hitchi2_build_FR_);
0271     frtree_->Branch("hitchi2_fit", &hitchi2_fit_FR_);
0272 
0273     frtree_->Branch("score_seed", &score_seed_FR_);
0274     frtree_->Branch("score_build", &score_build_FR_);
0275     frtree_->Branch("score_fit", &score_fit_FR_);
0276 
0277     // sim info of seed,build,fit tracks
0278     frtree_->Branch("mcID_seed", &mcID_seed_FR_);
0279     frtree_->Branch("mcID_build", &mcID_build_FR_);
0280     frtree_->Branch("mcID_fit", &mcID_fit_FR_);
0281 
0282     frtree_->Branch("mcmask_seed", &mcmask_seed_FR_);
0283     frtree_->Branch("mcmask_build", &mcmask_build_FR_);
0284     frtree_->Branch("mcmask_fit", &mcmask_fit_FR_);
0285 
0286     frtree_->Branch("mcTSmask_seed", &mcTSmask_seed_FR_);
0287     frtree_->Branch("mcTSmask_build", &mcTSmask_build_FR_);
0288     frtree_->Branch("mcTSmask_fit", &mcTSmask_fit_FR_);
0289 
0290     frtree_->Branch("pt_mc_seed", &pt_mc_seed_FR_);
0291     frtree_->Branch("pt_mc_build", &pt_mc_build_FR_);
0292     frtree_->Branch("pt_mc_fit", &pt_mc_fit_FR_);
0293 
0294     frtree_->Branch("phi_mc_seed", &phi_mc_seed_FR_);
0295     frtree_->Branch("phi_mc_build", &phi_mc_build_FR_);
0296     frtree_->Branch("phi_mc_fit", &phi_mc_fit_FR_);
0297 
0298     frtree_->Branch("eta_mc_seed", &eta_mc_seed_FR_);
0299     frtree_->Branch("eta_mc_build", &eta_mc_build_FR_);
0300     frtree_->Branch("eta_mc_fit", &eta_mc_fit_FR_);
0301 
0302     frtree_->Branch("nHits_mc_seed", &nHits_mc_seed_FR_);
0303     frtree_->Branch("nHits_mc_build", &nHits_mc_build_FR_);
0304     frtree_->Branch("nHits_mc_fit", &nHits_mc_fit_FR_);
0305 
0306     frtree_->Branch("nLayers_mc_seed", &nLayers_mc_seed_FR_);
0307     frtree_->Branch("nLayers_mc_build", &nLayers_mc_build_FR_);
0308     frtree_->Branch("nLayers_mc_fit", &nLayers_mc_fit_FR_);
0309 
0310     frtree_->Branch("lastlyr_mc_seed", &lastlyr_mc_seed_FR_);
0311     frtree_->Branch("lastlyr_mc_build", &lastlyr_mc_build_FR_);
0312     frtree_->Branch("lastlyr_mc_fit", &lastlyr_mc_fit_FR_);
0313 
0314     frtree_->Branch("helixchi2_seed", &helixchi2_seed_FR_);
0315     frtree_->Branch("helixchi2_build", &helixchi2_build_FR_);
0316     frtree_->Branch("helixchi2_fit", &helixchi2_fit_FR_);
0317 
0318     frtree_->Branch("duplmask_seed", &duplmask_seed_FR_);
0319     frtree_->Branch("duplmask_build", &duplmask_build_FR_);
0320     frtree_->Branch("duplmask_fit", &duplmask_fit_FR_);
0321 
0322     frtree_->Branch("iTkMatches_seed", &iTkMatches_seed_FR_);
0323     frtree_->Branch("iTkMatches_build", &iTkMatches_build_FR_);
0324     frtree_->Branch("iTkMatches_fit", &iTkMatches_fit_FR_);
0325 
0326     frtree_->Branch("algorithm", &algorithm_FR_);
0327 
0328     if (Config::keepHitInfo) {
0329       frtree_->Branch("hitlyrs_seed", &hitlyrs_seed_FR_);
0330       frtree_->Branch("hitlyrs_mc_seed", &hitlyrs_mc_seed_FR_);
0331       frtree_->Branch("hitlyrs_build", &hitlyrs_build_FR_);
0332       frtree_->Branch("hitlyrs_mc_build", &hitlyrs_mc_build_FR_);
0333       frtree_->Branch("hitlyrs_fit", &hitlyrs_fit_FR_);
0334       frtree_->Branch("hitlyrs_mc_fit", &hitlyrs_mc_fit_FR_);
0335 
0336       frtree_->Branch("hitidxs_seed", &hitidxs_seed_FR_);
0337       frtree_->Branch("hitidxs_mc_seed", &hitidxs_mc_seed_FR_);
0338       frtree_->Branch("hitidxs_build", &hitidxs_build_FR_);
0339       frtree_->Branch("hitidxs_mc_build", &hitidxs_mc_build_FR_);
0340       frtree_->Branch("hitidxs_fit", &hitidxs_fit_FR_);
0341       frtree_->Branch("hitidxs_mc_fit", &hitidxs_mc_fit_FR_);
0342 
0343       frtree_->Branch("hitmcTkIDs_seed", &hitmcTkIDs_seed_FR_);
0344       frtree_->Branch("hitmcTkIDs_mc_seed", &hitmcTkIDs_mc_seed_FR_);
0345       frtree_->Branch("hitmcTkIDs_build", &hitmcTkIDs_build_FR_);
0346       frtree_->Branch("hitmcTkIDs_mc_build", &hitmcTkIDs_mc_build_FR_);
0347       frtree_->Branch("hitmcTkIDs_fit", &hitmcTkIDs_fit_FR_);
0348       frtree_->Branch("hitmcTkIDs_mc_fit", &hitmcTkIDs_mc_fit_FR_);
0349 
0350       frtree_->Branch("hitxs_seed", &hitxs_seed_FR_);
0351       frtree_->Branch("hitxs_mc_seed", &hitxs_mc_seed_FR_);
0352       frtree_->Branch("hitxs_build", &hitxs_build_FR_);
0353       frtree_->Branch("hitxs_mc_build", &hitxs_mc_build_FR_);
0354       frtree_->Branch("hitxs_fit", &hitxs_fit_FR_);
0355       frtree_->Branch("hitxs_mc_fit", &hitxs_mc_fit_FR_);
0356 
0357       frtree_->Branch("hitys_seed", &hitys_seed_FR_);
0358       frtree_->Branch("hitys_mc_seed", &hitys_mc_seed_FR_);
0359       frtree_->Branch("hitys_build", &hitys_build_FR_);
0360       frtree_->Branch("hitys_mc_build", &hitys_mc_build_FR_);
0361       frtree_->Branch("hitys_fit", &hitys_fit_FR_);
0362       frtree_->Branch("hitys_mc_fit", &hitys_mc_fit_FR_);
0363 
0364       frtree_->Branch("hitzs_seed", &hitzs_seed_FR_);
0365       frtree_->Branch("hitzs_mc_seed", &hitzs_mc_seed_FR_);
0366       frtree_->Branch("hitzs_build", &hitzs_build_FR_);
0367       frtree_->Branch("hitzs_mc_build", &hitzs_mc_build_FR_);
0368       frtree_->Branch("hitzs_fit", &hitzs_fit_FR_);
0369       frtree_->Branch("hitzs_mc_fit", &hitzs_mc_fit_FR_);
0370     }
0371   }
0372 
0373   void TTreeValidation::initializeConfigTree() {
0374     // include config ++ real seeding parameters ...
0375     configtree_ = std::make_unique<TTree>("configtree", "configtree");
0376     configtree_->SetDirectory(0);
0377 
0378     configtree_->Branch("Ntracks", &Ntracks_);
0379     configtree_->Branch("Nevents", &Nevents_);
0380 
0381     configtree_->Branch("nLayers", &nLayers_);
0382 
0383     configtree_->Branch("nlayers_per_seed", &nlayers_per_seed_);
0384     configtree_->Branch("maxCand", &maxCand_);
0385     configtree_->Branch("chi2Cut_min", &chi2Cut_min_);
0386     configtree_->Branch("nSigma", &nSigma_);
0387     configtree_->Branch("minDPhi", &minDPhi_);
0388     configtree_->Branch("maxDPhi", &maxDPhi_);
0389     configtree_->Branch("minDEta", &minDEta_);
0390     configtree_->Branch("maxDEta", &maxDEta_);
0391 
0392     configtree_->Branch("beamspotX", &beamspotX_);
0393     configtree_->Branch("beamspotY", &beamspotY_);
0394     configtree_->Branch("beamspotZ", &beamspotZ_);
0395 
0396     configtree_->Branch("minSimPt", &minSimPt_);
0397     configtree_->Branch("maxSimPt", &maxSimPt_);
0398 
0399     configtree_->Branch("hitposerrXY", &hitposerrXY_);
0400     configtree_->Branch("hitposerrZ", &hitposerrZ_);
0401     configtree_->Branch("hitposerrR", &hitposerrR_);
0402 
0403     configtree_->Branch("varXY", &varXY_);
0404     configtree_->Branch("varZ", &varZ_);
0405 
0406     configtree_->Branch("ptinverr049", &ptinverr049_);
0407     configtree_->Branch("phierr049", &phierr049_);
0408     configtree_->Branch("thetaerr049", &thetaerr049_);
0409     configtree_->Branch("ptinverr012", &ptinverr012_);
0410     configtree_->Branch("phierr012", &phierr012_);
0411     configtree_->Branch("thetaerr012", &thetaerr012_);
0412   }
0413 
0414   void TTreeValidation::initializeCMSSWEfficiencyTree() {
0415     // cmssw reco track efficiency validation
0416     cmsswefftree_ = std::make_unique<TTree>("cmsswefftree", "cmsswefftree");
0417     cmsswefftree_->SetDirectory(0);
0418 
0419     cmsswefftree_->Branch("evtID", &evtID_ceff_);
0420     cmsswefftree_->Branch("cmsswID", &cmsswID_ceff_);
0421     cmsswefftree_->Branch("seedID_cmssw", &seedID_cmssw_ceff_);
0422 
0423     // CMSSW
0424     cmsswefftree_->Branch("x_cmssw", &x_cmssw_ceff_);
0425     cmsswefftree_->Branch("y_cmssw", &y_cmssw_ceff_);
0426     cmsswefftree_->Branch("z_cmssw", &z_cmssw_ceff_);
0427 
0428     cmsswefftree_->Branch("pt_cmssw", &pt_cmssw_ceff_);
0429     cmsswefftree_->Branch("phi_cmssw", &phi_cmssw_ceff_);
0430     cmsswefftree_->Branch("eta_cmssw", &eta_cmssw_ceff_);
0431 
0432     cmsswefftree_->Branch("nHits_cmssw", &nHits_cmssw_ceff_);
0433     cmsswefftree_->Branch("nLayers_cmssw", &nLayers_cmssw_ceff_);
0434     cmsswefftree_->Branch("lastlyr_cmssw", &lastlyr_cmssw_ceff_);
0435 
0436     // Build
0437     cmsswefftree_->Branch("cmsswmask_build", &cmsswmask_build_ceff_);
0438     cmsswefftree_->Branch("seedID_build", &seedID_build_ceff_);
0439     cmsswefftree_->Branch("mcTrackID_build", &mcTrackID_build_ceff_);
0440 
0441     cmsswefftree_->Branch("pt_build", &pt_build_ceff_);
0442     cmsswefftree_->Branch("ept_build", &ept_build_ceff_);
0443     cmsswefftree_->Branch("phi_build", &phi_build_ceff_);
0444     cmsswefftree_->Branch("ephi_build", &ephi_build_ceff_);
0445     cmsswefftree_->Branch("eta_build", &eta_build_ceff_);
0446     cmsswefftree_->Branch("eeta_build", &eeta_build_ceff_);
0447 
0448     cmsswefftree_->Branch("x_mc_build", &x_mc_build_ceff_);
0449     cmsswefftree_->Branch("y_mc_build", &y_mc_build_ceff_);
0450     cmsswefftree_->Branch("z_mc_build", &z_mc_build_ceff_);
0451     cmsswefftree_->Branch("pt_mc_build", &pt_mc_build_ceff_);
0452     cmsswefftree_->Branch("phi_mc_build", &phi_mc_build_ceff_);
0453     cmsswefftree_->Branch("eta_mc_build", &eta_mc_build_ceff_);
0454 
0455     cmsswefftree_->Branch("nHits_build", &nHits_build_ceff_);
0456     cmsswefftree_->Branch("nLayers_build", &nLayers_build_ceff_);
0457     cmsswefftree_->Branch("nHitsMatched_build", &nHitsMatched_build_ceff_);
0458     cmsswefftree_->Branch("fracHitsMatched_build", &fracHitsMatched_build_ceff_);
0459     cmsswefftree_->Branch("lastlyr_build", &lastlyr_build_ceff_);
0460 
0461     cmsswefftree_->Branch("xhit_build", &xhit_build_ceff_);
0462     cmsswefftree_->Branch("yhit_build", &yhit_build_ceff_);
0463     cmsswefftree_->Branch("zhit_build", &zhit_build_ceff_);
0464 
0465     cmsswefftree_->Branch("hitchi2_build", &hitchi2_build_ceff_);
0466     cmsswefftree_->Branch("helixchi2_build", &helixchi2_build_ceff_);
0467     cmsswefftree_->Branch("score_build", &score_build_ceff_);
0468     cmsswefftree_->Branch("dphi_build", &dphi_build_ceff_);
0469 
0470     cmsswefftree_->Branch("duplmask_build", &duplmask_build_ceff_);
0471     cmsswefftree_->Branch("nTkMatches_build", &nTkMatches_build_ceff_);
0472 
0473     cmsswefftree_->Branch("itermask_build", &itermask_build_ceff_);
0474     cmsswefftree_->Branch("iterduplmask_build", &iterduplmask_build_ceff_);
0475 
0476     // Fit
0477     cmsswefftree_->Branch("cmsswmask_fit", &cmsswmask_fit_ceff_);
0478     cmsswefftree_->Branch("seedID_fit", &seedID_fit_ceff_);
0479     cmsswefftree_->Branch("mcTrackID_fit", &mcTrackID_fit_ceff_);
0480 
0481     cmsswefftree_->Branch("pt_fit", &pt_fit_ceff_);
0482     cmsswefftree_->Branch("ept_fit", &ept_fit_ceff_);
0483     cmsswefftree_->Branch("phi_fit", &phi_fit_ceff_);
0484     cmsswefftree_->Branch("ephi_fit", &ephi_fit_ceff_);
0485     cmsswefftree_->Branch("eta_fit", &eta_fit_ceff_);
0486     cmsswefftree_->Branch("eeta_fit", &eeta_fit_ceff_);
0487 
0488     cmsswefftree_->Branch("x_mc_fit", &x_mc_fit_ceff_);
0489     cmsswefftree_->Branch("y_mc_fit", &y_mc_fit_ceff_);
0490     cmsswefftree_->Branch("z_mc_fit", &z_mc_fit_ceff_);
0491     cmsswefftree_->Branch("pt_mc_fit", &pt_mc_fit_ceff_);
0492     cmsswefftree_->Branch("phi_mc_fit", &phi_mc_fit_ceff_);
0493     cmsswefftree_->Branch("eta_mc_fit", &eta_mc_fit_ceff_);
0494 
0495     cmsswefftree_->Branch("nHits_fit", &nHits_fit_ceff_);
0496     cmsswefftree_->Branch("nLayers_fit", &nLayers_fit_ceff_);
0497     cmsswefftree_->Branch("nHitsMatched_fit", &nHitsMatched_fit_ceff_);
0498     cmsswefftree_->Branch("fracHitsMatched_fit", &fracHitsMatched_fit_ceff_);
0499     cmsswefftree_->Branch("lastlyr_fit", &lastlyr_fit_ceff_);
0500 
0501     cmsswefftree_->Branch("xhit_fit", &xhit_fit_ceff_);
0502     cmsswefftree_->Branch("yhit_fit", &yhit_fit_ceff_);
0503     cmsswefftree_->Branch("zhit_fit", &zhit_fit_ceff_);
0504 
0505     cmsswefftree_->Branch("hitchi2_fit", &hitchi2_fit_ceff_);
0506     cmsswefftree_->Branch("helixchi2_fit", &helixchi2_fit_ceff_);
0507     cmsswefftree_->Branch("score_fit", &score_fit_ceff_);
0508     cmsswefftree_->Branch("dphi_fit", &dphi_fit_ceff_);
0509 
0510     cmsswefftree_->Branch("duplmask_fit", &duplmask_fit_ceff_);
0511     cmsswefftree_->Branch("nTkMatches_fit", &nTkMatches_fit_ceff_);
0512 
0513     cmsswefftree_->Branch("itermask_fit", &itermask_fit_ceff_);
0514     cmsswefftree_->Branch("iterduplmask_fit", &iterduplmask_fit_ceff_);
0515 
0516     cmsswefftree_->Branch("algo_seed", &algo_seed_ceff_);
0517 
0518     if (Config::keepHitInfo) {
0519       cmsswefftree_->Branch("hitlyrs_cmssw", &hitlyrs_cmssw_ceff_);
0520       cmsswefftree_->Branch("hitlyrs_build", &hitlyrs_build_ceff_);
0521       cmsswefftree_->Branch("hitlyrs_mc_build", &hitlyrs_mc_build_ceff_);
0522       cmsswefftree_->Branch("hitlyrs_fit", &hitlyrs_fit_ceff_);
0523       cmsswefftree_->Branch("hitlyrs_mc_fit", &hitlyrs_mc_fit_ceff_);
0524 
0525       cmsswefftree_->Branch("hitidxs_cmssw", &hitidxs_cmssw_ceff_);
0526       cmsswefftree_->Branch("hitidxs_build", &hitidxs_build_ceff_);
0527       cmsswefftree_->Branch("hitidxs_mc_build", &hitidxs_mc_build_ceff_);
0528       cmsswefftree_->Branch("hitidxs_fit", &hitidxs_fit_ceff_);
0529       cmsswefftree_->Branch("hitidxs_mc_fit", &hitidxs_mc_fit_ceff_);
0530     }
0531   }
0532 
0533   void TTreeValidation::initializeCMSSWFakeRateTree() {
0534     // cmssw reco track efficiency validation
0535     cmsswfrtree_ = std::make_unique<TTree>("cmsswfrtree", "cmsswfrtree");
0536     cmsswfrtree_->SetDirectory(0);
0537 
0538     cmsswfrtree_->Branch("evtID", &evtID_cFR_);
0539     cmsswfrtree_->Branch("seedID", &seedID_cFR_);
0540     cmsswfrtree_->Branch("mcTrackID", &mcTrackID_cFR_);
0541 
0542     // mc
0543     cmsswfrtree_->Branch("x_mc", &x_mc_cFR_);
0544     cmsswfrtree_->Branch("y_mc", &y_mc_cFR_);
0545     cmsswfrtree_->Branch("z_mc", &z_mc_cFR_);
0546     cmsswfrtree_->Branch("pt_mc", &pt_mc_cFR_);
0547     cmsswfrtree_->Branch("phi_mc", &phi_mc_cFR_);
0548     cmsswfrtree_->Branch("eta_mc", &eta_mc_cFR_);
0549 
0550     // build
0551     cmsswfrtree_->Branch("cmsswID_build", &cmsswID_build_cFR_);
0552     cmsswfrtree_->Branch("cmsswmask_build", &cmsswmask_build_cFR_);
0553 
0554     cmsswfrtree_->Branch("pt_build", &pt_build_cFR_);
0555     cmsswfrtree_->Branch("ept_build", &ept_build_cFR_);
0556     cmsswfrtree_->Branch("phi_build", &phi_build_cFR_);
0557     cmsswfrtree_->Branch("ephi_build", &ephi_build_cFR_);
0558     cmsswfrtree_->Branch("eta_build", &eta_build_cFR_);
0559     cmsswfrtree_->Branch("eeta_build", &eeta_build_cFR_);
0560 
0561     cmsswfrtree_->Branch("nHits_build", &nHits_build_cFR_);
0562     cmsswfrtree_->Branch("nLayers_build", &nLayers_build_cFR_);
0563     cmsswfrtree_->Branch("nHitsMatched_build", &nHitsMatched_build_cFR_);
0564     cmsswfrtree_->Branch("fracHitsMatched_build", &fracHitsMatched_build_cFR_);
0565     cmsswfrtree_->Branch("lastlyr_build", &lastlyr_build_cFR_);
0566 
0567     cmsswfrtree_->Branch("xhit_build", &xhit_build_cFR_);
0568     cmsswfrtree_->Branch("yhit_build", &yhit_build_cFR_);
0569     cmsswfrtree_->Branch("zhit_build", &zhit_build_cFR_);
0570 
0571     cmsswfrtree_->Branch("hitchi2_build", &hitchi2_build_cFR_);
0572     cmsswfrtree_->Branch("helixchi2_build", &helixchi2_build_cFR_);
0573     cmsswfrtree_->Branch("score_build", &score_build_cFR_);
0574     cmsswfrtree_->Branch("dphi_build", &dphi_build_cFR_);
0575 
0576     cmsswfrtree_->Branch("duplmask_build", &duplmask_build_cFR_);
0577     cmsswfrtree_->Branch("iTkMatches_build", &iTkMatches_build_cFR_);
0578 
0579     cmsswfrtree_->Branch("seedID_cmssw_build", &seedID_cmssw_build_cFR_);
0580 
0581     cmsswfrtree_->Branch("x_cmssw_build", &x_cmssw_build_cFR_);
0582     cmsswfrtree_->Branch("y_cmssw_build", &y_cmssw_build_cFR_);
0583     cmsswfrtree_->Branch("z_cmssw_build", &z_cmssw_build_cFR_);
0584 
0585     cmsswfrtree_->Branch("pt_cmssw_build", &pt_cmssw_build_cFR_);
0586     cmsswfrtree_->Branch("phi_cmssw_build", &phi_cmssw_build_cFR_);
0587     cmsswfrtree_->Branch("eta_cmssw_build", &eta_cmssw_build_cFR_);
0588 
0589     cmsswfrtree_->Branch("nHits_cmssw_build", &nHits_cmssw_build_cFR_);
0590     cmsswfrtree_->Branch("nLayers_cmssw_build", &nLayers_cmssw_build_cFR_);
0591     cmsswfrtree_->Branch("lastlyr_cmssw_build", &lastlyr_cmssw_build_cFR_);
0592 
0593     // fit
0594     cmsswfrtree_->Branch("cmsswID_fit", &cmsswID_fit_cFR_);
0595     cmsswfrtree_->Branch("cmsswmask_fit", &cmsswmask_fit_cFR_);
0596 
0597     cmsswfrtree_->Branch("pt_fit", &pt_fit_cFR_);
0598     cmsswfrtree_->Branch("ept_fit", &ept_fit_cFR_);
0599     cmsswfrtree_->Branch("phi_fit", &phi_fit_cFR_);
0600     cmsswfrtree_->Branch("ephi_fit", &ephi_fit_cFR_);
0601     cmsswfrtree_->Branch("eta_fit", &eta_fit_cFR_);
0602     cmsswfrtree_->Branch("eeta_fit", &eeta_fit_cFR_);
0603 
0604     cmsswfrtree_->Branch("nHits_fit", &nHits_fit_cFR_);
0605     cmsswfrtree_->Branch("nLayers_fit", &nLayers_fit_cFR_);
0606     cmsswfrtree_->Branch("nHitsMatched_fit", &nHitsMatched_fit_cFR_);
0607     cmsswfrtree_->Branch("fracHitsMatched_fit", &fracHitsMatched_fit_cFR_);
0608     cmsswfrtree_->Branch("lastlyr_fit", &lastlyr_fit_cFR_);
0609 
0610     cmsswfrtree_->Branch("xhit_fit", &xhit_fit_cFR_);
0611     cmsswfrtree_->Branch("yhit_fit", &yhit_fit_cFR_);
0612     cmsswfrtree_->Branch("zhit_fit", &zhit_fit_cFR_);
0613 
0614     cmsswfrtree_->Branch("hitchi2_fit", &hitchi2_fit_cFR_);
0615     cmsswfrtree_->Branch("helixchi2_fit", &helixchi2_fit_cFR_);
0616     cmsswfrtree_->Branch("score_fit", &score_fit_cFR_);
0617     cmsswfrtree_->Branch("dphi_fit", &dphi_fit_cFR_);
0618 
0619     cmsswfrtree_->Branch("duplmask_fit", &duplmask_fit_cFR_);
0620     cmsswfrtree_->Branch("iTkMatches_fit", &iTkMatches_fit_cFR_);
0621 
0622     cmsswfrtree_->Branch("seedID_cmssw_fit", &seedID_cmssw_fit_cFR_);
0623 
0624     cmsswfrtree_->Branch("x_cmssw_fit", &x_cmssw_fit_cFR_);
0625     cmsswfrtree_->Branch("y_cmssw_fit", &y_cmssw_fit_cFR_);
0626     cmsswfrtree_->Branch("z_cmssw_fit", &z_cmssw_fit_cFR_);
0627 
0628     cmsswfrtree_->Branch("pt_cmssw_fit", &pt_cmssw_fit_cFR_);
0629     cmsswfrtree_->Branch("phi_cmssw_fit", &phi_cmssw_fit_cFR_);
0630     cmsswfrtree_->Branch("eta_cmssw_fit", &eta_cmssw_fit_cFR_);
0631 
0632     cmsswfrtree_->Branch("nHits_cmssw_fit", &nHits_cmssw_fit_cFR_);
0633     cmsswfrtree_->Branch("nLayers_cmssw_fit", &nLayers_cmssw_fit_cFR_);
0634     cmsswfrtree_->Branch("lastlyr_cmssw_fit", &lastlyr_cmssw_fit_cFR_);
0635 
0636     cmsswfrtree_->Branch("algorithm", &algorithm_cFR_);
0637 
0638     if (Config::keepHitInfo) {
0639       cmsswfrtree_->Branch("hitlyrs_mc", &hitlyrs_mc_cFR_);
0640       cmsswfrtree_->Branch("hitlyrs_build", &hitlyrs_build_cFR_);
0641       cmsswfrtree_->Branch("hitlyrs_cmssw_build", &hitlyrs_cmssw_build_cFR_);
0642       cmsswfrtree_->Branch("hitlyrs_fit", &hitlyrs_fit_cFR_);
0643       cmsswfrtree_->Branch("hitlyrs_cmssw_fit", &hitlyrs_cmssw_fit_cFR_);
0644 
0645       cmsswfrtree_->Branch("hitidxs_mc", &hitidxs_mc_cFR_);
0646       cmsswfrtree_->Branch("hitidxs_build", &hitidxs_build_cFR_);
0647       cmsswfrtree_->Branch("hitidxs_cmssw_build", &hitidxs_cmssw_build_cFR_);
0648       cmsswfrtree_->Branch("hitidxs_fit", &hitidxs_fit_cFR_);
0649       cmsswfrtree_->Branch("hitidxs_cmssw_fit", &hitidxs_cmssw_fit_cFR_);
0650     }
0651   }
0652 
0653   void TTreeValidation::initializeFitTree() {
0654     fittree_ = std::make_unique<TTree>("fittree", "fittree");
0655     fittree_->SetDirectory(0);
0656 
0657     fittree_->Branch("ntotallayers", &ntotallayers_fit_, "ntotallayers_fit_/I");
0658     fittree_->Branch("tkid", &tkid_fit_, "tkid_fit_/I");
0659     fittree_->Branch("evtid", &evtid_fit_, "evtid_fit_/I");
0660 
0661     fittree_->Branch("z_prop", &z_prop_fit_, "z_prop_fit_[ntotallayers_fit_]/F");
0662     fittree_->Branch("ez_prop", &ez_prop_fit_, "ez_prop_fit_[ntotallayers_fit_]/F");
0663     fittree_->Branch("z_hit", &z_hit_fit_, "z_hit_fit_[ntotallayers_fit_]/F");
0664     fittree_->Branch("ez_hit", &ez_hit_fit_, "ez_hit_fit_[ntotallayers_fit_]/F");
0665     fittree_->Branch("z_sim", &z_sim_fit_, "z_sim_fit_[ntotallayers_fit_]/F");
0666     fittree_->Branch("ez_sim", &ez_sim_fit_, "ez_sim_fit_[ntotallayers_fit_]/F");
0667 
0668     fittree_->Branch("pphi_prop", &pphi_prop_fit_, "pphi_prop_fit_[ntotallayers_fit_]/F");
0669     fittree_->Branch("epphi_prop", &epphi_prop_fit_, "epphi_prop_fit_[ntotallayers_fit_]/F");
0670     fittree_->Branch("pphi_hit", &pphi_hit_fit_, "pphi_hit_fit_[ntotallayers_fit_]/F");
0671     fittree_->Branch("epphi_hit", &epphi_hit_fit_, "epphi_hit_fit_[ntotallayers_fit_]/F");
0672     fittree_->Branch("pphi_sim", &pphi_sim_fit_, "pphi_sim_fit_[ntotallayers_fit_]/F");
0673     fittree_->Branch("epphi_sim", &epphi_sim_fit_, "epphi_sim_fit_[ntotallayers_fit_]/F");
0674 
0675     fittree_->Branch("pt_up", &pt_up_fit_, "pt_up_fit_[ntotallayers_fit_]/F");
0676     fittree_->Branch("ept_up", &ept_up_fit_, "ept_up_fit_[ntotallayers_fit_]/F");
0677     fittree_->Branch("pt_sim", &pt_sim_fit_, "pt_sim_fit_[ntotallayers_fit_]/F");
0678     fittree_->Branch("ept_sim", &ept_sim_fit_, "ept_sim_fit_[ntotallayers_fit_]/F");
0679 
0680     fittree_->Branch("mphi_up", &mphi_up_fit_, "mphi_up_fit_[ntotallayers_fit_]/F");
0681     fittree_->Branch("emphi_up", &emphi_up_fit_, "emphi_up_fit_[ntotallayers_fit_]/F");
0682     fittree_->Branch("mphi_sim", &mphi_sim_fit_, "mphi_sim_fit_[ntotallayers_fit_]/F");
0683     fittree_->Branch("emphi_sim", &emphi_sim_fit_, "emphi_sim_fit_[ntotallayers_fit_]/F");
0684 
0685     fittree_->Branch("meta_up", &meta_up_fit_, "meta_up_fit_[ntotallayers_fit_]/F");
0686     fittree_->Branch("emeta_up", &emeta_up_fit_, "emeta_up_fit_[ntotallayers_fit_]/F");
0687     fittree_->Branch("meta_sim", &meta_sim_fit_, "meta_sim_fit_[ntotallayers_fit_]/F");
0688     fittree_->Branch("emeta_sim", &emeta_sim_fit_, "emeta_sim_fit_[ntotallayers_fit_]/F");
0689   }
0690 
0691   void TTreeValidation::alignTracks(TrackVec& evt_tracks, TrackExtraVec& evt_extras, bool alignExtra) {
0692     std::lock_guard<std::mutex> locker(glock_);
0693 
0694     // redo trackExtras first if necessary
0695     if (alignExtra) {
0696       TrackExtraVec trackExtra_tmp(evt_tracks.size());
0697 
0698       // align temporary tkExVec with new track collection ordering
0699       for (int itrack = 0; itrack < (int)evt_tracks.size(); itrack++) {
0700         trackExtra_tmp[itrack] = evt_extras[evt_tracks[itrack].label()];  // label is old seedID!
0701       }
0702 
0703       // now copy the temporary back in the old one
0704       evt_extras = trackExtra_tmp;
0705     }
0706 
0707     // redo track labels to match index in vector
0708     for (int itrack = 0; itrack < (int)evt_tracks.size(); itrack++) {
0709       evt_tracks[itrack].setLabel(itrack);
0710     }
0711   }
0712 
0713   void TTreeValidation::collectFitInfo(const FitVal& tmpfitval, int tkid, int layer) {
0714     std::lock_guard<std::mutex> locker(glock_);
0715 
0716     fitValTkMapMap_[tkid][layer] = tmpfitval;
0717   }
0718 
0719   void TTreeValidation::resetValidationMaps() {
0720     std::lock_guard<std::mutex> locker(glock_);
0721     // reset fit validation map
0722     fitValTkMapMap_.clear();
0723 
0724     // reset map of sim tracks to reco tracks
0725     simToSeedMap_.clear();
0726     simToBuildMap_.clear();
0727     simToFitMap_.clear();
0728 
0729     // reset map of seed tracks to reco tracks
0730     seedToBuildMap_.clear();
0731     seedToFitMap_.clear();
0732 
0733     // reset map of cmssw tracks to reco tracks
0734     cmsswToBuildMap_.clear();
0735     cmsswToFitMap_.clear();
0736 
0737     // reset special map of seed labels to cmssw tracks
0738     seedToCmsswMap_.clear();
0739     cmsswToSeedMap_.clear();
0740 
0741     // reset special map of matching build tracks exactly to cmssw tracks through seedIDs
0742     buildToCmsswMap_.clear();
0743 
0744     // reset special maps used for pairing build to fit tracks CMSSW only
0745     buildToFitMap_.clear();
0746     fitToBuildMap_.clear();
0747 
0748     // reset special maps used for associating seed tracks to reco tracks for sim_val_for_cmssw
0749     candToSeedMapDumbCMSSW_.clear();
0750     fitToSeedMapDumbCMSSW_.clear();
0751   }
0752 
0753   void TTreeValidation::setTrackExtras(Event& ev) {
0754     std::lock_guard<std::mutex> locker(glock_);
0755 
0756     const auto& layerhits = ev.layerHits_;
0757 
0758     if (Config::sim_val_for_cmssw || Config::sim_val) {
0759       const auto& simhits = ev.simHitsInfo_;
0760       const auto& simtracks = ev.simTracks_;
0761       const auto& seedtracks = ev.seedTracks_;
0762       auto& seedextras = ev.seedTracksExtra_;
0763       const auto& buildtracks = ev.candidateTracks_;
0764       auto& buildextras = ev.candidateTracksExtra_;
0765       const auto& fittracks = ev.fitTracks_;
0766       auto& fitextras = ev.fitTracksExtra_;
0767 
0768       // set mcTrackID for seed tracks
0769       for (int itrack = 0; itrack < (int)seedtracks.size(); itrack++) {
0770         const auto& track = seedtracks[itrack];
0771         auto& extra = seedextras[itrack];
0772 
0773         extra.findMatchingSeedHits(track, track, layerhits);
0774         extra.setMCTrackIDInfo(
0775             track,
0776             layerhits,
0777             simhits,
0778             simtracks,
0779             true,
0780             (Config::seedInput == simSeeds));  // otherwise seeds are completely unmatched in ToyMC Sim Seeds
0781       }
0782 
0783       // set mcTrackID for built tracks
0784       for (int itrack = 0; itrack < (int)buildtracks.size(); itrack++) {
0785         const auto& track = buildtracks[itrack];
0786         auto& extra = buildextras[itrack];
0787 
0788         if (Config::sim_val) {
0789           extra.findMatchingSeedHits(track, seedtracks[track.label()], layerhits);
0790         } else if (Config::sim_val_for_cmssw) {
0791           extra.findMatchingSeedHits(track, seedtracks[candToSeedMapDumbCMSSW_[track.label()]], layerhits);
0792         }
0793 
0794         extra.setMCTrackIDInfo(track, layerhits, simhits, simtracks, false, (Config::seedInput == simSeeds));
0795       }
0796 
0797       // set mcTrackID for fit tracks
0798       for (int itrack = 0; itrack < (int)fittracks.size(); itrack++) {
0799         const auto& track = fittracks[itrack];
0800         auto& extra = fitextras[itrack];
0801 
0802         if (Config::sim_val) {
0803           extra.findMatchingSeedHits(track, seedtracks[track.label()], layerhits);
0804         } else if (Config::sim_val_for_cmssw) {
0805           extra.findMatchingSeedHits(track, seedtracks[fitToSeedMapDumbCMSSW_[track.label()]], layerhits);
0806         }
0807 
0808         extra.setMCTrackIDInfo(track, layerhits, simhits, simtracks, false, (Config::seedInput == simSeeds));
0809       }
0810     }
0811 
0812     if (Config::cmssw_val) {
0813       // store mcTrackID and seedID correctly
0814       storeSeedAndMCID(ev);
0815 
0816       const auto& cmsswtracks = ev.cmsswTracks_;
0817       const auto& cmsswextras = ev.cmsswTracksExtra_;
0818       const auto& seedtracks = ev.seedTracks_;
0819       const auto& buildtracks = ev.candidateTracks_;
0820       auto& buildextras = ev.candidateTracksExtra_;
0821       const auto& fittracks = ev.fitTracks_;
0822       auto& fitextras = ev.fitTracksExtra_;
0823 
0824       // store seed hits, reduced parameters, hit map of cmssw tracks, and global hit map
0825       RedTrackVec reducedCMSSW;
0826       LayIdxIDVecMapMap cmsswHitIDMap;
0827       setupCMSSWMatching(ev, reducedCMSSW, cmsswHitIDMap);
0828 
0829       // set cmsswTrackID for built tracks
0830       for (int itrack = 0; itrack < (int)buildtracks.size(); itrack++) {
0831         const auto& track = buildtracks[itrack];
0832         auto& extra = buildextras[itrack];
0833 
0834         // set vector of hitsOnTrack for seed
0835         extra.findMatchingSeedHits(track,
0836                                    seedtracks[track.label()],
0837                                    layerhits);  // itrack == track.label() == seedtrack index == seedtrack.label()
0838 
0839         if (Config::cmsswMatchingFW == trkParamBased) {
0840           extra.setCMSSWTrackIDInfoByTrkParams(track, layerhits, cmsswtracks, reducedCMSSW, true);
0841         } else if (Config::cmsswMatchingFW == hitBased) {
0842           extra.setCMSSWTrackIDInfoByHits(track,
0843                                           cmsswHitIDMap,
0844                                           cmsswtracks,
0845                                           cmsswextras,
0846                                           reducedCMSSW,
0847                                           -1);             // == -1 for not passing truth info about cmssw tracks
0848         } else if (Config::cmsswMatchingFW == labelBased)  // can only be used if using pure seeds!
0849         {
0850           extra.setCMSSWTrackIDInfoByHits(track,
0851                                           cmsswHitIDMap,
0852                                           cmsswtracks,
0853                                           cmsswextras,
0854                                           reducedCMSSW,
0855                                           reducedCMSSW[cmsswtracks[buildToCmsswMap_[track.label()]].label()].label());
0856         } else {
0857           std::cerr << "Specified CMSSW validation, but using an incorrect matching option! Exiting..." << std::endl;
0858           exit(1);
0859         }
0860       }
0861 
0862       // set cmsswTrackID for fit tracks
0863       for (int itrack = 0; itrack < (int)fittracks.size(); itrack++) {
0864         const auto& track = fittracks[itrack];
0865         auto& extra = fitextras[itrack];
0866 
0867         // set vector of hitsOnTrack for seed
0868         extra.findMatchingSeedHits(track,
0869                                    seedtracks[track.label()],
0870                                    layerhits);  // itrack == track.label() == seedtrack index == seedtrack.label()
0871 
0872         if (Config::cmsswMatchingBK == trkParamBased) {
0873           extra.setCMSSWTrackIDInfoByTrkParams(track, layerhits, cmsswtracks, reducedCMSSW, true);
0874         } else if (Config::cmsswMatchingBK == hitBased) {
0875           extra.setCMSSWTrackIDInfoByHits(track,
0876                                           cmsswHitIDMap,
0877                                           cmsswtracks,
0878                                           cmsswextras,
0879                                           reducedCMSSW,
0880                                           -1);             // == -1 not passing truth info about cmssw
0881         } else if (Config::cmsswMatchingBK == labelBased)  // can only be used if using pure seeds!
0882         {
0883           extra.setCMSSWTrackIDInfoByHits(
0884               track,
0885               cmsswHitIDMap,
0886               cmsswtracks,
0887               cmsswextras,
0888               reducedCMSSW,
0889               reducedCMSSW[cmsswtracks[buildToCmsswMap_[fitToBuildMap_[track.label()]]].label()].label());
0890         } else {
0891           std::cerr << "Specified CMSSW validation, but using an incorrect matching option! Exiting..." << std::endl;
0892           exit(1);
0893         }
0894       }
0895     }
0896   }
0897 
0898   void TTreeValidation::makeSimTkToRecoTksMaps(Event& ev) {
0899     std::lock_guard<std::mutex> locker(glock_);
0900     // map sim track ids to reco labels sort by each (simTracks set in order by default!)
0901     TTreeValidation::mapRefTkToRecoTks(ev.seedTracks_, ev.seedTracksExtra_, simToSeedMap_);
0902     TTreeValidation::mapRefTkToRecoTks(ev.candidateTracks_, ev.candidateTracksExtra_, simToBuildMap_);
0903     TTreeValidation::mapRefTkToRecoTks(ev.fitTracks_, ev.fitTracksExtra_, simToFitMap_);
0904   }
0905 
0906   void TTreeValidation::mapRefTkToRecoTks(const TrackVec& evt_tracks,
0907                                           TrackExtraVec& evt_extras,
0908                                           TkIDToTkIDVecMap& refTkMap) {
0909     for (auto itrack = 0; itrack < (int)evt_tracks.size(); ++itrack) {
0910       auto&& track(evt_tracks[itrack]);
0911       auto&& extra(evt_extras[itrack]);
0912       if (Config::sim_val_for_cmssw || Config::sim_val) {
0913         if (extra.mcTrackID() >= 0)  // skip fakes, don't store them at all in sim map
0914         {
0915           refTkMap[extra.mcTrackID()].push_back(
0916               track.label());  // store vector of reco tk labels, mapped to the sim track label (i.e. mcTrackID)
0917         }
0918       }
0919       if (Config::cmssw_val) {
0920         if (extra.cmsswTrackID() >= 0)  // skip fakes, don't store them at all in cmssw map
0921         {
0922           refTkMap[extra.cmsswTrackID()].push_back(
0923               track.label());  // store vector of reco tk labels, mapped to the cmssw track label (i.e. cmsswTrackID)
0924         }
0925       }
0926     }
0927 
0928     for (auto&& refTkMatches : refTkMap) {
0929       if (refTkMatches.second.size() < 2)  // no duplicates
0930       {
0931         auto& extra(evt_extras[refTkMatches.second[0]]);
0932         extra.setDuplicateInfo(0, bool(false));
0933       } else  // sort duplicates (ghosts) to keep best one --> best score
0934       {
0935         // really should sort on indices with a reduced data structure... this is a hack way to do this for now...
0936         // e.g. std::pair<int, int> (label, score)
0937         TrackVec tmpMatches;
0938         for (auto&& label :
0939              refTkMatches.second)  // loop over vector of reco track labels, push back the track with each label
0940         {
0941           tmpMatches.emplace_back(evt_tracks[label]);
0942         }
0943         //std::sort(tmpMatches.begin(), tmpMatches.end(), sortByHitsChi2); // sort the tracks
0944         std::sort(tmpMatches.begin(), tmpMatches.end(), sortByScoreCand);  // sort the tracks
0945         for (auto itrack = 0; itrack < (int)tmpMatches.size();
0946              itrack++)  // loop over sorted tracks, now set the vector of sorted labels match
0947         {
0948           refTkMatches.second[itrack] = tmpMatches[itrack].label();
0949         }
0950 
0951         int duplicateID = 0;
0952         for (auto&& label : refTkMatches.second)  // loop over vector of reco tracsk
0953         {
0954           auto& extra(evt_extras[label]);
0955           extra.setDuplicateInfo(duplicateID, bool(true));
0956           duplicateID++;  // used in fake rate trees!
0957         }
0958       }
0959     }
0960   }
0961 
0962   void TTreeValidation::makeSeedTkToRecoTkMaps(Event& ev) {
0963     std::lock_guard<std::mutex> locker(glock_);
0964     // map seed to reco tracks --> seed track collection assumed to map to itself, unless we use some cuts
0965     TTreeValidation::mapSeedTkToRecoTk(ev.candidateTracks_, ev.candidateTracksExtra_, seedToBuildMap_);
0966     TTreeValidation::mapSeedTkToRecoTk(ev.fitTracks_, ev.fitTracksExtra_, seedToFitMap_);
0967   }
0968 
0969   void TTreeValidation::mapSeedTkToRecoTk(const TrackVec& evt_tracks,
0970                                           const TrackExtraVec& evt_extras,
0971                                           TkIDToTkIDMap& seedTkMap) {
0972     for (auto&& track : evt_tracks) {
0973       seedTkMap[evt_extras[track.label()].seedID()] = track.label();
0974     }
0975   }
0976 
0977   void TTreeValidation::makeRecoTkToRecoTkMaps(Event& ev) {
0978     std::lock_guard<std::mutex> locker(glock_);
0979     TTreeValidation::makeRecoTkToRecoTkMap(
0980         buildToFitMap_, ev.candidateTracks_, ev.candidateTracksExtra_, ev.fitTracks_, ev.fitTracksExtra_);
0981     TTreeValidation::makeRecoTkToRecoTkMap(
0982         fitToBuildMap_, ev.fitTracks_, ev.fitTracksExtra_, ev.candidateTracks_, ev.candidateTracksExtra_);
0983   }
0984 
0985   void TTreeValidation::makeRecoTkToRecoTkMap(TkIDToTkIDMap& refToPairMap,
0986                                               const TrackVec& reftracks,
0987                                               const TrackExtraVec& refextras,
0988                                               const TrackVec& pairtracks,
0989                                               const TrackExtraVec& pairextras) {
0990     // at this point in the code, the labels of the tracks point their position inside the vector
0991     // while the seedID is the label prior to relabeling (in reality, this is the MC track ID)
0992     for (auto&& reftrack : reftracks) {
0993       const auto& refextra = refextras[reftrack.label()];
0994       for (auto&& pairtrack : pairtracks) {
0995         const auto& pairextra = pairextras[pairtrack.label()];
0996         if (refextra.seedID() == pairextra.seedID()) {
0997           refToPairMap[reftrack.label()] = pairtrack.label();
0998           break;
0999         }
1000       }
1001     }
1002   }
1003 
1004   void TTreeValidation::makeCMSSWTkToRecoTksMaps(Event& ev) {
1005     std::lock_guard<std::mutex> locker(glock_);
1006     // can reuse this function
1007     TTreeValidation::mapRefTkToRecoTks(ev.candidateTracks_, ev.candidateTracksExtra_, cmsswToBuildMap_);
1008     TTreeValidation::mapRefTkToRecoTks(ev.fitTracks_, ev.fitTracksExtra_, cmsswToFitMap_);
1009   }
1010 
1011   void TTreeValidation::makeSeedTkToCMSSWTkMap(Event& ev) {
1012     const auto& seedtracks = ev.seedTracks_;
1013     const auto& cmsswtracks = ev.cmsswTracks_;
1014     for (int itrack = 0; itrack < (int)seedtracks.size(); itrack++) {
1015       for (auto&& cmsswtrack : cmsswtracks) {
1016         if (cmsswtrack.label() == itrack) {
1017           seedToCmsswMap_[seedtracks[itrack].label()] = cmsswtrack.label();
1018           break;
1019         }
1020       }
1021     }
1022   }
1023 
1024   void TTreeValidation::makeCMSSWTkToSeedTkMap(Event& ev) {
1025     const auto& seedtracks = ev.seedTracks_;
1026 
1027     for (const auto& seedToCmsswPair : seedToCmsswMap_) {
1028       const auto seedlabel =
1029           seedToCmsswPair
1030               .first;  // !! in cmssw validation, seed label != seed index in vector as they are not aligned!! --> need to find itrack!
1031       const auto cmsswlabel = seedToCmsswPair.second;  // however, cmssw tracks ARE aligned for label == index
1032 
1033       for (int itrack = 0; itrack < (int)seedtracks.size(); itrack++) {
1034         const auto& seedtrack = seedtracks[itrack];
1035         if (seedtrack.label() == seedlabel) {
1036           cmsswToSeedMap_[cmsswlabel] = itrack;
1037           break;
1038         }
1039       }
1040     }
1041   }
1042 
1043   void TTreeValidation::makeRecoTkToSeedTkMapsDumbCMSSW(Event& ev) {
1044     std::lock_guard<std::mutex> locker(glock_);
1045     // special functions for matching seeds to reco tracks for sim_val_for_cmssw
1046     TTreeValidation::makeRecoTkToSeedTkMapDumbCMSSW(
1047         ev.candidateTracksExtra_, ev.seedTracksExtra_, candToSeedMapDumbCMSSW_);
1048     TTreeValidation::makeRecoTkToSeedTkMapDumbCMSSW(ev.fitTracksExtra_, ev.seedTracksExtra_, fitToSeedMapDumbCMSSW_);
1049   }
1050 
1051   void TTreeValidation::makeRecoTkToSeedTkMapDumbCMSSW(const TrackExtraVec& recoextras,
1052                                                        const TrackExtraVec& seedextras,
1053                                                        TkIDToTkIDMap& recoToSeedMap) {
1054     for (int itrack = 0; itrack < (int)recoextras.size(); itrack++) {
1055       const auto reco_seedID = recoextras[itrack].seedID();
1056       for (int jtrack = 0; jtrack < (int)seedextras.size(); jtrack++) {
1057         const auto seed_seedID = seedextras[jtrack].seedID();
1058         if (reco_seedID == seed_seedID) {
1059           recoToSeedMap[itrack] = jtrack;
1060           break;
1061         }
1062       }
1063     }
1064   }
1065 
1066   void TTreeValidation::setTrackScoresDumbCMSSW(Event& ev) {
1067     auto& seedtracks = ev.seedTracks_;
1068     auto& candtracks = ev.candidateTracks_;
1069     auto& fittracks = ev.fitTracks_;
1070 
1071     // first compute score...
1072     for (auto& seedtrack : seedtracks) {
1073       seedtrack.setScore(getScoreCand(seedtrack));
1074     }
1075 
1076     // ...then use map to set seed type to for build/fit tracks and compute scores
1077     for (const auto& candToSeedPair : candToSeedMapDumbCMSSW_) {
1078       auto& candtrack = candtracks[candToSeedPair.first];
1079 
1080       candtrack.setScore(getScoreCand(candtrack));
1081     }
1082     for (const auto& fitToSeedPair : fitToSeedMapDumbCMSSW_) {
1083       auto& fittrack = fittracks[fitToSeedPair.first];
1084 
1085       fittrack.setScore(getScoreCand(fittrack));
1086     }
1087   }
1088 
1089   void TTreeValidation::storeSeedAndMCID(Event& ev) {
1090     const auto& buildtracks = ev.candidateTracks_;
1091     auto& buildextras = ev.candidateTracksExtra_;
1092 
1093     const auto& fittracks = ev.fitTracks_;
1094     auto& fitextras = ev.fitTracksExtra_;
1095 
1096     const auto& cmsswtracks = ev.cmsswTracks_;
1097     auto& cmsswextras = ev.cmsswTracksExtra_;
1098 
1099     // first set candidate tracks, use as base for fittracks
1100     int newlabel = -1;
1101     for (int itrack = 0; itrack < (int)buildtracks.size(); itrack++) {
1102       auto& extra = buildextras[itrack];
1103       const int seedID = extra.seedID();
1104 
1105       extra.setmcTrackID(seedID);
1106 
1107       if (seedToCmsswMap_.count(seedID)) {
1108         extra.setseedID(seedToCmsswMap_[seedID]);
1109         if (Config::cmsswMatchingFW == labelBased || Config::cmsswMatchingBK == labelBased) {
1110           for (int ctrack = 0; ctrack < (int)cmsswextras.size(); ctrack++) {
1111             if (cmsswextras[ctrack].seedID() == extra.seedID()) {
1112               buildToCmsswMap_[itrack] = cmsswtracks[ctrack].label();  // cmsstracks[ctrack].label() == ctrack!
1113               break;
1114             }
1115           }
1116         }
1117       } else {
1118         extra.setseedID(--newlabel);
1119       }
1120     }
1121 
1122     // set according to candidate tracks for fit tracks through map
1123     for (int itrack = 0; itrack < (int)fittracks.size(); itrack++) {
1124       auto& extra = fitextras[itrack];
1125 
1126       extra.setmcTrackID(buildextras[fitToBuildMap_[itrack]].mcTrackID());
1127       extra.setseedID(buildextras[fitToBuildMap_[itrack]].seedID());
1128     }
1129   }
1130 
1131   void TTreeValidation::setupCMSSWMatching(const Event& ev,
1132                                            RedTrackVec& reducedCMSSW,
1133                                            LayIdxIDVecMapMap& cmsswHitIDMap) {
1134     // get the tracks + hits + extras
1135     const auto& layerhits = ev.layerHits_;
1136     const auto& cmsswtracks = ev.cmsswTracks_;
1137     auto& cmsswextras = ev.cmsswTracksExtra_;
1138     const auto& seedtracks = ev.seedTracks_;
1139 
1140     // resize accordingly
1141     reducedCMSSW.resize(cmsswtracks.size());
1142 
1143     for (int itrack = 0; itrack < (int)cmsswtracks.size(); itrack++) {
1144       // get the needed tracks and extras
1145       auto& cmsswextra = cmsswextras[itrack];
1146       const auto& cmsswtrack = cmsswtracks[itrack];
1147       const auto& seedtrack = seedtracks[cmsswToSeedMap_[cmsswtrack.label()]];  // since cmsswtrack.label() == itrack
1148 
1149       // set seed hits!
1150       cmsswextra.findMatchingSeedHits(cmsswtrack, seedtrack, layerhits);
1151 
1152       // get tmp vars
1153       const auto seedID = cmsswextra.seedID();
1154       const auto& params = cmsswtrack.parameters();
1155       SVector2 tmpv(params[3], params[5]);
1156 
1157       HitLayerMap tmpmap;
1158       for (int ihit = 0; ihit < cmsswtrack.nTotalHits(); ihit++) {
1159         const int lyr = cmsswtrack.getHitLyr(ihit);
1160         const int idx = cmsswtrack.getHitIdx(ihit);
1161 
1162         // don't bother with storing seed layers in reduced cmssw
1163         if (cmsswextra.isSeedHit(lyr, idx))
1164           continue;
1165 
1166         if (lyr >= 0 && idx >= 0) {
1167           tmpmap[lyr].push_back(idx);
1168           cmsswHitIDMap[lyr][idx].push_back(cmsswtrack.label());
1169         }
1170       }
1171 
1172       // index inside object is label (as cmsswtracks are now aligned)
1173       reducedCMSSW[itrack] = ReducedTrack(cmsswtrack.label(), seedID, tmpv, cmsswtrack.momPhi(), tmpmap);
1174     }
1175   }
1176 
1177   int TTreeValidation::getLastFoundHit(const int trackMCHitID, const int mcTrackID, const Event& ev) {
1178     int mcHitID = -1;
1179     if (ev.simHitsInfo_[trackMCHitID].mcTrackID() == mcTrackID) {
1180       mcHitID = trackMCHitID;
1181     } else {
1182       mcHitID = ev.simTracks_[mcTrackID].getMCHitIDFromLayer(ev.layerHits_, ev.simHitsInfo_[trackMCHitID].layer());
1183     }
1184     return mcHitID;
1185   }
1186 
1187   int TTreeValidation::getMaskAssignment(const int refID) {
1188     // initialize
1189     auto refmask = -99;
1190 
1191     if (refID >= 0)  // seed track matched to seed and sim
1192     {
1193       refmask = 1;  // matched track to sim
1194     } else if (refID == -10) {
1195       refmask = -2;
1196     } else {
1197       if (Config::inclusiveShorts)  // only used by standard simval!
1198       {
1199         if (refID == -1 || refID == -5 || refID == -8 || refID == -9) {
1200           refmask = 0;
1201         } else if (refID == -2) {
1202           refmask = 2;
1203         } else  // mcID == -3,-4,-6,-7
1204         {
1205           refmask = -1;
1206         }
1207       } else  // only count long tracks (in mtvLike: all reco tracks are counted!)
1208       {
1209         if (refID == -1 || refID == -9) {
1210           refmask = 0;
1211         } else if (Config::mtvLikeValidation && refID == -4) {
1212           refmask = 2;
1213         } else  // mcID == -2,-3,-4,-5,-6,-7,-8: standard simval
1214         {
1215           refmask = -1;
1216         }
1217       }
1218     }  // end check over not matched
1219 
1220     return refmask;
1221   }
1222 
1223   void TTreeValidation::resetFitBranches() {
1224     for (int ilayer = 0; ilayer < ntotallayers_fit_; ++ilayer) {
1225       z_prop_fit_[ilayer] = -1000.f;
1226       ez_prop_fit_[ilayer] = -1000.f;
1227       z_hit_fit_[ilayer] = -1000.f;
1228       ez_hit_fit_[ilayer] = -1000.f;
1229       z_sim_fit_[ilayer] = -1000.f;
1230       ez_sim_fit_[ilayer] = -1000.f;
1231 
1232       pphi_prop_fit_[ilayer] = -1000.f;
1233       epphi_prop_fit_[ilayer] = -1000.f;
1234       pphi_hit_fit_[ilayer] = -1000.f;
1235       epphi_hit_fit_[ilayer] = -1000.f;
1236       pphi_sim_fit_[ilayer] = -1000.f;
1237       epphi_sim_fit_[ilayer] = -1000.f;
1238 
1239       pt_up_fit_[ilayer] = -1000.f;
1240       ept_up_fit_[ilayer] = -1000.f;
1241       pt_sim_fit_[ilayer] = -1000.f;
1242       ept_sim_fit_[ilayer] = -1000.f;
1243 
1244       mphi_up_fit_[ilayer] = -1000.f;
1245       emphi_up_fit_[ilayer] = -1000.f;
1246       mphi_sim_fit_[ilayer] = -1000.f;
1247       emphi_sim_fit_[ilayer] = -1000.f;
1248 
1249       meta_up_fit_[ilayer] = -1000.f;
1250       emeta_up_fit_[ilayer] = -1000.f;
1251       meta_sim_fit_[ilayer] = -1000.f;
1252       emeta_sim_fit_[ilayer] = -1000.f;
1253     }
1254   }
1255 
1256   void TTreeValidation::fillFitTree(const Event& ev) {
1257     std::lock_guard<std::mutex> locker(glock_);
1258 
1259     evtid_fit_ = ev.evtID();
1260     const auto& simtracks = ev.simTracks_;
1261     const auto& layerhits = ev.layerHits_;
1262     const auto& simtrackstates = ev.simTrackStates_;
1263 
1264     for (auto&& fitvalmapmap : fitValTkMapMap_) {
1265       TTreeValidation::resetFitBranches();
1266 
1267       tkid_fit_ = fitvalmapmap.first;  // seed id (label) is the same as the mcID
1268 
1269       const auto& simtrack = simtracks[tkid_fit_];
1270       const auto& fitvalmap = fitvalmapmap.second;
1271       for (int ilayer = 0; ilayer < ntotallayers_fit_; ++ilayer) {
1272         if (fitvalmap.count(ilayer)) {
1273           const auto& hit = layerhits[ilayer][simtrack.getHitIdx(ilayer)];
1274           const auto& initTS = simtrackstates.at(hit.mcHitID());
1275           const auto& fitval = fitvalmap.at(ilayer);
1276 
1277           z_hit_fit_[ilayer] = hit.z();
1278           ez_hit_fit_[ilayer] = std::sqrt(hit.ezz());
1279           z_sim_fit_[ilayer] = initTS.z();
1280           ez_sim_fit_[ilayer] = initTS.ezz();
1281           z_prop_fit_[ilayer] = fitval.ppz;
1282           ez_prop_fit_[ilayer] = fitval.eppz;
1283 
1284           pphi_hit_fit_[ilayer] = hit.phi();
1285           epphi_hit_fit_[ilayer] = std::sqrt(hit.ephi());
1286           pphi_sim_fit_[ilayer] = initTS.posPhi();
1287           epphi_sim_fit_[ilayer] = initTS.eposPhi();
1288           pphi_prop_fit_[ilayer] = fitval.ppphi;
1289           epphi_prop_fit_[ilayer] = fitval.eppphi;
1290 
1291           pt_up_fit_[ilayer] = fitval.upt;
1292           ept_up_fit_[ilayer] = fitval.eupt;
1293           pt_sim_fit_[ilayer] = initTS.pT();
1294           ept_sim_fit_[ilayer] = initTS.epT();
1295 
1296           mphi_up_fit_[ilayer] = fitval.umphi;
1297           emphi_up_fit_[ilayer] = fitval.eumphi;
1298           mphi_sim_fit_[ilayer] = initTS.momPhi();
1299           emphi_sim_fit_[ilayer] = initTS.emomPhi();
1300 
1301           meta_up_fit_[ilayer] = fitval.umeta;
1302           emeta_up_fit_[ilayer] = fitval.eumeta;
1303           meta_sim_fit_[ilayer] = initTS.momEta();
1304           emeta_sim_fit_[ilayer] = initTS.emomEta();
1305         }
1306       }
1307       fittree_->Fill();
1308     }
1309   }
1310 
1311   void TTreeValidation::fillFullHitInfo(const Event& ev,
1312                                         const Track& track,
1313                                         std::vector<int>& lyrs,
1314                                         std::vector<int>& idxs,
1315                                         std::vector<int>& mcTkIDs,
1316                                         std::vector<float>& xs,
1317                                         std::vector<float>& ys,
1318                                         std::vector<float>& zs) {
1319     // get event info
1320     const auto& layerHits = ev.layerHits_;
1321     const auto& simHitsInfo = ev.simHitsInfo_;
1322 
1323     // resize vectors
1324     const auto nTotalHits = track.nTotalHits();
1325     lyrs.resize(nTotalHits);
1326     idxs.resize(nTotalHits);
1327     mcTkIDs.resize(nTotalHits, -99);
1328     xs.resize(nTotalHits, -9999.f);
1329     ys.resize(nTotalHits, -9999.f);
1330     zs.resize(nTotalHits, -9999.f);
1331 
1332     // loop over size of total hits
1333     for (auto ihit = 0; ihit < nTotalHits; ihit++) {
1334       const auto lyr = track.getHitLyr(ihit);
1335       const auto idx = track.getHitIdx(ihit);
1336 
1337       lyrs[ihit] = lyr;
1338       idxs[ihit] = idx;
1339 
1340       if (lyr < 0)
1341         continue;
1342       if (idx < 0)
1343         continue;
1344 
1345       const auto& hit = layerHits[lyr][idx];
1346       mcTkIDs[ihit] = hit.mcTrackID(simHitsInfo);
1347       xs[ihit] = hit.x();
1348       ys[ihit] = hit.y();
1349       zs[ihit] = hit.z();
1350     }
1351   }
1352 
1353   void TTreeValidation::fillMinHitInfo(const Track& track, std::vector<int>& lyrs, std::vector<int>& idxs) {
1354     for (int ihit = 0; ihit < track.nTotalHits(); ihit++) {
1355       lyrs.emplace_back(track.getHitLyr(ihit));
1356       idxs.emplace_back(track.getHitIdx(ihit));
1357     }
1358   }
1359 
1360   void TTreeValidation::fillEfficiencyTree(const Event& ev) {
1361     std::lock_guard<std::mutex> locker(glock_);
1362 
1363     const auto ievt = ev.evtID();
1364     const auto& evt_sim_tracks = ev.simTracks_;
1365     const auto& evt_seed_tracks = ev.seedTracks_;
1366     const auto& evt_seed_extras = ev.seedTracksExtra_;
1367     const auto& evt_build_tracks = ev.candidateTracks_;
1368     const auto& evt_build_extras = ev.candidateTracksExtra_;
1369     const auto& evt_fit_tracks = ev.fitTracks_;
1370     const auto& evt_fit_extras = ev.fitTracksExtra_;
1371     const auto& evt_layer_hits = ev.layerHits_;
1372     const auto& evt_sim_trackstates = ev.simTrackStates_;
1373 
1374     unsigned int count = 0;
1375     for (const auto& simtrack : evt_sim_tracks) {
1376       // clear the branches first
1377       if (Config::keepHitInfo) {
1378         hitlyrs_mc_eff_.clear();
1379         hitlyrs_seed_eff_.clear();
1380         hitlyrs_build_eff_.clear();
1381         hitlyrs_fit_eff_.clear();
1382 
1383         hitidxs_mc_eff_.clear();
1384         hitidxs_seed_eff_.clear();
1385         hitidxs_build_eff_.clear();
1386         hitidxs_fit_eff_.clear();
1387 
1388         hitmcTkIDs_mc_eff_.clear();
1389         hitmcTkIDs_seed_eff_.clear();
1390         hitmcTkIDs_build_eff_.clear();
1391         hitmcTkIDs_fit_eff_.clear();
1392 
1393         hitxs_mc_eff_.clear();
1394         hitxs_seed_eff_.clear();
1395         hitxs_build_eff_.clear();
1396         hitxs_fit_eff_.clear();
1397 
1398         hitys_mc_eff_.clear();
1399         hitys_seed_eff_.clear();
1400         hitys_build_eff_.clear();
1401         hitys_fit_eff_.clear();
1402 
1403         hitzs_mc_eff_.clear();
1404         hitzs_seed_eff_.clear();
1405         hitzs_build_eff_.clear();
1406         hitzs_fit_eff_.clear();
1407       }
1408 
1409       evtID_eff_ = ievt;
1410       mcID_eff_ = simtrack.label();
1411 
1412       // generated values
1413       x_mc_gen_eff_ = simtrack.x();
1414       y_mc_gen_eff_ = simtrack.y();
1415       z_mc_gen_eff_ = simtrack.z();
1416 
1417       pt_mc_gen_eff_ = simtrack.pT();
1418       phi_mc_gen_eff_ = simtrack.momPhi();
1419       eta_mc_gen_eff_ = simtrack.momEta();
1420       nHits_mc_eff_ = simtrack.nFoundHits();  // could be that the sim track skips layers!
1421       nLayers_mc_eff_ = simtrack.nUniqueLayers();
1422       lastlyr_mc_eff_ = simtrack.getLastFoundHitLyr();
1423 
1424       itermask_seed_eff_ = 0;
1425       itermask_build_eff_ = 0;
1426       itermask_fit_eff_ = 0;
1427       iterduplmask_seed_eff_ = 0;
1428       iterduplmask_build_eff_ = 0;
1429       iterduplmask_fit_eff_ = 0;
1430       algo_seed_eff_ = 0;
1431 
1432       if (Config::mtvRequireSeeds) {
1433         for (auto aa : ev.simTracksExtra_[count].seedAlgos()) {
1434           algo_seed_eff_ = (algo_seed_eff_ | (1 << aa));
1435         }
1436       }
1437       count++;
1438 
1439       // hit indices
1440       if (Config::keepHitInfo)
1441         TTreeValidation::fillFullHitInfo(ev,
1442                                          simtrack,
1443                                          hitlyrs_mc_eff_,
1444                                          hitidxs_mc_eff_,
1445                                          hitmcTkIDs_mc_eff_,
1446                                          hitxs_mc_eff_,
1447                                          hitys_mc_eff_,
1448                                          hitzs_mc_eff_);
1449 
1450       // matched seed track
1451       if (simToSeedMap_.count(mcID_eff_) &&
1452           simtrack
1453               .isFindable())  // recoToSim match : save best match with best score, i.e. simToSeedMap_[matched SimID][first element in vector]
1454       {
1455         for (unsigned int ii = 0; ii < simToSeedMap_[mcID_eff_].size(); ii++) {
1456           const int theAlgo = evt_seed_tracks[simToSeedMap_[mcID_eff_][ii]].algoint();
1457           if ((itermask_seed_eff_ >> theAlgo) & 1)
1458             iterduplmask_seed_eff_ = (iterduplmask_seed_eff_ | (1 << theAlgo));  //filled at the second time
1459           itermask_seed_eff_ = (itermask_seed_eff_ | (1 << theAlgo));
1460         }
1461         const auto& seedtrack =
1462             evt_seed_tracks[simToSeedMap_[mcID_eff_][0]];            // returns seedTrack best matched to sim track
1463         const auto& seedextra = evt_seed_extras[seedtrack.label()];  // returns track extra best aligned with seed track
1464         mcmask_seed_eff_ = 1;                                        // quick logic for matched
1465 
1466         seedID_seed_eff_ = seedextra.seedID();
1467 
1468         // use this to access correct sim track layer params
1469         const int mcHitID =
1470             TTreeValidation::getLastFoundHit(seedtrack.getLastFoundMCHitID(evt_layer_hits), mcID_eff_, ev);
1471         if (mcHitID >= 0 && Config::readSimTrackStates) {
1472           const TrackState& initLayTS = evt_sim_trackstates[mcHitID];
1473 
1474           pt_mc_seed_eff_ = initLayTS.pT();
1475           phi_mc_seed_eff_ = initLayTS.momPhi();
1476           eta_mc_seed_eff_ = initLayTS.momEta();
1477           helixchi2_seed_eff_ = computeHelixChi2(initLayTS.parameters, seedtrack.parameters(), seedtrack.errors());
1478 
1479           mcTSmask_seed_eff_ = 1;
1480         } else if (Config::tryToSaveSimInfo)  // can enter this block if: we actually read sim track states, but could not find the mchit OR we chose not to read the sim track states
1481         {
1482           // reuse info already set
1483           pt_mc_seed_eff_ = pt_mc_gen_eff_;
1484           phi_mc_seed_eff_ = phi_mc_gen_eff_;
1485           eta_mc_seed_eff_ = eta_mc_gen_eff_;
1486           helixchi2_seed_eff_ = computeHelixChi2(simtrack.parameters(), seedtrack.parameters(), seedtrack.errors());
1487 
1488           mcTSmask_seed_eff_ = 0;
1489         } else {
1490           pt_mc_seed_eff_ = -101;
1491           phi_mc_seed_eff_ = -101;
1492           eta_mc_seed_eff_ = -101;
1493           helixchi2_seed_eff_ = -101;
1494 
1495           mcTSmask_seed_eff_ = -2;
1496         }
1497 
1498         // last hit info
1499         const Hit& lasthit = evt_layer_hits[seedtrack.getLastFoundHitLyr()][seedtrack.getLastFoundHitIdx()];
1500         xhit_seed_eff_ = lasthit.x();
1501         yhit_seed_eff_ = lasthit.y();
1502         zhit_seed_eff_ = lasthit.z();
1503 
1504         pt_seed_eff_ = seedtrack.pT();
1505         ept_seed_eff_ = seedtrack.epT();
1506         phi_seed_eff_ = seedtrack.momPhi();
1507         ephi_seed_eff_ = seedtrack.emomPhi();
1508         eta_seed_eff_ = seedtrack.momEta();
1509         eeta_seed_eff_ = seedtrack.emomEta();
1510 
1511         // rest of mc info
1512         nHits_seed_eff_ = seedtrack.nFoundHits();
1513         nLayers_seed_eff_ = seedtrack.nUniqueLayers();
1514         nHitsMatched_seed_eff_ = seedextra.nHitsMatched();
1515         fracHitsMatched_seed_eff_ = seedextra.fracHitsMatched();
1516         lastlyr_seed_eff_ = seedtrack.getLastFoundHitLyr();
1517 
1518         // swim dphi
1519         dphi_seed_eff_ = seedextra.dPhi();
1520 
1521         // quality info
1522         hitchi2_seed_eff_ = seedtrack.chi2();  // currently not being used
1523         score_seed_eff_ = seedtrack.score();   // currently a constant by definition
1524 
1525         duplmask_seed_eff_ = seedextra.isDuplicate();
1526         nTkMatches_seed_eff_ = simToSeedMap_[mcID_eff_].size();  // n reco matches to this sim track.
1527 
1528         // hit indices
1529         if (Config::keepHitInfo)
1530           TTreeValidation::fillFullHitInfo(ev,
1531                                            seedtrack,
1532                                            hitlyrs_seed_eff_,
1533                                            hitidxs_seed_eff_,
1534                                            hitmcTkIDs_seed_eff_,
1535                                            hitxs_seed_eff_,
1536                                            hitys_seed_eff_,
1537                                            hitzs_seed_eff_);
1538       } else  // unmatched simTracks ... put -99 for all reco values to denote unmatched
1539       {
1540         mcmask_seed_eff_ = (simtrack.isFindable() ? 0 : -1);  // quick logic for not matched
1541 
1542         seedID_seed_eff_ = -99;
1543 
1544         pt_mc_seed_eff_ = -99;
1545         phi_mc_seed_eff_ = -99;
1546         eta_mc_seed_eff_ = -99;
1547         helixchi2_seed_eff_ = -99;
1548 
1549         mcTSmask_seed_eff_ = -1;  // mask means unmatched sim track
1550 
1551         xhit_seed_eff_ = -2000;
1552         yhit_seed_eff_ = -2000;
1553         zhit_seed_eff_ = -2000;
1554 
1555         pt_seed_eff_ = -99;
1556         ept_seed_eff_ = -99;
1557         phi_seed_eff_ = -99;
1558         ephi_seed_eff_ = -99;
1559         eta_seed_eff_ = -99;
1560         eeta_seed_eff_ = -99;
1561 
1562         nHits_seed_eff_ = -99;
1563         nLayers_seed_eff_ = -99;
1564         nHitsMatched_seed_eff_ = -99;
1565         fracHitsMatched_seed_eff_ = -99;
1566         lastlyr_seed_eff_ = -99;
1567 
1568         dphi_seed_eff_ = -99;
1569 
1570         hitchi2_seed_eff_ = -99;
1571         score_seed_eff_ = -17000;
1572 
1573         duplmask_seed_eff_ = -1;     // mask means unmatched sim track
1574         nTkMatches_seed_eff_ = -99;  // unmatched
1575       }
1576 
1577       // matched build track
1578       if (simToBuildMap_.count(mcID_eff_) &&
1579           simtrack
1580               .isFindable())  // recoToSim match : save best match with best score i.e. simToBuildMap_[matched SimID][first element in vector]
1581       {
1582         for (unsigned int ii = 0; ii < simToBuildMap_[mcID_eff_].size(); ii++) {
1583           const int theAlgo = evt_build_tracks[simToBuildMap_[mcID_eff_][ii]].algoint();
1584           if ((itermask_build_eff_ >> theAlgo) & 1)
1585             iterduplmask_build_eff_ = (iterduplmask_build_eff_ | (1 << theAlgo));  //filled at the second time
1586           itermask_build_eff_ = (itermask_build_eff_ | (1 << theAlgo));
1587         }
1588         const auto& buildtrack =
1589             evt_build_tracks[simToBuildMap_[mcID_eff_][0]];  // returns buildTrack best matched to sim track
1590         const auto& buildextra =
1591             evt_build_extras[buildtrack.label()];  // returns track extra best aligned with build track
1592         mcmask_build_eff_ = 1;                     // quick logic for matched
1593 
1594         seedID_build_eff_ = buildextra.seedID();
1595 
1596         // use this to access correct sim track layer params
1597         const int mcHitID =
1598             TTreeValidation::getLastFoundHit(buildtrack.getLastFoundMCHitID(evt_layer_hits), mcID_eff_, ev);
1599         if (mcHitID >= 0 && Config::readSimTrackStates) {
1600           const TrackState& initLayTS = evt_sim_trackstates[mcHitID];
1601 
1602           pt_mc_build_eff_ = initLayTS.pT();
1603           phi_mc_build_eff_ = initLayTS.momPhi();
1604           eta_mc_build_eff_ = initLayTS.momEta();
1605           helixchi2_build_eff_ = computeHelixChi2(initLayTS.parameters, buildtrack.parameters(), buildtrack.errors());
1606 
1607           mcTSmask_build_eff_ = 1;
1608         } else if (Config::tryToSaveSimInfo)  // can enter this block if: we actually read sim track states, but could not find the mchit OR we chose not to read the sim track states
1609         {
1610           // reuse info already set
1611           pt_mc_build_eff_ = pt_mc_gen_eff_;
1612           phi_mc_build_eff_ = phi_mc_gen_eff_;
1613           eta_mc_build_eff_ = eta_mc_gen_eff_;
1614           helixchi2_build_eff_ = computeHelixChi2(simtrack.parameters(), buildtrack.parameters(), buildtrack.errors());
1615 
1616           mcTSmask_build_eff_ = 0;
1617         } else {
1618           pt_mc_build_eff_ = -101;
1619           phi_mc_build_eff_ = -101;
1620           eta_mc_build_eff_ = -101;
1621           helixchi2_build_eff_ = -101;
1622 
1623           mcTSmask_build_eff_ = -2;
1624         }
1625 
1626         // last hit info
1627         const Hit& lasthit = evt_layer_hits[buildtrack.getLastFoundHitLyr()][buildtrack.getLastFoundHitIdx()];
1628         xhit_build_eff_ = lasthit.x();
1629         yhit_build_eff_ = lasthit.y();
1630         zhit_build_eff_ = lasthit.z();
1631 
1632         pt_build_eff_ = buildtrack.pT();
1633         ept_build_eff_ = buildtrack.epT();
1634         phi_build_eff_ = buildtrack.momPhi();
1635         ephi_build_eff_ = buildtrack.emomPhi();
1636         eta_build_eff_ = buildtrack.momEta();
1637         eeta_build_eff_ = buildtrack.emomEta();
1638 
1639         nHits_build_eff_ = buildtrack.nFoundHits();
1640         nLayers_build_eff_ = buildtrack.nUniqueLayers();
1641         nHitsMatched_build_eff_ = buildextra.nHitsMatched();
1642         fracHitsMatched_build_eff_ = buildextra.fracHitsMatched();
1643         lastlyr_build_eff_ = buildtrack.getLastFoundHitLyr();
1644 
1645         // swim dphi
1646         dphi_build_eff_ = buildextra.dPhi();
1647 
1648         // quality info
1649         hitchi2_build_eff_ = buildtrack.chi2();
1650         score_build_eff_ = buildtrack.score();
1651 
1652         duplmask_build_eff_ = buildextra.isDuplicate();
1653         nTkMatches_build_eff_ = simToBuildMap_[mcID_eff_].size();  // n reco matches to this sim track.
1654 
1655         // hit indices
1656         if (Config::keepHitInfo)
1657           TTreeValidation::fillFullHitInfo(ev,
1658                                            buildtrack,
1659                                            hitlyrs_build_eff_,
1660                                            hitidxs_build_eff_,
1661                                            hitmcTkIDs_build_eff_,
1662                                            hitxs_build_eff_,
1663                                            hitys_build_eff_,
1664                                            hitzs_build_eff_);
1665       } else  // unmatched simTracks ... put -99 for all reco values to denote unmatched
1666       {
1667         mcmask_build_eff_ = (simtrack.isFindable() ? 0 : -1);  // quick logic for not matched
1668 
1669         seedID_build_eff_ = -99;
1670 
1671         pt_mc_build_eff_ = -99;
1672         phi_mc_build_eff_ = -99;
1673         eta_mc_build_eff_ = -99;
1674         helixchi2_build_eff_ = -99;
1675 
1676         mcTSmask_build_eff_ = -1;
1677 
1678         xhit_build_eff_ = -2000;
1679         yhit_build_eff_ = -2000;
1680         zhit_build_eff_ = -2000;
1681 
1682         pt_build_eff_ = -99;
1683         ept_build_eff_ = -99;
1684         phi_build_eff_ = -99;
1685         ephi_build_eff_ = -99;
1686         eta_build_eff_ = -99;
1687         eeta_build_eff_ = -99;
1688 
1689         nHits_build_eff_ = -99;
1690         nLayers_build_eff_ = -99;
1691         nHitsMatched_build_eff_ = -99;
1692         fracHitsMatched_build_eff_ = -99;
1693         lastlyr_build_eff_ = -99;
1694 
1695         dphi_build_eff_ = -99;
1696 
1697         hitchi2_build_eff_ = -99;
1698         score_build_eff_ = -17000;
1699 
1700         duplmask_build_eff_ = -1;     // mask means unmatched sim track
1701         nTkMatches_build_eff_ = -99;  // unmatched
1702       }
1703 
1704       // matched fit track
1705       if (simToFitMap_.count(mcID_eff_) &&
1706           simtrack
1707               .isFindable())  // recoToSim match : save best match with best score i.e. simToFitMap_[matched SimID][first element in vector]
1708       {
1709         for (unsigned int ii = 0; ii < simToFitMap_[mcID_eff_].size(); ii++) {
1710           const int theAlgo = evt_fit_tracks[simToFitMap_[mcID_eff_][ii]].algoint();
1711           if ((itermask_fit_eff_ >> theAlgo) & 1)
1712             iterduplmask_fit_eff_ = (iterduplmask_fit_eff_ | (1 << theAlgo));  //filled at the second time
1713           itermask_fit_eff_ = (itermask_fit_eff_ | (1 << theAlgo));
1714         }
1715         const auto& fittrack =
1716             evt_fit_tracks[simToFitMap_[mcID_eff_][0]];           // returns fitTrack best matched to sim track
1717         const auto& fitextra = evt_fit_extras[fittrack.label()];  // returns track extra best aligned with fit track
1718         mcmask_fit_eff_ = 1;                                      // quick logic for matched
1719 
1720         seedID_fit_eff_ = fitextra.seedID();
1721 
1722         // use this to access correct sim track layer params
1723         const int mcHitID =
1724             TTreeValidation::getLastFoundHit(fittrack.getLastFoundMCHitID(evt_layer_hits), mcID_eff_, ev);
1725         if (mcHitID >= 0 && Config::readSimTrackStates) {
1726           const TrackState& initLayTS = evt_sim_trackstates[mcHitID];
1727 
1728           pt_mc_fit_eff_ = initLayTS.pT();
1729           phi_mc_fit_eff_ = initLayTS.momPhi();
1730           eta_mc_fit_eff_ = initLayTS.momEta();
1731           helixchi2_fit_eff_ = computeHelixChi2(initLayTS.parameters, fittrack.parameters(), fittrack.errors());
1732 
1733           mcTSmask_fit_eff_ = 1;
1734         } else if (Config::tryToSaveSimInfo)  // can enter this block if: we actually read sim track states, but could not find the mchit OR we chose not to read the sim track states
1735         {
1736           // reuse info already set
1737           pt_mc_fit_eff_ = pt_mc_gen_eff_;
1738           phi_mc_fit_eff_ = phi_mc_gen_eff_;
1739           eta_mc_fit_eff_ = eta_mc_gen_eff_;
1740           helixchi2_fit_eff_ = computeHelixChi2(simtrack.parameters(), fittrack.parameters(), fittrack.errors());
1741 
1742           mcTSmask_fit_eff_ = 0;
1743         } else {
1744           pt_mc_fit_eff_ = -101;
1745           phi_mc_fit_eff_ = -101;
1746           eta_mc_fit_eff_ = -101;
1747           helixchi2_fit_eff_ = -101;
1748 
1749           mcTSmask_fit_eff_ = -2;
1750         }
1751 
1752         // last hit info
1753         const Hit& lasthit = evt_layer_hits[fittrack.getLastFoundHitLyr()][fittrack.getLastFoundHitIdx()];
1754         xhit_fit_eff_ = lasthit.x();
1755         yhit_fit_eff_ = lasthit.y();
1756         zhit_fit_eff_ = lasthit.z();
1757 
1758         pt_fit_eff_ = fittrack.pT();
1759         ept_fit_eff_ = fittrack.epT();
1760         phi_fit_eff_ = fittrack.momPhi();
1761         ephi_fit_eff_ = fittrack.emomPhi();
1762         eta_fit_eff_ = fittrack.momEta();
1763         eeta_fit_eff_ = fittrack.emomEta();
1764 
1765         // rest of mc info
1766         nHits_fit_eff_ = fittrack.nFoundHits();
1767         nLayers_fit_eff_ = fittrack.nUniqueLayers();
1768         nHitsMatched_fit_eff_ = fitextra.nHitsMatched();
1769         fracHitsMatched_fit_eff_ = fitextra.fracHitsMatched();
1770         lastlyr_fit_eff_ = fittrack.getLastFoundHitLyr();
1771 
1772         // swim dphi
1773         dphi_fit_eff_ = fitextra.dPhi();
1774 
1775         // quality info
1776         hitchi2_fit_eff_ = fittrack.chi2();  // -10 when not used
1777         score_fit_eff_ = fittrack.score();
1778 
1779         duplmask_fit_eff_ = fitextra.isDuplicate();
1780         nTkMatches_fit_eff_ = simToFitMap_[mcID_eff_].size();  // n reco matches to this sim track.
1781 
1782         // hit indices
1783         if (Config::keepHitInfo)
1784           TTreeValidation::fillFullHitInfo(ev,
1785                                            fittrack,
1786                                            hitlyrs_fit_eff_,
1787                                            hitidxs_fit_eff_,
1788                                            hitmcTkIDs_fit_eff_,
1789                                            hitxs_fit_eff_,
1790                                            hitys_fit_eff_,
1791                                            hitzs_fit_eff_);
1792       } else  // unmatched simTracks ... put -99 for all reco values to denote unmatched
1793       {
1794         mcmask_fit_eff_ = (simtrack.isFindable() ? 0 : -1);  // quick logic for not matched
1795 
1796         seedID_fit_eff_ = -99;
1797 
1798         pt_mc_fit_eff_ = -99;
1799         phi_mc_fit_eff_ = -99;
1800         eta_mc_fit_eff_ = -99;
1801         helixchi2_fit_eff_ = -99;
1802 
1803         mcTSmask_fit_eff_ = -1;
1804 
1805         xhit_fit_eff_ = -2000;
1806         yhit_fit_eff_ = -2000;
1807         zhit_fit_eff_ = -2000;
1808 
1809         pt_fit_eff_ = -99;
1810         ept_fit_eff_ = -99;
1811         phi_fit_eff_ = -99;
1812         ephi_fit_eff_ = -99;
1813         eta_fit_eff_ = -99;
1814         eeta_fit_eff_ = -99;
1815 
1816         nHits_fit_eff_ = -99;
1817         nLayers_fit_eff_ = -99;
1818         nHitsMatched_fit_eff_ = -99;
1819         fracHitsMatched_fit_eff_ = -99;
1820         lastlyr_fit_eff_ = -99;
1821 
1822         dphi_fit_eff_ = -99;
1823 
1824         hitchi2_fit_eff_ = -99;
1825         score_fit_eff_ = -17000;
1826 
1827         duplmask_fit_eff_ = -1;     // mask means unmatched sim track
1828         nTkMatches_fit_eff_ = -99;  // unmatched
1829       }
1830 
1831       efftree_->Fill();  // fill it once per sim track!
1832     }
1833   }
1834 
1835   void TTreeValidation::fillFakeRateTree(const Event& ev) {
1836     std::lock_guard<std::mutex> locker(glock_);
1837 
1838     const auto ievt = ev.evtID();
1839     const auto& evt_sim_tracks =
1840         ev.simTracks_;  // store sim info at that final layer!!! --> gen info stored only in eff tree
1841     const auto& evt_seed_tracks = ev.seedTracks_;
1842     const auto& evt_seed_extras = ev.seedTracksExtra_;
1843     const auto& evt_build_tracks = ev.candidateTracks_;
1844     const auto& evt_build_extras = ev.candidateTracksExtra_;
1845     const auto& evt_fit_tracks = ev.fitTracks_;
1846     const auto& evt_fit_extras = ev.fitTracksExtra_;
1847     const auto& evt_layer_hits = ev.layerHits_;
1848     const auto& evt_sim_trackstates = ev.simTrackStates_;
1849 
1850     for (const auto& seedtrack : evt_seed_tracks) {
1851       if (Config::keepHitInfo) {
1852         hitlyrs_seed_FR_.clear();
1853         hitlyrs_mc_seed_FR_.clear();
1854         hitlyrs_build_FR_.clear();
1855         hitlyrs_mc_build_FR_.clear();
1856         hitlyrs_fit_FR_.clear();
1857         hitlyrs_mc_fit_FR_.clear();
1858 
1859         hitidxs_seed_FR_.clear();
1860         hitidxs_mc_seed_FR_.clear();
1861         hitidxs_build_FR_.clear();
1862         hitidxs_mc_build_FR_.clear();
1863         hitidxs_fit_FR_.clear();
1864         hitidxs_mc_fit_FR_.clear();
1865 
1866         hitmcTkIDs_seed_FR_.clear();
1867         hitmcTkIDs_mc_seed_FR_.clear();
1868         hitmcTkIDs_build_FR_.clear();
1869         hitmcTkIDs_mc_build_FR_.clear();
1870         hitmcTkIDs_fit_FR_.clear();
1871         hitmcTkIDs_mc_fit_FR_.clear();
1872 
1873         hitxs_seed_FR_.clear();
1874         hitxs_mc_seed_FR_.clear();
1875         hitxs_build_FR_.clear();
1876         hitxs_mc_build_FR_.clear();
1877         hitxs_fit_FR_.clear();
1878         hitxs_mc_fit_FR_.clear();
1879 
1880         hitys_seed_FR_.clear();
1881         hitys_mc_seed_FR_.clear();
1882         hitys_build_FR_.clear();
1883         hitys_mc_build_FR_.clear();
1884         hitys_fit_FR_.clear();
1885         hitys_mc_fit_FR_.clear();
1886 
1887         hitzs_seed_FR_.clear();
1888         hitzs_mc_seed_FR_.clear();
1889         hitzs_build_FR_.clear();
1890         hitzs_mc_build_FR_.clear();
1891         hitzs_fit_FR_.clear();
1892         hitzs_mc_fit_FR_.clear();
1893       }
1894 
1895       evtID_FR_ = ievt;
1896 
1897       // seed info
1898       const auto& seedextra = evt_seed_extras[seedtrack.label()];
1899       seedID_FR_ = seedextra.seedID();
1900       seedmask_seed_FR_ =
1901           1;  // automatically set to 1, because at the moment no cuts on seeds after conformal+KF fit.  seed triplets filtered by RZ chi2 before fitting.
1902 
1903       // last hit info
1904       // const Hit& lasthit = evt_layer_hits[seedtrack.getLastFoundHitLyr()][seedtrack.getLastFoundHitIdx()];
1905       xhit_seed_FR_ = 0;  //lasthit.x();
1906       yhit_seed_FR_ = 0;  //lasthit.y();
1907       zhit_seed_FR_ = 0;  //lasthit.z();
1908 
1909       pt_seed_FR_ = seedtrack.pT();
1910       ept_seed_FR_ = seedtrack.epT();
1911       phi_seed_FR_ = seedtrack.momPhi();
1912       ephi_seed_FR_ = seedtrack.emomPhi();
1913       eta_seed_FR_ = seedtrack.momEta();
1914       eeta_seed_FR_ = seedtrack.emomEta();
1915 
1916       nHits_seed_FR_ = seedtrack.nFoundHits();
1917       nLayers_seed_FR_ = seedtrack.nUniqueLayers();
1918       nHitsMatched_seed_FR_ = seedextra.nHitsMatched();
1919       fracHitsMatched_seed_FR_ = seedextra.fracHitsMatched();
1920       lastlyr_seed_FR_ = seedtrack.getLastFoundHitLyr();
1921 
1922       algorithm_FR_ = seedtrack.algoint();
1923 
1924       // swim dphi
1925       dphi_seed_FR_ = seedextra.dPhi();
1926 
1927       // quality info
1928       hitchi2_seed_FR_ = seedtrack.chi2();  //--> not currently used
1929       score_seed_FR_ = seedtrack.score();
1930 
1931       if (Config::keepHitInfo)
1932         TTreeValidation::fillFullHitInfo(ev,
1933                                          seedtrack,
1934                                          hitlyrs_seed_FR_,
1935                                          hitidxs_seed_FR_,
1936                                          hitmcTkIDs_seed_FR_,
1937                                          hitxs_seed_FR_,
1938                                          hitys_seed_FR_,
1939                                          hitzs_seed_FR_);
1940 
1941       // sim info for seed track
1942       mcID_seed_FR_ = seedextra.mcTrackID();
1943       mcmask_seed_FR_ = TTreeValidation::getMaskAssignment(mcID_seed_FR_);
1944 
1945       if (mcmask_seed_FR_ == 1)  // matched track to sim
1946       {
1947         const auto& simtrack = evt_sim_tracks[mcID_seed_FR_];
1948 
1949         const int mcHitID =
1950             TTreeValidation::getLastFoundHit(seedtrack.getLastFoundMCHitID(evt_layer_hits), mcID_seed_FR_, ev);
1951         if (mcHitID >= 0 && Config::readSimTrackStates) {
1952           const TrackState& initLayTS = evt_sim_trackstates[mcHitID];
1953           pt_mc_seed_FR_ = initLayTS.pT();
1954           phi_mc_seed_FR_ = initLayTS.momPhi();
1955           eta_mc_seed_FR_ = initLayTS.momEta();
1956           helixchi2_seed_FR_ = computeHelixChi2(initLayTS.parameters, seedtrack.parameters(), seedtrack.errors());
1957 
1958           mcTSmask_seed_FR_ = 1;
1959         } else if (Config::tryToSaveSimInfo) {
1960           pt_mc_seed_FR_ = simtrack.pT();
1961           phi_mc_seed_FR_ = simtrack.momPhi();
1962           eta_mc_seed_FR_ = simtrack.momEta();
1963           helixchi2_seed_FR_ = computeHelixChi2(simtrack.parameters(), seedtrack.parameters(), seedtrack.errors());
1964 
1965           mcTSmask_seed_FR_ = 0;
1966         } else {
1967           pt_mc_seed_FR_ = -101;
1968           phi_mc_seed_FR_ = -101;
1969           eta_mc_seed_FR_ = -101;
1970           helixchi2_seed_FR_ = -101;
1971 
1972           mcTSmask_seed_FR_ = -2;
1973         }
1974 
1975         nHits_mc_seed_FR_ = simtrack.nFoundHits();
1976         nLayers_mc_seed_FR_ = simtrack.nUniqueLayers();
1977         lastlyr_mc_seed_FR_ = simtrack.getLastFoundHitLyr();
1978 
1979         duplmask_seed_FR_ = seedextra.isDuplicate();
1980         iTkMatches_seed_FR_ =
1981             seedextra
1982                 .duplicateID();  // ith duplicate seed track, i = 0 "best" match, i > 0 "still matched, real reco, not as good as i-1 track"
1983 
1984         if (Config::keepHitInfo)
1985           TTreeValidation::fillFullHitInfo(ev,
1986                                            simtrack,
1987                                            hitlyrs_mc_seed_FR_,
1988                                            hitidxs_mc_seed_FR_,
1989                                            hitmcTkIDs_mc_seed_FR_,
1990                                            hitxs_mc_seed_FR_,
1991                                            hitys_mc_seed_FR_,
1992                                            hitzs_mc_seed_FR_);
1993       } else {
1994         // -99 for all sim info for reco tracks not associated to reco tracks
1995         pt_mc_seed_FR_ = -99;
1996         phi_mc_seed_FR_ = -99;
1997         eta_mc_seed_FR_ = -99;
1998         helixchi2_seed_FR_ = -99;
1999 
2000         mcTSmask_seed_FR_ = -1;
2001 
2002         nHits_mc_seed_FR_ = -99;
2003         nLayers_mc_seed_FR_ = -99;
2004         lastlyr_mc_seed_FR_ = -99;
2005 
2006         duplmask_seed_FR_ = -1;
2007         iTkMatches_seed_FR_ = -99;
2008       }
2009 
2010       //==========================//
2011 
2012       // fill build information if track still alive
2013       if (seedToBuildMap_.count(seedID_FR_)) {
2014         seedmask_build_FR_ = 1;  // quick logic
2015 
2016         const auto& buildtrack = evt_build_tracks[seedToBuildMap_[seedID_FR_]];
2017         const auto& buildextra = evt_build_extras[buildtrack.label()];
2018 
2019         // last hit info
2020         const Hit& lasthit = evt_layer_hits[buildtrack.getLastFoundHitLyr()][buildtrack.getLastFoundHitIdx()];
2021         xhit_build_FR_ = lasthit.x();
2022         yhit_build_FR_ = lasthit.y();
2023         zhit_build_FR_ = lasthit.z();
2024 
2025         pt_build_FR_ = buildtrack.pT();
2026         ept_build_FR_ = buildtrack.epT();
2027         phi_build_FR_ = buildtrack.momPhi();
2028         ephi_build_FR_ = buildtrack.emomPhi();
2029         eta_build_FR_ = buildtrack.momEta();
2030         eeta_build_FR_ = buildtrack.emomEta();
2031 
2032         nHits_build_FR_ = buildtrack.nFoundHits();
2033         nLayers_build_FR_ = buildtrack.nUniqueLayers();
2034         nHitsMatched_build_FR_ = buildextra.nHitsMatched();
2035         fracHitsMatched_build_FR_ = buildextra.fracHitsMatched();
2036         lastlyr_build_FR_ = buildtrack.getLastFoundHitLyr();
2037 
2038         // swim dphi
2039         dphi_build_FR_ = buildextra.dPhi();
2040 
2041         // quality info
2042         hitchi2_build_FR_ = buildtrack.chi2();
2043         score_build_FR_ = buildtrack.score();
2044 
2045         if (Config::keepHitInfo)
2046           TTreeValidation::fillFullHitInfo(ev,
2047                                            buildtrack,
2048                                            hitlyrs_build_FR_,
2049                                            hitidxs_build_FR_,
2050                                            hitmcTkIDs_build_FR_,
2051                                            hitxs_build_FR_,
2052                                            hitys_build_FR_,
2053                                            hitzs_build_FR_);
2054 
2055         // sim info for build track
2056         mcID_build_FR_ = buildextra.mcTrackID();
2057         mcmask_build_FR_ = TTreeValidation::getMaskAssignment(mcID_build_FR_);
2058 
2059         if (mcmask_build_FR_ == 1)  // build track matched to seed and sim
2060         {
2061           const auto& simtrack = evt_sim_tracks[mcID_build_FR_];
2062 
2063           const int mcHitID =
2064               TTreeValidation::getLastFoundHit(buildtrack.getLastFoundMCHitID(evt_layer_hits), mcID_build_FR_, ev);
2065           if (mcHitID >= 0 && Config::readSimTrackStates) {
2066             const TrackState& initLayTS = evt_sim_trackstates[mcHitID];
2067             pt_mc_build_FR_ = initLayTS.pT();
2068             phi_mc_build_FR_ = initLayTS.momPhi();
2069             eta_mc_build_FR_ = initLayTS.momEta();
2070             helixchi2_build_FR_ = computeHelixChi2(initLayTS.parameters, buildtrack.parameters(), buildtrack.errors());
2071 
2072             mcTSmask_build_FR_ = 1;
2073           } else if (Config::tryToSaveSimInfo) {
2074             pt_mc_build_FR_ = simtrack.pT();
2075             phi_mc_build_FR_ = simtrack.momPhi();
2076             eta_mc_build_FR_ = simtrack.momEta();
2077             helixchi2_build_FR_ = computeHelixChi2(simtrack.parameters(), buildtrack.parameters(), buildtrack.errors());
2078 
2079             mcTSmask_build_FR_ = 0;
2080           } else {
2081             pt_mc_build_FR_ = -101;
2082             phi_mc_build_FR_ = -101;
2083             eta_mc_build_FR_ = -101;
2084             helixchi2_build_FR_ = -101;
2085 
2086             mcTSmask_build_FR_ = -2;
2087           }
2088 
2089           nHits_mc_build_FR_ = simtrack.nFoundHits();
2090           nLayers_mc_build_FR_ = simtrack.nUniqueLayers();
2091           lastlyr_mc_build_FR_ = simtrack.getLastFoundHitLyr();
2092 
2093           duplmask_build_FR_ = buildextra.isDuplicate();
2094           iTkMatches_build_FR_ =
2095               buildextra
2096                   .duplicateID();  // ith duplicate build track, i = 0 "best" match, i > 0 "still matched, real reco, not as good as i-1 track"
2097 
2098           if (Config::keepHitInfo)
2099             TTreeValidation::fillFullHitInfo(ev,
2100                                              simtrack,
2101                                              hitlyrs_mc_build_FR_,
2102                                              hitidxs_mc_build_FR_,
2103                                              hitmcTkIDs_mc_build_FR_,
2104                                              hitxs_mc_build_FR_,
2105                                              hitys_mc_build_FR_,
2106                                              hitzs_mc_build_FR_);
2107         } else  // build track matched only to seed not to sim
2108         {
2109           // -99 for all sim info for reco tracks not associated to reco tracks
2110           pt_mc_build_FR_ = -99;
2111           phi_mc_build_FR_ = -99;
2112           eta_mc_build_FR_ = -99;
2113           helixchi2_build_FR_ = -99;
2114 
2115           mcTSmask_build_FR_ = -1;
2116 
2117           nHits_mc_build_FR_ = -99;
2118           nLayers_mc_build_FR_ = -99;
2119           lastlyr_mc_build_FR_ = -99;
2120 
2121           duplmask_build_FR_ = -1;
2122           iTkMatches_build_FR_ = -99;
2123         }  // matched seed to build, not build to sim
2124       }
2125 
2126       else  // seed has no matching build track (therefore no matching sim to build track)
2127       {
2128         seedmask_build_FR_ = 0;  // quick logic
2129 
2130         // -3000 for position info if no build track for seed
2131         xhit_build_FR_ = -3000;
2132         yhit_build_FR_ = -3000;
2133         zhit_build_FR_ = -3000;
2134 
2135         // -100 for all reco info as no actual build track for this seed
2136         pt_build_FR_ = -100;
2137         ept_build_FR_ = -100;
2138         phi_build_FR_ = -100;
2139         ephi_build_FR_ = -100;
2140         eta_build_FR_ = -100;
2141         eeta_build_FR_ = -100;
2142 
2143         nHits_build_FR_ = -100;
2144         nLayers_build_FR_ = -100;
2145         nHitsMatched_build_FR_ = -100;
2146         fracHitsMatched_build_FR_ = -100;
2147         lastlyr_build_FR_ = -100;
2148 
2149         dphi_build_FR_ = -100;
2150 
2151         hitchi2_build_FR_ = -100;
2152         score_build_FR_ = -5001;
2153 
2154         // keep -100 for all sim variables as no such reco exists for this seed
2155         mcmask_build_FR_ = -2;  // do not want to count towards build FR
2156         mcID_build_FR_ = -100;
2157 
2158         pt_mc_build_FR_ = -100;
2159         phi_mc_build_FR_ = -100;
2160         eta_mc_build_FR_ = -100;
2161         helixchi2_build_FR_ = -100;
2162 
2163         mcTSmask_build_FR_ = -3;
2164 
2165         nHits_mc_build_FR_ = -100;
2166         nLayers_mc_build_FR_ = -100;
2167         lastlyr_mc_build_FR_ = -100;
2168 
2169         duplmask_build_FR_ = -2;
2170         iTkMatches_build_FR_ = -100;
2171       }
2172 
2173       //============================// fit tracks
2174       if (seedToFitMap_.count(seedID_FR_)) {
2175         seedmask_fit_FR_ = 1;  // quick logic
2176 
2177         const auto& fittrack = evt_fit_tracks[seedToFitMap_[seedID_FR_]];
2178         const auto& fitextra = evt_fit_extras[fittrack.label()];
2179 
2180         // last hit info
2181         const Hit& lasthit = evt_layer_hits[fittrack.getLastFoundHitLyr()][fittrack.getLastFoundHitIdx()];
2182         xhit_fit_FR_ = lasthit.x();
2183         yhit_fit_FR_ = lasthit.y();
2184         zhit_fit_FR_ = lasthit.z();
2185 
2186         pt_fit_FR_ = fittrack.pT();
2187         ept_fit_FR_ = fittrack.epT();
2188         phi_fit_FR_ = fittrack.momPhi();
2189         ephi_fit_FR_ = fittrack.emomPhi();
2190         eta_fit_FR_ = fittrack.momEta();
2191         eeta_fit_FR_ = fittrack.emomEta();
2192 
2193         nHits_fit_FR_ = fittrack.nFoundHits();
2194         nLayers_fit_FR_ = fittrack.nUniqueLayers();
2195         nHitsMatched_fit_FR_ = fitextra.nHitsMatched();
2196         fracHitsMatched_fit_FR_ = fitextra.fracHitsMatched();
2197         lastlyr_fit_FR_ = fittrack.getLastFoundHitLyr();
2198 
2199         // swim dphi
2200         dphi_fit_FR_ = fitextra.dPhi();
2201 
2202         // quality info
2203         hitchi2_fit_FR_ = fittrack.chi2();  // -10 when not used
2204         score_fit_FR_ = fittrack.score();
2205 
2206         if (Config::keepHitInfo)
2207           TTreeValidation::fillFullHitInfo(ev,
2208                                            fittrack,
2209                                            hitlyrs_fit_FR_,
2210                                            hitidxs_fit_FR_,
2211                                            hitmcTkIDs_fit_FR_,
2212                                            hitxs_fit_FR_,
2213                                            hitys_fit_FR_,
2214                                            hitzs_fit_FR_);
2215 
2216         // sim info for fit track
2217         mcID_fit_FR_ = fitextra.mcTrackID();
2218         mcmask_fit_FR_ = TTreeValidation::getMaskAssignment(mcID_fit_FR_);
2219 
2220         if (mcmask_fit_FR_ == 1)  // fit track matched to seed and sim
2221         {
2222           const auto& simtrack = evt_sim_tracks[mcID_fit_FR_];
2223 
2224           const int mcHitID = TTreeValidation::getLastFoundHit(
2225               fittrack.getLastFoundMCHitID(evt_layer_hits), mcID_fit_FR_, ev);  // only works for outward fit for now
2226           if (mcHitID >= 0 && Config::readSimTrackStates) {
2227             const TrackState& initLayTS = evt_sim_trackstates[mcHitID];
2228             pt_mc_fit_FR_ = initLayTS.pT();
2229             phi_mc_fit_FR_ = initLayTS.momPhi();
2230             eta_mc_fit_FR_ = initLayTS.momEta();
2231             helixchi2_fit_FR_ = computeHelixChi2(initLayTS.parameters, fittrack.parameters(), fittrack.errors());
2232 
2233             mcTSmask_fit_FR_ = 1;
2234           } else if (Config::tryToSaveSimInfo) {
2235             pt_mc_fit_FR_ = simtrack.pT();
2236             phi_mc_fit_FR_ = simtrack.momPhi();
2237             eta_mc_fit_FR_ = simtrack.momEta();
2238             helixchi2_fit_FR_ = computeHelixChi2(simtrack.parameters(), fittrack.parameters(), fittrack.errors());
2239 
2240             mcTSmask_fit_FR_ = 0;
2241           } else {
2242             pt_mc_fit_FR_ = -101;
2243             phi_mc_fit_FR_ = -101;
2244             eta_mc_fit_FR_ = -101;
2245             helixchi2_fit_FR_ = -101;
2246 
2247             mcTSmask_fit_FR_ = -2;
2248           }
2249 
2250           nHits_mc_fit_FR_ = simtrack.nFoundHits();
2251           nLayers_mc_fit_FR_ = simtrack.nUniqueLayers();
2252           lastlyr_mc_fit_FR_ = simtrack.getLastFoundHitLyr();
2253 
2254           duplmask_fit_FR_ = fitextra.isDuplicate();
2255           iTkMatches_fit_FR_ =
2256               fitextra
2257                   .duplicateID();  // ith duplicate fit track, i = 0 "best" match, i > 0 "still matched, real reco, not as good as i-1 track"
2258 
2259           if (Config::keepHitInfo)
2260             TTreeValidation::fillFullHitInfo(ev,
2261                                              simtrack,
2262                                              hitlyrs_mc_fit_FR_,
2263                                              hitidxs_mc_fit_FR_,
2264                                              hitmcTkIDs_mc_fit_FR_,
2265                                              hitxs_mc_fit_FR_,
2266                                              hitys_mc_fit_FR_,
2267                                              hitzs_mc_fit_FR_);
2268         } else  // fit track matched only to seed not to sim
2269         {
2270           // -99 for all sim info for reco tracks not associated to reco tracks
2271           pt_mc_fit_FR_ = -99;
2272           phi_mc_fit_FR_ = -99;
2273           eta_mc_fit_FR_ = -99;
2274           helixchi2_fit_FR_ = -99;
2275 
2276           mcTSmask_fit_FR_ = -1;
2277 
2278           nHits_mc_fit_FR_ = -99;
2279           nLayers_mc_fit_FR_ = -99;
2280           lastlyr_mc_fit_FR_ = -99;
2281 
2282           duplmask_fit_FR_ = -1;
2283           iTkMatches_fit_FR_ = -99;
2284         }  // matched seed to fit, not fit to sim
2285       }
2286 
2287       else  // seed has no matching fit track (therefore no matching sim to fit track)
2288       {
2289         seedmask_fit_FR_ = 0;  // quick logic
2290 
2291         // -3000 for position info if no fit track for seed
2292         xhit_fit_FR_ = -3000;
2293         yhit_fit_FR_ = -3000;
2294         zhit_fit_FR_ = -3000;
2295 
2296         // -100 for all reco info as no actual fit track for this seed
2297         pt_fit_FR_ = -100;
2298         ept_fit_FR_ = -100;
2299         phi_fit_FR_ = -100;
2300         ephi_fit_FR_ = -100;
2301         eta_fit_FR_ = -100;
2302         eeta_fit_FR_ = -100;
2303 
2304         nHits_fit_FR_ = -100;
2305         nLayers_fit_FR_ = -100;
2306         nHitsMatched_fit_FR_ = -100;
2307         fracHitsMatched_fit_FR_ = -100;
2308         lastlyr_fit_FR_ = -100;
2309 
2310         dphi_fit_FR_ = -100;
2311 
2312         hitchi2_fit_FR_ = -100;
2313         score_fit_FR_ = -5001;
2314 
2315         // keep -100 for all sim variables as no such reco exists for this seed
2316         mcmask_fit_FR_ = -2;  // do not want to count towards fit FR
2317         mcID_fit_FR_ = -100;
2318 
2319         pt_mc_fit_FR_ = -100;
2320         phi_mc_fit_FR_ = -100;
2321         eta_mc_fit_FR_ = -100;
2322         helixchi2_fit_FR_ = -100;
2323 
2324         mcTSmask_fit_FR_ = -3;
2325 
2326         nHits_mc_fit_FR_ = -100;
2327         nLayers_mc_fit_FR_ = -100;
2328         lastlyr_mc_fit_FR_ = -100;
2329 
2330         duplmask_fit_FR_ = -2;
2331         iTkMatches_fit_FR_ = -100;
2332       }
2333 
2334       frtree_->Fill();  // fill once per seed!
2335     }                   // end of seed to seed loop
2336   }
2337 
2338   void TTreeValidation::fillConfigTree() {
2339     std::lock_guard<std::mutex> locker(glock_);
2340 
2341     Ntracks_ = Config::nTracks;
2342     Nevents_ = Config::nEvents;
2343 
2344     nLayers_ = Config::nLayers;
2345 
2346     nlayers_per_seed_ = Config::ItrInfo[0].m_params.nlayers_per_seed;
2347     maxCand_ = Config::ItrInfo[0].m_params.maxCandsPerSeed;
2348     chi2Cut_min_ = Config::ItrInfo[0].m_params.chi2Cut_min;
2349     nSigma_ = Config::nSigma;
2350     minDPhi_ = Config::minDPhi;
2351     maxDPhi_ = Config::maxDPhi;
2352     minDEta_ = Config::minDEta;
2353     maxDEta_ = Config::maxDEta;
2354 
2355     beamspotX_ = Config::beamspotX;
2356     beamspotY_ = Config::beamspotY;
2357     beamspotZ_ = Config::beamspotZ;
2358 
2359     minSimPt_ = Config::minSimPt;
2360     maxSimPt_ = Config::maxSimPt;
2361 
2362     hitposerrXY_ = Config::hitposerrXY;
2363     hitposerrZ_ = Config::hitposerrZ;
2364     hitposerrR_ = Config::hitposerrR;
2365     varXY_ = Config::varXY;
2366     varZ_ = Config::varZ;
2367 
2368     ptinverr049_ = Config::ptinverr049;
2369     phierr049_ = Config::phierr049;
2370     thetaerr049_ = Config::thetaerr049;
2371     ptinverr012_ = Config::ptinverr012;
2372     phierr012_ = Config::phierr012;
2373     thetaerr012_ = Config::thetaerr012;
2374 
2375     configtree_->Fill();
2376   }
2377 
2378   void TTreeValidation::fillCMSSWEfficiencyTree(const Event& ev) {
2379     std::lock_guard<std::mutex> locker(glock_);
2380 
2381     const auto ievt = ev.evtID();
2382     const auto& evt_sim_tracks = ev.simTracks_;
2383     const auto& evt_cmssw_tracks = ev.cmsswTracks_;
2384     const auto& evt_cmssw_extras = ev.cmsswTracksExtra_;
2385     const auto& evt_build_tracks = ev.candidateTracks_;
2386     const auto& evt_build_extras = ev.candidateTracksExtra_;
2387     const auto& evt_fit_tracks = ev.fitTracks_;
2388     const auto& evt_fit_extras = ev.fitTracksExtra_;
2389     const auto& evt_layer_hits = ev.layerHits_;
2390 
2391     for (const auto& cmsswtrack : evt_cmssw_tracks) {
2392       // clear hit info
2393       if (Config::keepHitInfo) {
2394         hitlyrs_cmssw_ceff_.clear();
2395         hitlyrs_build_ceff_.clear();
2396         hitlyrs_mc_build_ceff_.clear();
2397         hitlyrs_fit_ceff_.clear();
2398         hitlyrs_mc_fit_ceff_.clear();
2399 
2400         hitidxs_cmssw_ceff_.clear();
2401         hitidxs_build_ceff_.clear();
2402         hitidxs_mc_build_ceff_.clear();
2403         hitidxs_fit_ceff_.clear();
2404         hitidxs_mc_fit_ceff_.clear();
2405       }
2406 
2407       const auto& cmsswextra = evt_cmssw_extras[cmsswtrack.label()];
2408 
2409       evtID_ceff_ = ievt;
2410       cmsswID_ceff_ = cmsswtrack.label();
2411       seedID_cmssw_ceff_ = cmsswextra.seedID();
2412 
2413       // PCA parameters
2414       x_cmssw_ceff_ = cmsswtrack.x();
2415       y_cmssw_ceff_ = cmsswtrack.y();
2416       z_cmssw_ceff_ = cmsswtrack.z();
2417 
2418       pt_cmssw_ceff_ = cmsswtrack.pT();
2419       phi_cmssw_ceff_ = cmsswtrack.momPhi();
2420       eta_cmssw_ceff_ = cmsswtrack.momEta();
2421 
2422       nHits_cmssw_ceff_ = cmsswtrack.nFoundHits();
2423       nLayers_cmssw_ceff_ = cmsswtrack.nUniqueLayers();
2424       lastlyr_cmssw_ceff_ = cmsswtrack.getLastFoundHitLyr();
2425 
2426       itermask_build_ceff_ = 0;
2427       itermask_fit_ceff_ = 0;
2428       iterduplmask_build_ceff_ = 0;
2429       iterduplmask_fit_ceff_ = 0;
2430       algo_seed_ceff_ = 0;
2431 
2432       for (auto aa : cmsswextra.seedAlgos())
2433         algo_seed_ceff_ = (algo_seed_ceff_ | (1 << aa));
2434 
2435       if (Config::keepHitInfo)
2436         TTreeValidation::fillMinHitInfo(cmsswtrack, hitlyrs_cmssw_ceff_, hitidxs_cmssw_ceff_);
2437 
2438       // matched build track
2439       if (cmsswToBuildMap_.count(cmsswID_ceff_) &&
2440           cmsswtrack
2441               .isFindable())  // recoToCmssw match : save best match with best score i.e. cmsswToBuildMap_[matched CmsswID][first element in vector]
2442       {
2443         for (unsigned int ii = 0; ii < cmsswToBuildMap_[cmsswID_ceff_].size(); ii++) {
2444           const int theAlgo = evt_build_tracks[cmsswToBuildMap_[cmsswID_ceff_][ii]].algoint();
2445           if ((itermask_build_ceff_ >> theAlgo) & 1)
2446             iterduplmask_build_ceff_ = (iterduplmask_build_ceff_ | (1 << theAlgo));  //filled at the second time
2447           itermask_build_ceff_ = (itermask_build_ceff_ | (1 << theAlgo));
2448         }
2449 
2450         const auto& buildtrack =
2451             evt_build_tracks[cmsswToBuildMap_[cmsswID_ceff_][0]];  // returns buildTrack best matched to cmssw track
2452         const auto& buildextra =
2453             evt_build_extras[buildtrack.label()];  // returns track extra best aligned with build track
2454         cmsswmask_build_ceff_ = 1;                 // quick logic for matched
2455 
2456         seedID_build_ceff_ = buildextra.seedID();
2457         mcTrackID_build_ceff_ = buildextra.mcTrackID();
2458 
2459         // track parameters
2460         pt_build_ceff_ = buildtrack.pT();
2461         ept_build_ceff_ = buildtrack.epT();
2462         phi_build_ceff_ = buildtrack.momPhi();
2463         ephi_build_ceff_ = buildtrack.emomPhi();
2464         eta_build_ceff_ = buildtrack.momEta();
2465         eeta_build_ceff_ = buildtrack.emomEta();
2466 
2467         // gen info
2468         if (mcTrackID_build_ceff_ >= 0) {
2469           const auto& simtrack = evt_sim_tracks[mcTrackID_build_ceff_];
2470           x_mc_build_ceff_ = simtrack.x();
2471           y_mc_build_ceff_ = simtrack.y();
2472           z_mc_build_ceff_ = simtrack.z();
2473           pt_mc_build_ceff_ = simtrack.pT();
2474           phi_mc_build_ceff_ = simtrack.momPhi();
2475           eta_mc_build_ceff_ = simtrack.momEta();
2476 
2477           if (Config::keepHitInfo)
2478             TTreeValidation::fillMinHitInfo(simtrack, hitlyrs_mc_build_ceff_, hitidxs_mc_build_ceff_);
2479         } else {
2480           x_mc_build_ceff_ = -1000;
2481           y_mc_build_ceff_ = -1000;
2482           z_mc_build_ceff_ = -1000;
2483           pt_mc_build_ceff_ = -99;
2484           phi_mc_build_ceff_ = -99;
2485           eta_mc_build_ceff_ = -99;
2486         }
2487 
2488         // hit/layer info
2489         nHits_build_ceff_ = buildtrack.nFoundHits();
2490         nLayers_build_ceff_ = buildtrack.nUniqueLayers();
2491         nHitsMatched_build_ceff_ = buildextra.nHitsMatched();
2492         fracHitsMatched_build_ceff_ = buildextra.fracHitsMatched();
2493         lastlyr_build_ceff_ = buildtrack.getLastFoundHitLyr();
2494 
2495         // hit info
2496         const Hit& lasthit = evt_layer_hits[buildtrack.getLastFoundHitLyr()][buildtrack.getLastFoundHitIdx()];
2497         xhit_build_ceff_ = lasthit.x();
2498         yhit_build_ceff_ = lasthit.y();
2499         zhit_build_ceff_ = lasthit.z();
2500 
2501         // quality info
2502         hitchi2_build_ceff_ = buildtrack.chi2();
2503         helixchi2_build_ceff_ = buildextra.helixChi2();
2504         score_build_ceff_ = buildtrack.score();
2505 
2506         // swim dphi
2507         dphi_build_ceff_ = buildextra.dPhi();
2508 
2509         // duplicate info
2510         duplmask_build_ceff_ = buildextra.isDuplicate();
2511         nTkMatches_build_ceff_ = cmsswToBuildMap_[cmsswID_ceff_].size();  // n reco matches to this cmssw track.
2512 
2513         if (Config::keepHitInfo)
2514           TTreeValidation::fillMinHitInfo(buildtrack, hitlyrs_build_ceff_, hitidxs_build_ceff_);
2515       } else  // unmatched cmsswtracks ... put -99 for all reco values to denote unmatched
2516       {
2517         cmsswmask_build_ceff_ = (cmsswtrack.isFindable() ? 0 : -1);  // quick logic for not matched
2518 
2519         seedID_build_ceff_ = -99;
2520         mcTrackID_build_ceff_ = -99;
2521 
2522         pt_build_ceff_ = -99;
2523         ept_build_ceff_ = -99;
2524         phi_build_ceff_ = -99;
2525         ephi_build_ceff_ = -99;
2526         eta_build_ceff_ = -99;
2527         eeta_build_ceff_ = -99;
2528 
2529         x_mc_build_ceff_ = -2000;
2530         y_mc_build_ceff_ = -2000;
2531         z_mc_build_ceff_ = -2000;
2532         pt_mc_build_ceff_ = -99;
2533         phi_mc_build_ceff_ = -99;
2534         eta_mc_build_ceff_ = -99;
2535 
2536         nHits_build_ceff_ = -99;
2537         nLayers_build_ceff_ = -99;
2538         nHitsMatched_build_ceff_ = -99;
2539         fracHitsMatched_build_ceff_ = -99;
2540         lastlyr_build_ceff_ = -99;
2541 
2542         xhit_build_ceff_ = -2000;
2543         yhit_build_ceff_ = -2000;
2544         zhit_build_ceff_ = -2000;
2545 
2546         hitchi2_build_ceff_ = -99;
2547         helixchi2_build_ceff_ = -99;
2548         score_build_ceff_ = -17000;
2549 
2550         dphi_build_ceff_ = -99;
2551 
2552         duplmask_build_ceff_ = -1;     // mask means unmatched cmssw track
2553         nTkMatches_build_ceff_ = -99;  // unmatched
2554       }
2555 
2556       // matched fit track
2557       if (cmsswToFitMap_.count(cmsswID_ceff_) &&
2558           cmsswtrack
2559               .isFindable())  // recoToCmssw match : save best match with best score i.e. cmsswToFitMap_[matched CmsswID][first element in vector]
2560       {
2561         for (unsigned int ii = 0; ii < cmsswToFitMap_[cmsswID_ceff_].size(); ii++) {
2562           const int theAlgo = evt_build_tracks[cmsswToFitMap_[cmsswID_ceff_][ii]].algoint();
2563           if ((itermask_fit_ceff_ >> theAlgo) & 1)
2564             iterduplmask_fit_ceff_ = (iterduplmask_fit_ceff_ | (1 << theAlgo));  //filled at the second time
2565           itermask_fit_ceff_ = (itermask_fit_ceff_ | (1 << theAlgo));
2566         }
2567 
2568         const auto& fittrack =
2569             evt_fit_tracks[cmsswToFitMap_[cmsswID_ceff_][0]];     // returns fitTrack best matched to cmssw track
2570         const auto& fitextra = evt_fit_extras[fittrack.label()];  // returns track extra best aligned with fit track
2571         cmsswmask_fit_ceff_ = 1;                                  // quick logic for matched
2572 
2573         seedID_fit_ceff_ = fitextra.seedID();
2574         mcTrackID_fit_ceff_ = fitextra.mcTrackID();
2575 
2576         // track parameters
2577         pt_fit_ceff_ = fittrack.pT();
2578         ept_fit_ceff_ = fittrack.epT();
2579         phi_fit_ceff_ = fittrack.momPhi();
2580         ephi_fit_ceff_ = fittrack.emomPhi();
2581         eta_fit_ceff_ = fittrack.momEta();
2582         eeta_fit_ceff_ = fittrack.emomEta();
2583 
2584         // gen info
2585         if (mcTrackID_fit_ceff_ >= 0) {
2586           const auto& simtrack = evt_sim_tracks[mcTrackID_fit_ceff_];
2587           x_mc_fit_ceff_ = simtrack.x();
2588           y_mc_fit_ceff_ = simtrack.y();
2589           z_mc_fit_ceff_ = simtrack.z();
2590           pt_mc_fit_ceff_ = simtrack.pT();
2591           phi_mc_fit_ceff_ = simtrack.momPhi();
2592           eta_mc_fit_ceff_ = simtrack.momEta();
2593 
2594           if (Config::keepHitInfo)
2595             TTreeValidation::fillMinHitInfo(simtrack, hitlyrs_mc_fit_ceff_, hitidxs_mc_fit_ceff_);
2596         } else {
2597           x_mc_fit_ceff_ = -1000;
2598           y_mc_fit_ceff_ = -1000;
2599           z_mc_fit_ceff_ = -1000;
2600           pt_mc_fit_ceff_ = -99;
2601           phi_mc_fit_ceff_ = -99;
2602           eta_mc_fit_ceff_ = -99;
2603         }
2604 
2605         // hit/layer info
2606         nHits_fit_ceff_ = fittrack.nFoundHits();
2607         nLayers_fit_ceff_ = fittrack.nUniqueLayers();
2608         nHitsMatched_fit_ceff_ = fitextra.nHitsMatched();
2609         fracHitsMatched_fit_ceff_ = fitextra.fracHitsMatched();
2610         lastlyr_fit_ceff_ = fittrack.getLastFoundHitLyr();
2611 
2612         // hit info
2613         const Hit& lasthit = evt_layer_hits[fittrack.getLastFoundHitLyr()][fittrack.getLastFoundHitIdx()];
2614         xhit_fit_ceff_ = lasthit.x();
2615         yhit_fit_ceff_ = lasthit.y();
2616         zhit_fit_ceff_ = lasthit.z();
2617 
2618         // quality info
2619         hitchi2_fit_ceff_ = fittrack.chi2();
2620         helixchi2_fit_ceff_ = fitextra.helixChi2();
2621         score_fit_ceff_ = fittrack.score();
2622 
2623         // swim dphi
2624         dphi_fit_ceff_ = fitextra.dPhi();
2625 
2626         // duplicate info
2627         duplmask_fit_ceff_ = fitextra.isDuplicate();
2628         nTkMatches_fit_ceff_ = cmsswToFitMap_[cmsswID_ceff_].size();  // n reco matches to this cmssw track.
2629 
2630         if (Config::keepHitInfo)
2631           TTreeValidation::fillMinHitInfo(fittrack, hitlyrs_fit_ceff_, hitidxs_fit_ceff_);
2632       } else  // unmatched cmsswtracks ... put -99 for all reco values to denote unmatched
2633       {
2634         cmsswmask_fit_ceff_ = (cmsswtrack.isFindable() ? 0 : -1);  // quick logic for not matched
2635 
2636         seedID_fit_ceff_ = -99;
2637         mcTrackID_fit_ceff_ = -99;
2638 
2639         pt_fit_ceff_ = -99;
2640         ept_fit_ceff_ = -99;
2641         phi_fit_ceff_ = -99;
2642         ephi_fit_ceff_ = -99;
2643         eta_fit_ceff_ = -99;
2644         eeta_fit_ceff_ = -99;
2645 
2646         x_mc_fit_ceff_ = -2000;
2647         y_mc_fit_ceff_ = -2000;
2648         z_mc_fit_ceff_ = -2000;
2649         pt_mc_fit_ceff_ = -99;
2650         phi_mc_fit_ceff_ = -99;
2651         eta_mc_fit_ceff_ = -99;
2652 
2653         nHits_fit_ceff_ = -99;
2654         nLayers_fit_ceff_ = -99;
2655         nHitsMatched_fit_ceff_ = -99;
2656         fracHitsMatched_fit_ceff_ = -99;
2657         lastlyr_fit_ceff_ = -99;
2658 
2659         xhit_fit_ceff_ = -2000;
2660         yhit_fit_ceff_ = -2000;
2661         zhit_fit_ceff_ = -2000;
2662 
2663         hitchi2_fit_ceff_ = -99;
2664         helixchi2_fit_ceff_ = -99;
2665         score_fit_ceff_ = -17000;
2666 
2667         dphi_fit_ceff_ = -99;
2668 
2669         duplmask_fit_ceff_ = -1;     // mask means unmatched cmssw track
2670         nTkMatches_fit_ceff_ = -99;  // unmatched
2671       }
2672 
2673       cmsswefftree_->Fill();
2674     }
2675   }
2676 
2677   void TTreeValidation::fillCMSSWFakeRateTree(const Event& ev) {
2678     std::lock_guard<std::mutex> locker(glock_);
2679 
2680     auto ievt = ev.evtID();
2681     const auto& evt_sim_tracks = ev.simTracks_;
2682     const auto& evt_cmssw_tracks = ev.cmsswTracks_;
2683     const auto& evt_cmssw_extras = ev.cmsswTracksExtra_;
2684     const auto& evt_build_tracks = ev.candidateTracks_;
2685     const auto& evt_build_extras = ev.candidateTracksExtra_;
2686     const auto& evt_fit_tracks = ev.fitTracks_;
2687     const auto& evt_fit_extras = ev.fitTracksExtra_;
2688     const auto& evt_layer_hits = ev.layerHits_;
2689 
2690     for (const auto& buildtrack : evt_build_tracks) {
2691       if (Config::keepHitInfo) {
2692         hitlyrs_mc_cFR_.clear();
2693         hitlyrs_build_cFR_.clear();
2694         hitlyrs_cmssw_build_cFR_.clear();
2695         hitlyrs_fit_cFR_.clear();
2696         hitlyrs_cmssw_fit_cFR_.clear();
2697 
2698         hitidxs_mc_cFR_.clear();
2699         hitidxs_build_cFR_.clear();
2700         hitidxs_cmssw_build_cFR_.clear();
2701         hitidxs_fit_cFR_.clear();
2702         hitidxs_cmssw_fit_cFR_.clear();
2703       }
2704 
2705       algorithm_cFR_ = buildtrack.algoint();
2706 
2707       const auto& buildextra = evt_build_extras[buildtrack.label()];
2708 
2709       // same for fit and build tracks
2710       evtID_cFR_ = ievt;
2711       seedID_cFR_ = buildextra.seedID();
2712       mcTrackID_cFR_ = buildextra.mcTrackID();
2713 
2714       // track parameters
2715       pt_build_cFR_ = buildtrack.pT();
2716       ept_build_cFR_ = buildtrack.epT();
2717       phi_build_cFR_ = buildtrack.momPhi();
2718       ephi_build_cFR_ = buildtrack.emomPhi();
2719       eta_build_cFR_ = buildtrack.momEta();
2720       eeta_build_cFR_ = buildtrack.emomEta();
2721 
2722       // gen info
2723       if (mcTrackID_cFR_ >= 0) {
2724         const auto& simtrack = evt_sim_tracks[mcTrackID_cFR_];
2725         x_mc_cFR_ = simtrack.x();
2726         y_mc_cFR_ = simtrack.y();
2727         z_mc_cFR_ = simtrack.z();
2728         pt_mc_cFR_ = simtrack.pT();
2729         phi_mc_cFR_ = simtrack.momPhi();
2730         eta_mc_cFR_ = simtrack.momEta();
2731 
2732         if (Config::keepHitInfo)
2733           TTreeValidation::fillMinHitInfo(simtrack, hitlyrs_mc_cFR_, hitidxs_mc_cFR_);
2734       } else {
2735         x_mc_cFR_ = -1000;
2736         y_mc_cFR_ = -1000;
2737         z_mc_cFR_ = -1000;
2738         pt_mc_cFR_ = -99;
2739         phi_mc_cFR_ = -99;
2740         eta_mc_cFR_ = -99;
2741       }
2742 
2743       // hit/layer info
2744       nHits_build_cFR_ = buildtrack.nFoundHits();
2745       nLayers_build_cFR_ = buildtrack.nUniqueLayers();
2746       nHitsMatched_build_cFR_ = buildextra.nHitsMatched();
2747       fracHitsMatched_build_cFR_ = buildextra.fracHitsMatched();
2748       lastlyr_build_cFR_ = buildtrack.getLastFoundHitLyr();
2749 
2750       // hit info
2751       const Hit& lasthit = evt_layer_hits[buildtrack.getLastFoundHitLyr()][buildtrack.getLastFoundHitIdx()];
2752       xhit_build_cFR_ = lasthit.x();
2753       yhit_build_cFR_ = lasthit.y();
2754       zhit_build_cFR_ = lasthit.z();
2755 
2756       // quality info
2757       hitchi2_build_cFR_ = buildtrack.chi2();
2758       helixchi2_build_cFR_ = buildextra.helixChi2();
2759       score_build_cFR_ = buildtrack.score();
2760 
2761       // stored dphi
2762       dphi_build_cFR_ = buildextra.dPhi();
2763 
2764       if (Config::keepHitInfo)
2765         TTreeValidation::fillMinHitInfo(buildtrack, hitlyrs_build_cFR_, hitidxs_build_cFR_);
2766 
2767       // cmssw match?
2768       cmsswID_build_cFR_ = buildextra.cmsswTrackID();
2769       cmsswmask_build_cFR_ = TTreeValidation::getMaskAssignment(cmsswID_build_cFR_);
2770 
2771       if (cmsswmask_build_cFR_ == 1)  // matched track to cmssw
2772       {
2773         const auto& cmsswtrack = evt_cmssw_tracks[cmsswID_build_cFR_];
2774         const auto& cmsswextra = evt_cmssw_extras[cmsswtrack.label()];
2775 
2776         seedID_cmssw_build_cFR_ = cmsswextra.seedID();
2777 
2778         x_cmssw_build_cFR_ = cmsswtrack.x();
2779         y_cmssw_build_cFR_ = cmsswtrack.y();
2780         z_cmssw_build_cFR_ = cmsswtrack.z();
2781 
2782         pt_cmssw_build_cFR_ = cmsswtrack.pT();
2783         phi_cmssw_build_cFR_ = cmsswtrack.momPhi();
2784         eta_cmssw_build_cFR_ = cmsswtrack.momEta();
2785 
2786         nHits_cmssw_build_cFR_ = cmsswtrack.nFoundHits();
2787         nLayers_cmssw_build_cFR_ = cmsswtrack.nUniqueLayers();
2788         lastlyr_cmssw_build_cFR_ = cmsswtrack.getLastFoundHitLyr();
2789 
2790         // duplicate info
2791         duplmask_build_cFR_ = buildextra.isDuplicate();
2792         iTkMatches_build_cFR_ = buildextra.duplicateID();
2793 
2794         if (Config::keepHitInfo)
2795           TTreeValidation::fillMinHitInfo(cmsswtrack, hitlyrs_cmssw_build_cFR_, hitidxs_cmssw_build_cFR_);
2796       } else  // unmatched cmsswtracks ... put -99 for all reco values to denote unmatched
2797       {
2798         seedID_cmssw_build_cFR_ = -99;
2799 
2800         x_cmssw_build_cFR_ = -2000;
2801         y_cmssw_build_cFR_ = -2000;
2802         z_cmssw_build_cFR_ = -2000;
2803 
2804         pt_cmssw_build_cFR_ = -99;
2805         phi_cmssw_build_cFR_ = -99;
2806         eta_cmssw_build_cFR_ = -99;
2807 
2808         nHits_cmssw_build_cFR_ = -99;
2809         nLayers_cmssw_build_cFR_ = -99;
2810         lastlyr_cmssw_build_cFR_ = -99;
2811 
2812         duplmask_build_cFR_ = -1;
2813         iTkMatches_build_cFR_ = -99;
2814       }
2815 
2816       // ensure there is a fit track to mess with
2817       if (buildToFitMap_.count(buildtrack.label())) {
2818         const auto& fittrack = evt_fit_tracks[buildToFitMap_[buildtrack.label()]];
2819         const auto& fitextra = evt_fit_extras[fittrack.label()];
2820 
2821         // track parameters
2822         pt_fit_cFR_ = fittrack.pT();
2823         ept_fit_cFR_ = fittrack.epT();
2824         phi_fit_cFR_ = fittrack.momPhi();
2825         ephi_fit_cFR_ = fittrack.emomPhi();
2826         eta_fit_cFR_ = fittrack.momEta();
2827         eeta_fit_cFR_ = fittrack.emomEta();
2828 
2829         // hit/layer info
2830         nHits_fit_cFR_ = fittrack.nFoundHits();
2831         nLayers_fit_cFR_ = fittrack.nUniqueLayers();
2832         nHitsMatched_fit_cFR_ = fitextra.nHitsMatched();
2833         fracHitsMatched_fit_cFR_ = fitextra.fracHitsMatched();
2834         lastlyr_fit_cFR_ = fittrack.getLastFoundHitLyr();
2835 
2836         // hit info
2837         const Hit& lasthit = evt_layer_hits[fittrack.getLastFoundHitLyr()][fittrack.getLastFoundHitIdx()];
2838         xhit_fit_cFR_ = lasthit.x();
2839         yhit_fit_cFR_ = lasthit.y();
2840         zhit_fit_cFR_ = lasthit.z();
2841 
2842         // chi2 info
2843         hitchi2_fit_cFR_ = fittrack.chi2();
2844         helixchi2_fit_cFR_ = fitextra.helixChi2();
2845         score_fit_cFR_ = fittrack.score();
2846 
2847         // stored dphi
2848         dphi_fit_cFR_ = fitextra.dPhi();
2849 
2850         if (Config::keepHitInfo)
2851           TTreeValidation::fillMinHitInfo(buildtrack, hitlyrs_fit_cFR_, hitidxs_fit_cFR_);
2852 
2853         // cmssw match?
2854         cmsswID_fit_cFR_ = fitextra.cmsswTrackID();
2855         cmsswmask_fit_cFR_ = TTreeValidation::getMaskAssignment(cmsswID_fit_cFR_);
2856 
2857         if (cmsswmask_fit_cFR_ == 1)  // matched track to cmssw
2858         {
2859           const auto& cmsswtrack = evt_cmssw_tracks[cmsswID_fit_cFR_];
2860           const auto& cmsswextra = evt_cmssw_extras[cmsswtrack.label()];
2861 
2862           seedID_cmssw_fit_cFR_ = cmsswextra.seedID();
2863 
2864           x_cmssw_fit_cFR_ = cmsswtrack.x();
2865           y_cmssw_fit_cFR_ = cmsswtrack.y();
2866           z_cmssw_fit_cFR_ = cmsswtrack.z();
2867 
2868           pt_cmssw_fit_cFR_ = cmsswtrack.pT();
2869           phi_cmssw_fit_cFR_ = cmsswtrack.momPhi();
2870           eta_cmssw_fit_cFR_ = cmsswtrack.momEta();
2871 
2872           nHits_cmssw_fit_cFR_ = cmsswtrack.nFoundHits();
2873           nLayers_cmssw_fit_cFR_ = cmsswtrack.nUniqueLayers();
2874           lastlyr_cmssw_fit_cFR_ = cmsswtrack.getLastFoundHitLyr();
2875 
2876           // duplicate info
2877           duplmask_fit_cFR_ = fitextra.isDuplicate();
2878           iTkMatches_fit_cFR_ = fitextra.duplicateID();
2879 
2880           if (Config::keepHitInfo)
2881             TTreeValidation::fillMinHitInfo(fittrack, hitlyrs_cmssw_fit_cFR_, hitidxs_cmssw_fit_cFR_);
2882         } else  // unmatched cmsswtracks ... put -99 for all reco values to denote unmatched
2883         {
2884           seedID_cmssw_fit_cFR_ = -99;
2885 
2886           x_cmssw_fit_cFR_ = -2000;
2887           y_cmssw_fit_cFR_ = -2000;
2888           z_cmssw_fit_cFR_ = -2000;
2889 
2890           pt_cmssw_fit_cFR_ = -99;
2891           phi_cmssw_fit_cFR_ = -99;
2892           eta_cmssw_fit_cFR_ = -99;
2893 
2894           nHits_cmssw_fit_cFR_ = -99;
2895           nLayers_cmssw_fit_cFR_ = -99;
2896           lastlyr_cmssw_fit_cFR_ = -99;
2897 
2898           duplmask_fit_cFR_ = -1;
2899           iTkMatches_fit_cFR_ = -99;
2900         }
2901       } else  // no fit track to match to a build track!
2902       {
2903         pt_fit_cFR_ = -100;
2904         ept_fit_cFR_ = -100;
2905         phi_fit_cFR_ = -100;
2906         ephi_fit_cFR_ = -100;
2907         eta_fit_cFR_ = -100;
2908         eeta_fit_cFR_ = -100;
2909 
2910         nHits_fit_cFR_ = -100;
2911         nLayers_fit_cFR_ = -100;
2912         nHitsMatched_fit_cFR_ = -100;
2913         fracHitsMatched_fit_cFR_ = -100;
2914         lastlyr_fit_cFR_ = -100;
2915 
2916         xhit_fit_cFR_ = -3000;
2917         yhit_fit_cFR_ = -3000;
2918         zhit_fit_cFR_ = -3000;
2919 
2920         hitchi2_fit_cFR_ = -100;
2921         helixchi2_fit_cFR_ = -100;
2922         score_fit_cFR_ = -5001;
2923         dphi_fit_cFR_ = -100;
2924 
2925         cmsswID_fit_cFR_ = -100;
2926         cmsswmask_fit_cFR_ = -2;
2927 
2928         seedID_cmssw_fit_cFR_ = -100;
2929 
2930         x_cmssw_fit_cFR_ = -3000;
2931         y_cmssw_fit_cFR_ = -3000;
2932         z_cmssw_fit_cFR_ = -3000;
2933 
2934         pt_cmssw_fit_cFR_ = -100;
2935         phi_cmssw_fit_cFR_ = -100;
2936         eta_cmssw_fit_cFR_ = -100;
2937 
2938         nHits_cmssw_fit_cFR_ = -100;
2939         nLayers_cmssw_fit_cFR_ = -100;
2940         lastlyr_cmssw_fit_cFR_ = -100;
2941 
2942         duplmask_fit_cFR_ = -2;
2943         iTkMatches_fit_cFR_ = -100;
2944       }
2945 
2946       cmsswfrtree_->Fill();
2947     }
2948   }
2949 
2950   void TTreeValidation::saveTTrees() {
2951     std::lock_guard<std::mutex> locker(glock_);
2952     f_->cd();
2953 
2954     if (Config::sim_val_for_cmssw || Config::sim_val) {
2955       efftree_->SetDirectory(f_.get());
2956       efftree_->Write();
2957 
2958       frtree_->SetDirectory(f_.get());
2959       frtree_->Write();
2960     }
2961     if (Config::cmssw_val) {
2962       cmsswefftree_->SetDirectory(f_.get());
2963       cmsswefftree_->Write();
2964 
2965       cmsswfrtree_->SetDirectory(f_.get());
2966       cmsswfrtree_->Write();
2967     }
2968     if (Config::fit_val) {
2969       fittree_->SetDirectory(f_.get());
2970       fittree_->Write();
2971     }
2972 
2973     configtree_->SetDirectory(f_.get());
2974     configtree_->Write();
2975   }
2976 
2977 }  // end namespace mkfit
2978 #endif