AliRoot Core  v5-06-15 (45dab64)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliMpDataProcessor.cxx
Go to the documentation of this file.
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 //
18 // Category: basic
19 
20 //-----------------------------------------------------------------------------
21 // Class AliMpDataProcessor
22 // -----------------------
23 // Class for converting ASCII data files in the map of string
24 // Author: Ivana Hrivnacova, IPN Orsay
25 //-----------------------------------------------------------------------------
26 
27 #include "AliMpDataProcessor.h"
28 #include "AliMpDataMap.h"
29 #include "AliMpFiles.h"
30 
31 #include "AliLog.h"
32 
33 #include <TSystem.h>
34 #include <TObjString.h>
35 #include <TFile.h>
36 #include <Riostream.h>
37 
38 #include <fstream>
39 #include <sstream>
40 #include <map>
41 
42 //
43 // static private methods
44 //
45 
46 //_____________________________________________________________________________
48 {
50  static const TString kHeaderFileName = "add.h";
51  return kHeaderFileName;
52 }
53 
54 //_____________________________________________________________________________
56 {
58  static const TString kImplFileName = "add.cxx";
59  return kImplFileName;
60 }
61 
62 //
63 // ctors, dtor
64 //
65 
66 //_____________________________________________________________________________
68  : TObject(),
69  fCounter(0),
70  fHeaderFile(),
71  fImplFile()
72 {
74 
75  fHeaderFile.open(GetHeaderFileName().Data(), std::ios::out);
76  fImplFile.open(GetImplFileName().Data(), std::ios::out);
77 
78  // Add check
79 
80 }
81 
82 //_____________________________________________________________________________
84 {
86 
87 }
88 
89 //
90 // private methods
91 //
92 
93 
94 //_____________________________________________________________________________
95 void AliMpDataProcessor::ProcessDirectory(const TString& path,
96  AliMpDataMap* dataMap)
97 {
99 
100  AliDebugStream(2) << "ProcessDirectory " << path.Data() << endl;
101 
102  // skip some directories
103  //if ( path.Contains("manu_serial") ) return;
104  //if ( path.Contains("stationTrigger") ) return;
105 
106  gSystem->cd(path);
107  gSystem->Exec("ls > /tmp/mpfiles.txt");
108 
109  ifstream mpfiles("/tmp/mpfiles.txt");
110  TString mpfilesStr;
111  TString fileName;
112  while ( mpfiles >> fileName ) {
113  mpfilesStr += fileName.Data();
114  mpfilesStr += " ";
115  }
116 
117  istringstream mpFilesStream(mpfilesStr.Data());
118  while ( mpFilesStream >> fileName ) {
119  TString filePath = path + "/" + fileName;
120  if ( gSystem->OpenDirectory(filePath.Data()) )
121  ProcessDirectory(filePath, dataMap);
122  else
123  ProcessFile(filePath, dataMap);
124  }
125 
126  AliDebugStream(2) << "ProcessDirectory done " << path.Data() << endl;
127 }
128 
129 //_____________________________________________________________________________
130 void AliMpDataProcessor::ProcessFile(const TString& path, AliMpDataMap* dataMap)
131 {
134 
135  // cut top path
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);
141 
142  AliDebugStream(2) << "Processing file " << dataPath << endl;
143 
144  // skip macros
145  if ( dataPath.find(".C") != std::string::npos ||
146  dataPath.find(".h") != std::string::npos ) return;
147 
148  ifstream input(path.Data(), ios::in);
149  if ( ! input.is_open() ) {
150  cerr << "Cannot open input file " << path.Data() << endl;
151  return;
152  }
153 
154  string fileString;
155  string line;
156 
157  do {
158  getline(input, line);
159  if ( ! input.eof() ) {
160  fileString += line;
161  fileString += '\n';
162  }
163  }
164  while ( ! input.eof() );
165 
166  dataMap->Add(dataPath, fileString);
167  // dataMap->Add(new TObjString(dataPath.c_str()), new TObjString(fileString.c_str()));
168 }
169 
170 //_____________________________________________________________________________
171 void AliMpDataProcessor::GenerateFunction(const TString& path,
172  const TString& data)
173 {
176 
177  AliDebugStream(2) << "Processing path " << path.Data() << endl;
178 
179  // skip motif & padPos
180  //if ( dataPath.find("motif") != std::string::npos ||
181  // dataPath.find("padPos") != std::string::npos ) return;
182 
183  // add function declaration in the header file name
184  fHeaderFile << " void AddData" << ++fCounter << "();" << endl;
185 
186  // add function implementation in .cxx
187  fImplFile
188  << "//_____________________________________________________________________________" << endl
189  << "void AliMpDataMap::AddData" << fCounter << "()" << endl
190  << "{" << endl
191  << "/// Automatically generated function for data file: " << endl
192  << "/// " << path << endl
193  << endl
194  << " string path = \"" << path << "\";" << endl << endl;
195 
196  GenerateFileCode(data);
197 
198  fImplFile
199  << endl
200  << " fMap[path] = data;" << endl
201  << "}" << endl
202  << endl;
203 }
204 
205 //_____________________________________________________________________________
206 void AliMpDataProcessor::GenerateFileCode(const TString& data)
207 {
209 
210  istringstream in(data.Data());
211 
212  string line;
213  getline(in, line);
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;
218 
219  do {
220  getline(in, line);
221  if ( ! in.eof() ) {
222  fImplFile << endl << " data = data + \"";
223  // for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
224  fImplFile << line << " \\n\";";
225  }
226  }
227  while ( ! in.eof() );
228 
229  fImplFile << endl;
230 }
231 
232 //_____________________________________________________________________________
234 {
236 
237  // add function declaration in the header file name
238  fHeaderFile << endl << " void Fill();" << endl;
239 
240  // add function implementation in .cxx
241  fImplFile
242  << "//_____________________________________________________________________________" << endl
243  << "void AliMpDataMap::Fill()" << endl
244  << "{" << endl
245  << "/// Automatically generated function for calling all generated functions " << endl
246  << endl;
247 
248  for ( Int_t i=1; i<=fCounter; ++i )
249  fImplFile << " AddData" << i << "();" << endl;
250 
251  fImplFile << "}" << endl;
252 }
253 
254 
255 //
256 // public methods
257 //
258 
259 
260 //_____________________________________________________________________________
262 {
265 
266  TString curDir = gSystem->pwd();
267 
268  AliMpDataMap* dataMap = new AliMpDataMap();
269 
270  TString dataPath = AliMpFiles::GetTop() + "/" + dataDir;
271  ProcessDirectory(dataPath, dataMap);
272 
273  gSystem->cd(curDir);
274 
275  return dataMap;
276 }
277 
278 
279 //_____________________________________________________________________________
281  const TString& outputDataDir)
282 {
284 
285  TString curDir = gSystem->pwd();
286 
287  TString outputDataPath = AliMpFiles::GetTop() + "/" + outputDataDir;
288  if ( gSystem->OpenDirectory(outputDataPath.Data()) ) {
289  AliErrorStream()
290  << "Directory " << outputDataPath.Data() << " already exists" << endl;
291  return kFALSE;
292  }
293  else {
294  AliDebugStream(2) << "Making directory " << outputDataPath.Data() << endl;
295  gSystem->mkdir(outputDataPath.Data());
296  }
297 
298 /*
299  // std::map implementation
300 
301  const map<string, string>& kMap = dataMap->GetMap();
302  map<string, string>::const_iterator it;
303 
304  for ( it = kMap.begin(); it != kMap.end(); it++ ) {
305  string path = it->first;
306  string data = it->second;
307 */
308 
309  const TMap& kMap = dataMap->GetMap();
310  TMapIter it(&kMap);
311  TObject* keyObj;
312  while ( ( keyObj = it.Next() ) ) {
313 
314  TString tpath = ((TObjString*)keyObj)->String();
315 
316  TObject* dataObj = kMap.GetValue(keyObj);
317  if ( ! dataObj ) {
318  AliErrorStream()
319  << "Cannot find value when iterating over map." << endl;
320  return kFALSE;
321  }
322  TString tdata = ((TObjString*)dataObj)->String();
323 
324  string path = tpath.Data();
325  string data = tdata.Data();
326 
327  string::size_type slashPos = path.find_last_of("/");
328  if ( slashPos != string::npos ) {
329  string dataDir(path, 0, slashPos);
330  TString dataDirPath
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);
335  }
336  }
337 
338  TString dataPath
339  = outputDataPath + "/" + path.c_str();
340 
341  ofstream out(dataPath.Data(), ios::out);
342  if ( ! out.good() ) {
343  AliErrorStream()
344  << "Cannot open output file " << outputDataPath.Data() << endl;
345 
346  return kFALSE;
347  }
348 
349  out << data;
350  out.close();
351  }
352 
353  delete dataMap;
354  return kTRUE;
355 }
356 
357 //_____________________________________________________________________________
359 {
367 
368 /*
369  // std::map implementation
370 
371  const map<string, string>& kMap = dataMap->GetMap();
372  map<string, string>::const_iterator it;
373 
374  for ( it = kMap.begin(); it != kMap.end(); it++ ) {
375  string path = it->first;
376  string data = it->second;
377 */
378 
379  const TMap& kMap = dataMap->GetMap();
380  TMapIter it(&kMap);
381  TObject* keyObj;
382  while ( ( keyObj = it.Next() ) ) {
383 
384  TString tpath = ((TObjString*)keyObj)->String();
385 
386  TObject* dataObj = kMap.GetValue(keyObj);
387  if ( ! dataObj ) {
388  AliErrorStream()
389  << "Cannot find value when iterating over map." << endl;
390  return kFALSE;
391  }
392  TString tdata = ((TObjString*)dataObj)->String();
393 
394  GenerateFunction(tpath, tdata);
395  }
396 
397  GenerateFill();
398 
399  return kTRUE;
400 }
401 
402 
AliMpDataMap * CreateDataMap(const TString &dataDir="data")
void GenerateFunction(const TString &path, const TString &data)
void ProcessDirectory(const TString &path, AliMpDataMap *map)
void GenerateFileCode(const TString &path)
ofstream fHeaderFile
header file
static const TString & GetImplFileName()
ofstream fImplFile
implementation file
const TMap & GetMap() const
Return the map constant reference.
Definition: AliMpDataMap.h:32
Int_t fCounter
data files counter
TObject class containing a map of strings to strings.
Definition: AliMpDataMap.h:21
static TString GetTop()
Definition: AliMpFiles.cxx:504
void ProcessFile(const TString &path, AliMpDataMap *map)
Bool_t GenerateData(AliMpDataMap *dataMap, const TString &outputDataDir="data_new")
static const TString & GetHeaderFileName()
void Add(const TString &path, const TString &data)
Bool_t GenerateCode(AliMpDataMap *dataMap)