Update master to aliroot
[u/mrichter/AliRoot.git] / MUON / MUONmapping / AliMpDataStreams.cxx
CommitLineData
228fd720 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
ab167304 24// See detailed description in the header file.
228fd720 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
43ClassImp(AliMpDataStreams)
44/// \endcond
45
228fd720 46
47//______________________________________________________________________________
ab167304 48AliMpDataStreams::AliMpDataStreams(AliMpDataMap* map)
49 : TObject(),
50 fMap(map),
51 fReadFromFiles(kTRUE)
228fd720 52{
ab167304 53/// Standard and default constructor
228fd720 54
ab167304 55 if ( map ) fReadFromFiles = kFALSE;
56}
228fd720 57
58//______________________________________________________________________________
ab167304 59AliMpDataStreams::AliMpDataStreams(TRootIOCtor* /*ioCtor*/)
228fd720 60 : TObject(),
61 fMap(0),
ab167304 62 fReadFromFiles()
228fd720 63{
ab167304 64/// Root IO constructor
228fd720 65
66}
67
68//______________________________________________________________________________
69AliMpDataStreams::~AliMpDataStreams()
70{
71/// Destructor
72
ab167304 73 // delete fMap;
74 // Do not delete data map as it is a CDB object
75 // which is cached
228fd720 76}
77
78//
ab167304 79// private methods
228fd720 80//
81
228fd720 82//______________________________________________________________________________
ab167304 83void AliMpDataStreams::CutDataPath(string& dataPath) const
84{
85/// Cut the path defined in AliMpFiles as Top() + one more directory
228fd720 86
228fd720 87 string top = AliMpFiles::GetTop().Data();
ab167304 88 if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
89 dataPath.erase(0,dataPath.find('/')+1);
228fd720 90}
ab167304 91
92
93
94//
95// public methods
96//
228fd720 97
98//______________________________________________________________________________
99istream& 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
ab167304 120 string dataPath = path.Data();
121 CutDataPath(dataPath);
228fd720 122
123 istringstream* stringBuffer
124 = new istringstream(fMap->Get(dataPath).Data());
125 return *stringBuffer;
126 }
127}
128
129//______________________________________________________________________________
130Bool_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
ab167304 140 string dataPath = path.Data();
141 CutDataPath(dataPath);
142
228fd720 143 return ( fMap->Get(dataPath, kFALSE) != "" );
144 }
145}
146
228fd720 147//______________________________________________________________________________
ab167304 148void AliMpDataStreams::SetReadFromFiles()
228fd720 149{
150/// Set option to read data from files
151
ab167304 152 fReadFromFiles = kTRUE;
228fd720 153}
154
155//______________________________________________________________________________
156Bool_t AliMpDataStreams::GetReadFromFiles() const
157{
158/// Return the info where the data are loaded from
159
160 return fReadFromFiles;
161}