CMS 3D CMS Logo

Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Attributes

AlignmentIORootBase Class Reference

Base class for ROOT-based I/O of Alignment parameters etc. More...

#include <AlignmentIORootBase.h>

Inheritance diagram for AlignmentIORootBase:
AlignableDataIORoot AlignmentCorrelationsIORoot AlignmentParametersIORoot HIPUserVariablesIORoot MillePedeVariablesIORoot

List of all members.

Protected Member Functions

 AlignmentIORootBase ()
 constructor
int closeRoot (void)
 close IO
virtual void createBranches (void)=0
 create root branches
int openRoot (const char *filename, int iteration, bool writemode)
 open IO
virtual void setBranchAddresses (void)=0
 set root branches
int testFile (const char *filename, const TString &tname)
 test if file is existing and if so, what the highest iteration is
TString treeName (int iter, const TString &tname)
 compose tree name
virtual ~AlignmentIORootBase ()
 destructor

Protected Attributes

bool bWrite
TTree * tree
TString treename
TString treetxt

Static Protected Attributes

static const int itermax = 1000
static const int nParMax = 20

Private Attributes

TFile * myFile

Detailed Description

Base class for ROOT-based I/O of Alignment parameters etc.

Definition at line 11 of file AlignmentIORootBase.h.


Constructor & Destructor Documentation

AlignmentIORootBase::AlignmentIORootBase ( ) [inline, protected]

constructor

Definition at line 16 of file AlignmentIORootBase.h.

: tree(0), myFile(0) {}
AlignmentIORootBase::~AlignmentIORootBase ( ) [protected, virtual]

destructor

Definition at line 9 of file AlignmentIORootBase.cc.

References myFile.

{
  delete myFile; // tree is deleted automatically with file
}

Member Function Documentation

int AlignmentIORootBase::closeRoot ( void  ) [protected]

close IO

Definition at line 99 of file AlignmentIORootBase.cc.

References bWrite, myFile, and tree.

Referenced by AlignmentCorrelationsIORoot::close(), MillePedeVariablesIORoot::close(), AlignmentParametersIORoot::close(), AlignableDataIORoot::close(), and HIPUserVariablesIORoot::close().

{
  if (bWrite) { //writing
    tree->Write();
  }

  delete myFile;
  myFile = 0;
  tree = 0; // deleted with file

  return 0;
}
virtual void AlignmentIORootBase::createBranches ( void  ) [protected, pure virtual]
int AlignmentIORootBase::openRoot ( const char *  filename,
int  iteration,
bool  writemode 
) [protected]

open IO

Definition at line 17 of file AlignmentIORootBase.cc.

References bWrite, createBranches(), lut2db_cfg::filename, align_cfg::iteration, myFile, NULL, setBranchAddresses(), testFile(), tree, treeName(), treename, treetxt, and TablePrint::write.

Referenced by AlignableDataIORoot::open(), MillePedeVariablesIORoot::open(), HIPUserVariablesIORoot::open(), AlignmentParametersIORoot::open(), and AlignmentCorrelationsIORoot::open().

{
  bWrite=write;
  int iter;

  edm::LogInfo("AlignmentIORootBase") << "File: " << filename ;

  if (bWrite) { // writing

    int iterfile = testFile(filename,treename);
    if (iterfile == -1) {
      iter=iteration;
          edm::LogInfo("AlignmentIORootBase") << "Write to new file; first iteration: " << iter ;
          myFile = TFile::Open(filename,"recreate");
    } else {
      if (iteration == -1) {
        iter=iterfile+1;
                edm::LogInfo("AlignmentIORootBase") 
                  << "Write to existing file; highest iteration: " << iter;
      } else {
        if (iteration<=iterfile) {
          edm::LogError("AlignmentIORootBase") 
            << "Iteration " << iteration 
            <<" invalid or already exists for tree " << treename;
                  return -1;
        }
        iter = iteration;
        edm::LogInfo("AlignmentIORootBase")  << "Write to new iteration: " << iter;
      }
      myFile = TFile::Open(filename,"update");
          
    }

    // create tree
    myFile->cd();
    edm::LogInfo("AlignmentIORootBase") << "Tree: " << treeName(iter,treename);
    tree = new TTree(treeName(iter,treename),treetxt);
    createBranches();

  } else { // reading

    int iterfile = testFile(filename,treename);
    if ( iterfile == -1 ) {
      edm::LogError("AlignmentIORootBase") << "File does not exist!";
      return -1;
    } else if ( iterfile == -2 ) {
      edm::LogError("AlignmentIORootBase") << "Tree " << treename 
                                           << " does not exist in file " << filename;
      return -1;
    } else {
      if (iteration == -1) {
        iter=iterfile;
        edm::LogInfo("AlignmentIORootBase") << "Read from highest iteration: " << iter;
      } else {
        if (iteration>iterfile) {
        edm::LogError("AlignmentIORootBase")
          << "Iteration " << iteration << " does not exist for tree " << treename;
        return -1;
        }
        iter = iteration;
        edm::LogInfo("AlignmentIORootBase")  << "Read from specified iteration: " << iter;
      }
      myFile = TFile::Open(filename, "read");
    }

    myFile->cd();
    // set trees
    edm::LogInfo("AlignmentIORootBase") <<" Tree: " <<treeName(iter,treename);
    tree = (TTree*)myFile->Get(treeName(iter,treename));
    if (tree==NULL) {
      edm::LogError("AlignmentIORootBase") <<"Tree does not exist in file!";
      return -1;
    }
    setBranchAddresses();
  }

  return 0;
}
virtual void AlignmentIORootBase::setBranchAddresses ( void  ) [protected, pure virtual]
int AlignmentIORootBase::testFile ( const char *  filename,
const TString &  tname 
) [protected]

test if file is existing and if so, what the highest iteration is

Definition at line 116 of file AlignmentIORootBase.cc.

References itermax, NULL, and treeName().

Referenced by openRoot().

{
  FILE* testFILE;
  testFILE = fopen(filename,"r");
  if (testFILE == NULL) {
    return -1;
  } else {
    fclose(testFILE);
    int ihighest=-2;
    TFile *aFile = TFile::Open(filename,"read");
    for (int iter=0; iter<itermax; iter++) {
      if ((0 != (TTree*)aFile->Get(treeName(iter,tname))) 
          && (iter>ihighest)) ihighest=iter; 
    }
    delete aFile;
    return ihighest;
  }
}
TString AlignmentIORootBase::treeName ( int  iter,
const TString &  tname 
) [protected]

compose tree name

Definition at line 138 of file AlignmentIORootBase.cc.

Referenced by openRoot(), and testFile().

{
  return TString(tname + Form("_%i",iter));
}

Member Data Documentation

bool AlignmentIORootBase::bWrite [protected]

Definition at line 43 of file AlignmentIORootBase.h.

Referenced by AlignmentParametersIORoot::close(), closeRoot(), and openRoot().

const int AlignmentIORootBase::itermax = 1000 [static, protected]

Definition at line 46 of file AlignmentIORootBase.h.

Referenced by testFile().

TFile* AlignmentIORootBase::myFile [private]

Definition at line 49 of file AlignmentIORootBase.h.

Referenced by closeRoot(), openRoot(), and ~AlignmentIORootBase().

const int AlignmentIORootBase::nParMax = 20 [static, protected]

Definition at line 45 of file AlignmentIORootBase.h.

Referenced by AlignmentCorrelationsIORoot::read().

TTree* AlignmentIORootBase::tree [protected]
TString AlignmentIORootBase::treename [protected]
TString AlignmentIORootBase::treetxt [protected]