Message: Re: Problem with AIDA and read or restore data from file (xml format)? Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: Problem with AIDA and read or restore data from file (xml format)? 

Keywords: AIDA read restore file
Forum: Analysis
Re: Question Problem with AIDA and read or restore data from file (xml format)? (Shahrokh)
Date: 06 Mar, 2006
From: Guy Barrand <Guy Barrand>

 Hello Shahrokh

  Below is a corrected version of you write and read programs.
Corrections are commented with //GB : something.

  Mainly, you "write" program had an inconstency between the booking string
and the type of the variables. Vriables r1,r2,r3 had been booked with "double"
whilst declared float in the loop.
  
  For the read program, the compiler stopped because the cast instruction was incompleete :
       AIDA::ITuple* tuple = managedObject->cast("AIDA::ITuple");
should be :
       AIDA::ITuple* tuple = (AIDA::ITuple*)managedObject....

  But despite that, the read program need a lot of others corrections. See below.
  I have completed the read program with a reading loop.

  The below programs run for me on a Mac/Tiger with g++-4.

    Cheers

//////////////////////////////////////////////
///// write program ///////////////////////////
//////////////////////////////////////////////

using namespace AIDA;

#include <cmath> //GB

//prototype randgauss()
double randgauss(double, double, double, double);

//define randgauss()
double randgauss( double min, double max, double sigma, double center)
{
  double random= (min+ (max-min)*(double)rand()/RAND_MAX);

  double tmp=(random-center)/sigma;
  double gauss=std::exp(-tmp*tmp/2);
  return gauss;
}

int main()
{
  IAnalysisFactory *analysisFactory = AIDA_createAnalysisFactory();

  ITreeFactory *treeFactory = analysisFactory->createTreeFactory();

  //GB : ITree *tree = treeFactory->create(); 
  //GB : the upper is not needed if working with a ITree attached
  //GB : to a file. It can be used if working with the default 
  //GB : memory ITree (which is not the case here).

  ITree *tree = treeFactory->create("gaussian.aida", "xml", false, true);

  delete treeFactory; //GB : no more needed, delete it.

  ITupleFactory* tupleFactory = analysisFactory->createTupleFactory (*tree);

  ITuple* tuple = tupleFactory->create("tuplePhoton", "the   characteristics of photons ", "double r1,  r2,  r3");

  delete tupleFactory; //GB : no more needed, delete it.

  for(int i=0; i<24; i++)
    {
        //GB float r1=0.; // it can't be float if the upper booking is double.
        double r1=0.; //GB : then have double.
        double r2=0.;
        double r3=0.;
        double min=0., max=1.0, sigma=1.0, center=0.;
        r1=randgauss(min, max, sigma, center);
        r2=randgauss(min, max, sigma, center)+10;
        r3=randgauss(min, max, sigma, center)+100;
        tuple->fill(0,r1);
        tuple->fill(1,r2);
        tuple->fill(2,r3);
        tuple->addRow();
  }

  tree->commit();
  tree->close();

  delete analysisFactory; //GB : to delete all AIDA related things.

  return 0;
}

//////////////////////////////////////////////
///// read  program ///////////////////////////
//////////////////////////////////////////////

#include "AIDA/AIDA.h"
using namespace AIDA;

using namespace std; //GB

//GB
//#define CAST(aObject,aClass) (aClass*)(aObject)->cast(#aClass)  

int main()
{
  IAnalysisFactory *analysisFactory = AIDA_createAnalysisFactory();

  ITreeFactory *treeFactory = analysisFactory->createTreeFactory();

  //GB ITree *tree = treeFactory->create();
  ITree *tree = treeFactory->create("gaussian.aida","xml",true,false); //GB
  delete treeFactory; //GB : no more needed.

  IManagedObject* managedObject= tree->find("/tuplePhoton");
  if(!managedObject) cout<<"this pointer is NULL:"<<endl;
  if(managedObject)  cout<<"managedObject pointer is pointed to:"<<managedObject->name()<<endl;

  //if(!managedObject->cast("AIDA::ITuple"))          cout<<"error:"<<endl;
  //GB : the below      (AIDA::ITuple*) was missing in the original program.
  AIDA::ITuple *tuple = (AIDA::ITuple*)managedObject->cast("AIDA::ITuple"); //error is occurred at this line.

  //GB : you can use the upper CAST macro.
  //AIDA::ITuple* tuple =  CAST(*managedObject,AIDA::ITuple); //GB

  //GB : The loop :
  tuple->start();
  while ( tuple->next() ) {

    //GB : use getDouble is the variable had been booked with double.
    double r1 = tuple->getDouble(0); //r1
    double r2 = tuple->getDouble(1); //r2
    double r3 = tuple->getDouble(2); //r3

    std::cout << " r1 " << r1 
              << ", r2 = " << r2 
              << ", r3 = " << r3 
              << std::endl;
  }

  //tree->commit(); //GB : not needed when reading.
  tree->close();  

  delete analysisFactory; //GB : to delete all AIDA related things.

        return 0;
}

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

1 Ok: Re: Problem with AIDA and read or restore data from file (xml format)?   (Shahrokh - 06 Mar, 2006)
3 None: Re: Problem with AIDA and read or restore data from file (xml format)?   (Shahrokh - 06 Mar, 2006)
 Add Message Add Message
to: "Re: Problem with AIDA and read or restore data from file (xml format)?"

 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 ]