Skip to content

Commit

Permalink
Make it possible to patch ParticleID meta information when patching c…
Browse files Browse the repository at this point in the history
…ollections (#193)

* Avoid unnecessary copies of strings and vectors

* Remove unused variable

* Fix some trailing whitespaces

* Fix docstring

* Improve readability with structured bindings

* Use std::tie to get rid of unnecessary temporary

* Get rid of unnecessary branch

* Keep track of ParticleID metadata while scanning

* Make PIDMeta public to satisfy ROOT

* Patch ParticleID meta data on request

* Set the PID meta data after all collections have been converted

* Make things work with c++17

* Add patching grammar to docstring
  • Loading branch information
tmadlener authored Sep 5, 2024
1 parent 34fd6f8 commit 240d3cc
Show file tree
Hide file tree
Showing 2 changed files with 329 additions and 141 deletions.
66 changes: 55 additions & 11 deletions src/cpp/include/UTIL/CheckCollections.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace UTIL {


class PIDHandler;

/** Utility class for checking and patching events with respect to collections that are not present
* in every event of a set of files.
*
Expand Down Expand Up @@ -55,26 +56,69 @@ namespace UTIL {
Vector getConsistentCollections() const ;

/** Add a collection with (name,type) that should be added to events in patchEvent().
*
* Depending on the contents of name and type one of the following things
* will happen:
*
* - if type is an LCIO type an empty collection of the given type will be
put into the event using the passed name
* - if type is `LCRelation[<from-type>,<to-type>]` an LCRelation collection
* will be created setting the <from-type> and <to-type> as FromType and
* ToType collection parameters.
* - if type contains a '|' the whole (name, type) pair will be considered
* to be ParticleID meta information and the name will be used as a PID
* algorithm name and the type information will be parsed as
* <reco-coll-name>|[<param-name>,<param-names...>], I.e. a ParticleID
* algorithm of name will be attached to the ReconstructedParticle
* collection with name <reco-coll-name>. Additionally any parameter names
* that are in the comma-separated list after the '|' will be set via the
* PIDHandler for this ParticleID algorithm
*/
void addPatchCollection(const std::string name, std::string type){
_patchCols.push_back( {name, type} ) ;
}
void addPatchCollection(std::string name, std::string type);

/** Add a all collections as Vector(name,type), e.g. retrieved from getMissingCollections() that should be added to events in patchEvent().
/** Add all collections as Vector(name,type), e.g. retrieved from getMissingCollections() that should be added to events in patchEvent().
*/
void addPatchCollections(Vector cols){
for(const auto& p : cols)
_patchCols.push_back( p ) ;
}
void addPatchCollections(Vector cols);

/** Add and empty collection to the event for any collection that is in patchCollections and not in the Event
*/
void patchCollections(EVENT::LCEvent* evt ) const ;


/// Metadata for ParticleIDs that are handled via the PIDHandler. Necessary
/// for consistency with EDM4hep, where ParticleID no longer lives in
/// ReconstructedParticle and where the direction of the relation has been
/// reversed.
struct PIDMeta {
// c++17 doesn't yet have aggregate initialization in vectors, so we
// need this constructor
PIDMeta(const std::string &n, const std::vector<std::string> &parN,
uint32_t c = 0)
: name(n), paramNames(parN), count(c) {}

// Since we have one non-default constructor we need to default the rest
// explicitly
constexpr PIDMeta() = default;
PIDMeta(const PIDMeta &) = default;
PIDMeta &operator=(const PIDMeta &) = default;
PIDMeta(PIDMeta &&) = default;
PIDMeta &operator=(PIDMeta &&) = default;
~PIDMeta() = default;

std::string name{}; ///< algorithm name
std::vector<std::string> paramNames{}; ///< parameter names
uint32_t count{}; ///< How often this was found
};

private:

void insertParticleIDMetas(const UTIL::PIDHandler& pidHandler, const std::string& recoName);

unsigned _nEvents =0 ;
std::unordered_map< std::string, std::pair< std::string, unsigned > > _map{} ;
Vector _patchCols {} ;
/// Map from ReconstructedParticle collection names to attached ParticleID
/// meta information
std::unordered_map<std::string, std::vector<PIDMeta>> _particleIDMetas{};
Vector _patchCols{};

}; // class

Expand Down
Loading

0 comments on commit 240d3cc

Please sign in to comment.