Message: Re: How can we export data in .aida file into a spreadsheet? Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: How can we export data in .aida file into a spreadsheet? 

Forum: Analysis
Re: None How can we export data in .aida file into a spreadsheet? (Fada Guan)
Date: 12 Apr, 2011
From: Guy Barrand <Guy Barrand>

    Hi

  Below is the code of a program that opens a .aida (or .root, .hbook in fact) input file, find a tuple
 in it and produces a .csv file. (The separator is here a comma. See the "char sep" line
 to change that).

  To build and run :
    - save the below code, for example in a aida_tuple_2_csv.cpp file.
    - <source setup your AIDA implementation>
    - build the program with something as :
         c++ `aida-config --cflags` aida_tuple_2_csv.cpp `aida-config --libs`
   - run the program by passing in arguments :
        the name of the input file,
        the format of the input file, 
        the path to a tuple in the file.
     for example :
        ./a.out my_file.aida aida my_tuple
        ./a.out my_file.root root dir1/dir2/my_tuple

  The program produces a out.csv file.

  As the below code is over the AIDA API and C/C++ libs, it should work with
 any AIDA implementation.

   Cheers. Guy.

#include <AIDA/IAnalysisFactory.h>
#include <AIDA/ITreeFactory.h>
#include <AIDA/ITree.h>
#include <AIDA/IManagedObject.h>
#include <AIDA/ITuple.h>

#include <iostream>

bool process_file(AIDA::IAnalysisFactory& aAIDA,
                  const std::string& a_file,
                  const std::string& a_format,
                  const std::string& a_path,
                  std::ostream& a_out) {
  AIDA::ITreeFactory* treeFactory = aAIDA.createTreeFactory();
  if(!treeFactory) {
    std::cout << "process_file : can't get a TreeFactory." << std::endl;
    return false;
  }

  AIDA::ITree* tree = treeFactory->create(a_file,a_format,true,false);
  delete treeFactory;

  if(!tree) {
    std::cout << "process_file : can't open data file." << std::endl;
    return false;
  }

  std::string dir;
  std::string name;
 {std::string::size_type pos = a_path.rfind('/');
  if(pos==std::string::npos) {
    name = a_path;
  } else {
    dir = a_path.substr(0,pos);pos++;
    name = a_path.substr(pos,a_path.size()-pos);  
  }}

  if(dir.size()){
    if(!tree->cd(dir)) {
      std::cout << "process_file :"
                << " can't cd to " << dir << "."
                << std::endl;
      delete tree;
      return false;
    }
  }

  AIDA::IManagedObject* object = tree->find(name);
  if(!object) {
    std::cout << "process_file : "
              << " object " << name << " not found in tree." 
              << std::endl;
    delete tree;
    return false;
  }

  AIDA::ITuple* tuple = dynamic_cast<AIDA::ITuple*>(object);
  if(!tuple) {
    std::cout << "process_file : object not an AIDA::ITuple." << std::endl;
    delete tree;
    return false;
  }

  //bool verbose = false;
  bool verbose = true;
  if(verbose) {
    int coln = tuple->columns();
    for(int index=0;index<coln;index++) {
      std::cout << "process_file :"
                << " icol = " << index
                << ", label = " << tuple->columnName(index) 
                << ", type = " << tuple->columnType(index) 
                << std::endl;
    }
    std::cout << "process_file : rows = " << tuple->rows() << std::endl;
  }

  int coln = tuple->columns();
  std::vector<std::string> types = tuple->columnTypes();

  char sep = ',';

  tuple->start();
  while(tuple->next()) {

    for(int col=0;col<coln;col++) {
      if(col) a_out << sep;
      if(types[col]=="float") {
        float v = tuple->getFloat(col);a_out << v;
      } else if(types[col]=="double") {
        double v = tuple->getDouble(col);a_out << v;
      } else if(types[col]=="char") {
        char v = tuple->getChar(col);a_out << v;
      } else if(types[col]=="short") {
        short v = tuple->getShort(col);a_out << v;
      } else if(types[col]=="int") {
        int v = tuple->getInt(col);a_out << v;
      } else if(types[col]=="long") {
        long v = tuple->getLong(col);a_out << v;
      } else if(types[col]=="boolean") {
        bool v = tuple->getBoolean(col);a_out << v;
      } else if(types[col]=="string") {
	std::string v = tuple->getString(col);a_out << v;
      } else {
        std::cout << "process_file : unknown type." << types[col] << std::endl;
        delete tree;
        return false;
      }
    }
    a_out << std::endl;

  }

  delete tree;

  return true;
}

//////////////////////////////////////////////////////////////////////////////

#include <fstream>

int main(int argc,char* argv[]) {
  if(argc!=4) {
    std::cout << "main : three arguments expected :"
              << " <file>, <format>, <tuple path>"
              << std::endl;
    return 1;
  }

  std::string file(argv[1]);
  std::string format(argv[2]);
  std::string tuple(argv[3]);

  AIDA::IAnalysisFactory* aida = AIDA_createAnalysisFactory();
  if(!aida) {
    std::cout << "main : AIDA not found." << std::endl;
    return 1;
  }

  std::ofstream out("out.csv");
  if(out.fail()) {
    std::cout << "main : can't open out.csv." << std::endl;
    return 1;
  }

  if(!process_file(*aida,file,format,tuple,out)){
    std::cout << "main :"
              << " read_file " << file << " failed."
              << std::endl;
  }

  out.close();

  delete aida;

  std::cout << "main : exit..." << std::endl;

  return 1;
}

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

1 None: Re: How can we export data in .aida file into a spreadsheet?   (Fada Guan - 12 Apr, 2011)
2 None: Re: How can we export data in .aida file into a spreadsheet?   (George Tabatadze - 10 Dec, 2011)
 Add Message Add Message
to: "Re: How can we export data in .aida file into a spreadsheet?"

 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 ]