Message: Re: Sampling .root histogram within Geant4? Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: Sampling .root histogram within Geant4? 

Forum: Analysis
Re: Question Sampling .root histogram within Geant4? (Jonning Kennethson)
Date: 07 Apr, 2011
From: Andreas Pfeiffer <Andreas Pfeiffer>

Hi Jonning,

> Hi, here is what I am trying to do. I have a root histogram in a .root
> file with experimental data that I want to sample for my simulation with
> the TH1F GetRandom() method.
>
> Now this seems not to be trivial.
>
> I have both an aida connection to root and a direct connection.
>
> The direct connection works seemingly fine, I can open the root file get
> a pointer to the histo and all. But all GetRandom() gives me is not a
> number "nan". Also GetBinCenter(some integer) gives the same result.
>
> On the other hand, in AIDA I don't know how to import the histogram and
> sample it. I have not found documentation for anything else than writing
> output to histograms.

You can simply create a second tree from a tree-factory in
AIDA which is connected to the root file with the histogram
you want to sample from, then use the "find" method on the
tree to find the histogram you want to read, e.g.: tree->find("10")

> Is there perhaps some example of this somewhere? I can by no means be
> the first person doing this. Any help is greatly appreciated.

a small standalone example which reads histograms "10" and
"20" from a root file "example.root" and prints some properties
of the histograms is given below.

Please let us know if you need more information or if there
are problems ...

       cheers, andreas


#include <iostream>
#include <cstdlib>
#include <memory>

#include "AIDA/AIDA.h"

#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <typeinfo>
#include <boost/shared_ptr.hpp>

class ExaH1 {

public:
  ExaH1 ();
  ~ExaH1 ();

  void doIt(bool verbose=false) { read(); check(verbose); }

  // possibly these could be protected
  void read();
  void check(bool verbose=false);

private:
  // the factories and the tree
  AIDA::IAnalysisFactory *af;
  AIDA::ITree *tree;

  // the histograms
  typedef AIDA::IHistogram1D* H1P;
  typedef AIDA::IProfile1D* P1P;
  H1P h1p;
  P1P p1p;
};

ExaH1::ExaH1() {
  af = AIDA_createAnalysisFactory();
  AIDA::ITreeFactory *tf( af->createTreeFactory() );
  bool readOnly = true;
  bool createNew = false;
  tree = tf->create("example.root","root", readOnly, createNew);
  if (!tree) {
    std::cerr << "ERROR: cannot create root tree" << std::endl;
  }
}

ExaH1::~ExaH1() {
  tree->commit();
  tree->close();
  delete tree;  // this also deletes the histograms from memory
  delete af;
}

void ExaH1::read() {

  tree->ls();

  AIDA::IManagedObject * o;

  h1p = dynamic_cast< AIDA::IHistogram1D * >( tree->find("10") );
  if ( h1p == 0 ) {
    std::cerr << "ERROR reading histo   10 from file" << std::endl;
  }

  o = tree->find("20");
  if ( o == 0 ) {
    std::cerr << "ERROR reading object 20 from file" << std::endl;
  }
  p1p = dynamic_cast< AIDA::IProfile1D   * >( o );
  if ( p1p ==0 ) {
    std::cerr << "ERROR reading profile 20 from file" << std::endl;
  }

}

void ExaH1::check(bool verbose) {
  {
    // Printing some statistical values of the profile
    std::cout << "Title  : " << h1p->title() << std::endl;
    std::cout << "Entries: " << h1p->entries() << std::endl;
    std::cout << "Mean   : " << h1p->mean() << std::endl;
    std::cout << "RMS    : " << h1p->rms() << std::endl;

    if (verbose) {
       // Printing the contents of the histogram
       std::cout << "X value     entries    Y value (height)" << std::endl;
       const AIDA::IAxis& xAxis = h1p->axis();
       for ( int iBin = 0; iBin < xAxis.bins(); ++iBin ) {
         std::cout << h1p->binMean( iBin )
            << "       "   << h1p->binEntries( iBin )
            << "         " << h1p->binHeight( iBin )
            << std::endl;
       }
    }
  }
  std::cout << std::endl;
  {

    // Printing some statistical values of the profile
    std::cout << "Title  : " << p1p->title() << std::endl;
    std::cout << "Entries: " << p1p->entries() << std::endl;
    std::cout << "Mean   : " << p1p->mean() << std::endl;
    std::cout << "RMS    : " << p1p->rms() << std::endl;

    if (verbose) {
      // Printing the contents of the histogram
      std::cout << "X value     entries    Y value    Y rms" << std::endl;
      const AIDA::IAxis& xAxis = p1p->axis();
      for ( int iBin = 0; iBin < xAxis.bins(); ++iBin ) {
 std::cout << p1p->binMean( iBin )
    << "       " << p1p->binEntries( iBin )
    << "         " << p1p->binHeight( iBin )
    << "         " << p1p->binRms( iBin )
    << std::endl;
      }
    }
  }

  // Printing the annotation values of the histogram
  std::cout << std::endl << "Current annotation items/values : " << std::endl;
  const AIDA::IAnnotation& annotation = h1p->annotation();
  for ( int annotationIndex = 0; annotationIndex < annotation.size();
++annotationIndex ) {
    std::string key = annotation.key( annotationIndex );
    std::cout << annotation.key( annotationIndex ) << " : "
       << annotation.value( annotationIndex ) << std::endl;
  }
}

int main( int argc, char** )
{

  std::cout << "\n--------------------------------------------------------------------------------\n"
<< std::endl;
  std::cout << "readHbk> starting" << std::endl;

  ExaH1 exH1;
  if (argc > 1) {
    exH1.doIt();
  } else {
    exH1.doIt(true);
  }
  std::cout << "readHbk> That's it !" << std::endl;
  std::cout << "\n--------------------------------------------------------------------------------\n"
<< std::endl;
  return 0;
}

Inline Depth:
 1 1
 All All
Outline Depth:
 1 1
 2 2
 All All
Add message: (add)

1 Question: Re: Sampling .root histogram within Geant4?   (Jonning Kennethson - 07 Apr, 2011)
(_ None: Re: Sampling .root histogram within Geant4?   (Andreas Pfeiffer - 07 Apr, 2011)
(_ None: Re: Sampling .root histogram within Geant4?   (Guy Barrand - 08 Apr, 2011)
1 None: Re: Sampling .root histogram within Geant4?   (Guy Barrand - 08 Apr, 2011)
1 None: Re: Sampling .root histogram within Geant4?   (Andreas Pfeiffer - 09 Apr, 2011)
... 1 Message(s)
2 Question: Re: Sampling .root histogram within Geant4?   (Jonning Kennethson - 08 Apr, 2011)
1 None: Re: Sampling .root histogram within Geant4?   (Guy Barrand - 09 Apr, 2011)
... 1 Message(s)
2 None: Re: Sampling .root histogram within Geant4?   (Andreas Pfeiffer - 09 Apr, 2011)
3 None: Re: Sampling .root histogram within Geant4?   (Guy Barrand - 11 Apr, 2011)
 Add Message Add Message
to: "Re: Sampling .root histogram within Geant4?"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]