inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_rroot_rall 00005 #define inlib_rroot_rall 00006 00007 #include "streamers" 00008 #include "fac" 00009 #include "tree" 00010 00011 namespace inlib { 00012 namespace rroot { 00013 00014 inline bool read_key(std::ostream& a_out, 00015 inlib::rroot::key& a_key, 00016 bool a_dump){ 00017 unsigned int sz; 00018 char* buf = a_key.get_object_buffer(sz); //we don't have ownership of buf. 00019 if(!buf) { 00020 a_out << "inlib::rroot::read_key :" 00021 << " can't get data buffer of " << a_key.object_name() << "." 00022 << std::endl; 00023 return false; 00024 } 00025 00026 //a_out << "inlib::rroot::read_key :" 00027 // << " get data buffer size " << sz << "." 00028 // << std::endl; 00029 00030 inlib::rroot::buffer b(a_out,a_key.file().byte_swap(), 00031 sz,buf,a_key.key_length(),false); 00032 00033 if(a_key.object_class()==inlib::rroot::TH1F_cls()) { 00034 inlib::histo::h1d* h = inlib::rroot::TH1F_stream(b); 00035 if(!h) { 00036 a_out << "inlib::rroot::read_key :" 00037 << " TH1F streaming failed" 00038 << std::endl; 00039 } else { 00040 if(a_dump) h->hprint(a_out); 00041 } 00042 delete h; 00043 00044 } else if(a_key.object_class()==inlib::rroot::TH1D_cls()) { 00045 inlib::histo::h1d* h = inlib::rroot::TH1D_stream(b); 00046 if(!h) { 00047 a_out << "inlib::rroot::read_key :" 00048 << " TH1D streaming failed" 00049 << std::endl; 00050 } else { 00051 if(a_dump) h->hprint(a_out); 00052 } 00053 delete h; 00054 00055 } else if(a_key.object_class()==inlib::rroot::TH2F_cls()) { 00056 inlib::histo::h2d* h = inlib::rroot::TH2F_stream(b); 00057 if(!h) { 00058 a_out << "inlib::rroot::read_key :" 00059 << " TH2F streaming failed" 00060 << std::endl; 00061 } else { 00062 if(a_dump) h->hprint(a_out); 00063 } 00064 delete h; 00065 00066 } else if(a_key.object_class()==inlib::rroot::TH2D_cls()) { 00067 inlib::histo::h2d* h = 00068 inlib::rroot::TH2D_stream(b); //we get ownership of h. 00069 if(!h) { 00070 a_out << "inlib::rroot::read_key :" 00071 << " TH2D streaming failed" 00072 << std::endl; 00073 } else { 00074 if(a_dump) h->hprint(a_out); 00075 } 00076 delete h; 00077 00078 } else if(a_key.object_class()==inlib::rroot::TProfile_cls()) { 00079 inlib::histo::p1d* p = inlib::rroot::TProfile_stream(b); 00080 if(!p) { 00081 a_out << "inlib::rroot::read_key :" 00082 << " TProfile streaming failed" 00083 << std::endl; 00084 } else { 00085 if(a_dump) p->hprint(a_out); 00086 } 00087 delete p; 00088 00089 } else if(a_key.object_class()==TTree_cls()) { 00090 00091 inlib::rroot::fac fac(a_key.file()); 00092 inlib::rroot::tree tree(a_key.file(),fac); 00093 if(!tree.stream(b)) { 00094 a_out << "inlib::rroot::read_key :" 00095 << " TTree streaming failed" 00096 << std::endl; 00097 } else { 00098 //tree->dump(a_out); 00099 if(a_dump) { 00100 tree.dump(a_out,""," "); 00101 00102 uint32 entries = tree.entries(); 00103 00104 /* 00105 {for(uint32 j=0;j<10;j++){ //to test memory. 00106 for(uint32 i=0;i<entries;i++){ 00107 uint32 n; 00108 if(!tree.find_entry(i,n)) { 00109 a_out << " can't find entry " << i 00110 << std::endl; 00111 } 00112 } 00113 }} 00114 */ 00115 00116 for(uint32 i=0;i<5;i++){ 00117 if(!tree.show(a_out,i)) { 00118 a_out << " show failed for entry " << i 00119 << std::endl; 00120 } 00121 } 00122 for(uint32 i=mx<uint32>(5,entries-5);i<entries;i++){ 00123 if(!tree.show(a_out,i)) { 00124 a_out << " show failed for entry " << i 00125 << std::endl; 00126 } 00127 } 00128 00129 } 00130 } 00131 00132 } else if(a_key.object_class()==inlib::rroot::TDirectory::store_class()) { 00133 00134 //we should not pass here. 00135 00136 } else { 00137 a_out << "inlib::rroot::read_key :" 00138 << " dont't know how to read key with object class " 00139 << inlib::sout(a_key.object_class()) 00140 << std::endl; 00141 } 00142 return true; 00143 } 00144 00145 inline void read(std::ostream& a_out, 00146 inlib::rroot::ifile& a_file, 00147 const std::vector<inlib::rroot::key*>& a_keys, 00148 bool a_recursive, 00149 bool a_ls, 00150 bool a_dump, 00151 unsigned int a_spaces) { 00152 00153 {std::vector<inlib::rroot::key*>::const_iterator it; 00154 for(it=a_keys.begin();it!=a_keys.end();++it) { 00155 inlib::rroot::key& k = *(*it); 00156 if(k.object_class()!=inlib::rroot::TDirectory::store_class()) { 00157 if(a_ls||a_dump) { 00158 {for(unsigned index=0;index<a_spaces;index++) a_out << " ";} 00159 std::string label = k.object_name(); 00160 a_out << "object : " << sout(label) 00161 << ", class : " << k.object_class() 00162 << std::endl; 00163 //k.dump(a_out); 00164 } 00165 if(!read_key(a_out,k,a_dump)) return; 00166 } 00167 }} 00168 00169 {std::vector<inlib::rroot::key*>::const_iterator it; 00170 for(it=a_keys.begin();it!=a_keys.end();++it) { 00171 inlib::rroot::key& k = *(*it); 00172 if(k.object_class()==inlib::rroot::TDirectory::store_class()) { 00173 00174 if(a_ls||a_dump) { 00175 {for(unsigned index=0;index<a_spaces;index++) a_out << " ";} 00176 std::string label = k.object_name(); 00177 a_out << "directory : " << label << std::endl; 00178 } 00179 00180 if(!a_recursive) continue; 00181 00182 inlib::uint32 sz; 00183 char* buf = k.get_object_buffer(sz); 00184 if(!buf) { 00185 a_out << "read :" 00186 << " can't get directory data buffer." 00187 << std::endl; 00188 } else { 00189 inlib::rroot::buffer b(a_out,a_file.byte_swap(), 00190 sz,buf,k.key_length(),false); 00191 inlib::rroot::TDirectory dir(a_file); 00192 if(!dir.stream(b)) { 00193 a_out << "read :" 00194 << " can't stream TDirectory." 00195 << std::endl; 00196 } else { 00197 const std::vector<inlib::rroot::key*>& keys = dir.keys(); 00198 read(a_out,a_file,keys,a_recursive,a_ls,a_dump,a_spaces+1); 00199 } 00200 } 00201 } 00202 }} 00203 } 00204 00205 }} 00206 00207 #endif