CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/Alignment/CommonAlignmentAlgorithm/src/AlignmentIORootBase.cc

Go to the documentation of this file.
00001 // this class's header
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 
00004 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentIORootBase.h"
00005 
00006 #include "TFile.h"
00007 #include "TTree.h"
00008 
00009 AlignmentIORootBase::~AlignmentIORootBase()
00010 {
00011   delete myFile; // tree is deleted automatically with file
00012 }
00013 
00014 // ----------------------------------------------------------------------------
00015 // open file/trees for write
00016 
00017 int AlignmentIORootBase::openRoot(const char* filename, int iteration, bool write)
00018 {
00019   bWrite=write;
00020   int iter;
00021 
00022   edm::LogInfo("AlignmentIORootBase") << "File: " << filename ;
00023 
00024   if (bWrite) { // writing
00025 
00026     int iterfile = testFile(filename,treename);
00027     if (iterfile == -1) {
00028       iter=iteration;
00029           edm::LogInfo("AlignmentIORootBase") << "Write to new file; first iteration: " << iter ;
00030           myFile = TFile::Open(filename,"recreate");
00031     } else {
00032       if (iteration == -1) {
00033         iter=iterfile+1;
00034                 edm::LogInfo("AlignmentIORootBase") 
00035                   << "Write to existing file; highest iteration: " << iter;
00036       } else {
00037         if (iteration<=iterfile) {
00038           edm::LogError("AlignmentIORootBase") 
00039             << "Iteration " << iteration 
00040             <<" invalid or already exists for tree " << treename;
00041                   return -1;
00042         }
00043         iter = iteration;
00044         edm::LogInfo("AlignmentIORootBase")  << "Write to new iteration: " << iter;
00045       }
00046       myFile = TFile::Open(filename,"update");
00047           
00048     }
00049 
00050     // create tree
00051     myFile->cd();
00052     edm::LogInfo("AlignmentIORootBase") << "Tree: " << treeName(iter,treename);
00053     tree = new TTree(treeName(iter,treename),treetxt);
00054     createBranches();
00055 
00056   } else { // reading
00057 
00058     int iterfile = testFile(filename,treename);
00059     if ( iterfile == -1 ) {
00060       edm::LogError("AlignmentIORootBase") << "File does not exist!";
00061       return -1;
00062     } else if ( iterfile == -2 ) {
00063       edm::LogError("AlignmentIORootBase") << "Tree " << treename 
00064                                            << " does not exist in file " << filename;
00065       return -1;
00066     } else {
00067       if (iteration == -1) {
00068         iter=iterfile;
00069         edm::LogInfo("AlignmentIORootBase") << "Read from highest iteration: " << iter;
00070       } else {
00071         if (iteration>iterfile) {
00072         edm::LogError("AlignmentIORootBase")
00073           << "Iteration " << iteration << " does not exist for tree " << treename;
00074         return -1;
00075         }
00076         iter = iteration;
00077         edm::LogInfo("AlignmentIORootBase")  << "Read from specified iteration: " << iter;
00078       }
00079       myFile = TFile::Open(filename, "read");
00080     }
00081 
00082     myFile->cd();
00083     // set trees
00084     edm::LogInfo("AlignmentIORootBase") <<" Tree: " <<treeName(iter,treename);
00085     tree = (TTree*)myFile->Get(treeName(iter,treename));
00086     if (tree==NULL) {
00087       edm::LogError("AlignmentIORootBase") <<"Tree does not exist in file!";
00088       return -1;
00089     }
00090     setBranchAddresses();
00091   }
00092 
00093   return 0;
00094 }
00095 
00096 // ----------------------------------------------------------------------------
00097 // write tree and close file
00098 
00099 int AlignmentIORootBase::closeRoot(void)
00100 {
00101   if (bWrite) { //writing
00102     tree->Write();
00103   }
00104 
00105   delete myFile;
00106   myFile = 0;
00107   tree = 0; // deleted with file
00108 
00109   return 0;
00110 }
00111 
00112 // ----------------------------------------------------------------------------
00113 // returns highest existing iteration in file
00114 // if file does not exist: return -1
00115 
00116 int AlignmentIORootBase::testFile(const char* filename, const TString &tname)
00117 {
00118   FILE* testFILE;
00119   testFILE = fopen(filename,"r");
00120   if (testFILE == NULL) {
00121     return -1;
00122   } else {
00123     fclose(testFILE);
00124     int ihighest=-2;
00125     TFile *aFile = TFile::Open(filename,"read");
00126     for (int iter=0; iter<itermax; iter++) {
00127       if ((0 != (TTree*)aFile->Get(treeName(iter,tname))) 
00128           && (iter>ihighest)) ihighest=iter; 
00129     }
00130     delete aFile;
00131     return ihighest;
00132   }
00133 }
00134 
00135 // ----------------------------------------------------------------------------
00136 // create tree name from stub+iteration
00137 
00138 TString AlignmentIORootBase::treeName(int iter, const TString &tname)
00139 {
00140   return TString(tname + Form("_%i",iter));
00141 }