Project

General

Profile

fhiclcpp examples


A typical configuration

FHiCL configuration

# Sample configuration
pset : {

  oneAtom   : "my experiment" 
  threshold : 10
  list1     : [ 1, 3, 20 ]
  list2     : [ 2, 4, 30 ]
  pair      : [ 11, "electron" ]

  g4Config : {
    energyCutoff : 27
    physicsList  : "QGSP"  # optional
  }

}

fhiclcpp configuration

struct G4Config {
  Atom<double> energyCutoff { Name("energyCutoff"), Comment("This is a number in units of GeV.\n" 
                                                            "Geant4 uses it to interpolate between\n" 
                                                            "different physics lists.") };
  OptionalAtom<string> maybeListName { Name("physicsList") };
};

struct MyConfig {
  Atom<string>      oneAtom   { Name("oneAtom")   };
  Atom<int>         threshold { Name("threshold") };
  Sequence<int>     list1     { Name("list1")     };
  Sequence<int,3>   list2     { Name("list2")     };
  Tuple<int,string> pair      { Name("pair")      };
  Table<G4Config>   g4Config  { Name("g4Config")  };
};

Table<MyConfig> pset { Name("pset") };

Parameter retrieval

// FHiCL tables

MyConfig myConfig = pset();
G4Config g4Config_option1 = pset().g4Config();
G4Config g4Config_option2 = myConfig.g4Config();

/* ------------------------------------------------------------------ */

// FHiCL atoms

string  oneAtom      = pset().oneAtom();
int     threshold    = pset().threshold();
double  energyCutoff = pset().g4Config().energyCutoff();

string plName;
if ( pset().g4Config().maybeListName(plName) ) {
  // If "physicsList" is included in the configuration
  // then the above 'if' condition will be true, and 'plName'
  // will be filled with the value specified in the configuration.
}

/* ------------------------------------------------------------------ */

// FHiCL sequences

std::vector<int>  list1     = pset().list1();
std::array<int,3> list2     = pset().list2();
std::tuple<int,string> pair = pset().pair();

// -- individual sequence-element access for fhicl::Sequence

int list1_first  = pset().list1(0); // equiv. to list1.at(0)
int list2_second = pset().list2(1); // "      "  list2.at(1)

// -- individual sequence-element access for fhicl::Tuple

int pair_first_option1 = std::get<0>( pair );
int pair_first_option2 = std::get<0>( pset().pair() );

int pair_first_option3 = pset().pair.get<0>();   // N.B. No '()' call on 'pair' here

// -- Similar incantations for accessing the second element on the tuple.

// -- range-for fhicl::Sequence types

for ( int const i : pset().list1() ) std::cout << i << '\n';
for ( int const i : pset().list2() ) std::cout << i << '\n';

Configuration using Sequence<Sequence<T>>

FHiCL configuration

pset : {

  listsInList : [ [0, 4, 5],
                  [0, 3] ]

}

fhiclcpp configuration

struct MyConfig {
  Sequence< Sequence<int>, 2 > listsInList  { Name("listsInList") };
};

Table<MyConfig> pset { Name("pset") };

Parameter retrieval

std::array< std::vector<int>, 2 > listsInList = pset().listsInList();
std::vector<int> listInList_0 = pset().listsInList(0); // ==> [0, 4, 5]
std::vector<int> listInList_1 = pset().listsInList(1); // ==> [0, 3]

int listInList_0_1 = pset().listsInList(0).at(1); // ==> 4

// Print out contents of all vectors
for ( auto const& vec : pset().listsInList() ) {
  std::for_each( vec.begin(), vec.end(), 
                 [](int const i){ std::cout << i << " "; } ); 
  std::cout << '\n';
}

Configuration using Sequence<Tuple<T...>>

FHiCL configuration

pset : {

  tuplesInList : [ [ 11,      electron, signal        ],
                   [ 13,      muon    , background    ],
                   [ 1314024, invalid , "not yet set" ] ]

}

fhiclcpp configuration

struct MyConfig {
  Sequence< Tuple<int,string,string> > tuplesInList { Name("tuplesInList") };
};

Table<MyConfig> pset { Name("pset") };

Parameter retrieval

std::vector< std::tuple<int,string,string> > tuplesInList = pset().tuplesInList();
std::tuple<int,string,string> tupleInList_0 = pset().tuplesInList(0);

int tupleInList_1_2 = std::get<2>( pset().tuplesInList(1) ); // ==> "background" 

// Print out particle info
for ( auto const& tup : pset().tuplesInList() ) {
  double pid  = std::get<0>( tup );
  string name = std::get<1>( tup );
  string type = std::get<2>( tup );
  std::cout << pid << " " << name << " " << type << std::endl;
}

Configuration using Sequence<Table<T>>

FHiCL configuration

# Sample configuration
pset : {

   datasets: [ { name : "electrons"  description : "produced from photo-electric effect" },
               { name : "pions"      description : "parent particles of muon neutrinos" },
               { name : "photons"    description : "produced via bremsstrahlung" } ]

}

fhiclcpp configuration

struct Dataset {
  Atom<string> name        { Name("key") };
  Atom<string> description { Name("description") };
};

struct MyConfig {
  Sequence< Table< Dataset > > datasets { Name("datasets") };
};

Table<MyConfig> pset { Name("pset") };

Parameter retrieval

std::vector< Dataset > datasets = pset().dataset();
Dataset set_0 = pset().datasets(0);
Dataset set_2 = pset().datasets(2);

string photon_desc = pset().datasets(2).name();

// Print out datasets
for ( auto const& set : pset().datasets() ) {
  std::cout << "  Name: "        << set.name()
            << "  Description: " << set.description() << '\n';
}