-- DirkKruecker - 05-May-2011

Tools for Ntuples


  • EasyChain


ConfigReader config("filename") reads a configuration file and fills a static map. The static map makes the content available everywhere in your code. In addition variables can be defined within the code as a default value or on the command line.

Config file syntax:

  • principle structure of the config file: key = value.
  • comments are marked by #
  • trailing comments are allowed for all types but strings.
  • TABs are allowed.
  • for strings: the first space after the '=' is removed the rest is taken literally including trailing spaces etc.
Config file example:
# this is a comment
anInt           = 123 # trailing comments are allowed but not for strings
# tabs are allowed
aString         = 12 dfger
    aFloat      = 1e32
   aDouble      = 123
anotherDouble   = 123.456789012347E44
# for strings: the first space after the '=' is removed
#              the rest is taken literally including trailing spaces etc
anotherString   =             12 dfger
#anotherString is '           12 dfger       '
aBool = true # case insensitive: True TRUE tRuE is all the same
Code usage:
#include "ConfigReader.h"
int main(int argc, char** argv){

       ConfigReader config("config_RA5.txt",argc,argv);
       // see header file for available type and get methods
       double var1 = config.getDouble("variableName1");

void someFunc(){

       ConfigReader config;
       double var1 = config.getDouble("variableName1"); // same value as in main


Use cases:

{    ...
     // get an instance
     ConfigReader config;
     // case 1: variable is defined in config file 
     double var1 = config.getDouble("variableName",12.34); // var is taken from the config file
     // case 2: variable is NOT defined in config file    
     double var1 = config.getDouble("variableName",12.34); // the default is used: var1 = 12.34
     // case 3: variable is NOT defined in config file but has been used with a get before somewhere in your program
     double var2 = config.getDouble("variableName"); // var2=var1 wherever it has been defined before
     // error since  "variableName" has been used above with a different default!
     double var2 = config.getDouble("variableName",77.0);

A third way to set values is the command line (no spaces allowed):

           myAnalysis variableName=Value
  • The config file value overwrites the default value
  • The command line overwrites the config file

The eventually used values can be dumped to a file or cout:





This is include for convenience. It takes care that all created histograms are written to the output file automatically - this happens when the pointer is destroyed e.g. when the main is terminated. It is implemented as smart pointer (similar to stl auto_ptr but STL safe). It calls Write().

Code example:

#include "THTools.h"
#include "TH1.h"
int main()
     TFile *outfile = TFile::Open("out.root","RECREATE");
     // for a single histogram
     autoSavePtr<TH1F> myHisto = new TH1F("myHisto","# of Susy evts.",50,0,100); // should be the same unit where the output file is defined
     // or many
     vector< autoSavePtr<TH1F> > vec;
     for(int i=0;i<50;i++) {
          TString name="histxx";
          sprintf(a.Data()+4,"%02d", i); 
          vec.push_back(new TH1F(name,"histo array",50,0,6));

     // nothing to do at end of main - histograms will be written automatically  


ROOT allows you to access an existing histogram by name gDirectory->Get(name) fetchHisto adds a liitle type casting and checks if the histogram exists.

Code example:

#include "THTools.h"
#include "TH1.h"
void someFunc(){
       // a Histogram that had been defined already e.g. in main
       static TH1F* myHisto = fetchHisto<TH1F>("aHistogram"); // make it static to save time. ROOT has to search only once for a subroutine if it is static
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r3 - 2011-05-10 - DirkKruecker
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Sandbox All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback