#include "ScanRoot.h"
#include <iostream>
using namespace std;

ClassImp(ScanRoot)
ScanRoot::ScanRoot(TDirectory* dir):TNamed("ScanRoot","Scan for ROOT")
{
  mDir = dir;
	mDir->SetWritable(kTRUE);
  mIo = NULL;
  mKeepLast = false;
  setPath();
}
ScanRoot::ScanRoot(bool debug):TNamed("ScanRoot","Scan for ROOT")
{
  mDebug = debug;
  mIo = new PelIO();
  mIo->setDebug(mDebug);
  mDir = new TDirectory("Scan_Histograms","Scan Histograms directory");
	mDir->SetWritable(kTRUE);
  mKeepLast = false;
  setPath();
}      
void ScanRoot::setPath()
{
  // creating structure in the TBrowser object
  int i,n;
  gROOT->cd("/");
  TList* bl=gROOT->GetListOfBrowsables();
  n = bl->GetSize();
  TList *l = NULL;
  for(i=0;i<n;i++) if(!strcmp(bl->At(i)->ClassName(),"TList")) l = (TList*)bl->At(i);
  bl->Clear("nodelete");
  bl->Add(mDir);
  TSystemDirectory *dir = new TSystemDirectory(gSystem->pwd(),gSystem->pwd());
  bl->Add(dir);
  if(l)
  {
    l->SetName("ROOT Files");
    bl->Add(l);
  }

  // adding $PELLETRON/include to root include path
  TString path = gSystem->GetIncludePath();
  TString system = gSystem->GetBuildArch();
  if(!system.BeginsWith("win32")) path.Append(" -I$PELLETRON/include");
  gSystem->SetIncludePath(path.Data());

  //this is for the Zero option in the histogram menu
  mH1= false;
  mH2= false;

  gROOT->Add(this);
  mMaxNpar = 128;
  mHistLoaded = false;
  mL2Loaded = false;
  mHistLibrary = "";
  mL2Library = "";

  return;
}
ScanRoot::~ScanRoot()
{
  if(mIo) { delete mIo; mIo = NULL;}
}
void ScanRoot::go(int nEv)
{
  if(!mIo) return;
  int read=0;
  int stat = 0;
  long bytes = 0;
  bool ok = true;
  char line[400];
  int i;
  gROOT->cd();
  // loop de eventos
  // stat = 0 -> no IO
  // stat = 1 -> Ok
  // stat = 2 -> Problema ou fim de arquivo
  TStopwatch timing;
  timing.Start();
  do
  {
    // if set to true, keep the last event processed in the memory
    // good for event mixing studies
    if(mKeepLast)
    {
      mNparLast = mNpar;
      for(i = 0;i<mMaxNpar; i++) mParLast[i] = mPar[i];
    }
    stat = mIo->readEvent(mMaxNpar,mNpar,&mPar[0]);
    if(stat==1)
    {
      bytes+= mIo->getCurrentEventSize();
      ok = process(mNpar,&mPar[0]);
      if(ok) if(mIo->isOutputOpen(0)) mIo->writeEvent(0);
      read++;
    }
    if(nEv>0) if(read>=nEv) stat = 0;
  } while(stat==1);
  timing.Stop();
  log("Processed %d events. Time used = %f seconds  CPU time = %f seconds",read,timing.RealTime(),timing.CpuTime());
  return;
}
bool ScanRoot::process(short nPar, float* par)
{
  bool ok = true;
  if(mL2Loaded) ok = (*l2trigger)(nPar,par);
  if(ok) if(mHistLoaded) (*fillHistograms)(nPar,par);
  return ok;
}
int ScanRoot::loadHist(char* file)
{
  int st,i,n;
  int error,id;
  char line[200];
  unloadHist();

  mHistLibrary=file;
  TString system = gSystem->GetBuildArch();

  if(mHistLibrary.EndsWith(".cxx") || mHistLibrary.EndsWith(".C"))
  {
    TString lib;
    if(mHistLibrary.EndsWith(".cxx")) lib = mHistLibrary(0,mHistLibrary.Length()-4);
    else lib = mHistLibrary(0,mHistLibrary.Length()-2);
    st = gSystem->CompileMacro(mHistLibrary.Data(),"kfOc",lib.Data());
    //mHistLibrary = lib;
    if(st!=1)
    {
      log(2,"Problem Compiling histogram code");
      mHistLibrary="";
      mHistLoaded = false;
      return 1;
    }
    TString libso = lib;
	if(system.BeginsWith("linux")) libso+=".so";
	else libso+=".dll";
    gSystem->Load(libso.Data());
  }
  else
  {
    if(mHistLibrary.EndsWith(".so") || mHistLibrary.EndsWith(".dll"))
	{
	  gSystem->Load(mHistLibrary.Data());
    }
	else
    {
      log(1,"Not a valid histogram code");
      mHistLibrary="";
      mHistLoaded = false;
      return 2;
    }
  }
  // cd... to the Reserved area for the SPM histograms
  cd();
  TList* l = mDir->GetList();
  if(l && l->GetSize()>0)
  {
    l->Delete();
    l->Clear();
  }

  cd();
  id = getROOTString("bookHistograms",line);
  if(id<0)
  {
    log(2,"Problem finding ScanRoot pointer");
    mHistLibrary="";
    mHistLoaded = false;
    return 3;
  }
  gROOT->ProcessLine(line,&error);
  if(error!=0)
  {
    log(2,"Problem booking histograms");
    mHistLibrary="";
    mHistLoaded = false;
    return 4;
  }
  gROOT->cd();
  mHistLoaded = true;
  prepareHistograms(mDir);
  log("Histograms loaded");
  return 0;
}
void ScanRoot::unloadHist()
{  
  if(mHistLoaded)
  {
    TString lib = mHistLibrary;
    if(mHistLibrary.EndsWith(".cxx")) lib = mHistLibrary(0,mHistLibrary.Length()-4);
    else if(mHistLibrary.EndsWith(".C")) lib = mHistLibrary(0,mHistLibrary.Length()-2);
    else if(mHistLibrary.EndsWith(".so")) lib = mHistLibrary(0,mHistLibrary.Length()-3);
    else if(mHistLibrary.EndsWith(".dll")) lib = mHistLibrary(0,mHistLibrary.Length()-4);
    gSystem->Unload(lib.Data());
  }
  mHistLoaded = false;
  return;
}
void ScanRoot::unloadL2()
{  
  if(mL2Loaded)
  {
    TString lib = mL2Library;
    if(mL2Library.EndsWith(".cxx")) lib = mL2Library(0,mHistLibrary.Length()-4);
    else if(mL2Library.EndsWith(".C")) lib = mL2Library(0,mHistLibrary.Length()-2);
    else if(mL2Library.EndsWith(".so")) lib = mL2Library(0,mHistLibrary.Length()-3);
    else if(mL2Library.EndsWith(".dll")) lib = mL2Library(0,mHistLibrary.Length()-4);
    gSystem->Unload(lib.Data());
  }
  mL2Loaded = false;
  return;
}
int ScanRoot::loadL2(char* file)
{
  int st,error,id;
  char line[200];

  unloadL2();
  mL2Library=file;

  if(mL2Library.EndsWith(".cxx") || mL2Library.EndsWith(".C"))
  {
    TString lib;
    if(mL2Library.EndsWith(".cxx")) lib = mL2Library(0,mL2Library.Length()-4);
    else lib = mL2Library(0,mL2Library.Length()-2);
    st = gSystem->CompileMacro(mL2Library.Data(),"kfOc",lib.Data());
    //mL2Library = lib;
    if(st!=1)
    {
      log(2,"Problem Compiling Level 2 code");
      mL2Library="";
      mL2Loaded = false;
      return 1;
    }
    TString system = gSystem->GetBuildArch();
    TString libso = lib;
	if(system.BeginsWith("linux")) libso+=".so";
	else libso+=".dll";

    gSystem->Load(libso.Data());
  }
  else
  {
    if(mL2Library.EndsWith(".so") || mL2Library.EndsWith(".dll")) gSystem->Load(mL2Library.Data());
    else
    {
      log(1,"Not a valid Level 2 code");
      mL2Library="";
      mL2Loaded = false;
      return 2;
    }
  }
  id = getROOTString("configureL2",line);
  if(id<0)
  {
    log(2,"Problem finding ScanRoot pointer");
    mL2Library="";
    mL2Loaded = false;
    return 3;
  }
  gROOT->ProcessLine(line,&error);
  if(error!=0)
  {
    log(2,"Problem configuring Level 2");
    mHistLibrary="";
    mHistLoaded = false;
    return 4;
  }
  mL2Loaded = true;
  log("Level 2 loaded");
  return 0;
}
int ScanRoot::getROOTString(char* base,char*line)
{
  int id=-1;
  int n,i;
  TList *l = gROOT->GetList();
  n = l->GetSize();
  if(n==0) return -1;
  for(i=0;i<n;i++)
  {
    if(l->At(i)==this) { id=i; break;}
  }
  if(id==-1) return -1;
  sprintf(line,"%s((ScanRoot*)gROOT->GetList()->At(%d))",base,id);
  return id;
}
void ScanRoot::zero()
{
  zeroDir(mDir);
  return;
}
void ScanRoot::zero(char* name)
{
  TObject *o = findObject(mDir,name);
	mDir->cd();
  if(!o) return;
  TString n = o->ClassName();
  if(n.BeginsWith("TH2")) ((TH2*)o)->Reset();
  if(n.BeginsWith("TH1")) ((TH1*)o)->Reset();
  return;
}
void ScanRoot::zeroDir(TDirectory* dir)
{
  int n,i;
  TList* l = dir->GetList();
	//cout <<"Inside directory "<<dir->GetName()<<endl;
  if(l)
  {
    n = l->GetSize();
    if(n>0)
    {
      for(i=0;i<n;i++)
      {
        TObject *o = l->At(i);
        TString c = o->ClassName();
        if(c.BeginsWith("TH1")) ((TH2*)o)->Reset();
        if(c.BeginsWith("TH2")) ((TH1*)o)->Reset();
				//if(c.BeginsWith("TH")) cout <<"    Zeroing histogram "<<o->GetName()<<endl;
        if(c.BeginsWith("TDirectory")) zeroDir((TDirectory*)o);
			}
		}
	}
}
void ScanRoot::prepareHistograms(TDirectory* dir)
{
  int i,nn;
  TList* l = dir->GetList();
  if(l)
  {
    nn = l->GetSize();
    if(nn>0)
    {
      for(i=0;i<nn;i++)
      {
        TObject *o = l->At(i);
        TString n = o->ClassName();
	if(n.BeginsWith("TDirectory")) prepareHistograms((TDirectory*)o);
        if(n.BeginsWith("TH2"))
        {
          ((TH2*)o)->SetOption("colz");
          if(!mH2)
          {
            TClass *c = ((TH2*)o)->IsA();
            TList *ll = c->GetMenuList();
            ll->AddLast(new TClassMenuItem(TClassMenuItem::kPopupUserFunction, c,"Zero","Reset", NULL,NULL,-1,kTRUE));
            mH2 = true;
          }
        }
        if(n.BeginsWith("TH1") && !mH1)
        {
          TClass *c = ((TH1*)o)->IsA();
          TList *ll = c->GetMenuList();
          ll->AddLast(new TClassMenuItem(TClassMenuItem::kPopupUserFunction, c,"Zero","Reset", NULL,NULL,-1,kTRUE));
          mH1 = true;
        }
      }
    }
  }
  return;
}

TObject* ScanRoot::findObject(TDirectory* dir, char* name)
{
  int i = 0, n = 0;
  TObject *oo = 0;
  TDirectory *D = dir;
  //cout <<"Inside directory "<<dir->GetName()<<endl;

  TList* l = dir->GetList();
  if(l)
  {
    n = l->GetSize();
    if(n>0)
    {
      for(i=0;i<n;i++)
      {
        TObject *o = l->At(i);
        TString c = o->ClassName();
        if(c.BeginsWith("TDirectory"))
	{
	  if(!oo) oo = findObject((TDirectory*)o,name);
	}
	else if(!strcmp(name, o->GetName()))
	{
          //cout <<"    Found object "<<o->GetName()<<"  at  "<<o<<endl;
	  oo=o;
	  return oo;
	}
      }
    }
  }
  D->cd();
  return oo;
}

void ScanRoot::saveDir(TFile* f, TDirectory* parent, TDirectory* dir, bool root)
{
  int n, i;
	TDirectory *D = 0;
	if(root) D = parent;
	else
	{
	  D = parent->mkdir(dir->GetName());
		D->cd();
	}
	TList* l = dir->GetList();
	if(l)
	{
    n = l->GetSize();
    if(n>0)
    {
      for(i=0;i<n;i++)
      {
        TObject *o = l->At(i);
        TString n = o->ClassName();
        if(n.BeginsWith("TDirectory"))
				{
				  saveDir(f,D,(TDirectory*)o, false);
				}
				else o->Write();
      }
    }
	}
	parent->cd();
}

void ScanRoot::saveHist(char* file)
{

	TFile *f = new TFile(file,"RECREATE");
  f->cd();
	TDirectory *parent = gDirectory;
	saveDir(f,parent,mDir, true);
  f->Close();
  delete f;
	return;
}
void ScanRoot::addHist(char* file)
{
  int n,i;
  TFile *f = new TFile(file);
  if(!f) { log(2,"Error opening file or file does not exist"); return;}
  f->cd();
  TList* l = mDir->GetList();
  if(l)
  {
    n = l->GetSize();
    if(n>0)
    {
      for(i=0;i<n;i++)
      {
        TObject *o = l->At(i);
        TString n = o->ClassName();
        if(n.BeginsWith("TH1")) if(f->Get(o->GetName())) ((TH1*)o)->Add((TH1*)f->Get(o->GetName()),1);
        if(n.BeginsWith("TH2")) if(f->Get(o->GetName())) ((TH2*)o)->Add((TH2*)f->Get(o->GetName()),1);
      }
    }
  }
  f->Close();
  delete f;
  return;
}
void ScanRoot::init()
{
  int error;
  if(mHistLoaded) gROOT->ProcessLineFast("init()",&error);
  if(mL2Loaded)   gROOT->ProcessLineFast("initL2()",&error);
  return;
}
void ScanRoot::finish()
{
  int error;
  if(mHistLoaded) gROOT->ProcessLineFast("finish()",&error);
  if(mL2Loaded)   gROOT->ProcessLineFast("finishL2()",&error);
  return;
}
void ScanRoot::stat()
{
  int i = 0;
  log(10,"\n------------------------------------------");
  log("ScanRoot statistics");
  if(mIo->isInputOpen())
  {
    log(10,"Input file name (.FIL) = %s",mIo->getFileName());
    log(10,"NEvents read from file = %d",mIo->getNRead());
  }
  bool ok = false;
  for(i = 0;i<mIo->getMaxOutput();i++)
    if(mIo->isOutputOpen(i))
    {
      log(10,"Output file (.FIL) = %s",mIo->getFileNameOutput(i));
      log(10,"   NEvents written = %d",mIo->getNSaved(i));
      ok = true;
    }
  if(getIO())
  {
		if(getIO()->isSmearing())
      log(10,"Smearing mode = ON");
    else
      log(10,"Smearing mode = OFF");

		if(ok)
		{
      if(getIO()->isSaveOnlyNewPar())
        log(10,"Output saving mode = SAVE ONLY NEW PARAMETERS");
      else
        log(10,"Output saving mode = SAVE ALL PARAMETERS");
    }
  }
  if(mHistLoaded) log(10,"Histogram library = %s",(char*)mHistLibrary.Data());
  if(mL2Loaded) log(10,"Level 2 library = %s",(char*)mL2Library.Data());
  log(10,"Max number of parameters = %d",getNPar());
  log(10,"------------------------------------------\n");
  return;
}


ROOT page - Class index - Class Hierarchy - 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.