Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-26 09:01:07

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #pragma once
0012 
0013 #include <Gaudi/Accumulators/StaticHistogram.h>
0014 
0015 #include <Gaudi/Parsers/Factory.h>
0016 #include <GaudiKernel/ToStream.h>
0017 
0018 #include <boost/spirit/include/qi.hpp>
0019 
0020 /**
0021  * This file provides a Grammar for the type Gaudi::Accumulators::Axis
0022  * It allows to use that type from python with a format liks :
0023  *    ( nbins, min, max, title )
0024  * where title can be ommited.
0025  * note that for the parsing, min and max will be parsed as doubles before
0026  * being converted to the actual type used as Arithmetic for Axis, so an
0027  * automatic conversion between the 2 must be provided
0028  */
0029 namespace Gaudi {
0030   namespace Parsers {
0031 
0032     namespace qi = boost::spirit::qi;
0033 
0034     template <typename Iterator, typename Skipper, typename Arithmetic>
0035     struct AxisGrammar : qi::grammar<Iterator, Gaudi::Accumulators::Axis<Arithmetic>(), qi::locals<char>, Skipper> {
0036       using Axis = Gaudi::Accumulators::Axis<Arithmetic>;
0037       struct StoreNbinsOp {
0038         void operator()( Axis& res, unsigned int const& nBins ) const { res.setNumBins( nBins ); }
0039       };
0040       struct StoreMinValueOp {
0041         void operator()( Axis& res, Arithmetic const& minValue ) const { res.setMinValue( minValue ); }
0042       };
0043       struct StoreMaxValueOp {
0044         void operator()( Axis& res, Arithmetic const& maxValue ) const { res.setMaxValue( maxValue ); }
0045       };
0046       struct StoreTitleOp {
0047         void operator()( Axis& res, std::string const& title ) const { res.setTitle( title ); }
0048       };
0049       AxisGrammar() : AxisGrammar::base_type( axis ) {
0050         begin = enc::char_( '[' )[qi::_val = ']'] | enc::char_( '(' )[qi::_val = ')'];
0051         end   = enc::char_( qi::_r1 );
0052         core  = qi::int_[storeNbins( qi::_val, qi::_1 )] >> "," >> qi::double_[storeMinValue( qi::_val, qi::_1 )] >>
0053                "," >> qi::double_[storeMaxValue( qi::_val, qi::_1 )] >>
0054                -( "," >> title[storeTitle( qi::_val, qi::_1 )] );
0055         axis = begin[qi::_a = qi::_1] >> core[qi::_val = qi::_1] >> end( qi::_a );
0056       }
0057       qi::rule<Iterator, Axis(), qi::locals<char>, Skipper> axis;
0058       qi::rule<Iterator, Axis(), Skipper>                   core;
0059       qi::rule<Iterator, char()>                            begin;
0060       qi::rule<Iterator, void( char )>                      end;
0061       StringGrammar<Iterator, Skipper>                      title;
0062       ph::function<StoreNbinsOp>                            storeNbins;
0063       ph::function<StoreMinValueOp>                         storeMinValue;
0064       ph::function<StoreMaxValueOp>                         storeMaxValue;
0065       ph::function<StoreTitleOp>                            storeTitle;
0066     };
0067 
0068     template <typename Iterator, typename Skipper, typename Arithmetic>
0069     struct Grammar_<Iterator, Gaudi::Accumulators::Axis<Arithmetic>, Skipper> {
0070       using Grammar = AxisGrammar<Iterator, Skipper, Arithmetic>;
0071     };
0072 
0073     // Parse function... nothing special, but it must be done explicitely.
0074     template <typename Arithmetic>
0075     StatusCode parse( Gaudi::Accumulators::Axis<Arithmetic>& result, const std::string& input ) {
0076       return parse_( result, input );
0077     }
0078 
0079   } // namespace Parsers
0080 
0081 } // namespace Gaudi