00001
00002
00003 #include "emcDataManager.h"
00004 #include "emcManageable.h"
00005 #include "emcObjectManager.h"
00006 #include "emcObjectManagerRegister.h"
00007 #include <iostream>
00008 #include <cstdlib>
00009
00023 int emcDataManager::fVerboseLevel = 0 ;
00024
00025 using namespace std;
00026
00027
00028 emcDataManager::emcDataManager()
00029 {
00030 char* dir = getenv("EMCAL_DATA_COMMON");
00031 if (dir)
00032 {
00033 SetDestinationDir(dir);
00034 SetSourceDir(dir);
00035 }
00036 else
00037 {
00038 fDestinationDir = "";
00039 fSourceDir = "";
00040 }
00041 }
00042
00043
00044 emcDataManager* emcDataManager::GetInstance()
00045 {
00046 static emcDataManager* _instance = new emcDataManager() ;
00047 return _instance ;
00048 }
00049
00050
00051 emcManageable* emcDataManager::Collect(const emcManageable& object,
00052 const PHTimeStamp& when)
00053 {
00054
00055
00056
00057 emcManageable* value = 0 ;
00058
00059 emcObjectManagerMap cmap =
00060 emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00061 emcObjectManagerMap::const_iterator it ;
00062
00063 if ( cmap.empty() )
00064 {
00065 cout << "<I> I cannot handle any object type. Sorry." << endl ;
00066 }
00067 else
00068 {
00069 for ( it = cmap.begin() ; it != cmap.end() && value == 0 ; it++)
00070 {
00071 emcObjectManagerMap::value_type entry = *it ;
00072 if ( entry.second->CanCollect(object) )
00073 {
00074 value = entry.second->Collect(object, when) ;
00075 }
00076 }
00077 }
00078 return value ;
00079 }
00080
00081
00082 void emcDataManager::ls(const char*)
00083 {
00084 cout << (*this) ;
00085 }
00086
00087
00088 bool emcDataManager::Read(emcManageable& object,
00089 const PHTimeStamp& time_stamp,
00090 int bankID)
00091 {
00092
00093
00094
00095 bool kIsRead = false ;
00096 bool kFoundPlugin = false ;
00097
00098 emcObjectManagerMap cmap =
00099 emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00100 emcObjectManagerMap::const_iterator it ;
00101
00102 if ( cmap.empty() )
00103 {
00104 cout << "<I> I cannot handle any object type. Sorry." << endl ;
00105 }
00106 else
00107 {
00108 for ( it = cmap.begin() ; it != cmap.end() && !kIsRead ; it++)
00109 {
00110 emcObjectManagerMap::value_type entry = *it ;
00111 if ( GetVerboseLevel() >= 10 )
00112 {
00113 cout << "emcDataManager::Read : trying plugin "
00114 << entry.second->GetName()
00115 << ":"
00116 << entry.second->GetTitle()
00117 << endl;
00118 }
00119
00120 if ( entry.second->CanRead(object) )
00121 {
00122 kFoundPlugin = true ;
00123 kIsRead = entry.second->Read(object, time_stamp, bankID) ;
00124 }
00125 }
00126 }
00127
00128 if (!kFoundPlugin)
00129 {
00130 cerr << "<EMC-ERROR-from-DataManager> " << "Read : I did not find "
00131 << "a suitable plugin for this object : name=" << object.GetName()
00132 << " title=" << object.GetTitle()
00133 << " classname=" << object.GetClassName() << endl ;
00134 cerr << "<EMC-ADVICE> a) Check the list of available plugins by : "
00135 << endl
00136 << " emcDataManager* dm = emcDataManager::GetInstance() ; " << endl
00137 << " dm->ls() ; " << endl ;
00138 cerr << "<EMC-ADVICE> b) Check that the object you'd like to read "
00139 << endl
00140 << " has the correct source set, e.g. " << endl
00141 << " cout << object.GetSource() ; "
00142 << " object.SetSource(emcManageable::kFile_ASCII) " << endl
00143 << " or object.SetSource(emcManageable::kDB_Objy) " << endl
00144 << " (note that some plugins might not support all sources)"
00145 << endl ;
00146 }
00147 return kIsRead ;
00148 }
00149
00150
00151 bool emcDataManager::Read(emcManageable& object, int runnumber)
00152 {
00153
00154
00155
00156 bool kIsRead = false ;
00157 bool kFoundPlugin = false ;
00158
00159 emcObjectManagerMap cmap =
00160 emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00161 emcObjectManagerMap::const_iterator it ;
00162
00163 if ( cmap.empty() )
00164 {
00165 cout << "<I> I cannot handle any object type. Sorry." << endl ;
00166 }
00167 else
00168 {
00169 for ( it = cmap.begin() ; it != cmap.end() && !kIsRead ; it++)
00170 {
00171 emcObjectManagerMap::value_type entry = *it ;
00172 if ( entry.second->CanRead(object) )
00173 {
00174 kFoundPlugin = true ;
00175 kIsRead = entry.second->Read(object, runnumber) ;
00176 }
00177 }
00178 }
00179 if (!kFoundPlugin)
00180 {
00181 cerr << "<EMC-ERROR-from-DataManager> " << "Read : I did not find "
00182 << "a suitable plugin for this object : name=" << object.GetName()
00183 << " title=" << object.GetTitle()
00184 << " classname=" << object.GetClassName() << endl ;
00185 cerr << "<EMC-ADVICE> a) Check the list of available plugins by : " << endl
00186 << " emcDataManager* dm = emcDataManager::GetInstance() ; " << endl
00187 << " dm->ls() ; " << endl ;
00188 cerr << "<EMC-ADVICE> b) Check that the object you'd like to read " << endl
00189 << " has the correct source set, e.g. " << endl
00190 << " cout << object.GetSource() ; object.SetSource(emcManageable::kFile_ASCII) " << endl
00191 << " or object.SetSource(emcManageable::kDB_Objy) " << endl
00192 << " (note that some plugins might not support all sources)" << endl ;
00193 }
00194 return kIsRead ;
00195 }
00196
00197
00198 bool emcDataManager::ReadPreviousVersion(emcManageable& object,
00199 const PHTimeStamp& time_stamp,
00200 int bankID,
00201 int version)
00202 {
00203
00204
00205
00206 bool kIsRead = false ;
00207 bool kFoundPlugin = false ;
00208
00209 emcObjectManagerMap cmap =
00210 emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00211 emcObjectManagerMap::const_iterator it ;
00212
00213 if ( cmap.empty() )
00214 {
00215 cout << "<I> I cannot handle any object type. Sorry." << endl ;
00216 }
00217 else
00218 {
00219 for ( it = cmap.begin() ; it != cmap.end() && !kIsRead ; it++)
00220 {
00221 emcObjectManagerMap::value_type entry = *it ;
00222 if ( entry.second->CanRead(object) )
00223 {
00224 kFoundPlugin = true ;
00225 kIsRead = entry.second->ReadPreviousVersion
00226 (object, time_stamp, bankID, version) ;
00227 }
00228 }
00229 }
00230 if (!kFoundPlugin)
00231 {
00232 cerr << "<EMC-ERROR-from-DataManager> "
00233 << "ReadPreviousVersion : I did not find "
00234 << "a suitable plugin for this object : name=" << object.GetName()
00235 << " title=" << object.GetTitle()
00236 << " classname=" << object.GetClassName() << endl ;
00237 cerr << "<EMC-ADVICE> a) Check the list of available plugins by : " << endl
00238 << " emcDataManager* dm = emcDataManager::GetInstance() ; " << endl
00239 << " dm->ls() ; " << endl ;
00240 cerr << "<EMC-ADVICE> b) Check that the object you'd like to read " << endl
00241 << " has the correct source set, e.g. " << endl
00242 << " cout << object.GetSource() ; object.SetSource(emcManageable::kFile_ASCII) " << endl
00243 << " or object.SetSource(emcManageable::kDB_Objy) " << endl
00244 << " (note that some plugins might not support all sources)" << endl ;
00245 }
00246 return kIsRead ;
00247 }
00248
00249
00250 void emcDataManager::Reset(void)
00251 {
00252
00253
00254
00255 emcObjectManagerMap cmap =
00256 emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00257 emcObjectManagerMap::const_iterator it ;
00258
00259 for ( it = cmap.begin() ; it != cmap.end() ; it++)
00260 {
00261 emcObjectManagerMap::value_type entry = *it ;
00262 entry.second->Reset() ;
00263 }
00264 }
00265
00266
00267 bool emcDataManager::Write(const emcManageable& object,
00268 const PHTimeStamp& tStart,
00269 int bankID)
00270 {
00271
00272
00273
00274
00275 emcObjectManagerMap cmap =
00276 emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00277
00278 bool written = false ;
00279
00280 emcObjectManagerMap::const_iterator it ;
00281
00282 if ( cmap.empty() )
00283 {
00284 cout << "<I> I cannot handle any object type. Sorry." << endl ;
00285 }
00286 else
00287 {
00288 for ( it = cmap.begin() ; it != cmap.end() && !written ; it++)
00289 {
00290 emcObjectManagerMap::value_type entry = *it ;
00291 if ( entry.second->CanWrite(object) )
00292 {
00293 written = entry.second->Write(object, tStart, bankID) ;
00294 }
00295 }
00296 }
00297 return written ;
00298 }
00299
00300
00301 ostream& operator << (ostream& out, const emcDataManager&)
00302 {
00303 emcObjectManagerMap cmap = emcObjectManagerRegister::GetInstance()->GetObjectManagerMap() ;
00304 emcObjectManagerMap::const_iterator it ;
00305
00306 if ( cmap.empty() )
00307 {
00308 out << "<I> I cannot handle any object type. Sorry." << endl ;
00309 }
00310 else
00311 {
00312 out << "<I> Here are my plug-ins to handle objects : " << endl ;
00313 for ( it = cmap.begin() ; it != cmap.end() ; it++)
00314 {
00315 emcObjectManagerMap::value_type entry = *it ;
00316 out << " -> " << entry.second->GetName()
00317 << " " << entry.second->GetTitle() << endl ;
00318 }
00319 }
00320 return out ;
00321 }
00322
00323
00324