diff --git a/Phys/DaVinciFilters/src/FunctionalFilterDesktop.cpp b/Phys/DaVinciFilters/src/FunctionalFilterDesktop.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5587f78b976ac0d631703196c787788f5c6c5b2f --- /dev/null +++ b/Phys/DaVinciFilters/src/FunctionalFilterDesktop.cpp @@ -0,0 +1,142 @@ +/*****************************************************************************\ +* (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration * +* * +* This software is distributed under the terms of the GNU General Public * +* Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". * +* * +* In applying this licence, CERN does not waive the privileges and immunities * +* granted to it by virtue of its status as an Intergovernmental Organization * +* or submit itself to any jurisdiction. * +\*****************************************************************************/ + +#include "FunctionalFilterDesktop.h" +#include <algorithm> +#include <functional> +#include "GaudiKernel/StatEntity.h" +#include "GaudiKernel/IAlgContextSvc.h" +#include "Kernel/DaVinciFun.h" +#include "LoKi/IHybridFactory.h" +#include "boost/algorithm/string.hpp" + +DECLARE_COMPONENT(FunctionalFilterDesktop) + +typedef LoKi::BasicFunctors<const LHCb::Particle*>::BooleanConstant _PBOOL ; + +FunctionalFilterDesktop::FunctionalFilterDesktop(const std::string& name, ISvcLocator* pSvcLocator) + : MultiTransformer(name, pSvcLocator, + // Inputs + {KeyValue{"InputParticles", LHCb::ParticleLocation::Production}, + KeyValue{"InputVertices", LHCb::VertexLocation::Production}}, + // Outputs + {KeyValue{"SelectedParticles", ""}, + KeyValue{"SelectedVertices", ""}}), + m_factory("LoKi::Hybrid::Tool/HybridFactory:PUBLIC"), + m_preambulo(), + m_code("PNONE"), + m_cut(_PBOOL(false)), + m_to_be_updated1(true) {} + +StatusCode FunctionalFilterDesktop::initialize() { + auto sc = MultiTransformer::initialize(); + if (sc.isFailure()) return sc; + if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg; + + svc<IService> ("LoKiSvc"); + + // decode the cut: + sc = updateMajor(); + if ( sc.isFailure() ) + { return Error ("The error from updateMajor" , sc ); } + return StatusCode::SUCCESS; +} + +StatusCode FunctionalFilterDesktop::finalize() +{ + auto sc = MultiTransformer::finalize(); + if (sc.isFailure()) { + return sc; + } + // reset functors + m_cut = _PBOOL(false); + m_to_be_updated1 = true; + + return sc; +} + +std::string FunctionalFilterDesktop::preambulo() const +{ + std::string result ; + for ( std::vector<std::string>::const_iterator iline = + m_preambulo.begin() ; m_preambulo.end() != iline ; ++iline ) + { + if ( m_preambulo.begin() != iline ) { result += "\n" ; } + result += (*iline) ; + } + return result; +} + +StatusCode FunctionalFilterDesktop::updateMajor() +{ + // decode the code + StatusCode sc = decodeCode(); + if (sc.isFailure()){return Error ("Error from decodeCode()'");} + + // locate the factory + LoKi::IHybridFactory* factory = tool<LoKi::IHybridFactory> (m_factory, this); + + release(factory).ignore(); + m_to_be_updated1 = false; + + return StatusCode::SUCCESS; +} + +StatusCode FunctionalFilterDesktop::filter( + const LHCb::Particle::Range& input_particles, + const LHCb::Vertex::Range& input_vertices, + LHCb::Particle::Selection& selected_particles, + LHCb::Vertex::Selection& selected_vertices) const { + for (const auto particle : input_particles) { + if(!m_cut(particle)){ + m_npassed_filter+=false; + } + else{ + m_npassed_filter+=true; + selected_particles.insert(particle); + } + } + return StatusCode::SUCCESS; +} + +std::tuple<LHCb::Particle::Selection, LHCb::Vertex::Selection> +FunctionalFilterDesktop::operator()(const LHCb::Particle::Range& input_particles, const LHCb::Vertex::Range& input_vertices) const { + /* if (m_to_be_updated1) */ + /* { */ + /* StatusCode sc = updateMajor() ; */ + /* /1* if (sc.isFailure()) return Error ("Error from updateMajor", sc); *1/ */ + /* } */ + + std::tuple<LHCb::Particle::Selection, LHCb::Vertex::Selection> output_selection; + LHCb::Particle::Selection selected_particles; + LHCb::Vertex::Selection selected_vertices; + + StatusCode sc = filter(input_particles, input_vertices, std::get<0>(output_selection), std::get<1>(output_selection)); + + /* return std::make_tuple("Selection", selected_particles, selected_vertices); */ + return output_selection; +} + +StatusCode FunctionalFilterDesktop::decodeCode() +{ + // locate the factory + LoKi::IHybridFactory* factory_ = tool<LoKi::IHybridFactory>(factory(), this); + // use the factory + StatusCode sc = factory_ -> get(code(), m_cut ,preambulo()); + + if (sc.isFailure()){ + return Error ( "Error from LoKi/Bender 'hybrid' factory for Code='" + code() + "'", sc); + } + + release ( factory_ ).ignore(); + + return sc ; +} diff --git a/Phys/DaVinciFilters/src/FunctionalFilterDesktop.h b/Phys/DaVinciFilters/src/FunctionalFilterDesktop.h new file mode 100644 index 0000000000000000000000000000000000000000..e80046245f920d72654ce3e263c49725cb5a37ec --- /dev/null +++ b/Phys/DaVinciFilters/src/FunctionalFilterDesktop.h @@ -0,0 +1,83 @@ +/*****************************************************************************\ +* (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration * +* * +* This software is distributed under the terms of the GNU General Public * +* Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". * +* * +* In applying this licence, CERN does not waive the privileges and immunities * +* granted to it by virtue of its status as an Intergovernmental Organization * +* or submit itself to any jurisdiction. * +\*****************************************************************************/ + +#pragma once + +/* #include <string> */ +#include "GaudiAlg/Transformer.h" + +#include "GaudiKernel/StatEntity.h" +#include "GaudiKernel/IAlgContextSvc.h" +#include "LoKi/PhysTypes.h" +#include "Kernel/IPlotTool.h" + +/** @class FunctionalFilterDesktop FunctionalFilterDesktop.h + * + * @brief LHCb::Particle selection from input LHCb::Particle::Selection and Primary + * Vertices. + * + * TODO + * + * Example usage: + * + * @code {.py} + * # TODO + * @endcode + * + */ +class FunctionalFilterDesktop : + public Gaudi::Functional::MultiTransformer<std::tuple<LHCb::Particle::Selection, LHCb::Vertex::Selection>(const LHCb::Particle::Range&, const LHCb::Vertex::Range&)> { + +public: + FunctionalFilterDesktop(const std::string& name, ISvcLocator* pSvcLocator); + + StatusCode initialize() override; + + std::tuple<LHCb::Particle::Selection, LHCb::Vertex::Selection> operator()(const LHCb::Particle::Range& input_particles, + const LHCb::Vertex::Range& input_vertices) const override; + + virtual StatusCode filter(const LHCb::Particle::Range& input_particles, + const LHCb::Vertex::Range& input_vertices, + LHCb::Particle::Selection& selected_particles, + LHCb::Vertex::Selection& selected_vertices) const; + + StatusCode finalize() override; + +protected: + // construct the preambulo string + std::string preambulo() const; + virtual StatusCode decodeCode (); + /// update the major properties + StatusCode updateMajor(); + // get the actual predicate: + const LoKi::BasicFunctors<const LHCb::Particle*>::Predicate& predicate() const + {return m_cut;} + // access to the code-string + const std::string& code() const {return m_code;} + const std::string& factory() const {return m_factory;} + +private: + // LoKi/Bender "hybrid" factory name + Gaudi::Property<std::string> m_factory{this, "factory", "UNDEFINED", "Factory"}; + // the preambulo + Gaudi::Property<std::vector<std::string>> m_preambulo{this, "Preambulo", {}, "Preambulo"}; + // the code for the functor/predicate + Gaudi::Property<std::string> m_code{this, "code", "UNDEFINED", "FunctorName"}; + // the predicate itself + LoKi::BasicFunctors<const LHCb::Particle*>::PredicateFromPredicate m_cut; + /* LoKi::Types::Cut m_cut; */ + // the code for the functor/predicate + + /// the flag to indicate the nesessity of update + bool m_to_be_updated1 = false; + mutable Gaudi::Accumulators::BinomialCounter<> m_npassed_filter{this, "# passed filter"}; + +}; diff --git a/Phys/ParticleMaker/src/FunctionalParticleMaker.cpp b/Phys/ParticleMaker/src/FunctionalParticleMaker.cpp index 3c4233671ef108aa10b85a78a4bb503819ddaa79..a555a3afb28006bd1e88c609a05161c9ff34e44b 100644 --- a/Phys/ParticleMaker/src/FunctionalParticleMaker.cpp +++ b/Phys/ParticleMaker/src/FunctionalParticleMaker.cpp @@ -16,7 +16,7 @@ DECLARE_COMPONENT(FunctionalParticleMaker) FunctionalParticleMaker::FunctionalParticleMaker(const std::string& name, ISvcLocator* pSvcLocator) : Transformer(name, pSvcLocator, {KeyValue{"InputProtoParticles", LHCb::ProtoParticleLocation::Charged}}, - {KeyValue{"OutputParticles", ""}}) + {KeyValue{"Particles", ""}}) { } @@ -114,7 +114,6 @@ LHCb::Particles FunctionalParticleMaker::operator()(LHCb::ProtoParticles const& m_nparticles += nparticles; m_nantiparticles += nantiparticles; - return particles; }