1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
17 ///////////////////////////////////////////////////////////////////////////////
19 // Calibration class for DCS sensors //
20 // Authors: Marian Ivanov and Haavard Helstrup //
22 ///////////////////////////////////////////////////////////////////////////////
24 #include "AliDCSSensorArray.h"
28 ClassImp(AliDCSSensorArray)
30 const Double_t kSecInHour = 3600.; // seconds in one hour
31 const UInt_t kMinMapTime = 60; // don't fit maps shorter than one minute
33 //_____________________________________________________________________________
34 AliDCSSensorArray::AliDCSSensorArray():TNamed(),
42 fStartTime (2000,1,1,0,0,0),
43 fEndTime (2000,1,1,0,0,0),
47 // AliDCSSensorArray default constructor
51 //_____________________________________________________________________________
52 AliDCSSensorArray::AliDCSSensorArray(TClonesArray *arr):TNamed(),
60 fStartTime (2000,1,1,0,0,0),
61 fEndTime (2000,1,1,0,0,0),
65 // AliDCSSensorArray special constructor taking TClonesArray from ReadList
69 //_____________________________________________________________________________
70 AliDCSSensorArray::AliDCSSensorArray(Int_t run, const char* dbEntry) :
79 fStartTime (2000,1,1,0,0,0),
80 fEndTime (2000,1,1,0,0,0),
84 // Read configuration from OCDB
87 AliCDBEntry *entry = AliCDBManager::Instance()->Get(dbEntry,run);
88 TTree *tree = (TTree*) entry->GetObject();
89 fSensors = AliDCSSensor::ReadTree(tree);
91 //_____________________________________________________________________________
92 AliDCSSensorArray::AliDCSSensorArray(UInt_t startTime, UInt_t endTime,
102 fStartTime (2000,1,1,0,0,0),
103 fEndTime (2000,1,1,0,0,0),
108 // AliDCSSensorArray constructor for Shuttle preprocessor
109 // (confTree read from OCDB)
111 fSensors = AliDCSSensor::ReadTree(confTree);
112 fSensors->BypassStreamer(kFALSE);
113 fStartTime = TTimeStamp((time_t)startTime,0);
114 fEndTime = TTimeStamp((time_t)endTime,0);
118 //_____________________________________________________________________________
119 AliDCSSensorArray::AliDCSSensorArray(UInt_t startTime, UInt_t endTime,
120 TClonesArray *sensors) :
129 fStartTime (2000,1,1,0,0,0),
130 fEndTime (2000,1,1,0,0,0),
135 // AliDCSSensorArray constructor for Shuttle preprocessor
136 // (TClonesArray of AliDCSSensor objects)
138 fStartTime = TTimeStamp((time_t)startTime,0);
139 fEndTime = TTimeStamp((time_t)endTime,0);
142 //_____________________________________________________________________________
143 AliDCSSensorArray::AliDCSSensorArray(const AliDCSSensorArray &c):TNamed(c),
144 fMinGraph(c.fMinGraph),
145 fMinPoints(c.fMinPoints),
147 fMaxDelta(c.fMaxDelta),
150 fDiffCut(c.fDiffCut),
151 fStartTime (c.fStartTime),
152 fEndTime (c.fEndTime),
157 // AliDCSSensorArray copy constructor
160 fSensors = (TClonesArray*)c.fSensors->Clone();
163 ///_____________________________________________________________________________
164 AliDCSSensorArray::~AliDCSSensorArray()
167 // AliDCSSensorArray destructor
174 //_____________________________________________________________________________
175 AliDCSSensorArray &AliDCSSensorArray::operator=(const AliDCSSensorArray &c)
178 // Assignment operator
182 new (this) AliDCSSensorArray(c);
183 fSensors = (TClonesArray*)c.fSensors->Clone();
188 //____________________________________________________________________________
190 void AliDCSSensorArray::SetGraph(TMap *map)
193 // Read graphs from DCS maps
195 Int_t nsensors = fSensors->GetEntries();
196 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
197 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
198 TString stringID = entry->GetStringID();
199 TGraph *gr = (TGraph*)map->GetValue(stringID.Data());
201 entry->SetGraph((TGraph*)gr->Clone());
207 //_____________________________________________________________________________
208 void AliDCSSensorArray::MakeSplineFit(TMap *map, Bool_t keepMap)
211 // Make spline fits from DCS maps
213 Int_t nsensors = fSensors->GetEntries();
214 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
215 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
216 TString stringID = entry->GetStringID();
217 TGraph *gr = (TGraph*)map->GetValue(stringID.Data());
221 AliWarning(Form("sensor %s: no input graph",stringID.Data()));
224 UInt_t timeDiff = entry->GetEndTime() - entry->GetStartTime();
225 if ( timeDiff < kMinMapTime ) {
226 AliWarning(Form("sensor %s: map length < 60 s, DCS graph kept.",stringID.Data()));
227 entry->SetGraph((TGraph*)gr->Clone());
229 AliSplineFit *fit = new AliSplineFit();
230 fit->SetMinPoints(fMinGraph);
231 fit->InitKnots(gr,fMinPoints,fIter,fMaxDelta);
232 fit->SplineFit(fFitReq);
237 AliWarning(Form("sensor %s: no fit performed, DCS graph kept.",stringID.Data()));
238 entry->SetGraph((TGraph*)gr->Clone());
241 if (keepMap) entry->SetGraph((TGraph*)gr->Clone());
244 //_____________________________________________________________________________
245 void AliDCSSensorArray::MakeSplineFitAddPoints(TMap *map)
248 // Make spline fits from DCS maps
250 Int_t nsensors = fSensors->GetEntries();
251 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
252 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
254 // fetch old points from existing graph
256 TGraph *gr = entry->GetGraph();
261 TString stringID = entry->GetStringID();
263 // fetch new points from DCS map
265 TGraph *grAdd = (TGraph*)map->GetValue(stringID.Data());
268 // add new points to end of graph
270 Int_t nPointsOld=gr->GetN();
271 Int_t nPointsAdd=grAdd->GetN();
272 gr->Expand(nPointsOld+nPointsAdd);
273 gr->Set(nPointsOld+nPointsAdd);
274 Double_t *addX=grAdd->GetX();
275 Double_t *addY=grAdd->GetY();
276 for (Int_t i=0;i<nPointsAdd;i++) {
277 gr->SetPoint(nPointsOld+i,addX[i],addY[i]);
280 // make fit to complete graph
282 AliSplineFit *fit = new AliSplineFit();
283 fit->SetMinPoints(fMinGraph);
284 fit->InitKnots(gr,fMinPoints,fIter,fMaxDelta);
285 fit->SplineFit(fFitReq);
288 AliSplineFit *oldFit = entry->GetFit();
289 if (oldFit) delete oldFit;
292 AliWarning(Form("sensor %s: no new fit performed. If available, old fit kept.",stringID.Data()));
297 //_____________________________________________________________________________
298 Int_t AliDCSSensorArray::NumFits() const
301 // Return number of sensors where a succesful fit has been made
304 Int_t nsensors = fSensors->GetEntries();
305 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
306 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
307 if (entry->GetFit()) nfit++;
311 //_____________________________________________________________________________
312 Double_t AliDCSSensorArray::GetValue(UInt_t timeSec, Int_t sensor)
315 // Return sensor value at time timeSec (obtained from fitted function)
316 // timeSec = time in seconds from start of run
319 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(sensor);
320 return entry->GetValue(TTimeStamp((time_t)fStartTime.GetSec()+timeSec,0));
324 //_____________________________________________________________________________
325 TMap* AliDCSSensorArray::ExtractDCS(TMap *dcsMap, Bool_t keepStart)
328 // Extract temperature graphs from DCS maps
330 TMap *values = new TMap;
331 TObjArray * valueSet;
333 // Keep global start/end times
334 // to avoid extrapolations, the fits will only be valid from first
335 // measured point to last measured point. This is consistent with hardware,
336 // as there would be a new measured point if the value changed.
338 TTimeStamp startTime=fStartTime;
339 TTimeStamp endTime=fEndTime;
341 Int_t nsensors = fSensors->GetEntries();
342 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
343 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
344 TString stringID = entry->GetStringID();
345 TPair *pair = (TPair*)dcsMap->FindObject(stringID.Data());
346 if ( pair ) { // only try to read values
347 // if DCS object available
348 valueSet = (TObjArray*)pair->Value();
349 TGraph *graph = MakeGraph(valueSet,keepStart); // MakeGraph sets start/end time
351 values->Add(new TObjString(stringID.Data()),graph);
352 entry->SetStartTime(fStartTime);
353 entry->SetEndTime(fEndTime);
356 // Reset global start/end time
357 // ..... yes, I know this won't get a prize for structured programming..:-)
359 fStartTime=startTime;
365 //_____________________________________________________________________________
366 TGraph* AliDCSSensorArray::MakeGraph(TObjArray* valueSet, Bool_t keepStart){
368 // Make graph of temperature values read from DCS map
369 // (spline fit parameters will subsequently be obtained from this graph)
371 Int_t nentries = valueSet->GetEntriesFast();
372 if ( nentries == 0 ) return 0;
374 Float_t *x = new Float_t[nentries];
375 Float_t *y = new Float_t[nentries];
376 Int_t time0=0, previousTime=0;
377 TTimeStamp firstTime(0);
378 TTimeStamp lastTime(0);
380 firstTime = fStartTime;
381 time0 = firstTime.GetSec();
385 AliDCSValue *val = (AliDCSValue *)valueSet->At(0);
386 AliDCSValue::Type type = val->GetType();
387 if ( type == AliDCSValue::kInvalid || type == AliDCSValue::kBool ) return 0;
389 for (Int_t i=0; i<nentries; i++){
390 val = (AliDCSValue *)valueSet->At(i);
393 time0=val->GetTimeStamp();
394 firstTime= TTimeStamp((time_t)val->GetTimeStamp(),0);
395 lastTime=TTimeStamp((time_t)val->GetTimeStamp(),0);
399 case AliDCSValue::kFloat:
400 value = val->GetFloat();
402 case AliDCSValue::kChar:
403 value = static_cast<Float_t>(val->GetChar());
405 case AliDCSValue::kInt:
406 value = static_cast<Float_t>(val->GetInt());
408 case AliDCSValue::kUInt:
409 value = static_cast<Float_t>(val->GetUInt());
414 if (fValCut>0 && TMath::Abs(value)>fValCut) continue; // refuse values greater than cut
416 if ( out>0 && skipped<10 && TMath::Abs(value-y[out-1])>fDiffCut) {
417 skipped++; // refuse values changing
418 continue; // by > cut in one time step
422 if (val->GetTimeStamp()-time0>1000000) continue;
423 if (val->GetTimeStamp()-previousTime < 1 ) continue; // refuse duplicate recordings
424 previousTime=val->GetTimeStamp();
425 lastTime=TTimeStamp((time_t)val->GetTimeStamp(),0);
426 x[out] = (val->GetTimeStamp()-time0)/kSecInHour; // give times in fractions of hours
427 y[out] = val->GetFloat();
430 if (!keepStart) fStartTime=firstTime;
432 TGraph * graph = new TGraph(out,x,y);
438 //_____________________________________________________________________________
439 void AliDCSSensorArray::RemoveGraphDuplicates(Double_t tolerance){
441 // Remove points with same y value as the previous measured point
442 // (to save space for non-fitted graphs -- i.e. last measured point used)
444 Int_t nsensors = fSensors->GetEntries();
445 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
446 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
447 TGraph *graph = entry->GetGraph();
448 Double_t x=-999.,y=-999., x0=-999.,y0=-999.;
450 Int_t npoints=graph->GetN();
452 for (Int_t i=npoints-1;i>0;i--) {
453 graph->GetPoint(i,x,y);
454 graph->GetPoint(i-1,x0,y0);
455 if ( TMath::Abs(y-y0) < TMath::Abs(tolerance*y0) ) graph->RemovePoint(i);
463 //_____________________________________________________________________________
464 AliDCSSensor* AliDCSSensorArray::GetSensor(Int_t IdDCS)
467 // Return sensor information for sensor specified by IdDCS
469 Int_t nsensors = fSensors->GetEntries();
470 for (Int_t isensor=0; isensor<nsensors; isensor++) {
471 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
472 if (entry->GetIdDCS() == IdDCS) return entry;
476 //_____________________________________________________________________________
477 AliDCSSensor* AliDCSSensorArray::GetSensor(const TString& stringID)
480 // Return sensor information for sensor specified by stringID
482 Int_t nsensors = fSensors->GetEntries();
483 for (Int_t isensor=0; isensor<nsensors; isensor++) {
484 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
485 if (entry->GetStringID() == stringID) return entry;
489 //_____________________________________________________________________________
490 AliDCSSensor* AliDCSSensorArray::GetSensor(Double_t x, Double_t y, Double_t z)
493 // Return sensor closest to given position
495 Int_t nsensors = fSensors->GetEntries();
496 Double_t dist2min=1e99;
497 Double_t xs,ys,zs,dist2;
499 for (Int_t isensor=0; isensor<nsensors; isensor++) {
500 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
504 dist2 = (x-xs)*(x-xs) + (y-ys)*(y-ys) + (z-zs)*(z-zs);
505 if (dist2 < dist2min) {
511 return (AliDCSSensor*)fSensors->At(ind);
516 //_____________________________________________________________________________
517 AliDCSSensor* AliDCSSensorArray::GetSensorNum(Int_t ind)
520 // Return sensor given by array index
522 return (AliDCSSensor*)fSensors->At(ind);
525 //_____________________________________________________________________________
526 Int_t AliDCSSensorArray::SetSensor(const TString& stringID,
527 const AliDCSSensor& sensor)
530 // Update sensor information for sensor specified by stringID
532 Int_t nsensors = fSensors->GetEntries();
533 for (Int_t isensor=0; isensor<nsensors; isensor++) {
534 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
535 if (entry->GetStringID() == stringID)
537 new ((*fSensors)[isensor])AliDCSSensor(sensor);
543 //_____________________________________________________________________________
544 void AliDCSSensorArray::SetSensorNum(const Int_t ind, const AliDCSSensor& sensor)
547 // Update sensor information for sensor at index ind
549 new ((*fSensors)[ind])AliDCSSensor(sensor);
552 //_____________________________________________________________________________
553 void AliDCSSensorArray::RemoveSensorNum(Int_t ind)
556 // Return sensor given by array index
559 delete fSensors->RemoveAt(ind);
560 fSensors->Compress();
562 //_____________________________________________________________________________
563 void AliDCSSensorArray::RemoveSensor(Int_t IdDCS)
566 // Deletes Sensor by given IdDCS
569 Int_t nsensors = fSensors->GetEntries();
570 for (Int_t isensor=0; isensor<nsensors; isensor++) { // loop over sensors
571 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
572 if (entry->GetIdDCS()==IdDCS) {
573 delete fSensors->RemoveAt(isensor);
577 fSensors->Compress();
579 //_____________________________________________________________________________
580 TArrayI AliDCSSensorArray::OutsideThreshold(Double_t threshold, UInt_t timeSec, Bool_t below) const
583 // Return sensors with values outside threshold at time defined by second
585 // By default sensors with values below threshold are listed, if third
586 // parameter is set to kFALSE sensors with values above threshold are listed
588 Int_t nsensors = fSensors->GetEntries();
589 TArrayI array(nsensors);
591 for (Int_t isensor=0; isensor<nsensors; isensor++) { // loop over sensors
592 AliDCSSensor *entry = (AliDCSSensor*)fSensors->At(isensor);
593 Double_t val=entry->GetValue(timeSec);
595 if (val<threshold) array[outside++] = entry->GetIdDCS();
597 if (val>threshold) array[outside++] = entry->GetIdDCS();
604 //_____________________________________________________________________________
605 Int_t AliDCSSensorArray::GetFirstIdDCS() const
608 // Return DCS Id of first sensor
610 if ( fSensors != 0 ) {
611 return ((AliDCSSensor*)fSensors->At(0))->GetIdDCS();
617 //_____________________________________________________________________________
618 Int_t AliDCSSensorArray::GetLastIdDCS() const
621 // Return DCS Id of last sensor
623 if ( fSensors != 0 ) {
624 Int_t last = fSensors->GetEntries();
625 return ((AliDCSSensor*)fSensors->At(last-1))->GetIdDCS();
630 //_____________________________________________________________________________
631 void AliDCSSensorArray::ClearGraph()
634 // Delete DCS graphs from all sensors in array
637 Int_t nsensors = fSensors->GetEntries();
638 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
639 AliDCSSensor *sensor = (AliDCSSensor*)fSensors->At(isensor);
640 TGraph *gr = sensor->GetGraph();
648 //_____________________________________________________________________________
649 void AliDCSSensorArray::ClearFit()
652 // Delete spline fits from all sensors in array
655 Int_t nsensors = fSensors->GetEntries();
656 for ( Int_t isensor=0; isensor<nsensors; isensor++) {
657 AliDCSSensor *sensor = (AliDCSSensor*)fSensors->At(isensor);
658 AliSplineFit *fit = sensor->GetFit();
666 //_____________________________________________________________________________
667 void AliDCSSensorArray::AddSensors(AliDCSSensorArray *newSensors)
670 // add sensors from two sensor arrays
673 Int_t numNew = newSensors->NumSensors();
674 Int_t numOld = fSensors->GetEntries();
675 fSensors->Expand(numOld+numNew);
676 for (Int_t i=0;i<numNew;i++) {
677 AliDCSSensor *sens = newSensors->GetSensorNum(i);
678 new ((*fSensors)[numOld+i]) AliDCSSensor(*sens);