Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

FillData.cxx

Go to the documentation of this file.
00001 // $Id: FillData.cxx,v 1.2 2007/12/01 22:56:20 rustem Exp $
00002 
00003 // C/C++
00004 #include <iomanip>
00005 #include <sstream>
00006 
00007 // ROOT
00008 #include "TFile.h"
00009 #include "TTree.h"
00010 
00011 // Local
00012 #include "PhysicsNtuple/Default.h"
00013 #include "PhysicsNtuple/Factory.h"
00014 #include "FillData.h"
00015 
00016 REGISTER_ANP_OBJECT(AlgSnarl,FillData)
00017 
00018 using namespace std;
00019 
00020 //----------------------------------------------------------------------
00021 Anp::FillData::FillData()
00022    :fDir(0),
00023     fFile(0),
00024     fDirName("fill")
00025 {   
00026 }
00027 
00028 //----------------------------------------------------------------------
00029 Anp::FillData::~FillData()
00030 {
00031 }
00032 
00033 //----------------------------------------------------------------------
00034 bool Anp::FillData::Run(Record &record)
00035 {
00036    if(!fDir)
00037    {
00038       return true;
00039    }
00040 
00041    for(EventIter ievent = record.EventBeg(); ievent != record.EventEnd(); ++ievent)
00042    {
00043       Fill(ievent -> DataBeg(), ievent -> DataEnd(), "event");
00044    }
00045    for(TrackIter itrack = record.TrackBeg(); itrack != record.TrackEnd(); ++itrack)
00046    {
00047       Fill(itrack -> DataBeg(), itrack -> DataEnd(), "track");
00048    }  
00049 
00050    return true;
00051 }
00052 
00053 //----------------------------------------------------------------------
00054 void Anp::FillData::Fill(DataIter ibeg, DataIter iend, const string &key)
00055 {   
00056    //
00057    // Fill (create first time) tree and branches
00058    //
00059    if(!fDir || ibeg == iend || key.empty())
00060    {
00061       return;
00062    }
00063 
00064    for(DataIter dit = ibeg; dit != iend; ++dit)
00065    {
00066       stringstream tname;
00067       tname << key << "_" << setw(5) << setfill('0') << dit -> Key();
00068 
00069       //
00070       // Find (create first time) tree 
00071       //
00072       TreeMap::iterator itree = fTree.find(tname.str());
00073       if(itree == fTree.end())
00074       {
00075          itree = fTree.insert(TreeMap::value_type(tname.str(), FillData::Tree())).first;
00076 
00077          Tree &tree = itree -> second;
00078          tree.tree_ptr = new TTree(tname.str().c_str(), tname.str().c_str());
00079          tree.tree_ptr -> SetDirectory(fDir);
00080          tree.branch_ptr = tree.tree_ptr -> Branch("data", &tree.data, "data/F");       
00081       }
00082   
00083       (itree -> second).data = dit -> Data();
00084       (itree -> second).tree_ptr -> Fill();
00085    }
00086 }
00087 
00088 //----------------------------------------------------------------------
00089 void Anp::FillData::Set(TDirectory *dir)
00090 {
00091    //
00092    // If not ROOT file exist, then use outside TDirectory pointer if it is valid
00093    //
00094    if(!fDir)
00095    {
00096       fDir = Anp::GetDir(fDirName, dir);
00097    }
00098 }
00099 
00100 //----------------------------------------------------------------------
00101 void Anp::FillData::Config(const Registry &reg)
00102 {
00103    //
00104    // create an output file if a path is given
00105    //
00106 
00107    const char *value_char = 0;
00108    if(reg.Get("FillDataDirName", value_char) && value_char)
00109    {
00110       fDirName = value_char;
00111    }
00112 
00113    const char *path_char = 0;
00114    if(reg.Get("FillDataPath", path_char) && path_char)
00115    {
00116       fFile = new TFile(path_char, "RECREATE");   
00117       if(!fFile || !fFile -> IsOpen())
00118       {
00119          cerr << "FillData::Config - failed to create ROOT file:\n   " << path_char << endl;
00120          fFile = 0;
00121       }
00122       else
00123       {
00124          cout << "FillData::Config - recreated ROOT file:\n   " << path_char << endl;
00125       }
00126    
00127       fDir = Anp::GetDir(fDirName, fFile);
00128    }
00129 
00130    if(reg.KeyExists("PrintConfig"))
00131    {
00132       cout << "FillData::Config" << endl
00133            << "   DirName = " << fDirName << endl;
00134       
00135       if(path_char)
00136       {
00137          cout << "   Path = " << path_char << endl;
00138       }
00139    }
00140 }
00141 
00142 //----------------------------------------------------------------------
00143 void Anp::FillData::End(const DataBlock &)
00144 {
00145    if(fFile)
00146    {
00147       fFile -> Write();
00148       fFile -> Close();
00149    }
00150 }

Generated on Mon Feb 15 11:06:41 2010 for loon by  doxygen 1.3.9.1