Line data Source code
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 : //_____________________________________________________________________________
47 : const TString& AliMpDataProcessor::GetHeaderFileName()
48 : {
49 : /// Return the default name for generated header file
50 0 : static const TString kHeaderFileName = "add.h";
51 0 : return kHeaderFileName;
52 0 : }
53 :
54 : //_____________________________________________________________________________
55 : const TString& AliMpDataProcessor::GetImplFileName()
56 : {
57 : /// Return the default name for generated implementation file
58 0 : static const TString kImplFileName = "add.cxx";
59 0 : return kImplFileName;
60 0 : }
61 :
62 : //
63 : // ctors, dtor
64 : //
65 :
66 : //_____________________________________________________________________________
67 : AliMpDataProcessor::AliMpDataProcessor()
68 0 : : TObject(),
69 0 : fCounter(0),
70 0 : fHeaderFile(),
71 0 : fImplFile()
72 0 : {
73 : /// Default and standar constructor
74 :
75 0 : fHeaderFile.open(GetHeaderFileName().Data(), std::ios::out);
76 0 : fImplFile.open(GetImplFileName().Data(), std::ios::out);
77 :
78 : // Add check
79 :
80 0 : }
81 :
82 : //_____________________________________________________________________________
83 : AliMpDataProcessor:: ~AliMpDataProcessor()
84 0 : {
85 : /// Destructor
86 :
87 0 : }
88 :
89 : //
90 : // private methods
91 : //
92 :
93 :
94 : //_____________________________________________________________________________
95 : void AliMpDataProcessor::ProcessDirectory(const TString& path,
96 : AliMpDataMap* dataMap)
97 : {
98 : /// Recursive function to process data directory
99 :
100 0 : 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 0 : gSystem->cd(path);
107 0 : gSystem->Exec("ls > /tmp/mpfiles.txt");
108 :
109 0 : ifstream mpfiles("/tmp/mpfiles.txt");
110 0 : TString mpfilesStr;
111 0 : TString fileName;
112 0 : while ( mpfiles >> fileName ) {
113 0 : mpfilesStr += fileName.Data();
114 0 : mpfilesStr += " ";
115 : }
116 :
117 0 : istringstream mpFilesStream(mpfilesStr.Data());
118 0 : while ( mpFilesStream >> fileName ) {
119 0 : TString filePath = path + "/" + fileName;
120 0 : if ( gSystem->OpenDirectory(filePath.Data()) )
121 0 : ProcessDirectory(filePath, dataMap);
122 : else
123 0 : ProcessFile(filePath, dataMap);
124 0 : }
125 :
126 0 : AliDebugStream(2) << "ProcessDirectory done " << path.Data() << endl;
127 0 : }
128 :
129 : //_____________________________________________________________________________
130 : void AliMpDataProcessor::ProcessFile(const TString& path, AliMpDataMap* dataMap)
131 : {
132 : /// Dump the content of the file specified by its path
133 : /// in a string and fill it in the dataMap
134 :
135 : // cut top path
136 0 : string top = AliMpFiles::GetTop().Data();
137 0 : string fullDataPath = path.Data();
138 0 : string dataPath = fullDataPath;
139 0 : if ( dataPath.find(top) != string::npos ) dataPath.erase(0, top.size()+1);
140 0 : dataPath.erase(0,dataPath.find('/')+1);
141 :
142 0 : AliDebugStream(2) << "Processing file " << dataPath << endl;
143 :
144 : // skip macros
145 0 : if ( dataPath.find(".C") != std::string::npos ||
146 0 : dataPath.find(".h") != std::string::npos ) return;
147 :
148 0 : ifstream input(path.Data(), ios::in);
149 0 : if ( ! input.is_open() ) {
150 0 : cerr << "Cannot open input file " << path.Data() << endl;
151 0 : return;
152 : }
153 :
154 0 : string fileString;
155 0 : string line;
156 :
157 0 : do {
158 0 : getline(input, line);
159 0 : if ( ! input.eof() ) {
160 0 : fileString += line;
161 0 : fileString += '\n';
162 : }
163 0 : }
164 0 : while ( ! input.eof() );
165 :
166 0 : dataMap->Add(dataPath, fileString);
167 : // dataMap->Add(new TObjString(dataPath.c_str()), new TObjString(fileString.c_str()));
168 0 : }
169 :
170 : //_____________________________________________________________________________
171 : void AliMpDataProcessor::GenerateFunction(const TString& path,
172 : const TString& data)
173 : {
174 : /// Generate a C++ function which defines a string with the data content
175 : /// and map it to the given path in the map
176 :
177 0 : 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 0 : fHeaderFile << " void AddData" << ++fCounter << "();" << endl;
185 :
186 : // add function implementation in .cxx
187 0 : fImplFile
188 0 : << "//_____________________________________________________________________________" << endl
189 0 : << "void AliMpDataMap::AddData" << fCounter << "()" << endl
190 0 : << "{" << endl
191 0 : << "/// Automatically generated function for data file: " << endl
192 0 : << "/// " << path << endl
193 0 : << endl
194 0 : << " string path = \"" << path << "\";" << endl << endl;
195 :
196 0 : GenerateFileCode(data);
197 :
198 : fImplFile
199 0 : << endl
200 0 : << " fMap[path] = data;" << endl
201 0 : << "}" << endl
202 0 : << endl;
203 0 : }
204 :
205 : //_____________________________________________________________________________
206 : void AliMpDataProcessor::GenerateFileCode(const TString& data)
207 : {
208 : /// Dump the content of the file specified by its path as a C++ string
209 :
210 0 : istringstream in(data.Data());
211 :
212 0 : string line;
213 0 : getline(in, line);
214 0 : fImplFile << " string data = \"";
215 : //for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
216 0 : fImplFile << line << " \\n\";";
217 0 : if ( in.eof() ) return;
218 :
219 : do {
220 0 : getline(in, line);
221 0 : if ( ! in.eof() ) {
222 0 : fImplFile << endl << " data = data + \"";
223 : // for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
224 0 : fImplFile << line << " \\n\";";
225 : }
226 0 : }
227 0 : while ( ! in.eof() );
228 :
229 0 : fImplFile << endl;
230 0 : }
231 :
232 : //_____________________________________________________________________________
233 : void AliMpDataProcessor::GenerateFill()
234 : {
235 : /// Generate function which calls all previously generated functions
236 :
237 : // add function declaration in the header file name
238 0 : fHeaderFile << endl << " void Fill();" << endl;
239 :
240 : // add function implementation in .cxx
241 0 : fImplFile
242 0 : << "//_____________________________________________________________________________" << endl
243 0 : << "void AliMpDataMap::Fill()" << endl
244 0 : << "{" << endl
245 0 : << "/// Automatically generated function for calling all generated functions " << endl
246 0 : << endl;
247 :
248 0 : for ( Int_t i=1; i<=fCounter; ++i )
249 0 : fImplFile << " AddData" << i << "();" << endl;
250 :
251 0 : fImplFile << "}" << endl;
252 0 : }
253 :
254 :
255 : //
256 : // public methods
257 : //
258 :
259 :
260 : //_____________________________________________________________________________
261 : AliMpDataMap* AliMpDataProcessor::CreateDataMap(const TString& dataDir)
262 : {
263 : /// Process data directory and map a string with the content of each file to
264 : /// the file path.
265 :
266 0 : TString curDir = gSystem->pwd();
267 :
268 0 : AliMpDataMap* dataMap = new AliMpDataMap();
269 :
270 0 : TString dataPath = AliMpFiles::GetTop() + "/" + dataDir;
271 0 : ProcessDirectory(dataPath, dataMap);
272 :
273 0 : gSystem->cd(curDir);
274 :
275 : return dataMap;
276 0 : }
277 :
278 :
279 : //_____________________________________________________________________________
280 : Bool_t AliMpDataProcessor::GenerateData(AliMpDataMap* dataMap,
281 : const TString& outputDataDir)
282 : {
283 : /// Generate ASCII data files in outputDataDir from dataMap
284 :
285 0 : TString curDir = gSystem->pwd();
286 :
287 0 : TString outputDataPath = AliMpFiles::GetTop() + "/" + outputDataDir;
288 0 : if ( gSystem->OpenDirectory(outputDataPath.Data()) ) {
289 0 : AliErrorStream()
290 0 : << "Directory " << outputDataPath.Data() << " already exists" << endl;
291 0 : return kFALSE;
292 : }
293 : else {
294 0 : AliDebugStream(2) << "Making directory " << outputDataPath.Data() << endl;
295 0 : 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 0 : const TMap& kMap = dataMap->GetMap();
310 0 : TMapIter it(&kMap);
311 : TObject* keyObj;
312 0 : while ( ( keyObj = it.Next() ) ) {
313 :
314 0 : TString tpath = ((TObjString*)keyObj)->String();
315 :
316 0 : TObject* dataObj = kMap.GetValue(keyObj);
317 0 : if ( ! dataObj ) {
318 0 : AliErrorStream()
319 0 : << "Cannot find value when iterating over map." << endl;
320 0 : return kFALSE;
321 : }
322 0 : TString tdata = ((TObjString*)dataObj)->String();
323 :
324 0 : string path = tpath.Data();
325 0 : string data = tdata.Data();
326 :
327 0 : string::size_type slashPos = path.find_last_of("/");
328 0 : if ( slashPos != string::npos ) {
329 0 : string dataDir(path, 0, slashPos);
330 0 : TString dataDirPath
331 0 : = outputDataPath + "/" + dataDir.c_str();
332 0 : if ( ! gSystem->OpenDirectory(dataDirPath.Data()) ) {
333 0 : AliDebugStream(2) << "Making directory " << dataDirPath.Data() << endl;
334 0 : gSystem->mkdir(dataDirPath.Data(), kTRUE);
335 : }
336 0 : }
337 :
338 0 : TString dataPath
339 0 : = outputDataPath + "/" + path.c_str();
340 :
341 0 : ofstream out(dataPath.Data(), ios::out);
342 0 : if ( ! out.good() ) {
343 0 : AliErrorStream()
344 0 : << "Cannot open output file " << outputDataPath.Data() << endl;
345 :
346 0 : return kFALSE;
347 : }
348 :
349 0 : out << data;
350 0 : out.close();
351 0 : }
352 :
353 0 : delete dataMap;
354 0 : return kTRUE;
355 0 : }
356 :
357 : //_____________________________________________________________________________
358 : Bool_t AliMpDataProcessor::GenerateCode(AliMpDataMap* dataMap)
359 : {
360 : /// Generate C++ code from dataMap.
361 : /// <pre>
362 : /// AliMpDataProcessor mp;
363 : /// AliMpDataMap* dataMap = mp.CreateDataMap();
364 : /// mp.GenerateCode(dataMap);
365 : /// </pre>
366 : /// Not really used, but kept for eventual future explorations.
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 0 : const TMap& kMap = dataMap->GetMap();
380 0 : TMapIter it(&kMap);
381 : TObject* keyObj;
382 0 : while ( ( keyObj = it.Next() ) ) {
383 :
384 0 : TString tpath = ((TObjString*)keyObj)->String();
385 :
386 0 : TObject* dataObj = kMap.GetValue(keyObj);
387 0 : if ( ! dataObj ) {
388 0 : AliErrorStream()
389 0 : << "Cannot find value when iterating over map." << endl;
390 0 : return kFALSE;
391 : }
392 0 : TString tdata = ((TObjString*)dataObj)->String();
393 :
394 0 : GenerateFunction(tpath, tdata);
395 0 : }
396 :
397 0 : GenerateFill();
398 :
399 0 : return kTRUE;
400 0 : }
401 :
402 :
|