Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-19 09:50:48

0001 // Copyright 2023-2025, Simon Gardner
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 //
0004 //
0005 
0006 #include <Evaluator/DD4hepUnits.h>
0007 #include <JANA/JApplication.h>
0008 #include <JANA/JApplicationFwd.h>
0009 #include <JANA/Utils/JTypeInfo.h>
0010 #include <edm4eic/MCRecoTrackParticleAssociation.h>
0011 #include <edm4eic/MCRecoTrackParticleLinkCollection.h>
0012 #include <edm4eic/Track.h>
0013 #include <edm4eic/TrackerHit.h>
0014 #include <edm4eic/unit_system.h>
0015 #include <edm4hep/SimTrackerHit.h>
0016 #include <format> // IWYU pragma: keep
0017 #include <podio/detail/Link.h>
0018 #include <cmath>
0019 #include <cstddef>
0020 #include <deque>
0021 #include <functional>
0022 #include <map>
0023 #include <memory>
0024 #include <string>
0025 #include <vector>
0026 
0027 #include "algorithms/meta/SubDivideFunctors.h"
0028 #include "extensions/jana/JOmniFactoryGeneratorT.h"
0029 #include "factories/digi/PulseCombiner_factory.h"
0030 #include "factories/digi/PulseGeneration_factory.h"
0031 #include "factories/digi/PulseNoise_factory.h"
0032 #include "factories/digi/SiliconChargeSharing_factory.h"
0033 #include "factories/digi/SiliconTrackerDigi_factory.h"
0034 #include "factories/fardetectors/FarDetectorLinearTracking_factory.h"
0035 #include "factories/fardetectors/FarDetectorTrackerCluster_factory.h"
0036 #include "factories/fardetectors/FarDetectorTransportationPostML_factory.h"
0037 #include "factories/fardetectors/FarDetectorTransportationPreML_factory.h"
0038 #include "factories/meta/CollectionCollector_factory.h"
0039 #include "factories/meta/ONNXInference_factory.h"
0040 #include "factories/meta/SubDivideCollection_factory.h"
0041 #include "factories/tracking/TrackerHitReconstruction_factory.h"
0042 
0043 extern "C" {
0044 void InitPlugin(JApplication* app) {
0045   InitJANAPlugin(app);
0046 
0047   using namespace eicrecon;
0048 
0049   std::string readout = "TaggerTrackerHits";
0050 
0051   app->Add(new JOmniFactoryGeneratorT<SiliconChargeSharing_factory>(
0052       "TaggerTrackerChargeSharing", {"TaggerTrackerHits"}, {"TaggerTrackerSharedHits"},
0053       {
0054           .sigma_sharingx = 15 * dd4hep::um,
0055           .sigma_sharingy = 15 * dd4hep::um,
0056           .min_edep       = 0.1 * edm4eic::unit::keV,
0057           .readout        = readout,
0058       },
0059       app));
0060   //  Generate signal pulse from hits
0061   app->Add(new JOmniFactoryGeneratorT<PulseGeneration_factory<edm4hep::SimTrackerHit>>(
0062       "TaggerTrackerPulseGeneration", {"TaggerTrackerSharedHits"}, {"TaggerTrackerHitPulses"},
0063       {
0064           .pulse_shape_function = "LandauPulse",
0065           .pulse_shape_params   = {1.0, 2 * edm4eic::unit::ns},
0066           .ignore_thres         = 15.0e-8,
0067           .timestep             = 0.2 * edm4eic::unit::ns,
0068       },
0069       app));
0070 
0071   // Combine pulses into larger pulses
0072   app->Add(new JOmniFactoryGeneratorT<PulseCombiner_factory>(
0073       "TaggerTrackerPulseCombiner", {"TaggerTrackerHitPulses"}, {"TaggerTrackerCombinedPulses"},
0074       {
0075           .minimum_separation = 25 * edm4eic::unit::ns,
0076       },
0077       app));
0078 
0079   // Add noise to pulses
0080   app->Add(new JOmniFactoryGeneratorT<PulseNoise_factory>(
0081       "TaggerTrackerPulseNoise", {"EventHeader", "TaggerTrackerCombinedPulses"},
0082       {"TaggerTrackerCombinedPulsesWithNoise"},
0083       {
0084           .poles    = 5,
0085           .variance = 1.0,
0086           .alpha    = 0.5,
0087           .scale    = 0.000002,
0088       },
0089       app));
0090 
0091   // Hit time resolution, shared by the digitization smearing and TrackerHit::timeError
0092   const double timeResolution = 2 * edm4eic::unit::ns;
0093 
0094   // Digitization of silicon hits
0095   app->Add(new JOmniFactoryGeneratorT<SiliconTrackerDigi_factory>(
0096       "TaggerTrackerRawHits", {"EventHeader", "TaggerTrackerHits"},
0097       {"TaggerTrackerRawHits", "TaggerTrackerRawHitLinks", "TaggerTrackerRawHitAssociations"},
0098       {
0099           .threshold      = 1.5 * edm4eic::unit::keV,
0100           .timeResolution = timeResolution,
0101       },
0102       app));
0103 
0104   // Convert raw digitized hits into hits with geometry info (ready for tracking)
0105   app->Add(new JOmniFactoryGeneratorT<TrackerHitReconstruction_factory>(
0106       "TaggerTrackerRecHits", {"TaggerTrackerRawHits"}, {"TaggerTrackerRecHits"},
0107       {
0108           .timeResolution = timeResolution,
0109       },
0110       app));
0111 
0112   // Divide collection based on geometry segmentation labels
0113   // This should really be done before digitization as summing hits in the same cell couldn't even be mixed between layers. At the moment just prep for clustering.
0114   std::vector<std::string> geometryLabels{"module", "layer"};
0115   std::vector<int> moduleIDs{1, 2};
0116   std::vector<int> layerIDs{0, 1, 2, 3};
0117   std::vector<std::vector<long int>> geometryDivisions{};
0118   std::vector<std::string> geometryDivisionCollectionNames;
0119   std::vector<std::string> outputClusterCollectionNames;
0120   std::vector<std::string> outputTrackTags;
0121   std::vector<std::string> outputTrackLinkTags;
0122   std::vector<std::string> outputTrackAssociationTags;
0123   std::vector<std::vector<std::string>> moduleClusterTags;
0124 
0125   for (int mod_id : moduleIDs) {
0126     outputTrackTags.push_back(std::format("TaggerTrackerM{}LocalTracks", mod_id));
0127     outputTrackLinkTags.push_back(std::format("TaggerTrackerM{}LocalTrackLinks", mod_id));
0128     outputTrackAssociationTags.push_back(
0129         std::format("TaggerTrackerM{}LocalTrackAssociations", mod_id));
0130     moduleClusterTags.emplace_back();
0131     for (int lay_id : layerIDs) {
0132       geometryDivisions.push_back({mod_id, lay_id});
0133       geometryDivisionCollectionNames.push_back(
0134           std::format("TaggerTrackerM{}L{}RecHits", mod_id, lay_id));
0135       outputClusterCollectionNames.push_back(
0136           std::format("TaggerTrackerM{}L{}ClusterPositions", mod_id, lay_id));
0137       moduleClusterTags.back().push_back(outputClusterCollectionNames.back());
0138     }
0139   }
0140 
0141   app->Add(new JOmniFactoryGeneratorT<SubDivideCollection_factory<edm4eic::TrackerHit>>(
0142       "TaggerTrackerSplitHits", {"TaggerTrackerRecHits"}, geometryDivisionCollectionNames,
0143       {
0144           .function = GeometrySplit{geometryDivisions, readout, geometryLabels},
0145       },
0146       app));
0147 
0148   app->Add(new JOmniFactoryGeneratorT<FarDetectorTrackerCluster_factory>(
0149       "TaggerTrackerClustering", geometryDivisionCollectionNames, outputClusterCollectionNames,
0150       {
0151           .readout        = "TaggerTrackerHits",
0152           .x_field        = "x",
0153           .y_field        = "y",
0154           .hit_time_limit = 10 * edm4eic::unit::ns,
0155       },
0156       app));
0157 
0158   // Linear tracking for each module, loop over modules
0159   for (std::size_t i = 0; i < moduleIDs.size(); i++) {
0160     std::string outputTrackTag                = outputTrackTags[i];
0161     std::string outputTrackLinkTag            = outputTrackLinkTags[i];
0162     std::string outputTrackAssociationTag     = outputTrackAssociationTags[i];
0163     std::vector<std::string> inputClusterTags = moduleClusterTags[i];
0164 
0165     inputClusterTags.emplace_back("TaggerTrackerRawHitLinks");
0166     inputClusterTags.emplace_back("TaggerTrackerRawHitAssociations");
0167 
0168     app->Add(new JOmniFactoryGeneratorT<FarDetectorLinearTracking_factory>(
0169         outputTrackTag, {inputClusterTags},
0170         {outputTrackTag, outputTrackLinkTag, outputTrackAssociationTag},
0171         {
0172             .layer_hits_max       = 200,
0173             .chi2_max             = 0.001,
0174             .n_layer              = 4,
0175             .layer_weights        = {1.0, 1.0, 1.0, 1.0},
0176             .restrict_direction   = true,
0177             .optimum_theta        = -M_PI + 0.026,
0178             .optimum_phi          = 0,
0179             .step_angle_tolerance = 0.05,
0180         },
0181         app));
0182   }
0183 
0184   // Combine the tracks from each module into one collection
0185   app->Add(new JOmniFactoryGeneratorT<CollectionCollector_factory<edm4eic::Track, true>>(
0186       "TaggerTrackerLocalTracks", outputTrackTags, {"TaggerTrackerLocalTracks"}, app));
0187 
0188   // Combine the track links from each module into one collection
0189   app->Add(new JOmniFactoryGeneratorT<
0190            CollectionCollector_factory<edm4eic::MCRecoTrackParticleLink, true>>(
0191       "TaggerTrackerLocalTrackLinks", outputTrackLinkTags, {"TaggerTrackerLocalTrackLinks"}, app));
0192 
0193   // Combine the associations from each module into one collection
0194   app->Add(new JOmniFactoryGeneratorT<
0195            CollectionCollector_factory<edm4eic::MCRecoTrackParticleAssociation, true>>(
0196       "TaggerTrackerLocalTrackAssociations", outputTrackAssociationTags,
0197       {"TaggerTrackerLocalTrackAssociations"}, app));
0198 
0199   app->Add(new JOmniFactoryGeneratorT<FarDetectorTransportationPreML_factory>(
0200       "TaggerTrackerTransportationPreML",
0201       {"TaggerTrackerLocalTracks", "TaggerTrackerLocalTrackLinks", "MCBeamElectrons"},
0202       {"TaggerTrackerFeatureTensor", "TaggerTrackerTargetTensor"},
0203       {
0204           .beamE = 10.0,
0205       },
0206       app));
0207   app->Add(new JOmniFactoryGeneratorT<ONNXInference_factory>(
0208       "TaggerTrackerTransportationInference", {"TaggerTrackerFeatureTensor"},
0209       {"TaggerTrackerPredictionTensor"},
0210       {
0211           .modelPath = "calibrations/onnx/Low-Q2_Steering_Reconstruction.onnx",
0212       },
0213       app));
0214   app->Add(new JOmniFactoryGeneratorT<FarDetectorTransportationPostML_factory>(
0215       "TaggerTrackerTransportationPostML",
0216       {"TaggerTrackerPredictionTensor", "TaggerTrackerLocalTrackAssociations", "MCBeamElectrons"},
0217       {"TaggerTrackerReconstructedParticles", "TaggerTrackerReconstructedParticleLinks",
0218        "TaggerTrackerReconstructedParticleAssociations"},
0219       {
0220           .beamE = 10.0,
0221       },
0222       app));
0223 }
0224 }