]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDataProcessor.cxx
In mapping:
[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 = string(fullDataPath, top.size()+6);
119   
120   AliDebugStream(2) << "Processing file " << dataPath << endl;
121
122   // skip macros
123   if ( dataPath.find(".C") != std::string::npos || 
124        dataPath.find(".h") != std::string::npos ) return;
125  
126   ifstream input(path.Data(), ios::in);
127   if ( ! input.is_open() ) {
128     cerr << "Cannot open input file " << path.Data() << endl;
129     return;
130   }  
131
132   string fileString;
133   string line;
134
135   do {
136     getline(input, line);
137     if ( ! input.eof() ) { 
138       fileString += line;
139       fileString += '\n';
140     }  
141   }
142   while ( ! input.eof() );
143
144   dataMap->Add(dataPath, fileString);
145   // dataMap->Add(new TObjString(dataPath.c_str()), new TObjString(fileString.c_str()));
146 }  
147
148 //_____________________________________________________________________________
149 void AliMpDataProcessor::GenerateFunction(const TString& path, 
150                                           const TString& data) 
151 {
152 /// Generate a C++ function which defines a string with the data content 
153 /// and map it to the given path in the map
154
155   AliDebugStream(2) << "Processing path " << path.Data() << endl;
156
157   // skip motif & padPos
158   //if ( dataPath.find("motif") != std::string::npos || 
159   //     dataPath.find("padPos") != std::string::npos ) return;
160  
161   // add function declaration in the header file name
162   fHeaderFile << "    void AddData" << ++fCounter << "();" << endl;
163
164   // add function implementation in .cxx
165   fImplFile 
166     << "//_____________________________________________________________________________" << endl
167     << "void AliMpDataMap::AddData" << fCounter << "()" << endl
168     << "{" << endl
169     << "/// Automatically generated function for data file: " << endl
170     << "/// " << path << endl
171     << endl
172     << "  string path = \"" << path << "\";" << endl << endl;
173     
174   GenerateFileCode(data);
175   
176   fImplFile 
177     << endl
178     << "  fMap[path] = data;" << endl
179     << "}" << endl
180     << endl;      
181
182
183 //_____________________________________________________________________________
184 void AliMpDataProcessor::GenerateFileCode(const TString& data) 
185 {
186 /// Dump the content of the file specified by its path as a C++ string
187
188   istringstream in(data.Data());
189
190   string line;
191   getline(in, line);
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; 
196
197   do {
198     getline(in, line);
199     if ( ! in.eof() ) { 
200       fImplFile << endl << "  data = data + \"";
201       // for ( unsigned int i=line.size(); i<58; i++ ) line = line + " ";
202       fImplFile << line << " \\n\";";
203     }  
204   }
205   while ( ! in.eof() );
206
207   fImplFile << endl;
208 }  
209
210 //_____________________________________________________________________________
211 void AliMpDataProcessor::GenerateFill()
212 {
213 /// Generate function which calls all previously generated functions
214
215   // add function declaration in the header file name
216   fHeaderFile << endl << "    void Fill();" << endl;
217
218   // add function implementation in .cxx
219   fImplFile 
220     << "//_____________________________________________________________________________" << endl
221     << "void AliMpDataMap::Fill()" << endl
222     << "{" << endl
223     << "/// Automatically generated function for calling all generated functions " << endl
224     << endl;
225     
226   for ( Int_t i=1; i<=fCounter; ++i ) 
227    fImplFile << "  AddData" << i << "();" << endl;   
228       
229   fImplFile << "}" << endl;
230 }  
231       
232
233 //
234 // public methods
235 //
236
237
238 //_____________________________________________________________________________
239 AliMpDataMap* AliMpDataProcessor::CreateDataMap(const TString& dataDir) 
240 {
241 /// Process data directory and map a string with the content of each file to
242 /// the file path.
243
244   TString curDir = gSystem->pwd();
245
246   AliMpDataMap* dataMap = new AliMpDataMap();
247
248   TString dataPath = AliMpFiles::GetTop() + "/" + dataDir;
249   ProcessDirectory(dataPath, dataMap); 
250   
251   gSystem->cd(curDir); 
252   
253   return dataMap;
254 }
255   
256
257 //_____________________________________________________________________________
258 Bool_t AliMpDataProcessor::GenerateData(AliMpDataMap* dataMap,
259                                         const TString& outputDataDir) 
260 {
261 /// Generate ASCII data files in outputDataDir from dataMap
262
263   TString curDir = gSystem->pwd();  
264
265   TString outputDataPath = AliMpFiles::GetTop() + "/" + outputDataDir;
266   if ( gSystem->OpenDirectory(outputDataPath.Data()) ) {
267     AliErrorStream() 
268       << "Directory " << outputDataPath.Data() << " already exists" << endl;
269     return kFALSE;
270   }
271   else {
272     AliDebugStream(2) << "Making directory " <<  outputDataPath.Data() << endl;
273     gSystem->mkdir(outputDataPath.Data());
274   }  
275   
276 /*
277   // std::map implementation
278
279   const map<string, string>& kMap = dataMap->GetMap();
280   map<string, string>::const_iterator it;
281   
282   for ( it = kMap.begin(); it != kMap.end(); it++ ) {
283     string path = it->first;
284     string data = it->second; 
285 */    
286
287   const TMap& kMap = dataMap->GetMap();
288   TMapIter it(&kMap);
289   TObject* keyObj;
290   while ( ( keyObj = it.Next() ) ) {
291
292     TString tpath = ((TObjString*)keyObj)->String();
293
294     TObject* dataObj = kMap.GetValue(keyObj);
295     if ( ! dataObj ) {
296       AliErrorStream() 
297         << "Cannot find value when iterating over map." << endl;
298       return kFALSE;
299     }      
300     TString tdata = ((TObjString*)dataObj)->String();   
301   
302     string path = tpath.Data();
303     string data = tdata.Data(); 
304   
305     if ( path.find_last_of("/") != string::npos ) {
306       string dataDir(path, 0, path.find_last_of("/"));
307       TString dataDirPath
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);
312       }
313     }     
314
315     TString dataPath
316       = outputDataPath + "/" + path.c_str();
317
318     ofstream out(dataPath.Data(), ios::out);
319     if ( ! out.good() ) {
320       AliErrorStream() 
321         << "Cannot open output file  " << outputDataPath.Data() << endl;
322    
323       return kFALSE;  
324     }
325     
326     out << data;
327     out.close();
328   }
329
330   delete dataMap;
331   return kTRUE;
332 }
333   
334 //_____________________________________________________________________________
335 Bool_t AliMpDataProcessor::GenerateCode(AliMpDataMap* dataMap) 
336 {
337 /// Generate C++ code from dataMap.
338 /// <pre>
339 /// AliMpDataProcessor mp;
340 /// AliMpDataMap* dataMap = mp.CreateDataMap();
341 /// mp.GenerateCode(dataMap);
342 /// </pre>
343 /// Not really used, but kept for eventual future explorations.
344
345 /*
346   // std::map implementation
347
348   const map<string, string>& kMap = dataMap->GetMap();
349   map<string, string>::const_iterator it;
350   
351   for ( it = kMap.begin(); it != kMap.end(); it++ ) {
352     string path = it->first;
353     string data = it->second; 
354 */
355
356   const TMap& kMap = dataMap->GetMap();
357   TMapIter it(&kMap);
358   TObject* keyObj;
359   while ( ( keyObj = it.Next() ) ) {
360
361     TString tpath = ((TObjString*)keyObj)->String();
362
363     TObject* dataObj = kMap.GetValue(keyObj);
364     if ( ! dataObj ) {
365       AliErrorStream() 
366         << "Cannot find value when iterating over map." << endl;
367       return kFALSE;
368     }      
369     TString tdata = ((TObjString*)dataObj)->String();   
370   
371     GenerateFunction(tpath, tdata);
372   }
373   
374   GenerateFill();
375
376   return kTRUE;
377 }
378   
379