// ----------------------------------------------------------------------------
// $Id: LCDreadStdFile.cxx,v 1.2 2001/06/19 03:56:21 toshi Exp $
// ----------------------------------------------------------------------------
//
// $Log: LCDreadStdFile.cxx,v $
// Revision 1.2  2001/06/19 03:56:21  toshi
// Various changes to allow multi-file open for background overlay events.
//
// Revision 1.1  2001/05/10 22:06:39  toshi
// File IO (stdHEP, SIO,...) utilities.
//
//
#include "LCDreadStdFile.h"

#include "LCDMcPart.h"

#include <iostream.h>
#include <assert.h>

//______________________________________________________________________
// readStdFile
//
// This class is one type of event source.  It can get an event from a stdHEP
// file and feed the MC particle information into instances of MC

ClassImp(LCDreadStdFile)

//
// ----------------------------------------------------------------------------
 LCDreadStdFile::LCDreadStdFile() {
  m_ninputfile=0;

  m_event = new LCDEvent();
  m_ownevent=kTRUE;

}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 LCDreadStdFile::LCDreadStdFile(Char_t* filein, LCDEvent* event) {
  m_ninputfile=0;

  m_inputfile[m_ninputfile] = filein;
  m_istr[m_ninputfile] = m_ninputfile;

  if (event == 0) {
    m_event = new LCDEvent();
    m_ownevent=kTRUE;
  } else {
    m_event = event;
    m_ownevent=kFALSE;
  }

  assert(! StdHepXdrReadInit(filein, 1, m_istr[m_ninputfile]));

  m_ninputfile++;
}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 LCDreadStdFile::~LCDreadStdFile() {
  for (Int_t i=0 ; i < m_ninputfile ; i++) {
    StdHepXdrEnd(m_istr[i]);
    printf("Closing input file %s n",m_inputfile[i]);
  }
  if (m_ownevent) {
    m_event->Delete();
    delete m_event;
  }
}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 Int_t LCDreadStdFile::AddStdFile(Char_t* stdfilename) {
  m_inputfile[m_ninputfile] = stdfilename;
  m_istr[m_ninputfile] = m_ninputfile;

  if (m_ninputfile == 0) {
    assert(! StdHepXdrReadInit(stdfilename, 1, m_istr[0]));
  } else {
    assert(! StdHepXdrReadOpen(stdfilename,1,m_istr[m_ninputfile]));
  }

  return ++m_ninputfile;

}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 Int_t LCDreadStdFile::GetEvent(LCDEvent* event) {
  // From stdHEP file representation to MCpart instances
  if( ReadEvent() == 0) return 0;
  MakeMcPart(event);
  return 1;

}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 Int_t LCDreadStdFile::ReadEvent(Int_t ifile) {

  if (m_ninputfile <= 0) {
  // Check # of input file must be geater than 0.
    fprintf(stderr,
	    "LCDreadStdFile::ReadEvent No input file..n");
    return 0;
  }
  if (ifile < 0 || ifile >= m_ninputfile) {
  // Check input file # must be between 0 and m_ninputfile.
    fprintf(stderr,
	    "LCDreadStdFile::ReadEvent input file number error. iflie=%d m_ninputfile=%dn",
	    ifile,m_ninputfile);
    return 0;
  }

  // reading the binary file to fill the common block

  hepevt_.nhep = 0; Int_t lbl = 1;	

  StdHepZero();
	
  Int_t ierr = StdHepXdrRead(&lbl,m_istr[ifile]);

  if(lbl == 100){
    printf(" LCDreadStdFile::ReadEvent Reading StdHep begin run record.n");
    ierr = StdHepXdrRead(&lbl,m_istr[ifile]);
  } 
  if (ierr != 0) {
    printf(" unexpected end of file after %d eventsn",hepevt_.nevhep);
    return 0;
  }
  return hepevt_.nhep;
}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 Int_t LCDreadStdFile::ReadEventMulti(Int_t ifile) {

  if (m_ninputfile <= 0) {
  // Check # of input file must be geater than 0.
    fprintf(stderr,
	    "LCDreadStdFile::ReadEventMulti No input file..n");
    return 0;
  }
  if (ifile < 0 || ifile >= m_ninputfile) {
  // Check input file # must be between 0 and m_ninputfile.
    fprintf(stderr,
	    "LCDreadStdFile::ReadEventMulti input file number error. iflie=%d m_ninputfile=%dn",
	    ifile,m_ninputfile);
    return 0;
  }

  // reading the binary file to fill the common block

  Int_t lbl = 1;

  Int_t ierr = StdHepXdrReadMulti(&lbl,m_istr[ifile]);

  if(lbl == 100){
    printf(" LCDreadStdFile::ReadEventMulti Reading StdHep begin run record.n");
    ierr = StdHepXdrReadMulti(&lbl,m_istr[ifile]);
  } 
  if (ierr != 0) {
    printf(" unexpected end of file after %d eventsn",hepevt_.nevhep);
    return 0;
  }
  return hepevt_.nhep;
}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 Int_t LCDreadStdFile::MakeMcPart(LCDEvent* event, Int_t f_overlay) {
  LCDEvent* evt=0;
  if (event) {
    evt=event;
  } else if (m_event) {
    evt=m_event;
  } else {
    fprintf(stderr,
	    "LCDreadStdFile::MakeMcpart Pointer of LCDEvent is not given!n");
    return 0;
  }

  // Makes McPart objects from the stdHEP common

  Int_t          npart = hepevt_.nhep;
  Int_t          i        ;
  LCDMcPart*     part     ;
  Int_t          status   ;
  Int_t          id       ;
  Double_t       charge   ;
  TVector3       position ; 
  TLorentzVector momentum4;

  TClonesArray* mclist=evt->MCparticles();
  if (f_overlay == 0) mclist->Delete();

  Int_t mcoffset=mclist->GetEntries();
  for(i = 0; i < npart; i++) {
    if (i > 3999) continue;
    //if (npart > 3999) continue;
    // create and fill the McPart
    part = new((*mclist)[i+mcoffset]) LCDMcPart();

    status = hepevt_.isthep[i];
    id     = hepevt_.idhep[i];
    charge = partchg(id);

    // 09/22/2000 T.Abe mm to cm conversion.
    position.SetXYZ(hepevt_.vhep[i][0]/10.0,
		    hepevt_.vhep[i][1]/10.0,
		    hepevt_.vhep[i][2]/10.0);
    momentum4.SetPxPyPzE(hepevt_.phep[i][0],
			 hepevt_.phep[i][1],
			 hepevt_.phep[i][2],
			 hepevt_.phep[i][3]);

    part->SetStatus(status);
    part->SetParticleID(id);
    part->SetCharge(charge);
    part->SetPosition(position);
    part->Set4Momentum(momentum4);

  }

  // Fill parent and daughter information
  Int_t i_parent;
  LCDMcPart* parent;
  for(i = 0; i < npart; i++){
    part = (LCDMcPart*)(mclist->At(i+mcoffset));

    i_parent   =  hepevt_.jmohep[i][0] - 1 + mcoffset;
    part->SetParentIdx(i_parent);      

    if (i_parent >= 0) {
      parent=(LCDMcPart*)(mclist->At(i_parent));
      parent->SetTermPosition(*part->GetPositionPtr());
    }
  }

  return 1;
}
// ----------------------------------------------------------------------------

//
// ----------------------------------------------------------------------------
 Char_t* LCDreadStdFile::GetFileName(Int_t ifile) {

  if (m_ninputfile <= 0) {
  // Check # of input file must be geater than 0.
    fprintf(stderr,
	    "LCDreadStdFile::GetFileName No input file..n");
    return 0;
  }
  if (ifile < 0 || ifile >= m_ninputfile) {
  // Check input file # must be between 0 and m_ninputfile.
    fprintf(stderr,
	    "LCDreadStdFile::GetFileName input file number error. iflie=%d m_ninputfile=%dn",
	    ifile,m_ninputfile);
    return 0;
  }

  return m_inputfile[ifile];
}
// ----------------------------------------------------------------------------


ROOT page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.