Fix for raw ctp decoding (Marek)
[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                   delete tokens;
112                   continue;
113                 }
114                 if (ntokens == 1 && !(((TObjString*)tokens->At(0))->String()).CompareTo("END_OF_DATA")){
115                         AliDebug(2,"End of file reached");
116                         delete tokens;
117                         break;
118                 }
119                 if (ntokens < 4){  
120                         AliInfo(Form("Wrong number of tokens --> # tokens = %d at line %d",ntokens,nline));
121                         // requiring at least the index of the DP, the DP name, the format, and the number of entries
122                         delete tokens;
123                         continue;
124                 }
125                 Int_t lhcDPindex = (((TObjString*)tokens->At(0))->String()).Atoi();
126                 AliDebug(2,Form("lhcDPindex = %d",lhcDPindex));
127                 TObjString* lhcDPname = (TObjString*)tokens->At(1);
128                 TString lhcDPtype = ((TObjString*)tokens->At(2))->String();
129                 AliDebug(2,Form("lhcDPname = %s",(lhcDPname->String()).Data()));
130                 AliDebug(2,Form("lhcDPtype = %s",lhcDPtype.Data()));
131                 TObjArray* typeTokens = lhcDPtype.Tokenize(":");
132                 if (typeTokens->GetEntriesFast() < 2 ){  
133                         // requiring the the type and the number of elements for each measurement
134                         AliError(Form("The format does not match the expected one, skipping the current line for DP = %s", lhcDPtype.Data()));
135                         delete typeTokens;
136                         delete tokens;
137                         continue;
138                 }
139                 TString type = ((TObjString*)typeTokens->At(0))->String();
140                 AliDebug(2,Form("type = %s",type.Data()));
141                 Int_t nelements = (((TObjString*)typeTokens->At(1))->String()).Atoi();
142                 AliDebug(2,Form("nelements = %i",nelements));
143                 Int_t nentries = (((TObjString*)tokens->At(3))->String()).Atoi();
144                 AliDebug(2,Form("nentries = %i",nentries));
145                 Int_t nValuesPerEntry = nelements+1;
146                 Int_t nfixed = 4; // n. of fixed entries
147                 TObjArray* array;
148                 if (mapLHC->GetValue(lhcDPname)==0x0){
149                         array = new TObjArray();
150                         array->SetOwner(1);
151                         mapLHC->Add(new TObjString(lhcDPname->String()),array);                 
152                 }
153                 else{
154                         array = (TObjArray*)mapLHC->GetValue(lhcDPname);
155                         AliDebug(2,Form("entry found! --> %p",array));
156                 }
157                                         
158                 for (Int_t ientry=0; ientry< nentries; ientry ++){
159                         Int_t indextime = nfixed+nValuesPerEntry*ientry+nelements;
160                         TString strTimestamp = ((TObjString*)tokens->At(indextime))->String();
161                         Double_t timestamp = strTimestamp.Atof();
162                         AliDebug(2,Form("Timestamp in unix time = %f (s)",timestamp));
163                         if (fStartTime!=0 && fEndTime!=0 && (fStartTime > timestamp || fEndTime < timestamp)){
164                                 // error in case the measurement is not within the data taking time interval
165                                 AliError(Form("Timestamp for entry %d of DP %s not in [%d,%d]", ientry, lhcDPtype.Data(),fStartTime,fEndTime));
166                                 continue;
167                         }
168                         if (type == "i"){
169                                 Int_t* value = new Int_t[nelements];
170                                 for (Int_t ielement=0; ielement<nelements; ielement++){
171                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi();
172                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,value[ielement]));
173                                 }
174                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
175                                 array->Add(dcs);
176                                 delete[] value;
177                         }
178                         else if (type == "b"){
179                                 Bool_t* value = new Bool_t[nelements];
180                                 for (Int_t ielement=0; ielement<nelements; ielement++){
181                                         value[ielement] = Bool_t((((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi());
182                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,Int_t(value[ielement])));
183                                 }
184                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
185                                 array->Add(dcs);
186                                 delete[] value;
187                         }
188                         else if (type == "f"){ // the floats should be considered as doubles
189                                 Double_t* value = new Double_t[nelements];
190                                 for (Int_t ielement=0; ielement<nelements; ielement++){
191                                         TString tempstr = (TString)(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String());
192                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atof();
193                                         AliDebug(2,Form("Value at index %d = %f from string %s",nfixed+ielement+ientry*nValuesPerEntry,value[ielement],tempstr.Data()));
194                                 } 
195                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
196                                 array->Add(dcs);
197                                 delete[] value;
198                         } 
199                         else if (type == "s"){
200                                 TObjArray* value = new TObjArray();
201                                 value->SetOwner(1);
202                                 for (Int_t ielement=0; ielement<nelements; ielement++){
203                                   TObjString* strobj = (new TObjString(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()));
204                                         AliDebug(2,Form("Value at index %d = %s",nfixed+ielement+ientry*nValuesPerEntry,(strobj->String()).Data()));
205                                         value->Add(strobj);
206                                 }
207                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
208                                 array->Add(dcs);
209                                 delete value;
210                         }
211                         else{
212                                 AliError(Form("Non-expected type %s",type.Data()));
213                                 delete typeTokens;
214                                 delete tokens;  
215                                 file->close();
216                                 delete file;    
217                                 return NULL;
218                         } 
219                 }
220                 delete typeTokens;
221                 delete tokens;
222         }
223         file->close();
224         delete file;
225         return mapLHC;
226 }
227
228 //--------------------------------------------------------------------------
229 TObjArray* AliLHCReader::ReadSingleLHCDP(TString filename, TString alias)
230 {
231         //
232         // reading the file with the inputs for the selected alias
233         // returning the TObjArray containing the information only for the current alias
234         //
235   gSystem->ExpandPathName(filename);
236         if( gSystem->AccessPathName( filename.Data() ) ) {
237                 AliError(Form( "file (%s) not found", filename.Data() ) );
238                 return NULL;
239         }
240
241         TString selection = gSystem->GetFromPipe(Form("grep -P '^\\d+\\s+%s+\\s' %s",alias.Data(), filename.Data()));
242
243         if (selection.Length() == 0) {
244                 AliError(Form("Alias %s not fouond in LHC Data file, returning a null pointer",alias.Data()));
245                 return NULL;
246         }
247
248         Int_t nline =0;
249
250         TObjArray* tokenslines = selection.Tokenize("\n");
251         Int_t ntokenslines = tokenslines->GetEntriesFast();
252         AliDebug(3,Form("Number of tokenslines = %d",ntokenslines));
253
254         TObjArray* array = new TObjArray(); // array to be returned
255         array->SetOwner(1);
256
257         for (Int_t iline=0; iline<ntokenslines; iline++){
258                 TString strLine = ((TObjString*)tokenslines->At(iline))->String();
259                 AliDebug(4,Form("***************** line = %s\n",strLine.Data()));
260                 TObjArray* tokens = strLine.Tokenize("\t");
261                 Int_t ntokens = tokens->GetEntriesFast();
262                 AliDebug(3,Form("Number of tokens = %d",ntokens));
263                 if (ntokens < 4){  
264                         AliInfo(Form("Wrong number of tokens --> # tokens = %d at line %d",ntokens,nline));
265                         // requiring at least the index of the DP, the DP name, the format, and the number of entries
266                         delete tokens;
267                         continue;
268                 }
269                 Int_t lhcDPindex = (((TObjString*)tokens->At(0))->String()).Atoi();
270                 AliDebug(2,Form("lhcDPindex = %d",lhcDPindex));
271                 TObjString* lhcDPname = (TObjString*)tokens->At(1);
272                 TString lhcDPtype = ((TObjString*)tokens->At(2))->String();
273                 AliDebug(2,Form("lhcDPname = %s",(lhcDPname->String()).Data()));
274                 AliDebug(2,Form("lhcDPtype = %s",lhcDPtype.Data()));
275                 TObjArray* typeTokens = lhcDPtype.Tokenize(":");
276                 if (typeTokens->GetEntriesFast() < 2 ){  
277                         // requiring the the type and the number of elements for each measurement
278                         AliError(Form("The format does not match the expected one, skipping the current line for DP = %s", lhcDPtype.Data()));
279                         delete typeTokens;
280                         delete tokens;
281                         continue;
282                 }
283                 TString type = ((TObjString*)typeTokens->At(0))->String();
284                 AliDebug(2,Form("type = %s",type.Data()));
285                 Int_t nelements = (((TObjString*)typeTokens->At(1))->String()).Atoi();
286                 AliDebug(2,Form("nelements = %i",nelements));
287                 Int_t nentries = (((TObjString*)tokens->At(3))->String()).Atoi();
288                 AliDebug(2,Form("nentries = %i",nentries));
289                 Int_t nValuesPerEntry = nelements+1;
290                 Int_t nfixed = 4; // n. of fixed entries
291                 for (Int_t ientry=0; ientry< nentries; ientry ++){
292                         Int_t indextime = nfixed+nValuesPerEntry*ientry+nelements;
293                         TString strTimestamp = ((TObjString*)tokens->At(indextime))->String();
294                         Double_t timestamp = strTimestamp.Atof();
295                         AliDebug(2,Form("Timestamp in unix time = %f (s)",timestamp));
296                         if (fStartTime!=0 && fEndTime!=0 && (fStartTime > timestamp || fEndTime < timestamp)){
297                                 // error in case the measurement is not within the data taking time interval
298                                 AliError(Form("Timestamp for entry %d of DP %s not in [%d,%d]", ientry, lhcDPtype.Data(),fStartTime,fEndTime));
299                                 continue;
300                         }
301                         if (type == "i"){
302                                 Int_t* value = new Int_t[nelements];
303                                 for (Int_t ielement=0; ielement<nelements; ielement++){
304                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi();
305                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,value[ielement]));
306                                 }
307                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
308                                 array->Add(dcs);
309                                 delete[] value;
310                         }
311                         else if (type == "b"){
312                                 Bool_t* value = new Bool_t[nelements];
313                                 for (Int_t ielement=0; ielement<nelements; ielement++){
314                                         value[ielement] = Bool_t((((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi());
315                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,Int_t(value[ielement])));
316                                 }
317                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
318                                 array->Add(dcs);
319                                 delete[] value;
320                         }
321                         else if (type == "f"){ // the floats should be considered as doubles
322                                 Double_t* value = new Double_t[nelements];
323                                 for (Int_t ielement=0; ielement<nelements; ielement++){
324                                         TString tempstr = (TString)(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String());
325                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atof();
326                                         AliDebug(2,Form("Value at index %d = %f from string %s",nfixed+ielement+ientry*nValuesPerEntry,value[ielement],tempstr.Data()));
327                                 } 
328                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
329                                 array->Add(dcs);
330                                 delete[] value;
331                         } 
332                         else if (type == "s"){
333                                 TObjArray* value = new TObjArray();
334                                 value->SetOwner(1);
335                                 for (Int_t ielement=0; ielement<nelements; ielement++){
336                                   TObjString* strobj = (new TObjString(((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()));
337                                         AliDebug(2,Form("Value at index %d = %s",nfixed+ielement+ientry*nValuesPerEntry,(strobj->String()).Data()));
338                                         value->Add(strobj);
339                                 }
340                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
341                                 array->Add(dcs);
342                                 delete value;
343                         }
344                         else{
345                                 AliError(Form("Non-expected type %s",type.Data()));
346                                 delete typeTokens;
347                                 delete tokens;  
348                                 delete tokenslines;
349                                 return NULL;
350                         } 
351                 }
352                 delete typeTokens;
353                 delete tokens;
354         }
355         delete tokenslines;
356         return array;
357 }
358
359
360
361
362
363
364