Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 07:56:21

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2022 Sylvester Joosten, Chao, Chao Peng, Whitney Armstrong
0003 
0004 /*
0005  *  Reconstruct the cluster with Center of Gravity method
0006  *  Logarithmic weighting is used for mimicking energy deposit in transverse direction
0007  *
0008  *  Author: Chao Peng (ANL), 09/27/2020
0009  */
0010 
0011 #pragma once
0012 
0013 #include <algorithms/algorithm.h>
0014 #include <edm4eic/ClusterCollection.h>
0015 #include <edm4eic/EDM4eicVersion.h>
0016 #include <edm4hep/CaloHitContribution.h>
0017 #include <edm4hep/MCParticle.h>
0018 #if EDM4EIC_VERSION_MAJOR >= 7
0019 #include <edm4eic/MCRecoCalorimeterHitAssociationCollection.h>
0020 #else
0021 #include <edm4hep/SimCalorimeterHitCollection.h>
0022 #endif
0023 #include <edm4eic/MCRecoClusterParticleAssociationCollection.h>
0024 #include <edm4eic/ProtoClusterCollection.h>
0025 #include <algorithm>
0026 #include <cmath>
0027 #include <functional>
0028 #include <map>
0029 #include <memory>
0030 #include <optional>
0031 #include <string>
0032 #include <string_view>
0033 #include <utility>
0034 
0035 #include "CalorimeterClusterRecoCoGConfig.h"
0036 #include "algorithms/interfaces/WithPodConfig.h"
0037 
0038 static double constWeight(double /*E*/, double /*tE*/, double /*p*/, int /*type*/) { return 1.0; }
0039 static double linearWeight(double E, double /*tE*/, double /*p*/, int /*type*/) { return E; }
0040 static double logWeight(double E, double tE, double base, int /*type*/) {
0041   return std::max(0., base + std::log(E / tE));
0042 }
0043 
0044 static const std::map<std::string, std::function<double(double, double, double, int)>>
0045     weightMethods = {
0046         {"none", constWeight},
0047         {"linear", linearWeight},
0048         {"log", logWeight},
0049 };
0050 
0051 namespace eicrecon {
0052 
0053 using ClustersWithAssociations =
0054     std::pair<std::unique_ptr<edm4eic::ClusterCollection>,
0055               std::unique_ptr<edm4eic::MCRecoClusterParticleAssociationCollection>>;
0056 
0057 using CalorimeterClusterRecoCoGAlgorithm = algorithms::Algorithm<
0058     algorithms::Input<edm4eic::ProtoClusterCollection,
0059 #if EDM4EIC_VERSION_MAJOR >= 7
0060                       std::optional<edm4eic::MCRecoCalorimeterHitAssociationCollection>
0061 #else
0062                       std::optional<edm4hep::SimCalorimeterHitCollection>
0063 #endif
0064                       >,
0065     algorithms::Output<edm4eic::ClusterCollection,
0066                        std::optional<edm4eic::MCRecoClusterParticleAssociationCollection>>>;
0067 
0068 class CalorimeterClusterRecoCoG : public CalorimeterClusterRecoCoGAlgorithm,
0069                                   public WithPodConfig<CalorimeterClusterRecoCoGConfig> {
0070 
0071 public:
0072   CalorimeterClusterRecoCoG(std::string_view name) : CalorimeterClusterRecoCoGAlgorithm {
0073     name,
0074 #if EDM4EIC_VERSION_MAJOR >= 7
0075         {"inputProtoClusterCollection", "mcRawHitAssocations"},
0076 #else
0077         {"inputProtoClusterCollection", "mcHits"},
0078 #endif
0079         {"outputClusterCollection", "outputAssociations"},
0080         "Reconstruct a cluster with the Center of Gravity method. For "
0081         "simulation results it optionally creates a Cluster <-> MCParticle "
0082         "association provided both optional arguments are provided."
0083   }
0084   {}
0085 
0086 public:
0087   void init() final;
0088 
0089   void process(const Input&, const Output&) const final;
0090 
0091 private:
0092   std::function<double(double, double, double, int)> weightFunc;
0093 
0094 private:
0095   std::optional<edm4eic::MutableCluster> reconstruct(const edm4eic::ProtoCluster& pcl) const;
0096 #if EDM4EIC_VERSION_MAJOR >= 7
0097   void associate(const edm4eic::Cluster& cl,
0098                  const edm4eic::MCRecoCalorimeterHitAssociationCollection* mchitassociations,
0099                  edm4eic::MCRecoClusterParticleAssociationCollection* assocs) const;
0100 #else
0101   void associate(const edm4eic::Cluster& cl, const edm4hep::SimCalorimeterHitCollection* mchits,
0102                  edm4eic::MCRecoClusterParticleAssociationCollection* assocs) const;
0103 #endif
0104   static edm4hep::MCParticle get_primary(const edm4hep::CaloHitContribution& contrib);
0105 };
0106 
0107 } // namespace eicrecon