CMS 3D CMS Logo

SprRootWriter.cc

Go to the documentation of this file.
00001 //$Id: SprRootWriter.cc,v 1.3 2007/10/12 19:25:01 narsky Exp $
00002 
00003 #include "PhysicsTools/StatPatternRecognition/interface/SprExperiment.hh"
00004 #include "PhysicsTools/StatPatternRecognition/interface/SprRootWriter.hh"
00005 #include <TFile.h>
00006 
00007 #include <stdlib.h>
00008 #include <iostream>
00009 #include <errno.h>
00010 #include <sys/types.h>
00011 #include <sys/stat.h>
00012 #include <stdio.h>
00013 #include <algorithm>
00014 
00015 using namespace std;
00016 
00017 
00018 SprRootWriter::~SprRootWriter() 
00019 {
00020   delete [] data_;
00021   delete tuple_;
00022 }
00023 
00024 
00025 bool SprRootWriter::init(const char* filename)
00026 {
00027   // init
00028   fname_ = filename;
00029   string cmd;
00030 
00031   // check if file exists, delete and issue a warning
00032   struct stat buf;
00033   if( stat(fname_.c_str(),&buf) == 0 ) {
00034     cerr << "Warning: file " << fname_.c_str() << " will be deleted." << endl;
00035     cmd = "rm -f ";
00036     cmd += fname_.c_str();
00037     if( system(cmd.c_str()) != 0 ) {
00038       cerr << "Attempt to delete file " << fname_.c_str() 
00039            << " terminated with error " << errno << endl;
00040       return false;
00041     }
00042   }
00043 
00044   // exit
00045   return true;
00046 }
00047 
00048 
00049 int SprRootWriter::SetBranches()
00050 {
00051   // initial check
00052   if(setBranches_) {
00053     cerr <<"DANGER - already initialized - branch structure in danger - ABORT"
00054          <<endl;
00055     abort();
00056   }
00057   if( data_ != 0 ) {
00058     cerr << "Root data has been already filled - abort."<< endl;
00059     abort();
00060   }
00061 
00062   // make data pointer
00063   int size = axes_.size()+3;
00064   data_ = new Float_t[size];
00065 
00066   // make axis names
00067   TString values = "index/F:classification/F:weight/F";
00068   for(int i = 0; i < axes_.size(); i++) {
00069     values += ":";
00070     string temp = axes_[i];
00071     replace(temp.begin(),temp.end(),'/','_');
00072     values += temp.c_str();
00073     values += "/F";
00074   }
00075 
00076   // book tree
00077   if( tuple_ != 0 ) {
00078     cerr << "Root tree has been already booked - abort." << endl;
00079     abort();
00080   }
00081   tuple_ = new TTree("ClassRecord", "Classification Filling Information");
00082   tuple_->Branch("Vars", data_, values);
00083 
00084   // exit
00085   setBranches_ = true;
00086   return 1;
00087 }
00088 
00089 
00090 bool SprRootWriter::write(int cls, unsigned index, double weight,
00091                           const std::vector<double>& v, 
00092                           const std::vector<double>& f)
00093 {
00094   if(!setBranches_)
00095     SetBranches();
00096 
00097   // check vector sizes
00098   int check = v.size() + f.size();
00099   if(check != axes_.size()){
00100     cerr << "Dimensionality of input vector unequal to dimensionality " 
00101          << "of tuple: " << v.size() << " " << f.size()
00102          << " " << axes_.size() << endl;
00103     return false;
00104   }
00105 
00106   // fill data array  
00107   data_[0] = index;
00108   data_[1] = cls;
00109   data_[2] = weight;
00110   for(int i = 0; i < v.size(); i++)
00111       data_[i+3] = (Float_t) v[i];
00112   for(int i = 0; i < f.size(); i++)
00113     data_[i+3+v.size()] = (Float_t) f[i];
00114 
00115   // fill and save the root tree
00116   tuple_->Fill();
00117   if(index%1000 == 0) tuple_->AutoSave("SaveSelf");
00118 
00119   // exit
00120   return true;
00121 }
00122 
00123 
00124 bool SprRootWriter::close()
00125 {
00126   TFile file(fname_.c_str(), "recreate");
00127   file.cd();
00128   tuple_->Write();
00129   return true;
00130 }
00131 
00132 

Generated on Tue Jun 9 17:42:03 2009 for CMSSW by  doxygen 1.5.4