[ROOT] How to use std::ifstream in ROOT?

From: Exaos Lee (schlie@iris.ciae.ac.cn)
Date: Fri May 30 2003 - 04:19:39 MEST


Dear ROOTers,
I cannot compile the attachment. And it cannot be interpreted by ROOT 
correctly. I use string, ifstream here. What's wrong with it?

Regards


//
// This is the ROOT macro to convert Kmax event file to ROOT tree.
// 
// by Exaos Lee (schlie@iris.ciae.ac.cn)
//

int evnt2root();
// Change big endian to little endian: 4-byte interger
unsigned long swap32(unsigned long x) 
  //ULong_t swap32(ULong_t x) 
{
  return ((((x) & 0x00ff0000) >> 8 ) | \
	  (((x) & 0x0000ff00) << 8 ) | \
	  (((x) & 0xff000000) >> 24) | \
	  (((x) & 0x000000ff) << 24));
}
// Change big endian to little endian: 2-byte interger
unsigned short swap16(unsigned short x)
  //UShort_t swap16(UShort_t x)
{
  return ((((x) & 0xff00) >> 8) |  \
	  (((x) & 0x00ff) << 8));
}

#ifndef __CINT__
#include "TFile.h"
#include "TNtuple.h"
#include "TROOT.h"

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <cassert>
#include <string>
//------------------------------------------------------------------
int main()
{
  return evnt2root();
}
#endif

int evnt2root()
{
  TFile    datafile("n2n_2003_01.root","RECREATE","n2n event file"); // create root file

  // index file: ASCII format, each line contains one event filename for processing.
  ifstream idx_in("event_index.txt", ios::in); 
  ifstream evnt_in;  // Kmax .evnt file stream;
  //std::string   evnt_fn;  // Evnt filename
  char  evnt_fn[256];
  
  UShort_t evType, evSize; // Event type and size
  UShort_t par_num=0x0015; // Our experiment's parameter number: 21
  ULong_t  blkCount,itmp;  // Block count and temporary ULong_t variable
  Int_t    bufCount, total_buffers, total_blocks, total_events, total_files; // counters

  ULong_t  tmp_par[21];
  Float_t  par[18];     // Array of parameters
  TString  par_name("ph0:ph1:psd1:ph2:psd2:ph3:psd3:ph4:psd4:tof01:tof02:tof03:tof04:tof0:tof1:tof2:tof3:tof4");  // Parameter names
  Float_t  mpar[3];      // Parameters for monitor
  TString  mpar_name("phm:psdm:tofm");  // Parameter names of monitor.

  TNtuple  *par_nt  = new TNtuple("n2n_ntuple","Ntuple for n2n experiment",par_name.Data());
  TNtuple  *mpar_nt = new TNtuple("n2n_mntuple","Ntuple for monitor",mpar_name.Data());

  do {
    idx_in >> evnt_fn;  // read a filename
    //idx_in.getline(evnt_fn,256);
    if( !strlen(evnt_fn) && idx_in.eof()) break;
    evnt_in.open(evnt_fn,ios::in | ios::binary);  // open an event file to read
    assert(evnt_in.is_open());  // Make sure the file is open

    evnt_in.read(&itmp,1);  // read file format sign: 4 bytes
    if( itmp != 0x02000001 ) {
      printf("******ERROR****** File %s is not a Kmax binary event file!\n",evnt_fn);
      break;
    }

    bufCount = 0;
    evnt_in.seekg(512L,std::ios::beg); // Start of file 0x0000200: Beginning of event data
    printf("---Processing Event file: %s ----\n",evnt_fn);
    while(!evnt_in.eof()) {
      evnt_in.read(&evType,sizeof(unsigned short)*1); // Read evnt type 
      swap16(evType);
      evnt_in.read(&evSize,sizeof(unsigned short)*1); // Read evnt size
      swap16(evSize);
      if(evSize != par_num) {
	printf("******ERROR****** Event size is not compatible!\n");
	break;
      }
      evnt_in.read(&blkCount,sizeof(unsigned long)*1); // Read block count
      swap32(blkCount);
      if(blkCount>0 && blkCount !=0xffffffff) {
	printf("Buffer %5d -- Event Size: %5d  Event Type: %5d",++bufCount, evSize, evType);
	for(int i=0;i<blkCount;i++) {
	  int j;
	  // Read parameters
	  for(j=0;j<evSize;j++) {
	    evnt_in.read(&itmp,sizeof(unsigned long)*1);  
	    tmp_par[j]=swap32(itmp);
	  }
	  // Assign parameters to ntuples
	  // Of course, you can give thresholds here.
	  for(j=0;j<9;j++) par[j]=tmp_par[j]/8.0;
	  mpar[0]=tmp_par[9]/8.0; 
	  mpar[1]=tmp_par[10]/8.0;
	  mpar[2]=tmp_par[11]/4.0;
	  for(j=12;j<16;j++) par[j-3]=tmp_par[j]/8.0;
	  for(j=16;j<21;j++) par[j-3]=tmp_par[j]/4.0;
	  // Fill ntuples
	  par_nt->Fill(mpar);
	  mpar_nt->Fill(par);
	  
	  total_events++;  // this counter is used for counting selected events.
	                   // here, it is equivelant to total_buffers*evSize.
	} else
	  break;
      }
      printf(" -- read %10d blocks. OK!\n",blkCount);
      total_blocks+=blkCount;
    }
    printf("--- Total buffers: %d ---\n",bufCount);
    total_buffers+=bufCount;
    total_files++;

    evnt_in.close();
  } while(!idx_in.eof());

  printf("\nAll event files have been dumped.\n");
  printf("Total files: %5d  Total buffers: %10d  Total blocks: %10d\n", \
	 total_files,total_buffers,total_blocks);
  printf("Selected events: %10d\n",total_events);

  datafile.Write();
  datafile.Close();

  return 0;
}



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:12 MET