]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDataStreams.cxx
In mapping:
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpDataStreams.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 // $Id$
17 // $MpId: AliMpDataStreams.cxx,v 1.12 2006/05/23 13:09:54 ivana Exp $
18 // Category: basic
19
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 //-----------------------------------------------------------------------------
27
28 #include "AliMpDataStreams.h"
29 #include "AliMpDataMap.h"
30 #include "AliMpFiles.h"
31
32 #include "AliLog.h"
33
34 #include <TMap.h>
35 #include <TFile.h>
36 #include <TObjString.h>
37 #include <TString.h>
38 #include <Riostream.h>
39
40 #include <string>
41
42 /// \cond CLASSIMP
43 ClassImp(AliMpDataStreams)
44 /// \endcond
45
46 AliMpDataStreams* AliMpDataStreams::fgInstance = 0;
47
48 //
49 // static methods
50 //
51
52 //______________________________________________________________________________
53 AliMpDataStreams* AliMpDataStreams::Instance()
54 {
55 /// Return its instance
56
57   if ( ! fgInstance ) {
58     fgInstance = new AliMpDataStreams();
59   }  
60     
61   return fgInstance;
62 }    
63
64 //
65 // ctor, dtor
66 //
67
68
69 //______________________________________________________________________________
70 AliMpDataStreams::AliMpDataStreams() 
71   : TObject(),
72     fMap(0),
73     fReadFromFiles(kTRUE)
74 {
75 /// Standard and default constructor
76
77 }
78
79 //______________________________________________________________________________
80 AliMpDataStreams::~AliMpDataStreams() 
81 {
82 /// Destructor
83
84   delete fMap;
85
86   fgInstance = 0;
87 }
88
89 //
90 // public methods
91 //
92
93 /*
94 //______________________________________________________________________________
95 TString AliMpDataStreams::GetDataStream(const TString& path) const
96 {
97 /// Return the string with data in the mapping file spcified with path
98
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);
103
104   cout << "Go to get value for " << dataPath.c_str() << endl;
105   
106   // Find string object in the map
107   TObject* object = fMap->GetValue(dataPath.c_str());
108   if ( ! object ) {
109     AliErrorStream() 
110       << "Cannot find data for mapping file " << dataPath << endl;
111     return "";
112   }   
113   
114   cout << "Got: " << object << endl;   
115   
116   return ((TObjString*)object)->String();
117 }
118 */
119
120 //______________________________________________________________________________
121 istream& AliMpDataStreams::CreateDataStream(const TString& path) const
122 {
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.
126
127
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() ) {       
133        AliErrorStream() 
134          << "Cannot open file " << path.Data() << endl;
135     }     
136     return *fileBuffer;
137   }
138   else {
139     AliDebugStream(2) << "Opening stream " << path.Data() << endl;
140
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);
147
148     istringstream* stringBuffer 
149       = new istringstream(fMap->Get(dataPath).Data());
150     return *stringBuffer;
151    }    
152 }
153
154 //______________________________________________________________________________
155 Bool_t  AliMpDataStreams::IsDataStream(const TString& path) const
156 {
157 /// Return true, if data with given path exists
158
159   if ( fReadFromFiles ) {                                                                
160     ifstream fileBuffer(path.Data());
161     return fileBuffer.good();
162   }
163   else {
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);
170
171     // std::map implementation
172     // return fMap->GetMap().find(dataPath) != fMap->GetMap().end();
173     return ( fMap->Get(dataPath, kFALSE) != "" );
174   }
175 }      
176
177
178 //______________________________________________________________________________
179 void  AliMpDataStreams::SetDataMap(AliMpDataMap* map)
180 {
181 /// Set the data map and switch off readFromFiles
182
183   if ( fMap && fMap != map ) delete fMap;
184
185   fMap = map;
186   fReadFromFiles = kFALSE;
187 }  
188
189 //______________________________________________________________________________
190 void  AliMpDataStreams::SetReadFromFiles(Bool_t readFromFiles)
191 {
192 /// Set option to read data from files
193
194   if ( ! fMap && ! readFromFiles ) {
195     AliWarningStream()
196       << "Setting ignored: " << endl
197       << "Selected reading from streams, but data map has not been yet defined"
198       << endl;
199       
200     return;  
201   }     
202
203   fReadFromFiles = readFromFiles;
204 }  
205
206 //______________________________________________________________________________
207 Bool_t AliMpDataStreams::GetReadFromFiles() const
208
209 /// Return the info where the data are loaded from
210
211   return fReadFromFiles; 
212 }