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 = fullDataPath;
119 if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
120 dataPath.erase(0,dataPath.find('/')+1);
122 AliDebugStream(2) << "Processing file " << dataPath << endl;
125 if ( dataPath.find(".C") != std::string::npos ||
126 dataPath.find(".h") != std::string::npos ) return;
128 ifstream input(path.Data(), ios::in);
129 if ( ! input.is_open() ) {
130 cerr << "Cannot open input file " << path.Data() << endl;
138 getline(input, line);
139 if ( ! input.eof() ) {
144 while ( ! input.eof() );
146 dataMap->Add(dataPath, fileString);
147 // dataMap->Add(new TObjString(dataPath.c_str()), new TObjString(fileString.c_str()));
150 //_____________________________________________________________________________
151 void AliMpDataProcessor::GenerateFunction(const TString& path,
154 /// Generate a C++ function which defines a string with the data content
155 /// and map it to the given path in the map
157 AliDebugStream(2) << "Processing path " << path.Data() << endl;
159 // skip motif & padPos
160 //if ( dataPath.find("motif") != std::string::npos ||
161 // dataPath.find("padPos") != std::string::npos ) return;
163 // add function declaration in the header file name
164 fHeaderFile << " void AddData" << ++fCounter << "();" << endl;
166 // add function implementation in .cxx
168 << "//_____________________________________________________________________________" << endl
169 << "void AliMpDataMap::AddData" << fCounter << "()" << endl
171 << "/// Automatically generated function for data file: " << endl
172 << "/// " << path << endl
174 << " string path = \"" << path << "\";" << endl << endl;
176 GenerateFileCode(data);
180 << " fMap[path] = data;" << endl
185 //_____________________________________________________________________________
186 void AliMpDataProcessor::GenerateFileCode(const TString& data)
188 /// Dump the content of the file specified by its path as a C++ string
190 istringstream in(data.Data());
194 fImplFile << " string data = \"";
195 //for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
196 fImplFile << line << " \\n\";";
197 if ( in.eof() ) return;
202 fImplFile << endl << " data = data + \"";
203 // for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
204 fImplFile << line << " \\n\";";
207 while ( ! in.eof() );
212 //_____________________________________________________________________________
213 void AliMpDataProcessor::GenerateFill()
215 /// Generate function which calls all previously generated functions
217 // add function declaration in the header file name
218 fHeaderFile << endl << " void Fill();" << endl;
220 // add function implementation in .cxx
222 << "//_____________________________________________________________________________" << endl
223 << "void AliMpDataMap::Fill()" << endl
225 << "/// Automatically generated function for calling all generated functions " << endl
228 for ( Int_t i=1; i<=fCounter; ++i )
229 fImplFile << " AddData" << i << "();" << endl;
231 fImplFile << "}" << endl;
240 //_____________________________________________________________________________
241 AliMpDataMap* AliMpDataProcessor::CreateDataMap(const TString& dataDir)
243 /// Process data directory and map a string with the content of each file to
246 TString curDir = gSystem->pwd();
248 AliMpDataMap* dataMap = new AliMpDataMap();
250 TString dataPath = AliMpFiles::GetTop() + "/" + dataDir;
251 ProcessDirectory(dataPath, dataMap);
259 //_____________________________________________________________________________
260 Bool_t AliMpDataProcessor::GenerateData(AliMpDataMap* dataMap,
261 const TString& outputDataDir)
263 /// Generate ASCII data files in outputDataDir from dataMap
265 TString curDir = gSystem->pwd();
267 TString outputDataPath = AliMpFiles::GetTop() + "/" + outputDataDir;
268 if ( gSystem->OpenDirectory(outputDataPath.Data()) ) {
270 << "Directory " << outputDataPath.Data() << " already exists" << endl;
274 AliDebugStream(2) << "Making directory " << outputDataPath.Data() << endl;
275 gSystem->mkdir(outputDataPath.Data());
279 // std::map implementation
281 const map<string, string>& kMap = dataMap->GetMap();
282 map<string, string>::const_iterator it;
284 for ( it = kMap.begin(); it != kMap.end(); it++ ) {
285 string path = it->first;
286 string data = it->second;
289 const TMap& kMap = dataMap->GetMap();
292 while ( ( keyObj = it.Next() ) ) {
294 TString tpath = ((TObjString*)keyObj)->String();
296 TObject* dataObj = kMap.GetValue(keyObj);
299 << "Cannot find value when iterating over map." << endl;
302 TString tdata = ((TObjString*)dataObj)->String();
304 string path = tpath.Data();
305 string data = tdata.Data();
307 if ( path.find_last_of("/") != string::npos ) {
308 string dataDir(path, 0, path.find_last_of("/"));
310 = outputDataPath + "/" + dataDir.c_str();
311 if ( ! gSystem->OpenDirectory(dataDirPath.Data()) ) {
312 AliDebugStream(2) << "Making directory " << dataDirPath.Data() << endl;
313 gSystem->mkdir(dataDirPath.Data(), kTRUE);
318 = outputDataPath + "/" + path.c_str();
320 ofstream out(dataPath.Data(), ios::out);
321 if ( ! out.good() ) {
323 << "Cannot open output file " << outputDataPath.Data() << endl;
336 //_____________________________________________________________________________
337 Bool_t AliMpDataProcessor::GenerateCode(AliMpDataMap* dataMap)
339 /// Generate C++ code from dataMap.
341 /// AliMpDataProcessor mp;
342 /// AliMpDataMap* dataMap = mp.CreateDataMap();
343 /// mp.GenerateCode(dataMap);
345 /// Not really used, but kept for eventual future explorations.
348 // std::map implementation
350 const map<string, string>& kMap = dataMap->GetMap();
351 map<string, string>::const_iterator it;
353 for ( it = kMap.begin(); it != kMap.end(); it++ ) {
354 string path = it->first;
355 string data = it->second;
358 const TMap& kMap = dataMap->GetMap();
361 while ( ( keyObj = it.Next() ) ) {
363 TString tpath = ((TObjString*)keyObj)->String();
365 TObject* dataObj = kMap.GetValue(keyObj);
368 << "Cannot find value when iterating over map." << endl;
371 TString tdata = ((TObjString*)dataObj)->String();
373 GenerateFunction(tpath, tdata);