Fixes for bug #52499: Field polarities inconsistiency
[u/mrichter/AliRoot.git] / 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
35 #include "AliDCSArray.h"
36 #include "AliLHCReader.h"
37 #include "AliLog.h"
38
39 //--------------------------------------------------------------------------
40 AliLHCReader::AliLHCReader():
41         TObject(),
42         fStartTime(0),
43         fEndTime(0)
44 {
45         // default ctor 
46 }
47
48 //--------------------------------------------------------------------------
49 AliLHCReader::~AliLHCReader()
50 {
51         //
52         // dtor 
53         //
54 }
55
56 //--------------------------------------------------------------------------
57 TMap* AliLHCReader::ReadLHCDP(TString filename)
58 {
59         //
60         // reading the file with the inputs
61         //
62
63         if( gSystem->AccessPathName( filename.Data() ) ) {
64                 AliError(Form( "file (%s) not found", filename.Data() ) );
65                 return NULL;
66         }
67
68         ifstream *file = new ifstream ( filename.Data() );
69         if (!*file) {
70                 AliError(Form("Error opening file (%s) !",filename.Data()));
71                 file->close();
72                 delete file;
73                 return NULL;
74         }
75         TMap* mapLHC = new TMap();
76         mapLHC->SetOwner(1);
77         TString strLine;
78         while(strLine.ReadLine(*file)){
79                 // tokenize the line with tabs
80                 TObjArray* tokens = strLine.Tokenize(" \t");
81                 Int_t ntokens = tokens->GetEntriesFast();
82                 if (ntokens < 3){  
83                         // requiring at least the DP name, the format, and the number of entries
84                         delete tokens;
85                         continue;
86                 }
87                 TObjString* lhcDPname = (TObjString*)tokens->At(0);
88                 TString lhcDPtype = ((TObjString*)tokens->At(1))->String();
89                 AliDebug(2,Form("lhcDPtype = %s",lhcDPtype.Data()));
90                 TObjArray* typeTokens = lhcDPtype.Tokenize(":");
91                 if (typeTokens->GetEntriesFast() < 2 ){  
92                         // requiring the the type and the number of elements for each measurement
93                         AliError(Form("The format does not match the expected one, skipping the current line for DP = %s", lhcDPtype.Data()));
94                         delete typeTokens;
95                         continue;
96                 }
97                 TString type = ((TObjString*)typeTokens->At(0))->String();
98                 AliDebug(2,Form("type = %s",type.Data()));
99                 Int_t nelements = (((TObjString*)typeTokens->At(1))->String()).Atoi();
100                 AliDebug(2,Form("nelements = %i",nelements));
101                 Int_t nentries = (((TObjString*)tokens->At(2))->String()).Atoi();
102                 AliDebug(2,Form("nentries = %i",nentries));
103                 Int_t nValuesPerEntry = nelements+1;
104                 Int_t nfixed = 3; // n. of fixed entries
105                 TObjArray* array = new TObjArray();
106                 array->SetOwner(1);
107                 for (Int_t ientry=0; ientry< nentries; ientry ++){
108                         Int_t indextime = nfixed+nValuesPerEntry*ientry+nelements;
109                         TString strTimestamp = ((TObjString*)tokens->At(indextime))->String();
110                         TObjArray* timeTokens = strTimestamp.Tokenize(".");
111                         if (timeTokens->GetEntriesFast() < 2 ){  
112                                 // requiring both the seconds and the nseconds for the timestamp
113                                 AliError(Form("The timestamp format does not match the expected one, skipping entry %d for DP = %s", ientry, lhcDPtype.Data()));
114                                 continue;
115                         }
116                         time_t seconds = time_t((((TObjString*)timeTokens->At(0))->String()).Atoi());
117                         Int_t nseconds = Int_t((((TObjString*)timeTokens->At(1))->String()).Atoi());
118                         TTimeStamp* timestamp = new TTimeStamp(seconds, nseconds);
119                         AliDebug(2,Form("Timestamp in unix time = %d (s) = %d (ns)",timestamp->GetSec(),timestamp->GetNanoSec()));
120                         if (fStartTime!=0 && fEndTime!=0 && (fStartTime > (UInt_t)timestamp->GetSec() || fEndTime < (UInt_t)timestamp->GetSec())){
121                                 // error in case the measurement is not within the data taking time interval
122                                 AliError(Form("Timestamp for entry %d of DP %s not in [%d,%d]", ientry, lhcDPtype.Data(),fStartTime,fEndTime));
123                                 continue;
124                         }
125                         if (type == "i"){
126                                 Int_t* value = new Int_t[nelements];
127                                 for (Int_t ielement=0; ielement<nelements; ielement++){
128                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atoi();
129                                         AliDebug(2,Form("Value at index %d = %d",nfixed+ielement+ientry*nValuesPerEntry,value[ielement]));
130                                 }
131                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
132                                 array->Add(dcs);
133                         }
134                         else if (type == "f"){
135                                 Float_t* value = new Float_t[nelements];
136                                 for (Int_t ielement=0; ielement<nelements; ielement++){
137                                         value[ielement] = (((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String()).Atof();
138                                         AliDebug(2,Form("Value at index %d = %f",nfixed+ielement+ientry*nValuesPerEntry,value[ielement]));
139                                 } 
140                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
141                                 array->Add(dcs);
142                         } 
143                         else if (type == "s"){
144                                 TString* value = new TString[nelements];
145                                 for (Int_t ielement=0; ielement<nelements; ielement++){
146                                         value[ielement] = ((TObjString*)tokens->At(nfixed+ielement+ientry*nValuesPerEntry))->String();
147                                         AliDebug(2,Form("Value at index %d = %s",nfixed+ielement+ientry*nValuesPerEntry,value[ielement].Data()));
148                                 }
149                                 AliDCSArray* dcs = new AliDCSArray(nelements,value,timestamp);
150                                 array->Add(dcs);
151                         }
152                         else{
153                                 AliError("Non-expected type");
154                                 return NULL;
155                         } 
156                 }
157                 mapLHC->Add(lhcDPname,array);                   
158         }
159         return mapLHC;
160 }
161
162                                 
163
164
165
166
167
168