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>
42 const TString AliMpDataProcessor::fgkHeaderFileName = "add.h";
43 const TString AliMpDataProcessor::fgkImplFileName = "add.cxx";
46 //_____________________________________________________________________________
47 AliMpDataProcessor::AliMpDataProcessor()
53 /// Default and standar constructor
55 fHeaderFile.open(fgkHeaderFileName.Data(), std::ios::out);
56 fImplFile.open(fgkImplFileName.Data(), std::ios::out);
62 //_____________________________________________________________________________
63 AliMpDataProcessor:: ~AliMpDataProcessor()
74 //_____________________________________________________________________________
75 void AliMpDataProcessor::ProcessDirectory(const TString& path,
76 AliMpDataMap* dataMap)
78 /// Recursive function to process data directory
80 AliDebugStream(2) << "ProcessDirectory " << path.Data() << endl;
82 // skip some directories
83 //if ( path.Contains("manu_serial") ) return;
84 //if ( path.Contains("stationTrigger") ) return;
87 gSystem->Exec("ls > /tmp/mpfiles.txt");
89 ifstream mpfiles("/tmp/mpfiles.txt");
92 while ( mpfiles >> fileName ) {
93 mpfilesStr += fileName.Data();
97 istringstream mpFilesStream(mpfilesStr.Data());
98 while ( mpFilesStream >> fileName ) {
99 TString filePath = path + "/" + fileName;
100 if ( gSystem->OpenDirectory(filePath.Data()) )
101 ProcessDirectory(filePath, dataMap);
103 ProcessFile(filePath, dataMap);
106 AliDebugStream(2) << "ProcessDirectory done " << path.Data() << endl;
109 //_____________________________________________________________________________
110 void AliMpDataProcessor::ProcessFile(const TString& path, AliMpDataMap* dataMap)
112 /// Dump the content of the file specified by its path
113 /// in a string and fill it in the dataMap
116 string top = AliMpFiles::GetTop().Data();
117 string fullDataPath = path.Data();
118 string dataPath = string(fullDataPath, top.size()+6);
120 AliDebugStream(2) << "Processing file " << dataPath << endl;
123 if ( dataPath.find(".C") != std::string::npos ||
124 dataPath.find(".h") != std::string::npos ) return;
126 ifstream input(path.Data(), ios::in);
127 if ( ! input.is_open() ) {
128 cerr << "Cannot open input file " << path.Data() << endl;
136 getline(input, line);
137 if ( ! input.eof() ) {
142 while ( ! input.eof() );
144 dataMap->Add(dataPath, fileString);
145 // dataMap->Add(new TObjString(dataPath.c_str()), new TObjString(fileString.c_str()));
148 //_____________________________________________________________________________
149 void AliMpDataProcessor::GenerateFunction(const TString& path,
152 /// Generate a C++ function which defines a string with the data content
153 /// and map it to the given path in the map
155 AliDebugStream(2) << "Processing path " << path.Data() << endl;
157 // skip motif & padPos
158 //if ( dataPath.find("motif") != std::string::npos ||
159 // dataPath.find("padPos") != std::string::npos ) return;
161 // add function declaration in the header file name
162 fHeaderFile << " void AddData" << ++fCounter << "();" << endl;
164 // add function implementation in .cxx
166 << "//_____________________________________________________________________________" << endl
167 << "void AliMpDataMap::AddData" << fCounter << "()" << endl
169 << "/// Automatically generated function for data file: " << endl
170 << "/// " << path << endl
172 << " string path = \"" << path << "\";" << endl << endl;
174 GenerateFileCode(data);
178 << " fMap[path] = data;" << endl
183 //_____________________________________________________________________________
184 void AliMpDataProcessor::GenerateFileCode(const TString& data)
186 /// Dump the content of the file specified by its path as a C++ string
188 istringstream in(data.Data());
192 fImplFile << " string data = \"";
193 //for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
194 fImplFile << line << " \\n\";";
195 if ( in.eof() ) return;
200 fImplFile << endl << " data = data + \"";
201 // for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
202 fImplFile << line << " \\n\";";
205 while ( ! in.eof() );
210 //_____________________________________________________________________________
211 void AliMpDataProcessor::GenerateFill()
213 /// Generate function which calls all previously generated functions
215 // add function declaration in the header file name
216 fHeaderFile << endl << " void Fill();" << endl;
218 // add function implementation in .cxx
220 << "//_____________________________________________________________________________" << endl
221 << "void AliMpDataMap::Fill()" << endl
223 << "/// Automatically generated function for calling all generated functions " << endl
226 for ( Int_t i=1; i<=fCounter; ++i )
227 fImplFile << " AddData" << i << "();" << endl;
229 fImplFile << "}" << endl;
238 //_____________________________________________________________________________
239 AliMpDataMap* AliMpDataProcessor::CreateDataMap(const TString& dataDir)
241 /// Process data directory and map a string with the content of each file to
244 TString curDir = gSystem->pwd();
246 AliMpDataMap* dataMap = new AliMpDataMap();
248 TString dataPath = AliMpFiles::GetTop() + "/" + dataDir;
249 ProcessDirectory(dataPath, dataMap);
257 //_____________________________________________________________________________
258 Bool_t AliMpDataProcessor::GenerateData(AliMpDataMap* dataMap,
259 const TString& outputDataDir)
261 /// Generate ASCII data files in outputDataDir from dataMap
263 TString curDir = gSystem->pwd();
265 TString outputDataPath = AliMpFiles::GetTop() + "/" + outputDataDir;
266 if ( gSystem->OpenDirectory(outputDataPath.Data()) ) {
268 << "Directory " << outputDataPath.Data() << " already exists" << endl;
272 AliDebugStream(2) << "Making directory " << outputDataPath.Data() << endl;
273 gSystem->mkdir(outputDataPath.Data());
277 // std::map implementation
279 const map<string, string>& kMap = dataMap->GetMap();
280 map<string, string>::const_iterator it;
282 for ( it = kMap.begin(); it != kMap.end(); it++ ) {
283 string path = it->first;
284 string data = it->second;
287 const TMap& kMap = dataMap->GetMap();
290 while ( ( keyObj = it.Next() ) ) {
292 TString tpath = ((TObjString*)keyObj)->String();
294 TObject* dataObj = kMap.GetValue(keyObj);
297 << "Cannot find value when iterating over map." << endl;
300 TString tdata = ((TObjString*)dataObj)->String();
302 string path = tpath.Data();
303 string data = tdata.Data();
305 if ( path.find_last_of("/") != string::npos ) {
306 string dataDir(path, 0, path.find_last_of("/"));
308 = outputDataPath + "/" + dataDir.c_str();
309 if ( ! gSystem->OpenDirectory(dataDirPath.Data()) ) {
310 AliDebugStream(2) << "Making directory " << dataDirPath.Data() << endl;
311 gSystem->mkdir(dataDirPath.Data(), kTRUE);
316 = outputDataPath + "/" + path.c_str();
318 ofstream out(dataPath.Data(), ios::out);
319 if ( ! out.good() ) {
321 << "Cannot open output file " << outputDataPath.Data() << endl;
334 //_____________________________________________________________________________
335 Bool_t AliMpDataProcessor::GenerateCode(AliMpDataMap* dataMap)
337 /// Generate C++ code from dataMap.
339 /// AliMpDataProcessor mp;
340 /// AliMpDataMap* dataMap = mp.CreateDataMap();
341 /// mp.GenerateCode(dataMap);
343 /// Not really used, but kept for eventual future explorations.
346 // std::map implementation
348 const map<string, string>& kMap = dataMap->GetMap();
349 map<string, string>::const_iterator it;
351 for ( it = kMap.begin(); it != kMap.end(); it++ ) {
352 string path = it->first;
353 string data = it->second;
356 const TMap& kMap = dataMap->GetMap();
359 while ( ( keyObj = it.Next() ) ) {
361 TString tpath = ((TObjString*)keyObj)->String();
363 TObject* dataObj = kMap.GetValue(keyObj);
366 << "Cannot find value when iterating over map." << endl;
369 TString tdata = ((TObjString*)dataObj)->String();
371 GenerateFunction(tpath, tdata);