]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliDCSSensorArray.cxx
(Haavard)
[u/mrichter/AliRoot.git] / STEER / AliDCSSensorArray.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 //                                                                           //
19 //  Calibration class for DCS sensors                                        //
20 //  Authors: Marian Ivanov and Haavard Helstrup                              //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include "AliDCSSensorArray.h"
25
26 ClassImp(AliDCSSensorArray)
27
28 const Int_t  kMinGraph = 10;       // minimum #points of graph to be fitted
29 const Int_t  kMinPoints = 10;      // minimum number of points per knot in fit
30 const Int_t  kIter = 10;           // number of iterations for spline fit
31 const Double_t  kMaxDelta = 0.00;  // precision parameter for spline fit
32 const Int_t  kFitReq = 2;          // fit requirement, 2 = continuous 2nd derivative
33 const Double_t kSecInHour = 3600.; // seconds in one hour
34
35 //_____________________________________________________________________________
36 AliDCSSensorArray::AliDCSSensorArray():TNamed(), 
37   fStartTime (2000,1,1,0,0,0),
38   fEndTime   (2000,1,1,0,0,0),
39   fSensors(0)
40 {
41   //
42   // AliDCSSensorArray default constructor
43   //
44
45 }
46 //_____________________________________________________________________________
47 AliDCSSensorArray::AliDCSSensorArray(Int_t prevRun, const char* dbEntry) : 
48   TNamed(),
49   fStartTime (2000,1,1,0,0,0),
50   fEndTime   (2000,1,1,0,0,0),
51   fSensors(0)
52 {
53   //
54   // Read positions etc. from data base entry for previous run
55   // Delete all fits and graphs
56   //
57    AliDCSSensorArray *temp=0;
58    AliCDBEntry* entry = 
59        AliCDBManager::Instance()->Get(dbEntry,prevRun); 
60    if (entry){
61      entry->SetOwner(kTRUE);
62      temp = (AliDCSSensorArray*)entry->GetObject();
63    } 
64
65    TGraph *gr;
66    AliSplineFit* fit;
67
68 // Delete previous values
69    
70    Int_t nsensors = temp->fSensors->GetEntries();
71    for ( Int_t isensor=0; isensor<nsensors; isensor++) {
72      AliDCSSensor *entry = (AliDCSSensor*)temp->fSensors->At(isensor);
73      gr = entry->GetGraph();
74      if ( gr != 0 ) {
75        delete gr;
76        gr = 0;
77      }
78      fit = entry->GetFit();
79      if ( fit != 0 ) {
80        delete fit;
81        fit = 0;
82      }
83    }    
84    
85    new (this) AliDCSSensorArray(*temp);
86    delete temp;
87 }
88
89
90
91 //_____________________________________________________________________________
92 AliDCSSensorArray::AliDCSSensorArray(const AliDCSSensorArray &c):TNamed(c),
93   fStartTime (c.fStartTime),
94   fEndTime   (c.fEndTime),
95   fSensors(0)
96
97 {
98   //
99   // AliDCSSensorArray copy constructor
100   //
101
102   ((AliDCSSensorArray &) c).Copy(*this);
103
104 }
105
106 ///_____________________________________________________________________________
107 AliDCSSensorArray::~AliDCSSensorArray()
108 {
109   //
110   // AliDCSSensorArray destructor
111   //
112   fSensors->Delete();
113   delete fSensors;
114
115 }
116
117 //_____________________________________________________________________________
118 AliDCSSensorArray &AliDCSSensorArray::operator=(const AliDCSSensorArray &c)
119 {
120   //
121   // Assignment operator
122   //
123
124   if (this != &c) ((AliDCSSensorArray &) c).Copy(*this);
125   return *this;
126
127 }
128
129 //_____________________________________________________________________________
130 void AliDCSSensorArray::Copy(TObject &c) const
131 {
132   //
133   // Copy function
134   //
135
136   TObject::Copy(c);
137 }
138 //_____________________________________________________________________________
139 void AliDCSSensorArray::SetGraph(TMap *map, const char *amandaString) 
140 {
141   // 
142   // Read graphs from DCS maps 
143   //
144   char dname[100];
145   Int_t nsensors = fSensors->GetEntries();
146   for ( Int_t isensor=0; isensor<nsensors; isensor++) {
147     AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
148     Int_t dcsSensor=entry->GetIdDCS();
149     sprintf(dname,amandaString,dcsSensor);
150     TGraph *gr = (TGraph*)map->GetValue(dname);
151     entry->SetGraph(gr);
152   } 
153 }  
154 //_____________________________________________________________________________
155 void AliDCSSensorArray::MakeSplineFit(TMap *map, const char *amandaString,
156                                                                Bool_t keepMap) 
157 {
158   // 
159   // Make spline fits from DCS maps 
160   //
161   char dname[100];
162   Int_t nsensors = fSensors->GetEntries();
163   for ( Int_t isensor=0; isensor<nsensors; isensor++) {
164     AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
165     Int_t dcsSensor=entry->GetIdDCS();
166     sprintf(dname,amandaString,dcsSensor);
167     TGraph *gr = (TGraph*)map->GetValue(dname);
168     if (gr->GetN() < kMinGraph ) continue;    
169     AliSplineFit *fit = new AliSplineFit();
170     fit->InitKnots(gr,kMinPoints,kIter,kMaxDelta);
171     fit->SplineFit(kFitReq);
172     entry->SetStartTime(fStartTime);
173     entry->SetEndTime(fEndTime);
174     fit->Cleanup();
175     entry->SetFit(fit);
176     if (keepMap) entry->SetGraph(gr);
177   } 
178 }  
179
180 //_____________________________________________________________________________
181 Double_t AliDCSSensorArray::GetValue(UInt_t timeSec, Int_t sensor) 
182 {
183   // 
184   // Return sensor value at time timeSec (obtained from fitted function)
185   //  timeSec = time in seconds from start of run
186   //
187   AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(sensor);
188   return entry->GetValue(timeSec);
189 }
190     
191
192 //_____________________________________________________________________________
193 TMap* AliDCSSensorArray::ExtractDCS(TMap *dcsMap, const char *amandaString) 
194 {
195  //
196  // Extract temperature graphs from DCS maps
197  //
198  TMap *values = new TMap;
199  TObjArray * valueSet;
200  Int_t nsensors = fSensors->GetEntries();
201  for ( Int_t isensor=0; isensor<nsensors; isensor++) {
202    AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
203    Int_t dcsSensor=entry->GetIdDCS();
204    TString DPname = Form (amandaString,dcsSensor);
205    TPair *pair = (TPair*)dcsMap->FindObject(DPname.Data());
206    valueSet = (TObjArray*)pair->Value();
207    TGraph *graph = MakeGraph(valueSet);
208    values->Add(new TObjString(DPname.Data()),graph);
209  }
210  return values;
211 }
212
213 //_____________________________________________________________________________
214 TGraph* AliDCSSensorArray::MakeGraph(TObjArray* valueSet){
215   //
216   // Make graph of temperature values read from DCS map
217   //   (spline fit parameters will subsequently be obtained from this graph) 
218   //
219   Int_t nentries = valueSet->GetEntriesFast(); 
220   Float_t *x = new Float_t[nentries];
221   Float_t *y = new Float_t[nentries];
222   Int_t time0=0;
223   Int_t out=0;
224   Int_t skipped=0;
225   Float_t value;  
226   for (Int_t i=0; i<nentries; i++){
227     AliDCSValue * val = (AliDCSValue *)valueSet->At(i);
228     if (!val) continue;
229     if (time0==0){
230       time0=val->GetTimeStamp();
231     }
232     value = val->GetFloat();
233     if (TMath::Abs(value)>100) continue;   // refuse values exceeding 100
234     if ( out>0 && skipped<10 && TMath::Abs(value-y[out-1])>5) {
235       skipped++;                               // refuse temperatures changing 
236       continue;                                // by > 5 degrees in one time step
237     }                                          
238     skipped=0;                                        
239     if (val->GetTimeStamp()-time0>1000000) continue;
240     x[out] = (val->GetTimeStamp()-time0)/kSecInHour; // give times in fractions of hours 
241     y[out] = val->GetFloat();
242     out++;
243     
244   }
245   TGraph * graph = new TGraph(out,x,y);
246   delete [] x;
247   delete [] y;
248   return graph;
249 }
250   
251 //_____________________________________________________________________________
252 AliDCSSensor* AliDCSSensorArray::GetSensor(Int_t IdDCS) 
253 {
254  //
255  //  Return sensor information for sensor specified by IdDCS
256  //
257  Int_t nsensors = fSensors->GetEntries();
258  for (Int_t isensor=0; isensor<nsensors; isensor++) {
259    AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
260    if (entry->GetIdDCS() == IdDCS) return entry;
261  }
262  return 0;
263 }
264 //_____________________________________________________________________________
265 AliDCSSensor* AliDCSSensorArray::GetSensor(Double_t x, Double_t y, Double_t z) 
266 {
267  //
268  //  Return sensor closest to given position
269  //
270  Int_t nsensors = fSensors->GetEntries();
271  Double_t dist2min=1e99;
272  Double_t xs,ys,zs,dist2;
273  Int_t ind=-1;
274  for (Int_t isensor=0; isensor<nsensors; isensor++) {
275    AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
276    xs = entry->GetX();
277    ys = entry->GetY();
278    zs = entry->GetZ();
279    dist2 = (x-xs)*(x-xs) + (y-ys)*(y-ys) + (z-zs)*(z-zs);
280    if (dist2 < dist2min) {
281       ind=isensor;
282       dist2min = dist2;
283    } 
284  }
285  if ( ind >= 0 ) {
286     return (AliDCSSensor*)fSensors->At(ind);
287  } else {
288     return 0;
289  }
290 }
291
292 AliDCSSensor* AliDCSSensorArray::GetSensorNum(Int_t ind) 
293 {
294  //
295  //  Return sensor given by array index
296  //
297  return (AliDCSSensor*)fSensors->At(ind);
298 }
299
300 Int_t AliDCSSensorArray::GetFirstIdDCS() const
301 {
302  //
303  //  Return DCS Id of first sensor
304  //
305  if ( fSensors != 0 ) {
306     return ((AliDCSSensor*)fSensors->At(0))->GetIdDCS();
307  } else {
308     return 0;
309  }
310 }
311
312 Int_t AliDCSSensorArray::GetLastIdDCS() const 
313 {
314  //
315  //  Return DCS Id of last sensor
316  //
317  if ( fSensors != 0 ) {
318     Int_t last = fSensors->GetEntries();
319     return ((AliDCSSensor*)fSensors->At(last-1))->GetIdDCS();
320  } else {
321     return 0;
322  }
323 }