Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:56

0001 // Authors:  Philipp Zehetner, Wahid Redjeb, Aurora Perego, Felice Pantaleo
0002 
0003 #include "TTree.h"
0004 #include "TFile.h"
0005 
0006 #include <iostream>
0007 #include <fstream>
0008 #include <sstream>
0009 #include <variant>
0010 
0011 #include <memory>  // unique_ptr
0012 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0013 #include "FWCore/ParameterSet/interface/PluginDescription.h"
0014 #include "FWCore/ParameterSet/interface/allowedValues.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/Utilities/interface/ESGetToken.h"
0017 #include "FWCore/Framework/interface/ESHandle.h"
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/MakerMacros.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/ServiceRegistry/interface/Service.h"
0024 
0025 #include "DataFormats/Provenance/interface/EventID.h"
0026 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
0027 #include "DataFormats/HGCalReco/interface/Trackster.h"
0028 #include "DataFormats/HGCalReco/interface/TICLCandidate.h"
0029 #include "DataFormats/MuonReco/interface/Muon.h"
0030 #include "DataFormats/TrackReco/interface/Track.h"
0031 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0032 #include "DataFormats/DetId/interface/DetId.h"
0033 #include "DataFormats/Math/interface/Point3D.h"
0034 #include "DataFormats/GeometrySurface/interface/BoundDisk.h"
0035 #include "DataFormats/HGCalReco/interface/Common.h"
0036 #include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
0037 #include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
0038 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
0039 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
0040 
0041 #include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
0042 #include "RecoParticleFlow/PFProducer/interface/PFMuonAlgo.h"
0043 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0044 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
0045 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
0046 
0047 #include "MagneticField/Engine/interface/MagneticField.h"
0048 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0049 
0050 #include "Geometry/HGCalCommonData/interface/HGCalDDDConstants.h"
0051 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0052 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0053 
0054 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0055 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0056 #include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
0057 
0058 #include "SimDataFormats/Associations/interface/TICLAssociationMap.h"
0059 
0060 // TFileService
0061 #include "FWCore/ServiceRegistry/interface/Service.h"
0062 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0063 
0064 using TracksterToTracksterMap =
0065     ticl::AssociationMap<ticl::mapWithFractionAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>;
0066 // Helper class for geometry, magnetic field, etc
0067 class DetectorTools {
0068 public:
0069   DetectorTools(const HGCalDDDConstants& hgcons,
0070                 const CaloGeometry& geom,
0071                 const MagneticField& bfieldH,
0072                 const Propagator& propH)
0073       : hgcons(hgcons), rhtools(), bfield(bfieldH), propagator(propH) {
0074     rhtools.setGeometry(geom);
0075 
0076     // build disks at HGCal front & EM-Had interface for track propagation
0077     float zVal = hgcons.waferZ(1, true);
0078     std::pair<float, float> rMinMax = hgcons.rangeR(zVal, true);
0079 
0080     float zVal_interface = rhtools.getPositionLayer(rhtools.lastLayerEE()).z();
0081     std::pair<float, float> rMinMax_interface = hgcons.rangeR(zVal_interface, true);
0082 
0083     for (int iSide = 0; iSide < 2; ++iSide) {
0084       float zSide = (iSide == 0) ? (-1. * zVal) : zVal;
0085       firstDisk_[iSide] = std::make_unique<GeomDet>(
0086           Disk::build(Disk::PositionType(0, 0, zSide),
0087                       Disk::RotationType(),
0088                       SimpleDiskBounds(rMinMax.first, rMinMax.second, zSide - 0.5, zSide + 0.5))
0089               .get());
0090 
0091       zSide = (iSide == 0) ? (-1. * zVal_interface) : zVal_interface;
0092       interfaceDisk_[iSide] = std::make_unique<GeomDet>(
0093           Disk::build(Disk::PositionType(0, 0, zSide),
0094                       Disk::RotationType(),
0095                       SimpleDiskBounds(rMinMax_interface.first, rMinMax_interface.second, zSide - 0.5, zSide + 0.5))
0096               .get());
0097     }
0098   }
0099 
0100   const HGCalDDDConstants& hgcons;
0101   std::unique_ptr<GeomDet> firstDisk_[2];
0102   std::unique_ptr<GeomDet> interfaceDisk_[2];
0103   hgcal::RecHitTools rhtools;
0104   const MagneticField& bfield;
0105   const Propagator& propagator;
0106 };
0107 
0108 // Helper class that dumps a single trackster collection (either tracksters or simTracksters)
0109 class TracksterDumperHelper {
0110 public:
0111   enum class TracksterType {
0112     Trackster,       ///< Regular trackster (from RECO)
0113     SimTracksterCP,  ///< SimTrackster from CaloParticle
0114     SimTracksterSC   ///< SimTrackster from SimCluster
0115   };
0116 
0117   static TracksterType tracksterTypeFromString(std::string str) {
0118     if (str == "Trackster")
0119       return TracksterType::Trackster;
0120     if (str == "SimTracksterCP")
0121       return TracksterType::SimTracksterCP;
0122     if (str == "SimTracksterSC")
0123       return TracksterType::SimTracksterSC;
0124     throw std::runtime_error("TICLDumper : TracksterDumperHelper : Invalid trackster type " + str);
0125   }
0126 
0127   /** tracksterType : dtermines additional information that will be saved (calo truth information, track information) */
0128   TracksterDumperHelper(TracksterType tracksterType = TracksterType::Trackster) : tracksterType_(tracksterType) {}
0129 
0130   /**
0131    * To be called once after tree creation. eventId_ should be a pointer to the EventID.
0132    * *Do not copy/move or resize vector holding object after calling this function*
0133   */
0134   void initTree(TTree* trackster_tree_, edm::EventID* eventId_) {
0135     trackster_tree_->Branch("event", eventId_);
0136     trackster_tree_->Branch("NTracksters", &nTracksters);
0137     trackster_tree_->Branch("NClusters", &nClusters);
0138     trackster_tree_->Branch("time", &trackster_time);
0139     trackster_tree_->Branch("timeError", &trackster_timeError);
0140     trackster_tree_->Branch("regressed_energy", &trackster_regressed_energy);
0141     trackster_tree_->Branch("raw_energy", &trackster_raw_energy);
0142     trackster_tree_->Branch("raw_em_energy", &trackster_raw_em_energy);
0143     trackster_tree_->Branch("raw_pt", &trackster_raw_pt);
0144     trackster_tree_->Branch("raw_em_pt", &trackster_raw_em_pt);
0145     trackster_tree_->Branch("barycenter_x", &trackster_barycenter_x);
0146     trackster_tree_->Branch("barycenter_y", &trackster_barycenter_y);
0147     trackster_tree_->Branch("barycenter_z", &trackster_barycenter_z);
0148     trackster_tree_->Branch("barycenter_eta", &trackster_barycenter_eta);
0149     trackster_tree_->Branch("barycenter_phi", &trackster_barycenter_phi);
0150     trackster_tree_->Branch("EV1", &trackster_EV1);
0151     trackster_tree_->Branch("EV2", &trackster_EV2);
0152     trackster_tree_->Branch("EV3", &trackster_EV3);
0153     trackster_tree_->Branch("eVector0_x", &trackster_eVector0_x);
0154     trackster_tree_->Branch("eVector0_y", &trackster_eVector0_y);
0155     trackster_tree_->Branch("eVector0_z", &trackster_eVector0_z);
0156     trackster_tree_->Branch("sigmaPCA1", &trackster_sigmaPCA1);
0157     trackster_tree_->Branch("sigmaPCA2", &trackster_sigmaPCA2);
0158     trackster_tree_->Branch("sigmaPCA3", &trackster_sigmaPCA3);
0159     if (tracksterType_ != TracksterType::Trackster) {
0160       trackster_tree_->Branch("regressed_pt", &simtrackster_regressed_pt);
0161       trackster_tree_->Branch("pdgID", &simtrackster_pdgID);
0162       trackster_tree_->Branch("trackIdx", &simtrackster_trackIdx);
0163       trackster_tree_->Branch("trackTime", &simtrackster_trackTime);
0164       trackster_tree_->Branch("timeBoundary", &simtrackster_timeBoundary);
0165       trackster_tree_->Branch("boundaryX", &simtrackster_boundaryX);
0166       trackster_tree_->Branch("boundaryY", &simtrackster_boundaryY);
0167       trackster_tree_->Branch("boundaryZ", &simtrackster_boundaryZ);
0168       trackster_tree_->Branch("boundaryEta", &simtrackster_boundaryEta);
0169       trackster_tree_->Branch("boundaryPhi", &simtrackster_boundaryPhi);
0170       trackster_tree_->Branch("boundaryPx", &simtrackster_boundaryPx);
0171       trackster_tree_->Branch("boundaryPy", &simtrackster_boundaryPy);
0172       trackster_tree_->Branch("boundaryPz", &simtrackster_boundaryPz);
0173       trackster_tree_->Branch("track_boundaryX", &simtrackster_track_boundaryX);
0174       trackster_tree_->Branch("track_boundaryY", &simtrackster_track_boundaryY);
0175       trackster_tree_->Branch("track_boundaryZ", &simtrackster_track_boundaryZ);
0176       trackster_tree_->Branch("track_boundaryEta", &simtrackster_track_boundaryEta);
0177       trackster_tree_->Branch("track_boundaryPhi", &simtrackster_track_boundaryPhi);
0178       trackster_tree_->Branch("track_boundaryPx", &simtrackster_track_boundaryPx);
0179       trackster_tree_->Branch("track_boundaryPy", &simtrackster_track_boundaryPy);
0180       trackster_tree_->Branch("track_boundaryPz", &simtrackster_track_boundaryPz);
0181     }
0182     trackster_tree_->Branch("id_probabilities", &trackster_id_probabilities);
0183     trackster_tree_->Branch("vertices_indexes", &trackster_vertices_indexes);
0184     trackster_tree_->Branch("vertices_x", &trackster_vertices_x);
0185     trackster_tree_->Branch("vertices_y", &trackster_vertices_y);
0186     trackster_tree_->Branch("vertices_z", &trackster_vertices_z);
0187     trackster_tree_->Branch("vertices_time", &trackster_vertices_time);
0188     trackster_tree_->Branch("vertices_timeErr", &trackster_vertices_timeErr);
0189     trackster_tree_->Branch("vertices_energy", &trackster_vertices_energy);
0190     trackster_tree_->Branch("vertices_correctedEnergy", &trackster_vertices_correctedEnergy);
0191     trackster_tree_->Branch("vertices_correctedEnergyUncertainty", &trackster_vertices_correctedEnergyUncertainty);
0192     trackster_tree_->Branch("vertices_multiplicity", &trackster_vertices_multiplicity);
0193   }
0194 
0195   void clearVariables() {
0196     nTracksters = 0;
0197     nClusters = 0;
0198     trackster_time.clear();
0199     trackster_timeError.clear();
0200     trackster_regressed_energy.clear();
0201     trackster_raw_energy.clear();
0202     trackster_raw_em_energy.clear();
0203     trackster_raw_pt.clear();
0204     trackster_raw_em_pt.clear();
0205     trackster_barycenter_x.clear();
0206     trackster_barycenter_y.clear();
0207     trackster_barycenter_z.clear();
0208     trackster_EV1.clear();
0209     trackster_EV2.clear();
0210     trackster_EV3.clear();
0211     trackster_eVector0_x.clear();
0212     trackster_eVector0_y.clear();
0213     trackster_eVector0_z.clear();
0214     trackster_sigmaPCA1.clear();
0215     trackster_sigmaPCA2.clear();
0216     trackster_sigmaPCA3.clear();
0217 
0218     simtrackster_regressed_pt.clear();
0219     simtrackster_pdgID.clear();
0220     simtrackster_trackIdx.clear();
0221     simtrackster_trackTime.clear();
0222     simtrackster_timeBoundary.clear();
0223     simtrackster_boundaryX.clear();
0224     simtrackster_boundaryY.clear();
0225     simtrackster_boundaryZ.clear();
0226     simtrackster_boundaryEta.clear();
0227     simtrackster_boundaryPhi.clear();
0228     simtrackster_boundaryPx.clear();
0229     simtrackster_boundaryPy.clear();
0230     simtrackster_boundaryPz.clear();
0231     simtrackster_track_boundaryX.clear();
0232     simtrackster_track_boundaryY.clear();
0233     simtrackster_track_boundaryZ.clear();
0234     simtrackster_track_boundaryEta.clear();
0235     simtrackster_track_boundaryPhi.clear();
0236     simtrackster_track_boundaryPx.clear();
0237     simtrackster_track_boundaryPy.clear();
0238     simtrackster_track_boundaryPz.clear();
0239 
0240     trackster_barycenter_eta.clear();
0241     trackster_barycenter_phi.clear();
0242     trackster_id_probabilities.clear();
0243     trackster_vertices_indexes.clear();
0244     trackster_vertices_x.clear();
0245     trackster_vertices_y.clear();
0246     trackster_vertices_z.clear();
0247     trackster_vertices_time.clear();
0248     trackster_vertices_timeErr.clear();
0249     trackster_vertices_energy.clear();
0250     trackster_vertices_correctedEnergy.clear();
0251     trackster_vertices_correctedEnergyUncertainty.clear();
0252     trackster_vertices_multiplicity.clear();
0253   }
0254 
0255   void fillFromEvent(std::vector<ticl::Trackster> const& tracksters,
0256                      std::vector<reco::CaloCluster> const& clusters,
0257                      edm::ValueMap<std::pair<float, float>> const& layerClustersTimes,
0258                      DetectorTools const& detectorTools,
0259                      edm::Handle<std::vector<SimCluster>> simClusters_h,
0260                      edm::Handle<std::vector<CaloParticle>> caloparticles_h,
0261                      std::vector<reco::Track> const& tracks) {
0262     nTracksters = tracksters.size();
0263     nClusters = clusters.size();
0264     for (auto trackster_iterator = tracksters.begin(); trackster_iterator != tracksters.end(); ++trackster_iterator) {
0265       //per-trackster analysis
0266       trackster_time.push_back(trackster_iterator->time());
0267       trackster_timeError.push_back(trackster_iterator->timeError());
0268       trackster_regressed_energy.push_back(trackster_iterator->regressed_energy());
0269       trackster_raw_energy.push_back(trackster_iterator->raw_energy());
0270       trackster_raw_em_energy.push_back(trackster_iterator->raw_em_energy());
0271       trackster_raw_pt.push_back(trackster_iterator->raw_pt());
0272       trackster_raw_em_pt.push_back(trackster_iterator->raw_em_pt());
0273       trackster_barycenter_x.push_back(trackster_iterator->barycenter().x());
0274       trackster_barycenter_y.push_back(trackster_iterator->barycenter().y());
0275       trackster_barycenter_z.push_back(trackster_iterator->barycenter().z());
0276       trackster_barycenter_eta.push_back(trackster_iterator->barycenter().eta());
0277       trackster_barycenter_phi.push_back(trackster_iterator->barycenter().phi());
0278       trackster_EV1.push_back(trackster_iterator->eigenvalues()[0]);
0279       trackster_EV2.push_back(trackster_iterator->eigenvalues()[1]);
0280       trackster_EV3.push_back(trackster_iterator->eigenvalues()[2]);
0281       trackster_eVector0_x.push_back((trackster_iterator->eigenvectors()[0]).x());
0282       trackster_eVector0_y.push_back((trackster_iterator->eigenvectors()[0]).y());
0283       trackster_eVector0_z.push_back((trackster_iterator->eigenvectors()[0]).z());
0284       trackster_sigmaPCA1.push_back(trackster_iterator->sigmasPCA()[0]);
0285       trackster_sigmaPCA2.push_back(trackster_iterator->sigmasPCA()[1]);
0286       trackster_sigmaPCA3.push_back(trackster_iterator->sigmasPCA()[2]);
0287 
0288       if (tracksterType_ != TracksterType::Trackster) {  // is simtrackster
0289         auto const& simclusters = *simClusters_h;
0290         auto const& caloparticles = *caloparticles_h;
0291 
0292         simtrackster_timeBoundary.push_back(trackster_iterator->boundaryTime());
0293 
0294         if (tracksterType_ == TracksterType::SimTracksterCP)
0295           simtrackster_pdgID.push_back(caloparticles[trackster_iterator->seedIndex()].pdgId());
0296         else if (tracksterType_ == TracksterType::SimTracksterSC)
0297           simtrackster_pdgID.push_back(simclusters[trackster_iterator->seedIndex()].pdgId());
0298 
0299         using CaloObjectVariant = std::variant<CaloParticle, SimCluster>;
0300         CaloObjectVariant caloObj;
0301         if (trackster_iterator->seedID() == caloparticles_h.id()) {
0302           caloObj = caloparticles[trackster_iterator->seedIndex()];
0303         } else {
0304           caloObj = simclusters[trackster_iterator->seedIndex()];
0305         }
0306 
0307         auto const& simTrack = std::visit([](auto&& obj) { return obj.g4Tracks()[0]; }, caloObj);
0308         auto const& caloPt = std::visit([](auto&& obj) { return obj.pt(); }, caloObj);
0309         simtrackster_regressed_pt.push_back(caloPt);
0310         if (simTrack.crossedBoundary()) {
0311           simtrackster_boundaryX.push_back(simTrack.getPositionAtBoundary().x());
0312           simtrackster_boundaryY.push_back(simTrack.getPositionAtBoundary().y());
0313           simtrackster_boundaryZ.push_back(simTrack.getPositionAtBoundary().z());
0314           simtrackster_boundaryEta.push_back(simTrack.getPositionAtBoundary().eta());
0315           simtrackster_boundaryPhi.push_back(simTrack.getPositionAtBoundary().phi());
0316           simtrackster_boundaryPx.push_back(simTrack.getMomentumAtBoundary().x());
0317           simtrackster_boundaryPy.push_back(simTrack.getMomentumAtBoundary().y());
0318           simtrackster_boundaryPz.push_back(simTrack.getMomentumAtBoundary().z());
0319         } else {
0320           simtrackster_boundaryX.push_back(-999);
0321           simtrackster_boundaryY.push_back(-999);
0322           simtrackster_boundaryZ.push_back(-999);
0323           simtrackster_boundaryEta.push_back(-999);
0324           simtrackster_boundaryPhi.push_back(-999);
0325           simtrackster_boundaryPx.push_back(-999);
0326           simtrackster_boundaryPy.push_back(-999);
0327           simtrackster_boundaryPz.push_back(-999);
0328         }
0329 
0330         auto const trackIdx = trackster_iterator->trackIdx();
0331         simtrackster_trackIdx.push_back(trackIdx);
0332         if (trackIdx != -1) {
0333           const auto& track = tracks[trackIdx];
0334 
0335           int iSide = int(track.eta() > 0);
0336 
0337           const auto& fts = trajectoryStateTransform::outerFreeState((track), &detectorTools.bfield);
0338           // to the HGCal front
0339           const auto& tsos = detectorTools.propagator.propagate(fts, detectorTools.firstDisk_[iSide]->surface());
0340           if (tsos.isValid()) {
0341             const auto& globalPos = tsos.globalPosition();
0342             const auto& globalMom = tsos.globalMomentum();
0343             simtrackster_track_boundaryX.push_back(globalPos.x());
0344             simtrackster_track_boundaryY.push_back(globalPos.y());
0345             simtrackster_track_boundaryZ.push_back(globalPos.z());
0346             simtrackster_track_boundaryEta.push_back(globalPos.eta());
0347             simtrackster_track_boundaryPhi.push_back(globalPos.phi());
0348             simtrackster_track_boundaryPx.push_back(globalMom.x());
0349             simtrackster_track_boundaryPy.push_back(globalMom.y());
0350             simtrackster_track_boundaryPz.push_back(globalMom.z());
0351             simtrackster_trackTime.push_back(track.t0());
0352           } else {
0353             simtrackster_track_boundaryX.push_back(-999);
0354             simtrackster_track_boundaryY.push_back(-999);
0355             simtrackster_track_boundaryZ.push_back(-999);
0356             simtrackster_track_boundaryEta.push_back(-999);
0357             simtrackster_track_boundaryPhi.push_back(-999);
0358             simtrackster_track_boundaryPx.push_back(-999);
0359             simtrackster_track_boundaryPy.push_back(-999);
0360             simtrackster_track_boundaryPz.push_back(-999);
0361           }
0362         } else {
0363           simtrackster_track_boundaryX.push_back(-999);
0364           simtrackster_track_boundaryY.push_back(-999);
0365           simtrackster_track_boundaryZ.push_back(-999);
0366           simtrackster_track_boundaryEta.push_back(-999);
0367           simtrackster_track_boundaryPhi.push_back(-999);
0368           simtrackster_track_boundaryPx.push_back(-999);
0369           simtrackster_track_boundaryPy.push_back(-999);
0370           simtrackster_track_boundaryPz.push_back(-999);
0371         }
0372       }
0373 
0374       std::vector<float> id_probs;
0375       for (size_t i = 0; i < 8; i++)
0376         id_probs.push_back(trackster_iterator->id_probabilities(i));
0377       trackster_id_probabilities.push_back(id_probs);
0378 
0379       // Clusters
0380       std::vector<uint32_t> vertices_indexes;
0381       std::vector<float> vertices_x;
0382       std::vector<float> vertices_y;
0383       std::vector<float> vertices_z;
0384       std::vector<float> vertices_time;
0385       std::vector<float> vertices_timeErr;
0386       std::vector<float> vertices_energy;
0387       std::vector<float> vertices_correctedEnergy;
0388       std::vector<float> vertices_correctedEnergyUncertainty;
0389       for (auto idx : trackster_iterator->vertices()) {
0390         vertices_indexes.push_back(idx);
0391         const auto& associated_cluster = clusters[idx];
0392         vertices_x.push_back(associated_cluster.x());
0393         vertices_y.push_back(associated_cluster.y());
0394         vertices_z.push_back(associated_cluster.z());
0395         vertices_energy.push_back(associated_cluster.energy());
0396         vertices_correctedEnergy.push_back(associated_cluster.correctedEnergy());
0397         vertices_correctedEnergyUncertainty.push_back(associated_cluster.correctedEnergyUncertainty());
0398         vertices_time.push_back(layerClustersTimes.get(idx).first);
0399         vertices_timeErr.push_back(layerClustersTimes.get(idx).second);
0400       }
0401       trackster_vertices_indexes.push_back(vertices_indexes);
0402       trackster_vertices_x.push_back(vertices_x);
0403       trackster_vertices_y.push_back(vertices_y);
0404       trackster_vertices_z.push_back(vertices_z);
0405       trackster_vertices_time.push_back(vertices_time);
0406       trackster_vertices_timeErr.push_back(vertices_timeErr);
0407       trackster_vertices_energy.push_back(vertices_energy);
0408       trackster_vertices_correctedEnergy.push_back(vertices_correctedEnergy);
0409       trackster_vertices_correctedEnergyUncertainty.push_back(vertices_correctedEnergyUncertainty);
0410 
0411       // Multiplicity
0412       std::vector<float> vertices_multiplicity;
0413       for (auto multiplicity : trackster_iterator->vertex_multiplicity()) {
0414         vertices_multiplicity.push_back(multiplicity);
0415       }
0416       trackster_vertices_multiplicity.push_back(vertices_multiplicity);
0417     }
0418   }
0419 
0420 private:
0421   TracksterType tracksterType_;
0422 
0423   unsigned int nTracksters;
0424   unsigned int nClusters;
0425   std::vector<float> trackster_time;
0426   std::vector<float> trackster_timeError;
0427   std::vector<float> trackster_regressed_energy;
0428   std::vector<float> trackster_raw_energy;
0429   std::vector<float> trackster_raw_em_energy;
0430   std::vector<float> trackster_raw_pt;
0431   std::vector<float> trackster_raw_em_pt;
0432   std::vector<float> trackster_barycenter_x;
0433   std::vector<float> trackster_barycenter_y;
0434   std::vector<float> trackster_barycenter_z;
0435   std::vector<float> trackster_EV1;
0436   std::vector<float> trackster_EV2;
0437   std::vector<float> trackster_EV3;
0438   std::vector<float> trackster_eVector0_x;
0439   std::vector<float> trackster_eVector0_y;
0440   std::vector<float> trackster_eVector0_z;
0441   std::vector<float> trackster_sigmaPCA1;
0442   std::vector<float> trackster_sigmaPCA2;
0443   std::vector<float> trackster_sigmaPCA3;
0444   std::vector<float> trackster_barycenter_eta;
0445   std::vector<float> trackster_barycenter_phi;
0446 
0447   // for simtrackster
0448   std::vector<float> simtrackster_regressed_pt;
0449   std::vector<int> simtrackster_pdgID;
0450   std::vector<int> simtrackster_trackIdx;
0451   std::vector<float> simtrackster_trackTime;
0452   std::vector<float> simtrackster_timeBoundary;
0453   std::vector<float> simtrackster_boundaryX;
0454   std::vector<float> simtrackster_boundaryY;
0455   std::vector<float> simtrackster_boundaryZ;
0456   std::vector<float> simtrackster_boundaryEta;
0457   std::vector<float> simtrackster_boundaryPhi;
0458   std::vector<float> simtrackster_boundaryPx;
0459   std::vector<float> simtrackster_boundaryPy;
0460   std::vector<float> simtrackster_boundaryPz;
0461   std::vector<float> simtrackster_track_boundaryX;
0462   std::vector<float> simtrackster_track_boundaryY;
0463   std::vector<float> simtrackster_track_boundaryZ;
0464   std::vector<float> simtrackster_track_boundaryEta;
0465   std::vector<float> simtrackster_track_boundaryPhi;
0466   std::vector<float> simtrackster_track_boundaryPx;
0467   std::vector<float> simtrackster_track_boundaryPy;
0468   std::vector<float> simtrackster_track_boundaryPz;
0469 
0470   std::vector<std::vector<float>> trackster_id_probabilities;
0471   std::vector<std::vector<uint32_t>> trackster_vertices_indexes;
0472   std::vector<std::vector<float>> trackster_vertices_x;
0473   std::vector<std::vector<float>> trackster_vertices_y;
0474   std::vector<std::vector<float>> trackster_vertices_z;
0475   std::vector<std::vector<float>> trackster_vertices_time;
0476   std::vector<std::vector<float>> trackster_vertices_timeErr;
0477   std::vector<std::vector<float>> trackster_vertices_energy;
0478   std::vector<std::vector<float>> trackster_vertices_correctedEnergy;
0479   std::vector<std::vector<float>> trackster_vertices_correctedEnergyUncertainty;
0480   std::vector<std::vector<float>> trackster_vertices_multiplicity;
0481 };
0482 
0483 // Helper class to dump a TracksterToSimTrackster association map (dumps recoToSim and simToReco at the same time)
0484 class TracksterToSimTracksterAssociationHelper {
0485 public:
0486   /**
0487    * To be called once after tree creation. Output branches will be named prefix_recoToSim/simToReco_suffix_score/sharedE/
0488    * branchPrefix : for example tsCLUE3D. branchSuffix : usually one of SC or CP.
0489    * *Do not copy/move or resize vector holding object after calling this function*
0490   */
0491   void initTree(TTree* tree, std::string branchPrefix, std::string branchSuffix) {
0492     tree->Branch((branchPrefix + "_recoToSim_" + branchSuffix).c_str(), &recoToSim);
0493     tree->Branch((branchPrefix + "_recoToSim_" + branchSuffix + "_score").c_str(), &recoToSim_score);
0494     tree->Branch((branchPrefix + "_recoToSim_" + branchSuffix + "_sharedE").c_str(), &recoToSim_sharedE);
0495     tree->Branch((branchPrefix + "_simToReco_" + branchSuffix).c_str(), &simToReco);
0496     tree->Branch((branchPrefix + "_simToReco_" + branchSuffix + "_score").c_str(), &simToReco_score);
0497     tree->Branch((branchPrefix + "_simToReco_" + branchSuffix + "_sharedE").c_str(), &simToReco_sharedE);
0498   }
0499 
0500   void clearVariables() {
0501     recoToSim.clear();
0502     recoToSim_score.clear();
0503     recoToSim_sharedE.clear();
0504     simToReco.clear();
0505     simToReco_score.clear();
0506     simToReco_sharedE.clear();
0507   }
0508 
0509   void fillFromEvent(TracksterToTracksterMap const& recoToSimMap, TracksterToTracksterMap const& simToRecoMap) {
0510     // Reco -> Sim
0511     const auto numberOfTracksters = recoToSimMap.getMap().size();
0512     recoToSim.resize(numberOfTracksters);
0513     recoToSim_score.resize(numberOfTracksters);
0514     recoToSim_sharedE.resize(numberOfTracksters);
0515 
0516     for (size_t i = 0; i < numberOfTracksters; ++i) {
0517       for (const auto& [simTracksterIndex, sharedEnergyAndScorePair] : recoToSimMap[i]) {
0518         recoToSim[i].push_back(simTracksterIndex);
0519         recoToSim_sharedE[i].push_back(sharedEnergyAndScorePair.first);
0520         recoToSim_score[i].push_back(sharedEnergyAndScorePair.second);
0521       }
0522     }
0523 
0524     // Sim -> Reco
0525     const auto numberOfSimTracksters = simToRecoMap.getMap().size();
0526     simToReco.resize(numberOfSimTracksters);
0527     simToReco_score.resize(numberOfSimTracksters);
0528     simToReco_sharedE.resize(numberOfSimTracksters);
0529 
0530     for (size_t i = 0; i < numberOfSimTracksters; ++i) {
0531       for (const auto& [recoTracksterIndex, sharedEnergyAndScorePair] : simToRecoMap[i]) {
0532         simToReco[i].push_back(recoTracksterIndex);
0533         simToReco_sharedE[i].push_back(sharedEnergyAndScorePair.first);
0534         simToReco_score[i].push_back(sharedEnergyAndScorePair.second);
0535       }
0536     }
0537   }
0538 
0539 private:
0540   std::vector<std::vector<uint32_t>> recoToSim;
0541   std::vector<std::vector<float>> recoToSim_score;
0542   std::vector<std::vector<float>> recoToSim_sharedE;
0543   std::vector<std::vector<uint32_t>> simToReco;
0544   std::vector<std::vector<float>> simToReco_score;
0545   std::vector<std::vector<float>> simToReco_sharedE;
0546 };
0547 
0548 class TICLDumper : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0549 public:
0550   explicit TICLDumper(const edm::ParameterSet&);
0551   ~TICLDumper() override;
0552   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0553   typedef ticl::Vector Vector;
0554   typedef std::vector<double> Vec;
0555 
0556 private:
0557   void beginJob() override;
0558   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0559 
0560   void analyze(const edm::Event&, const edm::EventSetup&) override;
0561   void endRun(edm::Run const& iEvent, edm::EventSetup const&) override {};
0562   void endJob() override;
0563 
0564   // Define Tokens
0565   const std::vector<edm::ParameterSet>
0566       tracksters_parameterSets_;  ///< A parameter set for each trackster collection to dump (giving tree name, etc)
0567   std::vector<edm::EDGetTokenT<std::vector<ticl::Trackster>>>
0568       tracksters_token_;  ///< a token for each trackster collection to dump
0569   std::vector<TracksterDumperHelper>
0570       tracksters_dumperHelpers_;         ///< the dumper helpers for each trackster collection to dump
0571   std::vector<TTree*> tracksters_trees;  ///< TTree for each trackster collection to dump
0572 
0573   const edm::EDGetTokenT<std::vector<ticl::Trackster>> tracksters_in_candidate_token_;
0574   const edm::EDGetTokenT<std::vector<reco::CaloCluster>> layer_clusters_token_;
0575   const edm::EDGetTokenT<std::vector<TICLCandidate>> ticl_candidates_token_;
0576   const edm::EDGetTokenT<std::vector<ticl::Trackster>>
0577       ticl_candidates_tracksters_token_;  ///< trackster collection used by TICLCandidate
0578   const edm::EDGetTokenT<std::vector<reco::Track>> tracks_token_;
0579   const edm::EDGetTokenT<std::vector<bool>> tracks_mask_token_;
0580   const edm::EDGetTokenT<edm::ValueMap<float>> tracks_time_token_;
0581   const edm::EDGetTokenT<edm::ValueMap<float>> tracks_time_quality_token_;
0582   const edm::EDGetTokenT<edm::ValueMap<float>> tracks_time_err_token_;
0583   const edm::EDGetTokenT<edm::ValueMap<float>> tracks_beta_token_;
0584   const edm::EDGetTokenT<edm::ValueMap<float>> tracks_time_mtd_token_;
0585   const edm::EDGetTokenT<edm::ValueMap<float>> tracks_time_mtd_err_token_;
0586   const edm::EDGetTokenT<edm::ValueMap<GlobalPoint>> tracks_pos_mtd_token_;
0587   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_x_token_;
0588   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_y_token_;
0589   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_z_token_;
0590   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_eta_token_;
0591   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_phi_token_;
0592   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_px_token_;
0593   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_py_token_;
0594   const edm::EDGetTokenT<std::vector<double>> hgcaltracks_pz_token_;
0595   const edm::EDGetTokenT<std::vector<reco::Muon>> muons_token_;
0596   const edm::EDGetTokenT<edm::ValueMap<std::pair<float, float>>> clustersTime_token_;
0597   const edm::EDGetTokenT<std::vector<int>> tracksterSeeds_token_;
0598   edm::EDGetTokenT<std::vector<std::vector<unsigned int>>> superclustering_linkedResultTracksters_token;
0599   edm::EDGetTokenT<reco::SuperClusterCollection> recoSuperClusters_token;
0600   edm::EDGetTokenT<reco::CaloClusterCollection> recoSuperClusters_caloClusters_token;
0601   edm::EDGetTokenT<std::vector<ticl::Trackster>> recoSuperClusters_sourceTracksters_token;
0602   edm::ESGetToken<CaloGeometry, CaloGeometryRecord> caloGeometry_token_;
0603   const edm::EDGetTokenT<std::vector<ticl::Trackster>> simTracksters_SC_token_;  // needed for simticlcandidate
0604   const edm::EDGetTokenT<std::vector<TICLCandidate>> simTICLCandidate_token_;
0605 
0606   // associators
0607   const std::vector<edm::ParameterSet>
0608       associations_parameterSets_;  ///< A parameter set for each associator collection to dump (with treeName, etc)
0609   std::vector<edm::EDGetTokenT<TracksterToTracksterMap>>
0610       associations_simToReco_token_;  ///< The tokens for each assocation
0611   std::vector<edm::EDGetTokenT<TracksterToTracksterMap>> associations_recoToSim_token_;
0612   std::vector<TracksterToSimTracksterAssociationHelper>
0613       associations_dumperHelpers_;  ///< the dumper helpers for each association map to dump
0614 
0615   TTree* associations_tree_;
0616 
0617   const edm::EDGetTokenT<std::vector<SimCluster>> simclusters_token_;
0618   const edm::EDGetTokenT<std::vector<CaloParticle>> caloparticles_token_;
0619 
0620   const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> geometry_token_;
0621   const std::string detector_;
0622   const std::string propName_;
0623   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> bfield_token_;
0624   const edm::ESGetToken<Propagator, TrackingComponentsRecord> propagator_token_;
0625   edm::ESGetToken<HGCalDDDConstants, IdealGeometryRecord> hdc_token_;
0626   std::unique_ptr<DetectorTools> detectorTools_;
0627   bool saveLCs_;
0628   bool saveSuperclustering_;
0629   bool saveSuperclusteringDNNScore_;
0630   bool saveRecoSuperclusters_;
0631   bool saveTICLCandidate_;
0632   bool saveSimTICLCandidate_;
0633   bool saveTracks_;
0634 
0635   // Output tree
0636   TTree* tree_;
0637 
0638   void clearVariables();
0639 
0640   // Variables for branches
0641   edm::EventID eventId_;
0642   unsigned int nclusters_;
0643 
0644   std::vector<std::vector<unsigned int>>
0645       superclustering_linkedResultTracksters;  // Map of indices from superclusteredTracksters collection back into ticlTrackstersCLUE3DEM collection
0646   // reco::SuperCluster dump
0647   std::vector<double> recoSuperCluster_rawEnergy;
0648   std::vector<double> recoSuperCluster_energy;
0649   std::vector<double> recoSuperCluster_correctedEnergy;
0650   std::vector<double> recoSuperCluster_position_x;
0651   std::vector<double> recoSuperCluster_position_y;
0652   std::vector<double> recoSuperCluster_position_z;
0653   std::vector<double> recoSuperCluster_position_eta;
0654   std::vector<double> recoSuperCluster_position_phi;
0655   std::vector<uint32_t>
0656       recoSuperCluster_seedTs;  ///< Index to seed trackster (into the trackster collection used to make superclusters, given by config recoSuperClusters_sourceTracksterCollection)
0657   std::vector<std::vector<uint32_t>>
0658       recoSuperCluster_constituentTs;  ///< Indices to all tracksters inside the supercluster (same)
0659 
0660   std::vector<float> simTICLCandidate_raw_energy;
0661   std::vector<float> simTICLCandidate_regressed_energy;
0662   std::vector<std::vector<int>> simTICLCandidate_simTracksterCPIndex;
0663   std::vector<float> simTICLCandidate_boundaryX;
0664   std::vector<float> simTICLCandidate_boundaryY;
0665   std::vector<float> simTICLCandidate_boundaryZ;
0666   std::vector<float> simTICLCandidate_boundaryPx;
0667   std::vector<float> simTICLCandidate_boundaryPy;
0668   std::vector<float> simTICLCandidate_boundaryPz;
0669   std::vector<float> simTICLCandidate_caloParticleMass;
0670   std::vector<float> simTICLCandidate_time;
0671   std::vector<int> simTICLCandidate_pdgId;
0672   std::vector<int> simTICLCandidate_charge;
0673   std::vector<int> simTICLCandidate_track_in_candidate;
0674 
0675   // from TICLCandidate, product of linking
0676   size_t nCandidates;
0677   std::vector<int> candidate_charge;
0678   std::vector<int> candidate_pdgId;
0679   std::vector<float> candidate_energy;
0680   std::vector<float> candidate_raw_energy;
0681   std::vector<double> candidate_px;
0682   std::vector<double> candidate_py;
0683   std::vector<double> candidate_pz;
0684   std::vector<float> candidate_time;
0685   std::vector<float> candidate_time_err;
0686   std::vector<std::vector<float>> candidate_id_probabilities;
0687   std::vector<std::vector<uint32_t>> tracksters_in_candidate;
0688   std::vector<int> track_in_candidate;
0689 
0690   // Layer clusters
0691   std::vector<uint32_t> cluster_seedID;
0692   std::vector<float> cluster_energy;
0693   std::vector<float> cluster_correctedEnergy;
0694   std::vector<float> cluster_correctedEnergyUncertainty;
0695   std::vector<float> cluster_position_x;
0696   std::vector<float> cluster_position_y;
0697   std::vector<float> cluster_position_z;
0698   std::vector<float> cluster_position_eta;
0699   std::vector<float> cluster_position_phi;
0700   std::vector<unsigned int> cluster_layer_id;
0701   std::vector<int> cluster_type;
0702   std::vector<float> cluster_time;
0703   std::vector<float> cluster_timeErr;
0704   std::vector<uint32_t> cluster_number_of_hits;
0705 
0706   // Tracks
0707   std::vector<unsigned int> track_id;
0708   std::vector<float> track_hgcal_x;
0709   std::vector<float> track_hgcal_y;
0710   std::vector<float> track_hgcal_z;
0711   std::vector<float> track_hgcal_px;
0712   std::vector<float> track_hgcal_py;
0713   std::vector<float> track_hgcal_pz;
0714   std::vector<float> track_hgcal_eta;
0715   std::vector<float> track_hgcal_phi;
0716   std::vector<float> track_hgcal_pt;
0717   std::vector<float> track_pt;
0718   std::vector<int> track_quality;
0719   std::vector<int> track_missing_outer_hits;
0720   std::vector<int> track_missing_inner_hits;
0721   std::vector<int> track_charge;
0722   std::vector<double> track_time;
0723   std::vector<float> track_time_quality;
0724   std::vector<float> track_time_err;
0725   std::vector<float> track_beta;
0726   std::vector<float> track_time_mtd;
0727   std::vector<float> track_time_mtd_err;
0728   std::vector<GlobalPoint> track_pos_mtd;
0729   std::vector<int> track_nhits;
0730   std::vector<int> track_isMuon;
0731   std::vector<int> track_isTrackerMuon;
0732 
0733   TTree* cluster_tree_;
0734   TTree* candidate_tree_;
0735   TTree* superclustering_tree_;
0736   TTree* tracks_tree_;
0737   TTree* simTICLCandidate_tree;
0738 };
0739 
0740 void TICLDumper::clearVariables() {
0741   // event info
0742   nclusters_ = 0;
0743 
0744   for (TracksterDumperHelper& tsDumper : tracksters_dumperHelpers_) {
0745     tsDumper.clearVariables();
0746   }
0747 
0748   superclustering_linkedResultTracksters.clear();
0749 
0750   recoSuperCluster_rawEnergy.clear();
0751   recoSuperCluster_energy.clear();
0752   recoSuperCluster_correctedEnergy.clear();
0753   recoSuperCluster_position_x.clear();
0754   recoSuperCluster_position_y.clear();
0755   recoSuperCluster_position_z.clear();
0756   recoSuperCluster_position_eta.clear();
0757   recoSuperCluster_position_phi.clear();
0758   recoSuperCluster_seedTs.clear();
0759   recoSuperCluster_constituentTs.clear();
0760 
0761   simTICLCandidate_raw_energy.clear();
0762   simTICLCandidate_regressed_energy.clear();
0763   simTICLCandidate_simTracksterCPIndex.clear();
0764   simTICLCandidate_boundaryX.clear();
0765   simTICLCandidate_boundaryY.clear();
0766   simTICLCandidate_boundaryZ.clear();
0767   simTICLCandidate_boundaryPx.clear();
0768   simTICLCandidate_boundaryPy.clear();
0769   simTICLCandidate_boundaryPz.clear();
0770   simTICLCandidate_time.clear();
0771   simTICLCandidate_caloParticleMass.clear();
0772   simTICLCandidate_pdgId.clear();
0773   simTICLCandidate_charge.clear();
0774   simTICLCandidate_track_in_candidate.clear();
0775 
0776   nCandidates = 0;
0777   candidate_charge.clear();
0778   candidate_pdgId.clear();
0779   candidate_energy.clear();
0780   candidate_raw_energy.clear();
0781   candidate_px.clear();
0782   candidate_py.clear();
0783   candidate_pz.clear();
0784   candidate_time.clear();
0785   candidate_time_err.clear();
0786   candidate_id_probabilities.clear();
0787   tracksters_in_candidate.clear();
0788   track_in_candidate.clear();
0789 
0790   for (auto& helper : associations_dumperHelpers_) {
0791     helper.clearVariables();
0792   }
0793 
0794   cluster_seedID.clear();
0795   cluster_energy.clear();
0796   cluster_correctedEnergy.clear();
0797   cluster_correctedEnergyUncertainty.clear();
0798   cluster_position_x.clear();
0799   cluster_position_y.clear();
0800   cluster_position_z.clear();
0801   cluster_position_eta.clear();
0802   cluster_position_phi.clear();
0803   cluster_layer_id.clear();
0804   cluster_type.clear();
0805   cluster_time.clear();
0806   cluster_timeErr.clear();
0807   cluster_number_of_hits.clear();
0808 
0809   track_id.clear();
0810   track_hgcal_x.clear();
0811   track_hgcal_y.clear();
0812   track_hgcal_z.clear();
0813   track_hgcal_eta.clear();
0814   track_hgcal_phi.clear();
0815   track_hgcal_px.clear();
0816   track_hgcal_py.clear();
0817   track_hgcal_pz.clear();
0818   track_hgcal_pt.clear();
0819   track_quality.clear();
0820   track_pt.clear();
0821   track_missing_outer_hits.clear();
0822   track_missing_inner_hits.clear();
0823   track_charge.clear();
0824   track_time.clear();
0825   track_time_quality.clear();
0826   track_time_err.clear();
0827   track_beta.clear();
0828   track_time_mtd.clear();
0829   track_time_mtd_err.clear();
0830   track_pos_mtd.clear();
0831   track_nhits.clear();
0832   track_isMuon.clear();
0833   track_isTrackerMuon.clear();
0834 };
0835 
0836 TICLDumper::TICLDumper(const edm::ParameterSet& ps)
0837     : tracksters_parameterSets_(ps.getParameter<std::vector<edm::ParameterSet>>("tracksterCollections")),
0838       tracksters_token_(),
0839       tracksters_in_candidate_token_(
0840           consumes<std::vector<ticl::Trackster>>(ps.getParameter<edm::InputTag>("trackstersInCand"))),
0841       layer_clusters_token_(consumes<std::vector<reco::CaloCluster>>(ps.getParameter<edm::InputTag>("layerClusters"))),
0842       ticl_candidates_token_(consumes<std::vector<TICLCandidate>>(ps.getParameter<edm::InputTag>("ticlcandidates"))),
0843       ticl_candidates_tracksters_token_(
0844           consumes<std::vector<ticl::Trackster>>(ps.getParameter<edm::InputTag>("ticlcandidates"))),
0845       tracks_token_(consumes<std::vector<reco::Track>>(ps.getParameter<edm::InputTag>("tracks"))),
0846       tracks_time_token_(consumes<edm::ValueMap<float>>(ps.getParameter<edm::InputTag>("tracksTime"))),
0847       tracks_time_quality_token_(consumes<edm::ValueMap<float>>(ps.getParameter<edm::InputTag>("tracksTimeQual"))),
0848       tracks_time_err_token_(consumes<edm::ValueMap<float>>(ps.getParameter<edm::InputTag>("tracksTimeErr"))),
0849       tracks_beta_token_(consumes<edm::ValueMap<float>>(ps.getParameter<edm::InputTag>("tracksBeta"))),
0850       tracks_time_mtd_token_(consumes<edm::ValueMap<float>>(ps.getParameter<edm::InputTag>("tracksTimeMtd"))),
0851       tracks_time_mtd_err_token_(consumes<edm::ValueMap<float>>(ps.getParameter<edm::InputTag>("tracksTimeMtdErr"))),
0852       tracks_pos_mtd_token_(consumes<edm::ValueMap<GlobalPoint>>(ps.getParameter<edm::InputTag>("tracksPosMtd"))),
0853       muons_token_(consumes<std::vector<reco::Muon>>(ps.getParameter<edm::InputTag>("muons"))),
0854       clustersTime_token_(
0855           consumes<edm::ValueMap<std::pair<float, float>>>(ps.getParameter<edm::InputTag>("layer_clustersTime"))),
0856       superclustering_linkedResultTracksters_token(
0857           consumes<std::vector<std::vector<unsigned int>>>(ps.getParameter<edm::InputTag>("superclustering"))),
0858       recoSuperClusters_token(
0859           consumes<reco::SuperClusterCollection>(ps.getParameter<edm::InputTag>("recoSuperClusters"))),
0860       recoSuperClusters_caloClusters_token(
0861           consumes<reco::CaloClusterCollection>(ps.getParameter<edm::InputTag>("recoSuperClusters"))),
0862       recoSuperClusters_sourceTracksters_token(consumes<std::vector<ticl::Trackster>>(
0863           ps.getParameter<edm::InputTag>("recoSuperClusters_sourceTracksterCollection"))),
0864       caloGeometry_token_(esConsumes<CaloGeometry, CaloGeometryRecord, edm::Transition::BeginRun>()),
0865       simTracksters_SC_token_(
0866           consumes<std::vector<ticl::Trackster>>(ps.getParameter<edm::InputTag>("simtrackstersSC"))),
0867       simTICLCandidate_token_(
0868           consumes<std::vector<TICLCandidate>>(ps.getParameter<edm::InputTag>("simTICLCandidates"))),
0869       associations_parameterSets_(ps.getParameter<std::vector<edm::ParameterSet>>("associators")),
0870       // The DumperHelpers should not be moved after construction (needed by TTree branch pointers), so construct them all here
0871       associations_dumperHelpers_(associations_parameterSets_.size()),
0872       simclusters_token_(consumes(ps.getParameter<edm::InputTag>("simclusters"))),
0873       caloparticles_token_(consumes(ps.getParameter<edm::InputTag>("caloparticles"))),
0874       geometry_token_(esConsumes<CaloGeometry, CaloGeometryRecord, edm::Transition::BeginRun>()),
0875       detector_(ps.getParameter<std::string>("detector")),
0876       propName_(ps.getParameter<std::string>("propagator")),
0877       bfield_token_(esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>()),
0878       propagator_token_(
0879           esConsumes<Propagator, TrackingComponentsRecord, edm::Transition::BeginRun>(edm::ESInputTag("", propName_))),
0880       saveLCs_(ps.getParameter<bool>("saveLCs")),
0881       saveSuperclustering_(ps.getParameter<bool>("saveSuperclustering")),
0882       //saveSuperclusteringDNNScore_(ps.getParameter<bool>("saveSuperclusteringDNNScore")),
0883       saveRecoSuperclusters_(ps.getParameter<bool>("saveRecoSuperclusters")),
0884       saveTICLCandidate_(ps.getParameter<bool>("saveSimTICLCandidate")),
0885       saveSimTICLCandidate_(ps.getParameter<bool>("saveSimTICLCandidate")),
0886       saveTracks_(ps.getParameter<bool>("saveTracks")) {
0887   if (saveSuperclustering_) {
0888     superclustering_linkedResultTracksters_token =
0889         consumes<std::vector<std::vector<unsigned int>>>(ps.getParameter<edm::InputTag>("superclustering"));
0890     recoSuperClusters_token =
0891         consumes<reco::SuperClusterCollection>(ps.getParameter<edm::InputTag>("recoSuperClusters"));
0892     recoSuperClusters_caloClusters_token =
0893         consumes<reco::CaloClusterCollection>(ps.getParameter<edm::InputTag>("recoSuperClusters"));
0894     recoSuperClusters_sourceTracksters_token = consumes<std::vector<ticl::Trackster>>(
0895         ps.getParameter<edm::InputTag>("recoSuperClusters_sourceTracksterCollection"));
0896   }
0897   std::string detectorName_ = (detector_ == "HFNose") ? "HGCalHFNoseSensitive" : "HGCalEESensitive";
0898   hdc_token_ =
0899       esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(edm::ESInputTag("", detectorName_));
0900 
0901   for (edm::ParameterSet const& tracksterPset : tracksters_parameterSets_) {
0902     tracksters_token_.push_back(
0903         consumes<std::vector<ticl::Trackster>>(tracksterPset.getParameter<edm::InputTag>("inputTag")));
0904     tracksters_dumperHelpers_.emplace_back(
0905         TracksterDumperHelper::tracksterTypeFromString(tracksterPset.getParameter<std::string>("tracksterType")));
0906   }
0907 
0908   for (edm::ParameterSet const& associationPset : associations_parameterSets_) {
0909     associations_recoToSim_token_.push_back(consumes<TracksterToTracksterMap>(
0910         edm::InputTag(associationPset.getParameter<edm::InputTag>("associatorRecoToSimInputTag"))));
0911     associations_simToReco_token_.push_back(consumes<TracksterToTracksterMap>(
0912         edm::InputTag(associationPset.getParameter<edm::InputTag>("associatorSimToRecoInputTag"))));
0913   }
0914 };
0915 
0916 TICLDumper::~TICLDumper() { clearVariables(); };
0917 
0918 void TICLDumper::beginRun(edm::Run const&, edm::EventSetup const& es) {
0919   detectorTools_ = std::make_unique<DetectorTools>(es.getData(hdc_token_),
0920                                                    es.getData(caloGeometry_token_),
0921                                                    es.getData(bfield_token_),
0922                                                    es.getData(propagator_token_));
0923 }
0924 
0925 // Define tree and branches
0926 void TICLDumper::beginJob() {
0927   edm::Service<TFileService> fs;
0928 
0929   // Trackster trees
0930   for (unsigned int i = 0; i < tracksters_parameterSets_.size(); i++) {
0931     edm::ParameterSet const& tracksterPset = tracksters_parameterSets_[i];
0932     TTree* tree =
0933         fs->make<TTree>(tracksterPset.getParameter<std::string>("treeName").c_str(),
0934                         ("Tracksters : " + tracksterPset.getParameter<std::string>("treeName") +
0935                          " (InputTag : " + tracksterPset.getParameter<edm::InputTag>("inputTag").encode() + ")")
0936                             .c_str());
0937     tracksters_trees.push_back(tree);
0938     tracksters_dumperHelpers_[i].initTree(tree, &eventId_);
0939   }
0940   if (saveLCs_) {
0941     cluster_tree_ = fs->make<TTree>("clusters", "TICL tracksters");
0942     cluster_tree_->Branch("event", &eventId_);
0943     cluster_tree_->Branch("seedID", &cluster_seedID);
0944     cluster_tree_->Branch("energy", &cluster_energy);
0945     cluster_tree_->Branch("correctedEnergy", &cluster_correctedEnergy);
0946     cluster_tree_->Branch("correctedEnergyUncertainty", &cluster_correctedEnergyUncertainty);
0947     cluster_tree_->Branch("position_x", &cluster_position_x);
0948     cluster_tree_->Branch("position_y", &cluster_position_y);
0949     cluster_tree_->Branch("position_z", &cluster_position_z);
0950     cluster_tree_->Branch("position_eta", &cluster_position_eta);
0951     cluster_tree_->Branch("position_phi", &cluster_position_phi);
0952     cluster_tree_->Branch("cluster_layer_id", &cluster_layer_id);
0953     cluster_tree_->Branch("cluster_type", &cluster_type);
0954     cluster_tree_->Branch("cluster_time", &cluster_time);
0955     cluster_tree_->Branch("cluster_timeErr", &cluster_timeErr);
0956     cluster_tree_->Branch("cluster_number_of_hits", &cluster_number_of_hits);
0957   }
0958   if (saveTICLCandidate_) {
0959     candidate_tree_ = fs->make<TTree>("candidates", "TICL candidates");
0960     candidate_tree_->Branch("event", &eventId_);
0961     candidate_tree_->Branch("NCandidates", &nCandidates);
0962     candidate_tree_->Branch("candidate_charge", &candidate_charge);
0963     candidate_tree_->Branch("candidate_pdgId", &candidate_pdgId);
0964     candidate_tree_->Branch("candidate_id_probabilities", &candidate_id_probabilities);
0965     candidate_tree_->Branch("candidate_time", &candidate_time);
0966     candidate_tree_->Branch("candidate_timeErr", &candidate_time_err);
0967     candidate_tree_->Branch("candidate_energy", &candidate_energy);
0968     candidate_tree_->Branch("candidate_raw_energy", &candidate_raw_energy);
0969     candidate_tree_->Branch("candidate_px", &candidate_px);
0970     candidate_tree_->Branch("candidate_py", &candidate_py);
0971     candidate_tree_->Branch("candidate_pz", &candidate_pz);
0972     candidate_tree_->Branch("track_in_candidate", &track_in_candidate);
0973     candidate_tree_->Branch("tracksters_in_candidate", &tracksters_in_candidate);
0974   }
0975   if (saveSuperclustering_ || saveRecoSuperclusters_) {
0976     superclustering_tree_ = fs->make<TTree>("superclustering", "Superclustering in HGCAL CE-E");
0977     superclustering_tree_->Branch("event", &eventId_);
0978   }
0979   if (saveSuperclustering_) {
0980     superclustering_tree_->Branch("linkedResultTracksters", &superclustering_linkedResultTracksters);
0981   }
0982   if (saveRecoSuperclusters_) {
0983     superclustering_tree_->Branch("recoSuperCluster_rawEnergy", &recoSuperCluster_rawEnergy);
0984     superclustering_tree_->Branch("recoSuperCluster_energy", &recoSuperCluster_energy);
0985     superclustering_tree_->Branch("recoSuperCluster_correctedEnergy", &recoSuperCluster_correctedEnergy);
0986     superclustering_tree_->Branch("recoSuperCluster_position_x", &recoSuperCluster_position_x);
0987     superclustering_tree_->Branch("recoSuperCluster_position_y", &recoSuperCluster_position_y);
0988     superclustering_tree_->Branch("recoSuperCluster_position_z", &recoSuperCluster_position_z);
0989     superclustering_tree_->Branch("recoSuperCluster_position_eta", &recoSuperCluster_position_eta);
0990     superclustering_tree_->Branch("recoSuperCluster_position_phi", &recoSuperCluster_position_phi);
0991     superclustering_tree_->Branch("recoSuperCluster_seedTs", &recoSuperCluster_seedTs);
0992     superclustering_tree_->Branch("recoSuperCluster_constituentTs", &recoSuperCluster_constituentTs);
0993   }
0994 
0995   if (associations_parameterSets_.size() > 0) {
0996     associations_tree_ = fs->make<TTree>("associations", "Associations");
0997     associations_tree_->Branch("event", &eventId_);
0998   }
0999   for (unsigned int i = 0; i < associations_parameterSets_.size(); i++) {
1000     associations_dumperHelpers_[i].initTree(associations_tree_,
1001                                             associations_parameterSets_[i].getParameter<std::string>("branchName"),
1002                                             associations_parameterSets_[i].getParameter<std::string>("suffix"));
1003   }
1004 
1005   if (saveTracks_) {
1006     tracks_tree_ = fs->make<TTree>("tracks", "Tracks");
1007     tracks_tree_->Branch("event", &eventId_);
1008     tracks_tree_->Branch("track_id", &track_id);
1009     tracks_tree_->Branch("track_hgcal_x", &track_hgcal_x);
1010     tracks_tree_->Branch("track_hgcal_y", &track_hgcal_y);
1011     tracks_tree_->Branch("track_hgcal_z", &track_hgcal_z);
1012     tracks_tree_->Branch("track_hgcal_eta", &track_hgcal_eta);
1013     tracks_tree_->Branch("track_hgcal_phi", &track_hgcal_phi);
1014     tracks_tree_->Branch("track_hgcal_pt", &track_hgcal_pt);
1015     tracks_tree_->Branch("track_pt", &track_pt);
1016     tracks_tree_->Branch("track_missing_outer_hits", &track_missing_outer_hits);
1017     tracks_tree_->Branch("track_missing_inner_hits", &track_missing_inner_hits);
1018     tracks_tree_->Branch("track_quality", &track_quality);
1019     tracks_tree_->Branch("track_charge", &track_charge);
1020     tracks_tree_->Branch("track_time", &track_time);
1021     tracks_tree_->Branch("track_time_quality", &track_time_quality);
1022     tracks_tree_->Branch("track_time_err", &track_time_err);
1023     tracks_tree_->Branch("track_beta", &track_beta);
1024     tracks_tree_->Branch("track_time_mtd", &track_time_mtd);
1025     tracks_tree_->Branch("track_time_mtd_err", &track_time_mtd_err);
1026     tracks_tree_->Branch("track_pos_mtd", &track_pos_mtd);
1027     tracks_tree_->Branch("track_nhits", &track_nhits);
1028     tracks_tree_->Branch("track_isMuon", &track_isMuon);
1029     tracks_tree_->Branch("track_isTrackerMuon", &track_isTrackerMuon);
1030   }
1031 
1032   if (saveSimTICLCandidate_) {
1033     simTICLCandidate_tree = fs->make<TTree>("simTICLCandidate", "Sim TICL Candidate");
1034     simTICLCandidate_tree->Branch("event", &eventId_);
1035     simTICLCandidate_tree->Branch("simTICLCandidate_raw_energy", &simTICLCandidate_raw_energy);
1036     simTICLCandidate_tree->Branch("simTICLCandidate_regressed_energy", &simTICLCandidate_regressed_energy);
1037     simTICLCandidate_tree->Branch("simTICLCandidate_simTracksterCPIndex", &simTICLCandidate_simTracksterCPIndex);
1038     simTICLCandidate_tree->Branch("simTICLCandidate_boundaryX", &simTICLCandidate_boundaryX);
1039     simTICLCandidate_tree->Branch("simTICLCandidate_boundaryY", &simTICLCandidate_boundaryY);
1040     simTICLCandidate_tree->Branch("simTICLCandidate_boundaryZ", &simTICLCandidate_boundaryZ);
1041     simTICLCandidate_tree->Branch("simTICLCandidate_boundaryPx", &simTICLCandidate_boundaryPx);
1042     simTICLCandidate_tree->Branch("simTICLCandidate_boundaryPy", &simTICLCandidate_boundaryPy);
1043     simTICLCandidate_tree->Branch("simTICLCandidate_boundaryPz", &simTICLCandidate_boundaryPz);
1044     simTICLCandidate_tree->Branch("simTICLCandidate_time", &simTICLCandidate_time);
1045     simTICLCandidate_tree->Branch("simTICLCandidate_caloParticleMass", &simTICLCandidate_caloParticleMass);
1046     simTICLCandidate_tree->Branch("simTICLCandidate_pdgId", &simTICLCandidate_pdgId);
1047     simTICLCandidate_tree->Branch("simTICLCandidate_charge", &simTICLCandidate_charge);
1048     simTICLCandidate_tree->Branch("simTICLCandidate_track_in_candidate", &simTICLCandidate_track_in_candidate);
1049   }
1050 }
1051 
1052 void TICLDumper::analyze(const edm::Event& event, const edm::EventSetup& setup) {
1053   eventId_ = event.id();
1054   clearVariables();
1055 
1056   edm::Handle<std::vector<ticl::Trackster>> tracksters_in_candidate_handle;
1057   event.getByToken(tracksters_in_candidate_token_, tracksters_in_candidate_handle);
1058 
1059   //get all the layer clusters
1060   edm::Handle<std::vector<reco::CaloCluster>> layer_clusters_h;
1061   event.getByToken(layer_clusters_token_, layer_clusters_h);
1062   const auto& clusters = *layer_clusters_h;
1063 
1064   edm::Handle<edm::ValueMap<std::pair<float, float>>> clustersTime_h;
1065   event.getByToken(clustersTime_token_, clustersTime_h);
1066   const auto& layerClustersTimes = *clustersTime_h;
1067 
1068   //TICL Candidate
1069   edm::Handle<std::vector<TICLCandidate>> candidates_h;
1070   event.getByToken(ticl_candidates_token_, candidates_h);
1071   const auto& ticlcandidates = *candidates_h;
1072   edm::Handle<std::vector<ticl::Trackster>> ticlcandidates_tracksters_h =
1073       event.getHandle(ticl_candidates_tracksters_token_);
1074 
1075   //Track
1076   edm::Handle<std::vector<reco::Track>> tracks_h;
1077   event.getByToken(tracks_token_, tracks_h);
1078   const auto& tracks = *tracks_h;
1079 
1080   edm::Handle<edm::ValueMap<float>> trackTime_h;
1081   event.getByToken(tracks_time_token_, trackTime_h);
1082   const auto& trackTime = *trackTime_h;
1083 
1084   edm::Handle<edm::ValueMap<float>> trackTimeErr_h;
1085   event.getByToken(tracks_time_err_token_, trackTimeErr_h);
1086   const auto& trackTimeErr = *trackTimeErr_h;
1087 
1088   edm::Handle<edm::ValueMap<float>> trackBeta_h;
1089   event.getByToken(tracks_beta_token_, trackBeta_h);
1090   const auto& trackBeta = *trackBeta_h;
1091 
1092   edm::Handle<edm::ValueMap<float>> trackTimeQual_h;
1093   event.getByToken(tracks_time_quality_token_, trackTimeQual_h);
1094   const auto& trackTimeQual = *trackTimeQual_h;
1095 
1096   edm::Handle<edm::ValueMap<float>> trackTimeMtd_h;
1097   event.getByToken(tracks_time_mtd_token_, trackTimeMtd_h);
1098   const auto& trackTimeMtd = *trackTimeMtd_h;
1099 
1100   edm::Handle<edm::ValueMap<float>> trackTimeMtdErr_h;
1101   event.getByToken(tracks_time_mtd_err_token_, trackTimeMtdErr_h);
1102   const auto& trackTimeMtdErr = *trackTimeMtdErr_h;
1103 
1104   edm::Handle<edm::ValueMap<GlobalPoint>> trackPosMtd_h;
1105   event.getByToken(tracks_pos_mtd_token_, trackPosMtd_h);
1106   const auto& trackPosMtd = *trackPosMtd_h;
1107 
1108   // superclustering
1109   if (saveSuperclustering_)  // To support running with Mustache
1110     superclustering_linkedResultTracksters = event.get(superclustering_linkedResultTracksters_token);
1111 
1112   // muons
1113   edm::Handle<std::vector<reco::Muon>> muons_h;
1114   event.getByToken(muons_token_, muons_h);
1115   auto& muons = *muons_h;
1116 
1117   // recoSuperClusters
1118   if (saveRecoSuperclusters_) {
1119     reco::SuperClusterCollection const& recoSuperClusters = event.get(recoSuperClusters_token);
1120     // reco::CaloClusterCollection const& recoCaloClusters = event.get(recoSuperClusters_caloClusters_token);
1121     std::vector<ticl::Trackster> const& recoSuperClusters_sourceTracksters =
1122         event.get(recoSuperClusters_sourceTracksters_token);
1123 
1124     // Map for fast lookup of hit to trackster index in recoSuperClusters_sourceTracksters
1125     std::unordered_map<DetId, unsigned> hitToTracksterMap;
1126 
1127     for (unsigned ts_id = 0; ts_id < recoSuperClusters_sourceTracksters.size(); ts_id++) {
1128       for (unsigned int lc_index : recoSuperClusters_sourceTracksters[ts_id].vertices()) {
1129         for (auto [detId, fraction] : clusters[lc_index].hitsAndFractions()) {
1130           bool insertionSucceeded = hitToTracksterMap.emplace(detId, ts_id).second;
1131           assert(insertionSucceeded && "TICLDumper found tracksters sharing rechits");
1132         }
1133       }
1134     }
1135 
1136     for (auto const& recoSc : recoSuperClusters) {
1137       recoSuperCluster_rawEnergy.push_back(recoSc.rawEnergy());
1138       recoSuperCluster_energy.push_back(recoSc.energy());
1139       recoSuperCluster_correctedEnergy.push_back(recoSc.correctedEnergy());
1140       recoSuperCluster_position_x.push_back(recoSc.position().x());
1141       recoSuperCluster_position_y.push_back(recoSc.position().y());
1142       recoSuperCluster_position_z.push_back(recoSc.position().z());
1143       recoSuperCluster_position_eta.push_back(recoSc.position().eta());
1144       recoSuperCluster_position_phi.push_back(recoSc.position().phi());
1145 
1146       // Finding the trackster that was used to create the CaloCluster, using the DetId of a hit (we assume there is no sharing of rechits between tracksters)
1147 
1148       // Seed trackster of the supercluster : Using the DetId of the seed rechit of the seed CaloCluster
1149       recoSuperCluster_seedTs.push_back(hitToTracksterMap.at(recoSc.seed()->seed()));
1150       recoSuperCluster_constituentTs.emplace_back();
1151       for (edm::Ptr<reco::CaloCluster> const& caloClusterPtr : recoSc.clusters()) {
1152         // Using the DetId of the seed rechit of the CaloCluster
1153         recoSuperCluster_constituentTs.back().push_back(hitToTracksterMap.at(caloClusterPtr->seed()));
1154       }
1155     }
1156   }
1157 
1158   edm::Handle<std::vector<TICLCandidate>> simTICLCandidates_h;
1159   event.getByToken(simTICLCandidate_token_, simTICLCandidates_h);
1160   const auto& simTICLCandidates = *simTICLCandidates_h;
1161 
1162   edm::Handle<std::vector<CaloParticle>> caloparticles_h;
1163   event.getByToken(caloparticles_token_, caloparticles_h);
1164 
1165   auto simclusters_h = event.getHandle(simclusters_token_);
1166 
1167   nclusters_ = clusters.size();
1168 
1169   // Save all the trackster collections
1170   for (unsigned int i = 0; i < tracksters_dumperHelpers_.size(); i++) {
1171     edm::Handle<std::vector<ticl::Trackster>> tracksters_handle;
1172     std::vector<ticl::Trackster> const& tracksters = event.get<std::vector<ticl::Trackster>>(tracksters_token_[i]);
1173     tracksters_dumperHelpers_[i].fillFromEvent(
1174         tracksters, clusters, layerClustersTimes, *detectorTools_, simclusters_h, caloparticles_h, tracks);
1175     tracksters_trees[i]->Fill();
1176   }
1177 
1178   const auto& simTrackstersSC_h = event.getHandle(simTracksters_SC_token_);
1179   simTICLCandidate_track_in_candidate.resize(simTICLCandidates.size(), -1);
1180   for (size_t i = 0; i < simTICLCandidates.size(); ++i) {
1181     auto const& cand = simTICLCandidates[i];
1182 
1183     simTICLCandidate_raw_energy.push_back(cand.rawEnergy());
1184     simTICLCandidate_regressed_energy.push_back(cand.p4().energy());
1185     simTICLCandidate_pdgId.push_back(cand.pdgId());
1186     simTICLCandidate_charge.push_back(cand.charge());
1187     simTICLCandidate_time.push_back(cand.time());
1188     std::vector<int> tmpIdxVec;
1189     for (auto const& simTS : cand.tracksters()) {
1190       auto trackster_idx = simTS.get() - (edm::Ptr<ticl::Trackster>(simTrackstersSC_h, 0)).get();
1191       tmpIdxVec.push_back(trackster_idx);
1192     }
1193     simTICLCandidate_simTracksterCPIndex.push_back(tmpIdxVec);
1194     tmpIdxVec.clear();
1195     auto const& trackPtr = cand.trackPtr();
1196     if (!trackPtr.isNull()) {
1197       auto const& track = *trackPtr;
1198       int iSide = int(track.eta() > 0);
1199       int tk_idx = trackPtr.get() - (edm::Ptr<reco::Track>(tracks_h, 0)).get();
1200       simTICLCandidate_track_in_candidate[i] = tk_idx;
1201 
1202       const auto& fts = trajectoryStateTransform::outerFreeState((track), &detectorTools_->bfield);
1203       // to the HGCal front
1204       const auto& tsos = detectorTools_->propagator.propagate(fts, detectorTools_->firstDisk_[iSide]->surface());
1205       if (tsos.isValid()) {
1206         const auto& globalPos = tsos.globalPosition();
1207         const auto& globalMom = tsos.globalMomentum();
1208         simTICLCandidate_boundaryX.push_back(globalPos.x());
1209         simTICLCandidate_boundaryY.push_back(globalPos.y());
1210         simTICLCandidate_boundaryZ.push_back(globalPos.z());
1211         simTICLCandidate_boundaryPx.push_back(globalMom.x());
1212         simTICLCandidate_boundaryPy.push_back(globalMom.y());
1213         simTICLCandidate_boundaryPz.push_back(globalMom.z());
1214       } else {
1215         simTICLCandidate_boundaryX.push_back(-999);
1216         simTICLCandidate_boundaryY.push_back(-999);
1217         simTICLCandidate_boundaryZ.push_back(-999);
1218         simTICLCandidate_boundaryPx.push_back(-999);
1219         simTICLCandidate_boundaryPy.push_back(-999);
1220         simTICLCandidate_boundaryPz.push_back(-999);
1221       }
1222     } else {
1223       simTICLCandidate_boundaryX.push_back(-999);
1224       simTICLCandidate_boundaryY.push_back(-999);
1225       simTICLCandidate_boundaryZ.push_back(-999);
1226       simTICLCandidate_boundaryPx.push_back(-999);
1227       simTICLCandidate_boundaryPy.push_back(-999);
1228       simTICLCandidate_boundaryPz.push_back(-999);
1229     }
1230   }
1231 
1232   int c_id = 0;
1233 
1234   for (auto cluster_iterator = clusters.begin(); cluster_iterator != clusters.end(); ++cluster_iterator) {
1235     auto lc_seed = cluster_iterator->seed();
1236     cluster_seedID.push_back(lc_seed);
1237     cluster_energy.push_back(cluster_iterator->energy());
1238     cluster_correctedEnergy.push_back(cluster_iterator->correctedEnergy());
1239     cluster_correctedEnergyUncertainty.push_back(cluster_iterator->correctedEnergyUncertainty());
1240     cluster_position_x.push_back(cluster_iterator->x());
1241     cluster_position_y.push_back(cluster_iterator->y());
1242     cluster_position_z.push_back(cluster_iterator->z());
1243     cluster_position_eta.push_back(cluster_iterator->eta());
1244     cluster_position_phi.push_back(cluster_iterator->phi());
1245     auto haf = cluster_iterator->hitsAndFractions();
1246     auto layerId = detectorTools_->rhtools.getLayerWithOffset(haf[0].first);
1247     cluster_layer_id.push_back(layerId);
1248     uint32_t number_of_hits = cluster_iterator->hitsAndFractions().size();
1249     cluster_number_of_hits.push_back(number_of_hits);
1250     cluster_type.push_back(detectorTools_->rhtools.getCellType(lc_seed));
1251     cluster_timeErr.push_back(layerClustersTimes.get(c_id).second);
1252     cluster_time.push_back(layerClustersTimes.get(c_id).first);
1253     c_id += 1;
1254   }
1255 
1256   tracksters_in_candidate.resize(ticlcandidates.size());
1257   track_in_candidate.resize(ticlcandidates.size(), -1);
1258   nCandidates = ticlcandidates.size();
1259   for (int i = 0; i < static_cast<int>(ticlcandidates.size()); ++i) {
1260     const auto& candidate = ticlcandidates[i];
1261     candidate_charge.push_back(candidate.charge());
1262     candidate_pdgId.push_back(candidate.pdgId());
1263     candidate_energy.push_back(candidate.energy());
1264     candidate_raw_energy.push_back(candidate.rawEnergy());
1265     candidate_px.push_back(candidate.px());
1266     candidate_py.push_back(candidate.py());
1267     candidate_pz.push_back(candidate.pz());
1268     candidate_time.push_back(candidate.time());
1269     candidate_time_err.push_back(candidate.timeError());
1270     std::vector<float> id_probs;
1271     for (int j = 0; j < 8; j++) {
1272       ticl::Trackster::ParticleType type = static_cast<ticl::Trackster::ParticleType>(j);
1273       id_probs.push_back(candidate.id_probability(type));
1274     }
1275     candidate_id_probabilities.push_back(id_probs);
1276 
1277     auto trackster_ptrs = candidate.tracksters();
1278     auto track_ptr = candidate.trackPtr();
1279     for (const auto& ts_ptr : trackster_ptrs) {
1280       auto ts_idx = ts_ptr.get() - (edm::Ptr<ticl::Trackster>(tracksters_in_candidate_handle, 0)).get();
1281       tracksters_in_candidate[i].push_back(ts_idx);
1282     }
1283     if (track_ptr.isNull())
1284       continue;
1285     int tk_idx = track_ptr.get() - (edm::Ptr<reco::Track>(tracks_h, 0)).get();
1286     track_in_candidate[i] = tk_idx;
1287   }
1288 
1289   // trackster to simTrackster associations
1290   for (unsigned int i = 0; i < associations_dumperHelpers_.size(); i++) {
1291     associations_dumperHelpers_[i].fillFromEvent(event.get(associations_recoToSim_token_[i]),
1292                                                  event.get(associations_simToReco_token_[i]));
1293   }
1294   if (associations_dumperHelpers_.size() > 0)
1295     associations_tree_->Fill();
1296 
1297   //Tracks
1298   for (size_t i = 0; i < tracks.size(); i++) {
1299     const auto& track = tracks[i];
1300     reco::TrackRef trackref = reco::TrackRef(tracks_h, i);
1301     int iSide = int(track.eta() > 0);
1302     const auto& fts = trajectoryStateTransform::outerFreeState((track), &detectorTools_->bfield);
1303     // to the HGCal front
1304     const auto& tsos = detectorTools_->propagator.propagate(fts, detectorTools_->firstDisk_[iSide]->surface());
1305     if (tsos.isValid()) {
1306       const auto& globalPos = tsos.globalPosition();
1307       const auto& globalMom = tsos.globalMomentum();
1308       track_id.push_back(i);
1309       track_hgcal_x.push_back(globalPos.x());
1310       track_hgcal_y.push_back(globalPos.y());
1311       track_hgcal_z.push_back(globalPos.z());
1312       track_hgcal_eta.push_back(globalPos.eta());
1313       track_hgcal_phi.push_back(globalPos.phi());
1314       track_hgcal_px.push_back(globalMom.x());
1315       track_hgcal_py.push_back(globalMom.y());
1316       track_hgcal_pz.push_back(globalMom.z());
1317       track_hgcal_pt.push_back(globalMom.perp());
1318       track_pt.push_back(track.pt());
1319       track_quality.push_back(track.quality(reco::TrackBase::highPurity));
1320       track_missing_outer_hits.push_back(track.missingOuterHits());
1321       track_missing_inner_hits.push_back(track.missingInnerHits());
1322       track_charge.push_back(track.charge());
1323       track_time.push_back(trackTime[trackref]);
1324       track_time_quality.push_back(trackTimeQual[trackref]);
1325       track_time_err.push_back(trackTimeErr[trackref]);
1326       track_beta.push_back(trackBeta[trackref]);
1327       track_time_mtd.push_back(trackTimeMtd[trackref]);
1328       track_time_mtd_err.push_back(trackTimeMtdErr[trackref]);
1329       track_pos_mtd.push_back(trackPosMtd[trackref]);
1330       track_nhits.push_back(tracks[i].recHitsSize());
1331       int muId = PFMuonAlgo::muAssocToTrack(trackref, *muons_h);
1332       if (muId != -1) {
1333         const reco::MuonRef muonref = reco::MuonRef(muons_h, muId);
1334         track_isMuon.push_back(PFMuonAlgo::isMuon(muonref));
1335         track_isTrackerMuon.push_back(muons[muId].isTrackerMuon());
1336       } else {
1337         track_isMuon.push_back(-1);
1338         track_isTrackerMuon.push_back(-1);
1339       }
1340     }
1341   }
1342 
1343   if (saveLCs_)
1344     cluster_tree_->Fill();
1345   if (saveTICLCandidate_)
1346     candidate_tree_->Fill();
1347   if (saveSuperclustering_ || saveRecoSuperclusters_)
1348     superclustering_tree_->Fill();
1349   if (saveTracks_)
1350     tracks_tree_->Fill();
1351   if (saveSimTICLCandidate_)
1352     simTICLCandidate_tree->Fill();
1353 }
1354 
1355 void TICLDumper::endJob() {}
1356 
1357 void TICLDumper::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
1358   edm::ParameterSetDescription desc;
1359 
1360   // Settings for dumping trackster collections
1361   edm::ParameterSetDescription tracksterDescValidator;
1362   tracksterDescValidator.add<std::string>("treeName")
1363       ->setComment("Name of the output tree for the trackster collection");
1364   tracksterDescValidator.add<edm::InputTag>("inputTag")->setComment("Input tag for the trackster collection to write");
1365   tracksterDescValidator.ifValue(
1366       edm::ParameterDescription<std::string>(
1367           "tracksterType",
1368           "Trackster",
1369           true,
1370           edm::Comment("Type of trackster. Trackster=regular trackster (from RECO). SimTracksterCP=Simtrackster "
1371                        "from CaloParticle. SimTracksterSC=Simtrackster from SimCluster")),
1372       edm::allowedValues<std::string>("Trackster", "SimTracksterCP", "SimTracksterSC"));
1373   desc.addVPSet("tracksterCollections", tracksterDescValidator)->setComment("Trackster collections to dump");
1374 
1375   desc.add<edm::InputTag>("trackstersInCand", edm::InputTag("ticlTrackstersCLUE3DHigh"));
1376 
1377   desc.add<edm::InputTag>("layerClusters", edm::InputTag("hgcalMergeLayerClusters"));
1378   desc.add<edm::InputTag>("layer_clustersTime", edm::InputTag("hgcalMergeLayerClusters", "timeLayerCluster"));
1379   desc.add<edm::InputTag>("ticlcandidates", edm::InputTag("ticlTrackstersMerge"));
1380   desc.add<edm::InputTag>("tracks", edm::InputTag("generalTracks"));
1381   desc.add<edm::InputTag>("tracksTime", edm::InputTag("tofPID:t0"));
1382   desc.add<edm::InputTag>("tracksTimeQual", edm::InputTag("mtdTrackQualityMVA:mtdQualMVA"));
1383   desc.add<edm::InputTag>("tracksTimeErr", edm::InputTag("tofPID:sigmat0"));
1384   desc.add<edm::InputTag>("tracksBeta", edm::InputTag("trackExtenderWithMTD:generalTrackBeta"));
1385   desc.add<edm::InputTag>("tracksTimeMtd", edm::InputTag("trackExtenderWithMTD:generalTracktmtd"));
1386   desc.add<edm::InputTag>("tracksTimeMtdErr", edm::InputTag("trackExtenderWithMTD:generalTracksigmatmtd"));
1387   desc.add<edm::InputTag>("tracksPosMtd", edm::InputTag("trackExtenderWithMTD:generalTrackmtdpos"));
1388   desc.add<edm::InputTag>("muons", edm::InputTag("muons1stStep"));
1389   desc.add<edm::InputTag>("superclustering", edm::InputTag("ticlTracksterLinksSuperclusteringDNN"));
1390   desc.add<edm::InputTag>("recoSuperClusters", edm::InputTag("particleFlowSuperClusterHGCal"))
1391       ->setComment(
1392           "egamma supercluster collection (either from PFECALSuperClusterProducer for Mustache, or from "
1393           "TICL->Egamma converter in case of TICL DNN superclusters)");
1394   desc.add<edm::InputTag>("recoSuperClusters_sourceTracksterCollection", edm::InputTag("ticlTrackstersMerge"))
1395       ->setComment(
1396           "Trackster collection used to produce the reco::SuperCluster, used to provide a mapping back to the "
1397           "tracksters used in superclusters");
1398 
1399   desc.add<edm::InputTag>("simtrackstersSC", edm::InputTag("ticlSimTracksters"))
1400       ->setComment("SimTrackster from CaloParticle collection to use for simTICLcandidates");
1401   desc.add<edm::InputTag>("simTICLCandidates", edm::InputTag("ticlSimTracksters"));
1402 
1403   // Settings for dumping trackster associators (recoToSim & simToReco)
1404   edm::ParameterSetDescription associatorDescValidator;
1405   associatorDescValidator.add<std::string>("branchName")->setComment("Name of the output branches in the tree");
1406   associatorDescValidator.add<std::string>("suffix")->setComment("Should be CP or SC (for the output branch name)");
1407   associatorDescValidator.add<edm::InputTag>("associatorRecoToSimInputTag")
1408       ->setComment("Input tag for the RecoToSim associator to dump");
1409   associatorDescValidator.add<edm::InputTag>("associatorSimToRecoInputTag")
1410       ->setComment("Input tag for the SimToReco associator to dump");
1411   desc.addVPSet("associators", associatorDescValidator)->setComment("Tracksters to SimTracksters associators to dump");
1412 
1413   desc.add<edm::InputTag>("simclusters", edm::InputTag("mix", "MergedCaloTruth"));
1414   desc.add<edm::InputTag>("caloparticles", edm::InputTag("mix", "MergedCaloTruth"));
1415   desc.add<std::string>("detector", "HGCAL");
1416   desc.add<std::string>("propagator", "PropagatorWithMaterial");
1417 
1418   desc.add<bool>("saveLCs", true);
1419   desc.add<bool>("saveTICLCandidate", true);
1420   desc.add<bool>("saveSimTICLCandidate", true);
1421   desc.add<bool>("saveTracks", true);
1422   desc.add<bool>("saveSuperclustering", true);
1423   desc.add<bool>("saveRecoSuperclusters", true)
1424       ->setComment("Save superclustering Egamma collections (as reco::SuperCluster)");
1425   descriptions.add("ticlDumper", desc);
1426 }
1427 
1428 DEFINE_FWK_MODULE(TICLDumper);