1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 // $MpId: AliMpDataStreams.cxx,v 1.12 2006/05/23 13:09:54 ivana Exp $
20 //-----------------------------------------------------------------------------
21 // Class AliMpDataStreams
22 // ----------------------
23 // Class for providing mapping data streams
24 // Se detailed description in the header file.
25 // Author: Ivana Hrivnacova; IPN Orsay
26 //-----------------------------------------------------------------------------
28 #include "AliMpDataStreams.h"
29 #include "AliMpDataMap.h"
30 #include "AliMpFiles.h"
36 #include <TObjString.h>
38 #include <Riostream.h>
43 ClassImp(AliMpDataStreams)
46 AliMpDataStreams* AliMpDataStreams::fgInstance = 0;
52 //______________________________________________________________________________
53 AliMpDataStreams* AliMpDataStreams::Instance()
55 /// Return its instance
58 fgInstance = new AliMpDataStreams();
69 //______________________________________________________________________________
70 AliMpDataStreams::AliMpDataStreams()
75 /// Standard and default constructor
79 //______________________________________________________________________________
80 AliMpDataStreams::~AliMpDataStreams()
94 //______________________________________________________________________________
95 TString AliMpDataStreams::GetDataStream(const TString& path) const
97 /// Return the string with data in the mapping file spcified with path
99 // Cut top from the path
100 string top = AliMpFiles::GetTop().Data();
101 string fullDataPath = path.Data();
102 string dataPath = string(fullDataPath, top.size()+1);
104 cout << "Go to get value for " << dataPath.c_str() << endl;
106 // Find string object in the map
107 TObject* object = fMap->GetValue(dataPath.c_str());
110 << "Cannot find data for mapping file " << dataPath << endl;
114 cout << "Got: " << object << endl;
116 return ((TObjString*)object)->String();
120 //______________________________________________________________________________
121 istream& AliMpDataStreams::CreateDataStream(const TString& path) const
123 /// Return the string with data in the mapping file spcified with path.
124 /// Both full path in the file system and a short path (without
125 /// $LICE_ROOT/mapping/data string) can be used.
128 if ( fReadFromFiles ) {
129 AliDebugStream(2) << "Opening file " << path.Data() << endl;
130 ifstream* fileBuffer = new ifstream();
131 fileBuffer->open(path.Data());
132 if ( ! fileBuffer->good() ) {
134 << "Cannot open file " << path.Data() << endl;
139 AliDebugStream(2) << "Opening stream " << path.Data() << endl;
141 // Cut top from the path
142 string top = AliMpFiles::GetTop().Data();
143 string fullDataPath = path.Data();
144 string dataPath = fullDataPath;
145 if ( dataPath.find(top) != string::npos )
146 dataPath.erase(0, top.size()+6);
148 istringstream* stringBuffer
149 = new istringstream(fMap->Get(dataPath).Data());
150 return *stringBuffer;
154 //______________________________________________________________________________
155 Bool_t AliMpDataStreams::IsDataStream(const TString& path) const
157 /// Return true, if data with given path exists
159 if ( fReadFromFiles ) {
160 ifstream fileBuffer(path.Data());
161 return fileBuffer.good();
164 // Cut top from the path
165 string top = AliMpFiles::GetTop().Data();
166 string fullDataPath = path.Data();
167 string dataPath = fullDataPath;
168 if ( dataPath.find(top) != string::npos )
169 dataPath.erase(0, top.size()+6);
171 // std::map implementation
172 // return fMap->GetMap().find(dataPath) != fMap->GetMap().end();
173 return ( fMap->Get(dataPath, kFALSE) != "" );
178 //______________________________________________________________________________
179 void AliMpDataStreams::SetDataMap(AliMpDataMap* map)
181 /// Set the data map and switch off readFromFiles
183 if ( fMap && fMap != map ) delete fMap;
186 fReadFromFiles = kFALSE;
189 //______________________________________________________________________________
190 void AliMpDataStreams::SetReadFromFiles(Bool_t readFromFiles)
192 /// Set option to read data from files
194 if ( ! fMap && ! readFromFiles ) {
196 << "Setting ignored: " << endl
197 << "Selected reading from streams, but data map has not been yet defined"
203 fReadFromFiles = readFromFiles;
206 //______________________________________________________________________________
207 Bool_t AliMpDataStreams::GetReadFromFiles() const
209 /// Return the info where the data are loaded from
211 return fReadFromFiles;