Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:39

0001 // this class's header
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentIORootBase.h"
0005 
0006 #include "TFile.h"
0007 #include "TTree.h"
0008 
0009 AlignmentIORootBase::~AlignmentIORootBase() {
0010   delete myFile;  // tree is deleted automatically with file
0011 }
0012 
0013 // ----------------------------------------------------------------------------
0014 // open file/trees for write
0015 
0016 int AlignmentIORootBase::openRoot(const char* filename, int iteration, bool write) {
0017   bWrite = write;
0018   int iter;
0019 
0020   edm::LogInfo("AlignmentIORootBase") << "File: " << filename;
0021 
0022   if (bWrite) {  // writing
0023 
0024     int iterfile = testFile(filename, treename);
0025     if (iterfile == -1) {
0026       iter = iteration;
0027       edm::LogInfo("AlignmentIORootBase") << "Write to new file; first iteration: " << iter;
0028       myFile = TFile::Open(filename, "recreate");
0029     } else {
0030       if (iteration == -1) {
0031         iter = iterfile + 1;
0032         edm::LogInfo("AlignmentIORootBase") << "Write to existing file; highest iteration: " << iter;
0033       } else {
0034         if (iteration <= iterfile) {
0035           edm::LogError("AlignmentIORootBase")
0036               << "Iteration " << iteration << " invalid or already exists for tree " << treename;
0037           return -1;
0038         }
0039         iter = iteration;
0040         edm::LogInfo("AlignmentIORootBase") << "Write to new iteration: " << iter;
0041       }
0042       myFile = TFile::Open(filename, "update");
0043     }
0044 
0045     // create tree
0046     myFile->cd();
0047     edm::LogInfo("AlignmentIORootBase") << "Tree: " << treeName(iter, treename);
0048     tree = new TTree(treeName(iter, treename), treetxt);
0049     createBranches();
0050 
0051   } else {  // reading
0052 
0053     int iterfile = testFile(filename, treename);
0054     if (iterfile == -1) {
0055       edm::LogError("AlignmentIORootBase") << "File does not exist!";
0056       return -1;
0057     } else if (iterfile == -2) {
0058       edm::LogError("AlignmentIORootBase") << "Tree " << treename << " does not exist in file " << filename;
0059       return -1;
0060     } else {
0061       if (iteration == -1) {
0062         iter = iterfile;
0063         edm::LogInfo("AlignmentIORootBase") << "Read from highest iteration: " << iter;
0064       } else {
0065         if (iteration > iterfile) {
0066           edm::LogError("AlignmentIORootBase") << "Iteration " << iteration << " does not exist for tree " << treename;
0067           return -1;
0068         }
0069         iter = iteration;
0070         edm::LogInfo("AlignmentIORootBase") << "Read from specified iteration: " << iter;
0071       }
0072       myFile = TFile::Open(filename, "read");
0073     }
0074 
0075     myFile->cd();
0076     // set trees
0077     edm::LogInfo("AlignmentIORootBase") << " Tree: " << treeName(iter, treename);
0078     tree = (TTree*)myFile->Get(treeName(iter, treename));
0079     if (tree == nullptr) {
0080       edm::LogError("AlignmentIORootBase") << "Tree does not exist in file!";
0081       return -1;
0082     }
0083     setBranchAddresses();
0084   }
0085 
0086   return 0;
0087 }
0088 
0089 // ----------------------------------------------------------------------------
0090 // write tree and close file
0091 
0092 int AlignmentIORootBase::closeRoot(void) {
0093   if (bWrite) {  //writing
0094     tree->Write();
0095   }
0096 
0097   delete myFile;
0098   myFile = nullptr;
0099   tree = nullptr;  // deleted with file
0100 
0101   return 0;
0102 }
0103 
0104 // ----------------------------------------------------------------------------
0105 // returns highest existing iteration in file
0106 // if file does not exist: return -1
0107 
0108 int AlignmentIORootBase::testFile(const char* filename, const TString& tname) {
0109   FILE* testFILE;
0110   testFILE = fopen(filename, "r");
0111   if (testFILE == nullptr) {
0112     return -1;
0113   } else {
0114     fclose(testFILE);
0115     int ihighest = -2;
0116     TFile* aFile = TFile::Open(filename, "read");
0117     for (int iter = 0; iter < itermax; iter++) {
0118       if ((nullptr != (TTree*)aFile->Get(treeName(iter, tname))) && (iter > ihighest))
0119         ihighest = iter;
0120     }
0121     delete aFile;
0122     return ihighest;
0123   }
0124 }
0125 
0126 // ----------------------------------------------------------------------------
0127 // create tree name from stub+iteration
0128 
0129 TString AlignmentIORootBase::treeName(int iter, const TString& tname) { return TString(tname + Form("_%i", iter)); }