]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/CDB/AliDCSSensor.cxx
porting from the master branch
[u/mrichter/AliRoot.git] / STEER / CDB / AliDCSSensor.cxx
1 /**************************************************************************
2  * Copyright(c) 2006-07, 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 //                                                                            //
19 // Class describing time dependent values read from DCS sensors               //  
20 // (including pointers to graphs/fits)                                        //
21 // Authors: Marian Ivanov, Haavard Helstrup and Martin Siska                  //
22 //                                                                            //
23 ////////////////////////////////////////////////////////////////////////////////
24
25
26 #include "AliDCSSensor.h"
27 #include "TDatime.h"
28 #include "TCanvas.h"
29 ClassImp(AliDCSSensor)
30
31 const Double_t kSecInHour = 3600.; // seconds in one hour
32
33
34
35 AliDCSSensor::AliDCSSensor():
36   fId(),
37   fIdDCS(0),
38   fStringID(),
39   fStartTime(0),
40   fEndTime(0),
41   fGraph(0),
42   fFit(0),
43   fX(0),
44   fY(0),
45   fZ(0)
46 {
47   //
48   //  Standard constructor
49   //
50 }
51
52 AliDCSSensor::AliDCSSensor(const AliDCSSensor& source) :
53    TNamed(source),
54    fId(source.fId),
55    fIdDCS(source.fIdDCS),
56    fStringID(source.fStringID),
57    fStartTime(source.fStartTime),
58    fEndTime(source.fEndTime),
59    fGraph(0),
60    fFit(0),
61    fX(source.fX),
62    fY(source.fY),
63    fZ(source.fZ)
64 //
65 //  Copy constructor
66 //
67
68    if (source.fGraph) fGraph = (TGraph*)source.fGraph->Clone();
69    if (source.fFit) fFit = (AliSplineFit*)source.fFit->Clone();
70 }
71
72 AliDCSSensor& AliDCSSensor::operator=(const AliDCSSensor& source){
73 //
74 // assignment operator
75 //
76   if (&source == this) return *this;
77   new (this) AliDCSSensor(source);
78
79   return *this;
80 }
81
82
83 void AliDCSSensor::Print(const Option_t* option) const{
84   //
85   // print function
86   //  
87   TString opt = option; opt.ToLower();
88   printf("%s:%s\n",GetTitle(), GetName());
89   printf("%s\n",fStringID.Data());
90
91 }
92
93 void AliDCSSensor::Draw(Option_t* option) {
94   //
95   // draw function - to viusalize sensor
96   // Unfortuantelly - it  make a memory leak as function Draw does not return the object pointer
97   //
98   TCanvas * canvas = new TCanvas((fStringID+option).Data(), (fStringID+option).Data()); 
99   if (fGraph){
100     // transform points to time in s
101     Int_t npoints = fGraph->GetN();
102     for (Int_t i=0; i<npoints; i++){
103       fGraph->GetX()[i]=fGraph->GetX()[i]*3600+fStartTime;
104     }
105     fGraph->Draw("alp");
106     return;
107   }
108   canvas->cd();
109   TGraph * graph = MakeGraph(100);  // memory leak - we can not modify the content - const method
110   graph->Draw(option);              // 
111   //
112 }
113
114
115
116 //_____________________________________________________________________________
117 Double_t AliDCSSensor::GetValue(UInt_t timeSec)
118 {
119  // 
120  // Get DCS value for actual sensor
121  //  timeSec given as offset from start-of-map measured in seconds
122  //  *NOTE* In the current TPC setup, start-of-map is defined as the 
123  //         first measured point for each sensor. This will be different
124  //         for each sensor in the array. If you want to get a value at the 
125  //         same absolute time, use AliDCSSensor::GetValue(TTimeStamp time)
126  //         or AliDCSSensorArray::GetValue (UInt_t timeSec, Int_t sensor)
127  //         which measure offsets with respect to the (global) start-of-run
128  //
129  Bool_t inside=kTRUE;
130  return Eval(TTimeStamp((time_t)(fStartTime+timeSec),0),inside);
131 }
132 //_____________________________________________________________________________
133 Double_t AliDCSSensor::GetValue(TTimeStamp time) 
134 {
135  // Get DCS value for actual sensor
136  //  time given as absolute TTimeStamp
137  //
138  Bool_t inside=kTRUE;
139  return Eval(time, inside);
140 }
141
142 //_____________________________________________________________________________
143
144 Double_t AliDCSSensor::Eval(const TTimeStamp& time, Bool_t& inside) const
145 {
146   // 
147   // Return DCS value at given time
148   //  The value is calculated from the AliSplineFit, if a fit is not available 
149   //    the most recent reading from the Graph of DCS points is returned (if 
150   //    the graph is present)
151   //  If time < start of map  return value at start of map, inside = false
152   //  If time > end of map    return value at end of map, inside = false
153   
154   UInt_t timeSec = time.GetSec();
155   UInt_t diff = timeSec-fStartTime;
156   inside = true;
157   
158   if ( timeSec < fStartTime ) { 
159      inside=false;
160      diff=0;
161   }
162   if ( timeSec > fEndTime ) {
163      inside=false;
164      diff = fEndTime-fStartTime;
165   }
166  
167   Double_t timeHour = diff/kSecInHour;
168   if ( fFit ) {
169      return fFit->Eval(timeHour); 
170   } else {
171      if ( fGraph ) {
172        return EvalGraph(timeHour);
173      } else {  
174        return -99;
175      }
176   }
177 }
178 //_____________________________________________________________________________
179
180 Double_t AliDCSSensor::EvalGraph(const TTimeStamp& time, Bool_t& inside) const
181 {
182   // 
183   // Return DCS value from graph of DCS points (i.e return last reading before
184   //  the time specified by TTimeStamp
185   //  If time < start of map  return value at start of map, inside = false
186   //  If time > end of map    return value at end of map, inside = false
187   
188   UInt_t timeSec = time.GetSec();
189   UInt_t diff = timeSec-fStartTime;
190   inside = true;
191   
192   if ( timeSec < fStartTime ) { 
193      inside=false;
194      diff=0;
195   }
196   if ( timeSec > fEndTime ) {
197      inside=false;
198      diff = fEndTime-fStartTime;
199   }
200  
201   Double_t timeHour = diff/kSecInHour;
202   if ( fGraph ) {
203      return EvalGraph(timeHour);
204   } else {  
205      return -99;
206   }  
207 }
208 //_____________________________________________________________________________
209 Double_t AliDCSSensor::EvalGraph(const Double_t& timeHour) const 
210 {
211   //
212   // Extract last value in graph observed before time given by timeHour
213   //
214
215   // return -99 if point specified is before beginning of graph
216   Double_t x=0; Double_t y=0;
217   fGraph->GetPoint(0,x,y);
218   if ( timeHour < x ) return -99;
219   
220   // return previous point when first time > timeHour is observed
221   
222   Int_t npoints = fGraph->GetN();
223   for (Int_t i=1; i<npoints; i++) {
224      fGraph->GetPoint(i,x,y);
225      if ( timeHour < x ) {
226        fGraph->GetPoint(i-1,x,y);
227        return y;
228      }
229   }
230   
231   // return last point if all times are < timeHour
232   return y;
233
234         
235
236 //_____________________________________________________________________________
237 TGraph* AliDCSSensor::MakeGraph(Int_t nPoints, Bool_t debug) const
238 {
239   //
240   // Make graph from start time to end time of DCS values 
241   //
242
243  
244
245   UInt_t stepTime = (fEndTime-fStartTime)/nPoints;
246   
247   if (debug==kTRUE) {
248      printf ("Start time %d, End time %d, step time %d\n",
249      fStartTime,fEndTime,stepTime);
250      TTimeStamp t((time_t)fStartTime,0); t.Print();
251      TTimeStamp t2((time_t)fEndTime,0); t2.Print();
252   }     
253   
254   if ( !fFit ) return 0;
255
256   Double_t *x = new Double_t[nPoints+1];
257   Double_t *y = new Double_t[nPoints+1];
258   for (Int_t ip=0; ip<nPoints; ip++) {
259     x[ip] = (time_t)(fStartTime+ip*stepTime);
260     y[ip] = fFit->Eval(ip*stepTime/kSecInHour);
261     if (debug==kTRUE) {
262      TTimeStamp t3((time_t)x[ip],0); 
263      printf ("x=%f, y=%f  ",x[ip],y[ip]);
264      t3.Print();
265     }
266   }
267   
268   TGraph *graph = new TGraph(nPoints,x,y);
269   delete [] x;
270   delete [] y;
271   
272   graph->GetXaxis()->SetTimeDisplay(1);
273   graph->GetXaxis()->SetLabelOffset(0.02);
274   graph->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}");
275
276   return graph;
277 }
278
279 //_____________________________________________________________________________
280
281 TClonesArray * AliDCSSensor::ReadTree(TTree* tree) {
282   //
283   // read values from ascii file
284   //
285
286   Int_t nentries = tree->GetEntries();
287
288   char stringId[100];
289   Int_t num=0;
290   Int_t idDCS=0;
291   Double_t x=0;
292   Double_t y=0;
293   Double_t z=0;
294
295   tree->SetBranchAddress("StringID",&stringId);
296   tree->SetBranchAddress("IdDCS",&idDCS);
297   tree->SetBranchAddress("Num",&num);
298   tree->SetBranchAddress("X",&x);
299   tree->SetBranchAddress("Y",&y);
300   tree->SetBranchAddress("Z",&z);
301
302   // firstSensor = (Int_t)tree->GetMinimum("ECha");
303   // lastSensor = (Int_t)tree->GetMaximum("ECha");
304
305   TClonesArray * array = new TClonesArray("AliDCSSensor",nentries);
306    printf ("nentries = %d\n",nentries);
307
308   for (Int_t isensor=0; isensor<nentries; isensor++){
309     AliDCSSensor * sens = new ((*array)[isensor])AliDCSSensor;
310     tree->GetEntry(isensor);
311     sens->SetId(isensor);
312     sens->SetIdDCS(idDCS);
313     sens->SetStringID(TString(stringId));
314     sens->SetX(x);
315     sens->SetY(y);
316     sens->SetZ(z);
317
318   }
319   return array;
320 }
321