210 likes | 458 Views
“How to write LoKi functors ?” . Vanya BELYAEV. … . I am not sure that this question is right or valid question at all… Let me try to convince you … Each framework has its own application range The complicated stuff for one framework is usually just a trivial within another framework
E N D
“How to write LoKifunctors?” Vanya BELYAEV
… • I am not sure that this question is right or valid question at all… • Let me try to convince you … • Each framework has its own application range • The complicated stuff for one framework is usually just a trivial within another framework • Choose right framework… • Each problem could be drastically simplified with “non-dogmatic” approach • Otherwise: back-up solution for couple of last slides… Vanya BELYAEV
Frameworks and their purposes • We have many “frameworks”, somehow suited for “analysis” purposes: • pure Gaudi + LHCb event model • + the same in Python • “minimal” DaVinci: input/output, standard components, low-level tools: fitters, makers, etc.. • CombineParticles & FilterDesktop • + TupleTool&Co • LoKi • Bender Vanya BELYAEV
“Analysis I” • Select/filter events/candidates using CombineParticle & FilterDesktop • Rather generic • Dump the event into (complicated) NTuple/TTree (“image of event”) using TupleTool&Co • Rather generic • Analyse it with (complicated) ROOT/HBOOK macros/kumacs • Specific Vanya BELYAEV
“Analysis – II” • Embed the NTuples/histograms into the specific C++/Python “analysis” algorithm • “Analysis” • Specific C++/python code • Easy& intuitive structure of NTuple/Tree/histos • (multidimensional histogram) • Event selection/filtering is not main goal, but one gets it for free • Analysis in ROOT/PAW is statistical only • No “reconstruction” Vanya BELYAEV
Analysis & Selection • Try to distinguish these concepts… • “Analysis” is a bit more private stuff • Well suited for your particular needs • Your own choice of tools & methods • “Selection” needs to satisfy some general criteria & constraints • E.g. must run for event stripping, or needs to be “HLT2-aware” But there is no Berlin Wall between these concepts Vanya BELYAEV
Some cuts/variables are rather difficult to CombineParticles/FiltyerDesktop framework • Either code your own functor • Or (much better) reconsider the framework • The main difficulty is access to “other” event–data, e.g. primary vertices and selection of “the best” • Or notorious “vertex isolation” Vanya BELYAEV
Event Data access • Trivial for “other” frameworks: VRangeprimaries = vselect ( "Primaries", PRIMARY ) ; … const LHCb::Particle* B = … ; VRange::iteratoripv = select_min( primaries.begin() , primaries.end() , VIPCHI2( B , geo() ) , VALL ) ; … const LHCb::VertexBase* primary = *ipv; Vanya BELYAEV
Event Data access • Trivial for “other” frameworks: pvs= self.vselect( "Primaries", PRIMARY ) ; B = … ; pv= LoKi.SelectVertex.selectMin( pvs, VIPCHI2( B , self.geo() ) , VALL ) ; # or even code five lines yourself: def selectMin ( lst , fun , cut ) : for o in lst : … return … Vanya BELYAEV
Backup solution • Each LoKifunctor inherits from • LoKi::Functor<TYPE1,TYPE2> • TYPE2 : • double - “ function” • bool - “predicate” • std::vector<TYPE3> - various streamers • TYPE1: const LHCb::Particle*, const LHCb::VertexBase*, const LHCb::MCParticle*, const LHCb::MCVertex*, const HepMC::GenParticle*, const HepMC::GenVertex*, LHCb::Track, LHCb::RecVertex, void, const LHCb::L0DUReport*, const LHCb::HltDecisionReport*, const LHCb::ODIN*, LoKi::Holder<TYPE3,TYPE4>, std::vector<TYPE5> Vanya BELYAEV
“The method” • Only one essential method: result_type operator() ( argument a ) const ; • Never use TYPE1, TYPE2: • Use result_type and argument only Vanya BELYAEV
Required by system: • Virtual destructor: virtual ~MyFunctor() {} • Usually empty… • Clone method (“virtual constructor”) virtual MyFunctor* clone() const ; • Typical implementation (recommended) return new MyFunctor(*this) ; • Attention: refers to copy constructor • And probably implicitly relies on their agreement also Vanya BELYAEV
Optional • Nice self-printout: std::ostream& fillStream( std::ostream& s ) const; • The implementation is up to you… • The default: print the actual C++ type • Recommended: • Be compatible with python (the same symbol) • Valid python expression (instance!) return s << “M” ; Vanya BELYAEV
Internals: • Useful inside the implementation Error, Warning, Exceptions, Assert • Similar to GaudiAlgorithm/GaudiTool “toString”, “objType”, “id”, “printOut” • Services: LoKi::ILoKiSvc* lokiSvc() const ; • Many services are available through SmartIF SmartIF<ISvcLocator> svc ( lokiSvc()); if (!svc ) { … } SmartIF<IToolSvc> tool ( lokiSvc()); Vanya BELYAEV
Rare stuff: long event() const ; void setEvent( long evt ) const ; void setEvent() const ; • Could be used for various optimization • Should be used with some care (fragile) Vanya BELYAEV
For BPV* and *DV class MyFunctor :virtual public LoKi::Functor<T1,T2> ,virtual public LoKi::AuxDesktopBase IPhysDesktop* getDesktop() const ; boolvalidDesktop() const ; void loadDesktop() Vanya BELYAEV
The last C++ hint • Get access to DVAlgorithm: DVAlgorithm* dv = Gaudi::Utils::getDVAlgorithm ( const IAlgContextSvc* ) ; SmartIF<IAlgContextSvc> cntx ( lokiSvc() ) ; Vanya BELYAEV
Decoration • Optional, but very useful • Add a symbol into LoKi::Cuts namespace • either type: typedefMyFunctor MYNICEFUNCTOR ; • Or instance const MyFunctor MYNICEFUNCTOR(…) ; • please be compatible with Python • Up to now all symbols in LoKi::Cuts namespace are in uppercase Vanya BELYAEV
Want to use it in Python? • Make dictionaries: <class name=“…::MyFunctor” /> • Hint: put it into one of the “standard” namespaces, it will be picked up automatically • Find proper python/LoKiXXX/functions.py • declare your symbol to the python: • “typedef”: MYNICEFUNCTOR = XXX.MyFunctor • “instance” MYNICEFUNCTOR = XXX.MyFunctor(…) • Please be in agreement with LoKi::Cuts Vanya BELYAEV
Your functor is ready • Now you can use your function in C++ using namespace LoKi::Cuts • The functor is available in Python from LoKiXXX.decorators import * • The functor is available for CombineParticles/FilterDesktop framework from Configurables import LoKi__Hybrid__Tool as Factory Factory ( “HybridFactory” , Modules += [ “LoKiXXX.decorators” ] ) Vanya BELYAEV
Warnings… • Please do not code functors like: const LHCb::Particle* B = … ; const bool ok = SelectSpecificDecay ( B ) ; • Please do not code functors like sin( Slog pt)/cosh( S ip ) Vanya BELYAEV