]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDataStreams.cxx
PMD module
[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 // See 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
47 //______________________________________________________________________________
48 AliMpDataStreams::AliMpDataStreams(AliMpDataMap* map) 
49   : TObject(),
50     fMap(map),
51     fReadFromFiles(kTRUE)
52 {
53 /// Standard and default constructor
54
55   if ( map ) fReadFromFiles = kFALSE;
56 }
57
58 //______________________________________________________________________________
59 AliMpDataStreams::AliMpDataStreams(TRootIOCtor* /*ioCtor*/) 
60   : TObject(),
61     fMap(0),
62     fReadFromFiles()
63 {
64 /// Root IO constructor
65
66 }
67
68 //______________________________________________________________________________
69 AliMpDataStreams::~AliMpDataStreams() 
70 {
71 /// Destructor
72
73   // delete fMap;
74        // Do not delete data map as it is a CDB object 
75        // which is cached
76 }
77
78 //
79 // private methods
80 //
81
82 //______________________________________________________________________________
83 void AliMpDataStreams::CutDataPath(string& dataPath) const
84
85 /// Cut the path defined in AliMpFiles as Top() + one more directory
86
87   string top = AliMpFiles::GetTop().Data();
88   if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
89   dataPath.erase(0,dataPath.find('/')+1); 
90 }
91   
92
93
94 //
95 // public methods
96 //
97
98 //______________________________________________________________________________
99 istream& AliMpDataStreams::CreateDataStream(const TString& path) const
100 {
101 /// Return the string with data in the mapping file spcified with path.
102 /// Both full path in the file system and a short path (without 
103 /// $LICE_ROOT/mapping/data string) can be used.
104
105
106   if ( fReadFromFiles ) {                                                                
107     AliDebugStream(2) << "Opening file " << path.Data() << endl;
108     ifstream* fileBuffer = new ifstream();
109     fileBuffer->open(path.Data());
110     if ( ! fileBuffer->good() ) {       
111        AliErrorStream() 
112          << "Cannot open file " << path.Data() << endl;
113     }     
114     return *fileBuffer;
115   }
116   else {
117     AliDebugStream(2) << "Opening stream " << path.Data() << endl;
118
119     // Cut top from the path 
120     string dataPath = path.Data();
121     CutDataPath(dataPath);
122
123     istringstream* stringBuffer 
124       = new istringstream(fMap->Get(dataPath).Data());
125     return *stringBuffer;
126    }    
127 }
128
129 //______________________________________________________________________________
130 Bool_t  AliMpDataStreams::IsDataStream(const TString& path) const
131 {
132 /// Return true, if data with given path exists
133
134   if ( fReadFromFiles ) {                                                                
135     ifstream fileBuffer(path.Data());
136     return fileBuffer.good();
137   }
138   else {
139     // Cut top from the path 
140     string dataPath = path.Data();
141     CutDataPath(dataPath);
142
143     return ( fMap->Get(dataPath, kFALSE) != "" );
144   }
145 }      
146
147 //______________________________________________________________________________
148 void  AliMpDataStreams::SetReadFromFiles()
149 {
150 /// Set option to read data from files
151
152   fReadFromFiles = kTRUE;
153 }  
154
155 //______________________________________________________________________________
156 Bool_t AliMpDataStreams::GetReadFromFiles() const
157
158 /// Return the info where the data are loaded from
159
160   return fReadFromFiles; 
161 }