Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-06-01 07:06:45

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2023, Christopher Dilks
0003 
0004 // Converters of ParticleID objects
0005 //
0006 
0007 #pragma once
0008 
0009 #include <cstddef>
0010 #include <map>
0011 
0012 // data model
0013 #include <edm4eic/CherenkovParticleID.h>
0014 #include <edm4eic/CherenkovParticleIDHypothesis.h>
0015 #include <edm4hep/ParticleIDCollection.h>
0016 
0017 namespace eicrecon {
0018 
0019   class ConvertParticleID {
0020     public:
0021 
0022       // index map, used for external access to the converted objects
0023       using IndexMap = std::map<std::size_t, unsigned int>; // <collection index, object ID>
0024 
0025       // convert edm4eic::CherenkovParticleID hypotheses to list of edm4hep::ParticleID objects
0026       // - requires input `CherenkovParticleID` object `in_pid`
0027       // - adds output `ParticleID` objects to collection `out_pids`
0028       // - sorted by likelihood, if `sort_by_likelihood==true`
0029       // - returns a map of the new `out_pid` indices to new `ParticleID` IDs, so the caller can
0030       //   access the newly created `ParticleID` objects
0031       static IndexMap ConvertToParticleIDs(
0032           const edm4eic::CherenkovParticleID& in_pid,
0033           edm4hep::ParticleIDCollection&      out_pids,
0034           bool                                sort_by_likelihood = false
0035           )
0036       {
0037 
0038         // output vector of collection indices
0039         IndexMap out_indices;
0040 
0041         // build list of (hypothesis index, hypothesis weight)
0042         using HypIndex = std::pair<std::size_t, decltype(edm4eic::CherenkovParticleIDHypothesis::weight)>;
0043         std::vector<HypIndex> hyp_indices;
0044         for(std::size_t hyp_index=0; hyp_index<in_pid.hypotheses_size(); hyp_index++)
0045           hyp_indices.push_back(HypIndex{
0046               hyp_index,
0047               in_pid.getHypotheses(hyp_index).weight
0048               });
0049 
0050         // sort it by likelihood, if needed
0051         if(sort_by_likelihood)
0052           std::sort(
0053               hyp_indices.begin(),
0054               hyp_indices.end(),
0055               [] (HypIndex& a, HypIndex& b) { return a.second > b.second; }
0056               );
0057 
0058         // create and fill output objects
0059         for(const auto& [hyp_index, hyp_weight] : hyp_indices) {
0060 
0061           // get the input hypothesis
0062           auto in_hyp = in_pid.getHypotheses(hyp_index);
0063 
0064           // create output `ParticleID` object
0065           auto out_index = out_pids.size();
0066           auto out_pid   = out_pids.create();
0067           out_indices.insert({out_index, out_pid.getObjectID().index});
0068 
0069           // fill scalars
0070           out_pid.setPDG(           static_cast<decltype(edm4hep::ParticleIDData::PDG)>           (in_hyp.PDG)    );
0071           out_pid.setLikelihood(    static_cast<decltype(edm4hep::ParticleIDData::likelihood)>    (in_hyp.weight) );
0072           out_pid.setType(          static_cast<decltype(edm4hep::ParticleIDData::type)>          (0)             ); // FIXME: not used yet
0073           out_pid.setAlgorithmType( static_cast<decltype(edm4hep::ParticleIDData::algorithmType)> (0)             ); // FIXME: not used yet
0074 
0075           // fill parameters vector
0076           out_pid.addToParameters( static_cast<float> (in_hyp.npe) ); // NPE for this hypothesis
0077 
0078         }
0079 
0080         return out_indices;
0081       }
0082 
0083   }; // class ConvertParticleID
0084 } // namespace eicrecon