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 **************************************************************************/
20 //-----------------------------------------------------------------------------
21 // Class AliMpDataProcessor
22 // -----------------------
23 // Class for converting ASCII data files in the map of string
24 // Author: Ivana Hrivnacova, IPN Orsay
25 //-----------------------------------------------------------------------------
27 #include "AliMpDataProcessor.h"
28 #include "AliMpDataMap.h"
29 #include "AliMpFiles.h"
34 #include <TObjString.h>
36 #include <Riostream.h>
43 // static private methods
46 //_____________________________________________________________________________
47 const TString& AliMpDataProcessor::GetHeaderFileName()
49 /// Return the default name for generated header file
50 static const TString kHeaderFileName = "add.h";
51 return kHeaderFileName;
54 //_____________________________________________________________________________
55 const TString& AliMpDataProcessor::GetImplFileName()
57 /// Return the default name for generated implementation file
58 static const TString kImplFileName = "add.cxx";
66 //_____________________________________________________________________________
67 AliMpDataProcessor::AliMpDataProcessor()
73 /// Default and standar constructor
75 fHeaderFile.open(GetHeaderFileName().Data(), std::ios::out);
76 fImplFile.open(GetImplFileName().Data(), std::ios::out);
82 //_____________________________________________________________________________
83 AliMpDataProcessor:: ~AliMpDataProcessor()
94 //_____________________________________________________________________________
95 void AliMpDataProcessor::ProcessDirectory(const TString& path,
96 AliMpDataMap* dataMap)
98 /// Recursive function to process data directory
100 AliDebugStream(2) << "ProcessDirectory " << path.Data() << endl;
102 // skip some directories
103 //if ( path.Contains("manu_serial") ) return;
104 //if ( path.Contains("stationTrigger") ) return;
107 gSystem->Exec("ls > /tmp/mpfiles.txt");
109 ifstream mpfiles("/tmp/mpfiles.txt");
112 while ( mpfiles >> fileName ) {
113 mpfilesStr += fileName.Data();
117 istringstream mpFilesStream(mpfilesStr.Data());
118 while ( mpFilesStream >> fileName ) {
119 TString filePath = path + "/" + fileName;
120 if ( gSystem->OpenDirectory(filePath.Data()) )
121 ProcessDirectory(filePath, dataMap);
123 ProcessFile(filePath, dataMap);
126 AliDebugStream(2) << "ProcessDirectory done " << path.Data() << endl;
129 //_____________________________________________________________________________
130 void AliMpDataProcessor::ProcessFile(const TString& path, AliMpDataMap* dataMap)
132 /// Dump the content of the file specified by its path
133 /// in a string and fill it in the dataMap
136 string top = AliMpFiles::GetTop().Data();
137 string fullDataPath = path.Data();
138 string dataPath = fullDataPath;
139 if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
140 dataPath.erase(0,dataPath.find('/')+1);
142 AliDebugStream(2) << "Processing file " << dataPath << endl;
145 if ( dataPath.find(".C") != std::string::npos ||
146 dataPath.find(".h") != std::string::npos ) return;
148 ifstream input(path.Data(), ios::in);
149 if ( ! input.is_open() ) {
150 cerr << "Cannot open input file " << path.Data() << endl;
158 getline(input, line);
159 if ( ! input.eof() ) {
164 while ( ! input.eof() );
166 dataMap->Add(dataPath, fileString);
167 // dataMap->Add(new TObjString(dataPath.c_str()), new TObjString(fileString.c_str()));
170 //_____________________________________________________________________________
171 void AliMpDataProcessor::GenerateFunction(const TString& path,
174 /// Generate a C++ function which defines a string with the data content
175 /// and map it to the given path in the map
177 AliDebugStream(2) << "Processing path " << path.Data() << endl;
179 // skip motif & padPos
180 //if ( dataPath.find("motif") != std::string::npos ||
181 // dataPath.find("padPos") != std::string::npos ) return;
183 // add function declaration in the header file name
184 fHeaderFile << " void AddData" << ++fCounter << "();" << endl;
186 // add function implementation in .cxx
188 << "//_____________________________________________________________________________" << endl
189 << "void AliMpDataMap::AddData" << fCounter << "()" << endl
191 << "/// Automatically generated function for data file: " << endl
192 << "/// " << path << endl
194 << " string path = \"" << path << "\";" << endl << endl;
196 GenerateFileCode(data);
200 << " fMap[path] = data;" << endl
205 //_____________________________________________________________________________
206 void AliMpDataProcessor::GenerateFileCode(const TString& data)
208 /// Dump the content of the file specified by its path as a C++ string
210 istringstream in(data.Data());
214 fImplFile << " string data = \"";
215 //for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
216 fImplFile << line << " \\n\";";
217 if ( in.eof() ) return;
222 fImplFile << endl << " data = data + \"";
223 // for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
224 fImplFile << line << " \\n\";";
227 while ( ! in.eof() );
232 //_____________________________________________________________________________
233 void AliMpDataProcessor::GenerateFill()
235 /// Generate function which calls all previously generated functions
237 // add function declaration in the header file name
238 fHeaderFile << endl << " void Fill();" << endl;
240 // add function implementation in .cxx
242 << "//_____________________________________________________________________________" << endl
243 << "void AliMpDataMap::Fill()" << endl
245 << "/// Automatically generated function for calling all generated functions " << endl
248 for ( Int_t i=1; i<=fCounter; ++i )
249 fImplFile << " AddData" << i << "();" << endl;
251 fImplFile << "}" << endl;
260 //_____________________________________________________________________________
261 AliMpDataMap* AliMpDataProcessor::CreateDataMap(const TString& dataDir)
263 /// Process data directory and map a string with the content of each file to
266 TString curDir = gSystem->pwd();
268 AliMpDataMap* dataMap = new AliMpDataMap();
270 TString dataPath = AliMpFiles::GetTop() + "/" + dataDir;
271 ProcessDirectory(dataPath, dataMap);
279 //_____________________________________________________________________________
280 Bool_t AliMpDataProcessor::GenerateData(AliMpDataMap* dataMap,
281 const TString& outputDataDir)
283 /// Generate ASCII data files in outputDataDir from dataMap
285 TString curDir = gSystem->pwd();
287 TString outputDataPath = AliMpFiles::GetTop() + "/" + outputDataDir;
288 if ( gSystem->OpenDirectory(outputDataPath.Data()) ) {
290 << "Directory " << outputDataPath.Data() << " already exists" << endl;
294 AliDebugStream(2) << "Making directory " << outputDataPath.Data() << endl;
295 gSystem->mkdir(outputDataPath.Data());
299 // std::map implementation
301 const map<string, string>& kMap = dataMap->GetMap();
302 map<string, string>::const_iterator it;
304 for ( it = kMap.begin(); it != kMap.end(); it++ ) {
305 string path = it->first;
306 string data = it->second;
309 const TMap& kMap = dataMap->GetMap();
312 while ( ( keyObj = it.Next() ) ) {
314 TString tpath = ((TObjString*)keyObj)->String();
316 TObject* dataObj = kMap.GetValue(keyObj);
319 << "Cannot find value when iterating over map." << endl;
322 TString tdata = ((TObjString*)dataObj)->String();
324 string path = tpath.Data();
325 string data = tdata.Data();
327 string::size_type slashPos = path.find_last_of("/");
328 if ( slashPos != string::npos ) {
329 string dataDir(path, 0, slashPos);
331 = outputDataPath + "/" + dataDir.c_str();
332 if ( ! gSystem->OpenDirectory(dataDirPath.Data()) ) {
333 AliDebugStream(2) << "Making directory " << dataDirPath.Data() << endl;
334 gSystem->mkdir(dataDirPath.Data(), kTRUE);
339 = outputDataPath + "/" + path.c_str();
341 ofstream out(dataPath.Data(), ios::out);
342 if ( ! out.good() ) {
344 << "Cannot open output file " << outputDataPath.Data() << endl;
357 //_____________________________________________________________________________
358 Bool_t AliMpDataProcessor::GenerateCode(AliMpDataMap* dataMap)
360 /// Generate C++ code from dataMap.
362 /// AliMpDataProcessor mp;
363 /// AliMpDataMap* dataMap = mp.CreateDataMap();
364 /// mp.GenerateCode(dataMap);
366 /// Not really used, but kept for eventual future explorations.
369 // std::map implementation
371 const map<string, string>& kMap = dataMap->GetMap();
372 map<string, string>::const_iterator it;
374 for ( it = kMap.begin(); it != kMap.end(); it++ ) {
375 string path = it->first;
376 string data = it->second;
379 const TMap& kMap = dataMap->GetMap();
382 while ( ( keyObj = it.Next() ) ) {
384 TString tpath = ((TObjString*)keyObj)->String();
386 TObject* dataObj = kMap.GetValue(keyObj);
389 << "Cannot find value when iterating over map." << endl;
392 TString tdata = ((TObjString*)dataObj)->String();
394 GenerateFunction(tpath, tdata);