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