Compatibility with ROOT trunk
[u/mrichter/AliRoot.git] / STEER / STEER / AliLHCReader.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 ///////////////////////////////////////////////////////////////////////////////
17 //                                                                           //
18 //  Class to read the file coming from DCS containing the information        //
19 //  from LHC. Everything is stored in a TMap, where:                         //
20 //  Key   --> DP name, as passed by LHC                                      // 
21 //  value --> TObjArray of AliDCSArray objects                               //
22 //                                                                           //
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include <Riostream.h>
26 #include <time.h>
27
28 #include <TObjArray.h>
29 #include <TObjString.h>
30 #include <TObject.h>
31 #include <TString.h>
32 #include <TMap.h>
33 #include <TSystem.h>
34 #include <TError.h>
35
36 #include "AliDCSArray.h"
37 #include "AliLHCReader.h"
38 #include "AliLog.h"
39
40 using std::ifstream;
41 ClassImp(AliLHCReader)
42
43 //--------------------------------------------------------------------------
44 AliLHCReader::AliLHCReader():
45         TObject(),
46         fStartTime(0),
47         fEndTime(0)
48 {
49         // default ctor 
50 }
51
52 //--------------------------------------------------------------------------
53 AliLHCReader::~AliLHCReader()
54 {
55         //
56         // dtor 
57         //
58 }
59
60 //--------------------------------------------------------------------------
61 TMap* AliLHCReader::ReadLHCDP(TString filename)
62 {
63         //
64         // reading the file with the inputs
65         //
66   gSystem->ExpandPathName(filename);
67         if( gSystem->AccessPathName( filename.Data() ) ) {
68                 AliError(Form( "file (%s) not found", filename.Data() ) );
69                 return NULL;
70         }
71
72         ifstream *file = new ifstream ( filename.Data() );
73         if (!*file) {
74                 AliError(Form("Error opening file (%s) !",filename.Data()));
75                 file->close();
76                 delete file;
77                 return NULL;
78         }
79         TMap* mapLHC = new TMap();
80         mapLHC->SetOwnerKeyValue();
81         TString strLine;
82         Int_t lhcEntries;
83         Int_t nBlocks = 0;
84         Int_t nline =0;
85         while(strLine.ReadLine(*file)){
86                 nline++;
87                 AliDebug(3,Form("line n. = %d",nline));
88                 // tokenize the line with tabs
89                 //if (strLine.BeginsWith("=")) continue;
90                 //if (!strLine.CompareTo("END_OF_BLOCK")) {
91                 if (strLine.Contains("END_OF_BLOCK")) {
92                         AliDebug(2,"END_OF_BLOCK");
93                         nBlocks++;
94                         continue;
95                 }
96                 TObjArray* tokens = strLine.Tokenize("\t");
97                 Int_t ntokens = tokens->GetEntriesFast();
98                 //
99                 if ( strLine.Contains("LHC_ENTRIES ") ) {
100                   // RS: special treatment for "LHC_ENTRIES N" record, which is space (and not tab) separated)
101                   delete tokens;
102                   tokens = strLine.Tokenize(" ");
103                   ntokens = tokens->GetEntriesFast();
104                 }
105                 //
106                 AliDebug(3,Form("Number of tokens = %d",ntokens));
107                 if (ntokens == 2 && !(((TObjString*)tokens->At(0))->String()).CompareTo("LHC_ENTRIES")){
108                         lhcEntries = (((TObjString*)tokens->At(1))->String()).Atoi();
109                         AliInfo(Form("LHC entries = %d",lhcEntries));
110                         AliDebug(3,Form("LHC entries = %d",lhcEntries));
111                         continue;
112                 }
113                 if (ntokens == 1 && !(((TObjString*)tokens->At(0))->String()).CompareTo("END_OF_DATA")){
114                         AliDebug(2,"End of file reached");
115                         delete tokens;
116                         break;
117                 }
118                 if (ntokens < 4){  
119                         AliInfo(Form("Wrong number of tokens --> # tokens = %d at line %d",ntokens,nline));
120                         // requiring at least the index of the DP, the DP name, the format, and the number of entries
121                         delete tokens;
122                         continue;
123                 }
124                 Int_t lhcDPindex = (((TObjString*)tokens->At(0))->String()).Atoi();
125                 AliDebug(2,Form("lhcDPindex = %d",lhcDPindex));
126                 TObjString* lhcDPname = (TObjString*)tokens->At(1);
127                 TString lhcDPtype = ((TObjString*)tokens->At(2))->String();
128                 AliDebug(2,Form("lhcDPname = %s",(lhcDPname->String()).Data()));
129                 AliDebug(2,Form("lhcDPtype = %s",lhcDPtype.Data()));
130                 TObjArray* typeTokens = lhcDPtype.Tokenize(":");
131                 if (typeTokens->GetEntriesFast() < 2 ){  
132                         // requiring the the type and the number of elements for each measurement
133                         AliError(Form("The format does not match the expected one, skipping the current line for DP = %s", lhcDPtype.Data()));
134                         delete typeTokens;
135                         delete tokens;
136                         continue;
137                 }
138                 TString type = ((TObjString*)typeTokens->At(0))->String();
139                 AliDebug(2,Form("type = %s",type.Data()));
140                 Int_t nelements = (((TObjString*)typeTokens->At(1))->String()).Atoi();
141                 AliDebug(2,Form("nelements = %i",nelements));
142                 Int_t nentries = (((TObjString*)tokens->At(3))->String()).Atoi();
143                 AliDebug(2,Form("nentries = %i",nentries));
144                 Int_t nValuesPerEntry = nelements+1;
145                 Int_t nfixed = 4; // n. of fixed entries
146                 TObjArray* array;
147                 if (mapLHC->GetValue(lhcDPname)==0x0){
148                         array = new TObjArray();
149                         array->SetOwner(1);
150                         mapLHC->Add(new TObjString(lhcDPname->String()),array);                 
151                 }
152                 else{
153                         array = (TObjArray*)mapLHC->GetValue(lhcDPname);
154                         AliDebug(2,Form("entry found! --> %p",array));
155                 }
156                                         
157                 for (Int_t ientry=0; ientry< nentries; ientry ++){
158                         Int_t indextime = nfixed+nValuesPerEntry*ientry+nelements;
159                         TString strTimestamp = ((TObjString*)tokens->At(indextime))->String();
160                         Double_t timestamp = strTimestamp.Atof();
161                         AliDebug(2,Form("Timestamp in unix time = %f (s)",timestamp));
162                         if (fStartTime!=0 && fEndTime!=0 && (fStartTime > timestamp || fEndTime < timestamp)){
163                                 // error in case the measurement is not within the data taking time interval
164                                 AliError(Form("Timestamp for entry %d of DP %s not in [%d,%d]", ientry, lhcDPtype.Data(),fStartTime,fEndTime));
165                                 continue;
166                         }
167                         if (type == "i"){
168                                 Int_t* value = new Int_t[nelements];
169                                 for (Int_t ielement=0; ielement<nelements; ielement++){
170                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi();
171                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,value[ielement]));
172                                 }
173                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
174                                 array->Add(dcs);
175                                 delete[] value;
176                         }
177                         else if (type == "b"){
178                                 Bool_t* value = new Bool_t[nelements];
179                                 for (Int_t ielement=0; ielement<nelements; ielement++){
180                                         value[ielement] = Bool_t((((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi());
181                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,Int_t(value[ielement])));
182                                 }
183                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
184                                 array->Add(dcs);
185                                 delete[] value;
186                         }
187                         else if (type == "f"){ // the floats should be considered as doubles
188                                 Double_t* value = new Double_t[nelements];
189                                 for (Int_t ielement=0; ielement<nelements; ielement++){
190                                         TString tempstr = (TString)(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String());
191                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atof();
192                                         AliDebug(2,Form("Value at index %d = %f from string %s",nfixed+ielement+ientry*nValuesPerEntry,value[ielement],tempstr.Data()));
193                                 } 
194                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
195                                 array->Add(dcs);
196                                 delete[] value;
197                         } 
198                         else if (type == "s"){
199                                 TObjArray* value = new TObjArray();
200                                 value->SetOwner(1);
201                                 for (Int_t ielement=0; ielement<nelements; ielement++){
202                                   TObjString* strobj = (new TObjString(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()));
203                                         AliDebug(2,Form("Value at index %d = %s",nfixed+ielement+ientry*nValuesPerEntry,(strobj->String()).Data()));
204                                         value->Add(strobj);
205                                 }
206                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
207                                 array->Add(dcs);
208                                 delete value;
209                         }
210                         else{
211                                 AliError(Form("Non-expected type %s",type.Data()));
212                                 delete typeTokens;
213                                 delete tokens;  
214                                 file->close();
215                                 delete file;    
216                                 return NULL;
217                         } 
218                 }
219                 delete typeTokens;
220                 delete tokens;
221         }
222         file->close();
223         delete file;
224         return mapLHC;
225 }
226
227 //--------------------------------------------------------------------------
228 TObjArray* AliLHCReader::ReadSingleLHCDP(TString filename, TString alias)
229 {
230         //
231         // reading the file with the inputs for the selected alias
232         // returning the TObjArray containing the information only for the current alias
233         //
234   gSystem->ExpandPathName(filename);
235         if( gSystem->AccessPathName( filename.Data() ) ) {
236                 AliError(Form( "file (%s) not found", filename.Data() ) );
237                 return NULL;
238         }
239
240         TString selection = gSystem->GetFromPipe(Form("grep -P '^\\d+\\s+%s+\\s' %s",alias.Data(), filename.Data()));
241
242         if (selection.Length() == 0) {
243                 AliError(Form("Alias %s not fouond in LHC Data file, returning a null pointer",alias.Data()));
244                 return NULL;
245         }
246
247         Int_t nline =0;
248
249         TObjArray* tokenslines = selection.Tokenize("\n");
250         Int_t ntokenslines = tokenslines->GetEntriesFast();
251         AliDebug(3,Form("Number of tokenslines = %d",ntokenslines));
252
253         TObjArray* array = new TObjArray(); // array to be returned
254         array->SetOwner(1);
255
256         for (Int_t iline=0; iline<ntokenslines; iline++){
257                 TString strLine = ((TObjString*)tokenslines->At(iline))->String();
258                 AliDebug(4,Form("***************** line = %s\n",strLine.Data()));
259                 TObjArray* tokens = strLine.Tokenize("\t");
260                 Int_t ntokens = tokens->GetEntriesFast();
261                 AliDebug(3,Form("Number of tokens = %d",ntokens));
262                 if (ntokens < 4){  
263                         AliInfo(Form("Wrong number of tokens --> # tokens = %d at line %d",ntokens,nline));
264                         // requiring at least the index of the DP, the DP name, the format, and the number of entries
265                         delete tokens;
266                         continue;
267                 }
268                 Int_t lhcDPindex = (((TObjString*)tokens->At(0))->String()).Atoi();
269                 AliDebug(2,Form("lhcDPindex = %d",lhcDPindex));
270                 TObjString* lhcDPname = (TObjString*)tokens->At(1);
271                 TString lhcDPtype = ((TObjString*)tokens->At(2))->String();
272                 AliDebug(2,Form("lhcDPname = %s",(lhcDPname->String()).Data()));
273                 AliDebug(2,Form("lhcDPtype = %s",lhcDPtype.Data()));
274                 TObjArray* typeTokens = lhcDPtype.Tokenize(":");
275                 if (typeTokens->GetEntriesFast() < 2 ){  
276                         // requiring the the type and the number of elements for each measurement
277                         AliError(Form("The format does not match the expected one, skipping the current line for DP = %s", lhcDPtype.Data()));
278                         delete typeTokens;
279                         delete tokens;
280                         continue;
281                 }
282                 TString type = ((TObjString*)typeTokens->At(0))->String();
283                 AliDebug(2,Form("type = %s",type.Data()));
284                 Int_t nelements = (((TObjString*)typeTokens->At(1))->String()).Atoi();
285                 AliDebug(2,Form("nelements = %i",nelements));
286                 Int_t nentries = (((TObjString*)tokens->At(3))->String()).Atoi();
287                 AliDebug(2,Form("nentries = %i",nentries));
288                 Int_t nValuesPerEntry = nelements+1;
289                 Int_t nfixed = 4; // n. of fixed entries
290                 for (Int_t ientry=0; ientry< nentries; ientry ++){
291                         Int_t indextime = nfixed+nValuesPerEntry*ientry+nelements;
292                         TString strTimestamp = ((TObjString*)tokens->At(indextime))->String();
293                         Double_t timestamp = strTimestamp.Atof();
294                         AliDebug(2,Form("Timestamp in unix time = %f (s)",timestamp));
295                         if (fStartTime!=0 && fEndTime!=0 && (fStartTime > timestamp || fEndTime < timestamp)){
296                                 // error in case the measurement is not within the data taking time interval
297                                 AliError(Form("Timestamp for entry %d of DP %s not in [%d,%d]", ientry, lhcDPtype.Data(),fStartTime,fEndTime));
298                                 continue;
299                         }
300                         if (type == "i"){
301                                 Int_t* value = new Int_t[nelements];
302                                 for (Int_t ielement=0; ielement<nelements; ielement++){
303                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi();
304                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,value[ielement]));
305                                 }
306                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
307                                 array->Add(dcs);
308                                 delete[] value;
309                         }
310                         else if (type == "b"){
311                                 Bool_t* value = new Bool_t[nelements];
312                                 for (Int_t ielement=0; ielement<nelements; ielement++){
313                                         value[ielement] = Bool_t((((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi());
314                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,Int_t(value[ielement])));
315                                 }
316                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
317                                 array->Add(dcs);
318                                 delete[] value;
319                         }
320                         else if (type == "f"){ // the floats should be considered as doubles
321                                 Double_t* value = new Double_t[nelements];
322                                 for (Int_t ielement=0; ielement<nelements; ielement++){
323                                         TString tempstr = (TString)(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String());
324                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atof();
325                                         AliDebug(2,Form("Value at index %d = %f from string %s",nfixed+ielement+ientry*nValuesPerEntry,value[ielement],tempstr.Data()));
326                                 } 
327                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
328                                 array->Add(dcs);
329                                 delete[] value;
330                         } 
331                         else if (type == "s"){
332                                 TObjArray* value = new TObjArray();
333                                 value->SetOwner(1);
334                                 for (Int_t ielement=0; ielement<nelements; ielement++){
335                                   TObjString* strobj = (new TObjString(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()));
336                                         AliDebug(2,Form("Value at index %d = %s",nfixed+ielement+ientry*nValuesPerEntry,(strobj->String()).Data()));
337                                         value->Add(strobj);
338                                 }
339                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
340                                 array->Add(dcs);
341                                 delete value;
342                         }
343                         else{
344                                 AliError(Form("Non-expected type %s",type.Data()));
345                                 delete typeTokens;
346                                 delete tokens;  
347                                 delete tokenslines;
348                                 return NULL;
349                         } 
350                 }
351                 delete typeTokens;
352                 delete tokens;
353         }
354         delete tokenslines;
355         return array;
356 }
357
358
359
360
361
362
363