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