]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDDataDCS.cxx
New DCS code by Wilfried
[u/mrichter/AliRoot.git] / TRD / AliTRDDataDCS.cxx
1
2
3 #include <TGraph.h>
4 #include <TObjArray.h>
5 #include <AliCDBMetaData.h>
6 #include <TMap.h>
7
8 #include "AliDCSValue.h"
9 #include "AliDCSValue.h"
10 #include "AliLog.h"
11 #include "AliTRDDataDCS.h"
12 #include "AliSplineFit.h"
13
14 ClassImp(AliTRDDataDCS)
15
16
17 /*
18 Typical use :
19
20 AliTRDDataDCS dataDcs;
21 dataDcs.ExtractDCS (dcs);
22 ->call a storeRef function
23 dataDcs.PerformFit ();
24 dataDcs.ClearGraphs ();
25 ->call a store function
26 dataDcs.ClearFits ();
27
28 */
29
30 //_____________________________________________________________________________
31 AliTRDDataDCS::AliTRDDataDCS () 
32   :  chamberByteStatus  (0),
33      preTrigger         (1),
34      goofyHv            (2),
35      goofyPeakPos       (3),
36      goofyPeakArea      (4),
37      goofyTemp          (5),
38      goofyPressure      (6),
39      goofyVelocity      (7),
40      goofyGain          (8),
41      goofyCO2           (9),
42      goofyN2            (10),
43      gasO2              (11),
44      gasOverpressure    (12),
45      envTemp            (13),
46      hvAnodeImon        (14),
47      hvDriftImon        (15),
48      hvAnodeUmon        (16),
49      hvDriftUmon        (17),
50      adcClkPhase        (18),
51      atmPressure        (19),
52      luminosity         (20),
53      magneticField      (21),
54      fNAlias            (22),
55      graphsAreIni       (false),
56      fitsAreIni         (false)
57 {
58   Init ();
59 }
60
61 //_____________________________________________________________________________
62 AliTRDDataDCS::~AliTRDDataDCS ()
63 {
64   ClearFits ();
65   ClearGraphs ();
66 }
67
68 //_____________________________________________________________________________
69 void AliTRDDataDCS::Init ()
70 {
71   SetConf (chamberByteStatus, "trd_chamberByteStatus%03d", 'c', 540, true, false, 10,10,0,2); 
72   SetConf (preTrigger,          "trd_preTrigger",               'c', 1, true, true,     10,10,0,2);
73   SetConf (goofyHv,                     "trd_goofyHv",                  'f', 1, true, true,     10,10,0,2);
74   SetConf (goofyPeakPos,                "trd_goofyPeakPos%02d", 'f', 2, true, true,     10,10,0,2);
75   SetConf (goofyPeakArea,       "trd_goofyPeakArea%02d",'f', 2, true, true,     10,10,0,2);
76   SetConf (goofyTemp,           "trd_goofyTemp%02d",    'f', 2, true, true,     10,10,0,2);
77   SetConf (goofyPressure,       "trd_goofyPressure",    'f', 1, true, true,     10,10,0,2);
78   SetConf (goofyVelocity,       "trd_goofyVelocity",    'f', 1, true, true,     10,10,0,2);
79   SetConf (goofyGain,           "trd_goofyGain%02d",    'f', 2, true, true,     10,10,0,2);
80   SetConf (goofyCO2,                    "trd_goofyCO2",                 'f', 1, true, true,     10,10,0,2);
81   SetConf (goofyN2,                     "trd_goofyN2",                  'f', 1, true, true,     10,10,0,2);
82   SetConf (gasO2,                       "trd_gasO2",                    'f', 1, true, true,     10,10,0,2);
83   SetConf (gasOverpressure,     "trd_gasOverpressure",  'f', 1, true, true,     10,10,0,2);
84   SetConf (envTemp,                     "trd_envTemp%03d",              'f', 540, true, true,   10,10,0,2);
85   SetConf (hvAnodeImon,                 "trd_hvAnodeImon%03d",  'f', 540, true, true,   10,10,0,2);
86   SetConf (hvDriftImon,                 "trd_hvDriftImon%03d",  'f', 540, true, true,   10,10,0,2);
87   SetConf (hvAnodeUmon,                 "trd_hvAnodeUmon%03d",  'f', 540, true, true,   10,10,0,2);
88   SetConf (hvDriftUmon,                 "trd_hvDriftUmon%03d",  'f', 540, true, true,   10,10,0,2);
89   SetConf (adcClkPhase,                 "trd_adcClkPhase",              'f', 1, true, true,     10,10,0,2);
90   SetConf (atmPressure,                 "trd_atmPressure",              'f', 1, true, true,     10,10,0,2);
91   SetConf (luminosity,          "trd_luminosity",               'f', 1, true, true,     10,10,0,2);
92   SetConf (magneticField,       "trd_magneticField",    'f', 1, true, true,     10,10,0,2);             
93   
94   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) {
95     fDatas[iAlias].graph.SetOwner (1);
96     fDatas[iAlias].fit.SetOwner (1);
97   }
98 }
99
100 //_____________________________________________________________________________
101 void AliTRDDataDCS::SetConf (UInt_t iAlias, const char * amanda, 
102                              char dataType, UInt_t nChannel, 
103                              Bool_t enableGraph, Bool_t enableFit, Int_t kMinPoints, 
104                              Int_t kIter, Double_t kMaxDelta, Int_t kFitReq)
105 {
106   if (iAlias >= fNAlias) {
107     AliWarning (Form("Alias %d is not correct", iAlias));
108     return;
109   }
110   
111   fConfs[iAlias].amanda = amanda;
112   fConfs[iAlias].dataType = dataType;
113   fConfs[iAlias].nChannel = nChannel;
114   fConfs[iAlias].enableGraph = enableGraph;
115   fConfs[iAlias].enableFit = enableFit;
116   fConfs[iAlias].kMinPoints = kMinPoints;
117   fConfs[iAlias].kIter = kIter;
118   fConfs[iAlias].kMaxDelta = kMaxDelta;
119   fConfs[iAlias].kFitReq = kFitReq;
120   
121 }
122
123 //_____________________________________________________________________________
124 void AliTRDDataDCS::InitFits ()
125 {
126   if (fitsAreIni)
127     return;
128   
129   UInt_t nChannel;
130   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) {
131     nChannel = fConfs[iAlias].enableFit ? fConfs[iAlias].nChannel : 0;
132     fDatas[iAlias].fit.Expand (nChannel);       
133   }
134   
135   fitsAreIni = true;
136 }
137
138 //_____________________________________________________________________________
139 void AliTRDDataDCS::InitGraphs ()
140 {
141   if (graphsAreIni)
142     return;
143   
144   UInt_t nChannel;      
145   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) {
146     nChannel = fConfs[iAlias].enableGraph ? fConfs[iAlias].nChannel : 0;         
147     fDatas[iAlias].graph.Expand (nChannel);
148   }
149   
150   graphsAreIni = true;
151 }
152
153 //_____________________________________________________________________________
154 void AliTRDDataDCS::ClearFits ()
155 {
156   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) {
157     fDatas[iAlias].fit.Clear ();
158     fDatas[iAlias].fit.Expand (0);
159   }
160   
161   fitsAreIni = false;
162 }
163
164 //_____________________________________________________________________________
165 void AliTRDDataDCS::ClearGraphs ()
166 {       
167   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) {
168     fDatas[iAlias].graph.Clear ();
169     fDatas[iAlias].graph.Expand (0);
170   }
171   
172   graphsAreIni = false;
173 }
174
175 //_____________________________________________________________________________
176 void AliTRDDataDCS::Streamer(TBuffer &R__b) {
177   
178   if (R__b.IsReading()) {
179     
180     R__b.ReadBool (graphsAreIni);
181     R__b.ReadBool (fitsAreIni);
182     
183     
184     for (UInt_t iAlias=0; iAlias<fNAlias; iAlias++) {
185       TString::ReadString (R__b, TString::Class());
186       R__b.ReadChar (fConfs[iAlias].dataType);
187       R__b.ReadUInt (fConfs[iAlias].nChannel);
188       R__b.ReadBool (fConfs[iAlias].enableGraph);
189       R__b.ReadBool (fConfs[iAlias].enableFit);
190       R__b.ReadInt  (fConfs[iAlias].kMinPoints);
191       R__b.ReadInt  (fConfs[iAlias].kIter);
192       R__b.ReadDouble  (fConfs[iAlias].kMaxDelta);
193       R__b.ReadInt  (fConfs[iAlias].kFitReq);
194     }
195     
196     
197     if (graphsAreIni) {
198       for (UInt_t iAlias=0; iAlias<fNAlias; iAlias++)
199         fDatas[iAlias].graph = *(TObjArray*)R__b.ReadObject (TObjArray::Class());
200     }
201     
202     if (fitsAreIni) {
203       for (UInt_t iAlias=0; iAlias<fNAlias; iAlias++)
204         fDatas[iAlias].fit = *(TObjArray*)R__b.ReadObject (TObjArray::Class());
205     }
206     
207     AliInfo (Form("Read %d octets to the stream (%d Ko)", R__b.Length(), R__b.Length()/1024));
208     
209   } else {
210     R__b.WriteBool (graphsAreIni);
211     R__b.WriteBool (fitsAreIni);
212     
213     for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) {
214       TString::WriteString (R__b, &fConfs[iAlias].amanda);
215       R__b.WriteChar (fConfs[iAlias].dataType);
216       R__b.WriteUInt (fConfs[iAlias].nChannel);
217       R__b.WriteBool (fConfs[iAlias].enableGraph);
218       R__b.WriteBool (fConfs[iAlias].enableFit);
219       R__b.WriteInt  (fConfs[iAlias].kMinPoints);
220       R__b.WriteInt  (fConfs[iAlias].kIter);
221       R__b.WriteDouble  (fConfs[iAlias].kMaxDelta);
222       R__b.WriteInt  (fConfs[iAlias].kFitReq);
223     }
224     
225     if (graphsAreIni) {
226       for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) 
227         R__b.WriteObject ((TObject*)&fDatas[iAlias].graph);
228     }
229     
230     if (fitsAreIni) {
231       for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++) 
232         R__b.WriteObject ((TObject*)&fDatas[iAlias].fit);
233     }
234     
235     AliInfo (Form("Write %d octets to the stream (%d Ko)", R__b.Length(), R__b.Length()/1024));
236   }
237 }
238
239 //_____________________________________________________________________________
240 Bool_t AliTRDDataDCS::ExtractDCS (TMap * dcsAlias)
241 {
242   if (dcsAlias == 0x0) {
243     AliWarning ("No DCS Map");
244     return kFALSE;
245   }
246   
247   ClearGraphs ();
248   InitGraphs ();
249   
250   TGraph * graphTemp;
251   UInt_t nChannel;
252   
253   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++){
254     
255     // extract dcs only when it is needed
256     nChannel = fDatas[iAlias].graph.GetSize ();
257     
258     for (UInt_t iSensor=0; iSensor < nChannel; iSensor++) {
259       
260       graphTemp = FindAndMakeGraph (dcsAlias, 
261                                     Form (fConfs[iAlias].amanda.Data(), iSensor), 
262                                     fConfs[iAlias].dataType); 
263       
264       fDatas[iAlias].graph.AddAt (graphTemp, iSensor);
265     }
266   }
267   
268   return kTRUE;
269 }
270
271 //_____________________________________________________________________________
272 TGraph * AliTRDDataDCS::FindAndMakeGraph (TMap * dcsMap, const char * amandaStr, 
273                                           char dataType)
274 {
275   TGraph * graph;
276   
277   TPair * pair = (TPair*)dcsMap->FindObject(amandaStr);
278   if (pair == 0x0) {
279     AliWarning (Form("Can't find %s in dcsMap", amandaStr));
280     return 0x0;
281   }
282   
283   TObjArray * valueSet = (TObjArray*)pair->Value();
284   
285   //
286   // Make graph of values read from DCS map
287   //   (spline fit parameters will subsequently be obtained from this graph) 
288   //
289   
290   Int_t nEntries = valueSet->GetEntriesFast();
291   if (nEntries == 0) {
292     AliWarning (Form("Entry %s in dcsMap contain no datas", amandaStr));
293     return 0x0;
294   }
295   
296   Float_t * x = new Float_t [nEntries];
297   Float_t * y = new Float_t [nEntries];
298   
299   Bool_t ok = true;
300   Int_t time0 = 0;
301   Int_t iEntries;
302   for (iEntries = 0;  iEntries< nEntries; iEntries++) {
303     
304     AliDCSValue * val = (AliDCSValue *)valueSet->At(iEntries);
305     
306     if (val == 0x0) { 
307       ok = false;
308       AliError (Form("Entry %s at %d contain no datas", amandaStr, iEntries));
309       break;
310     }
311     
312     if (time0 == 0) 
313       time0 = val->GetTimeStamp();
314     
315     
316     x[iEntries] = (val->GetTimeStamp() - time0)/3600.0; // give times in fractions of hours 
317     
318     switch (dataType) {
319     case 'f' :
320       y[iEntries] = val->GetFloat();
321       break;
322       
323     case 'c' :
324       y[iEntries] = (Float_t) val->GetChar();
325       break;
326       
327     default :
328       ok = false;
329       AliError (Form("Bad type for entry %s", amandaStr));
330       break;
331     }
332   }
333   
334   if (ok)
335     graph = new TGraph (iEntries, x, y);
336   else
337     graph = 0x0;
338   
339   delete [] x;
340   delete [] y;
341   
342   return graph;
343 }
344
345
346 //_____________________________________________________________________________
347 Bool_t AliTRDDataDCS::PerformFit ()
348 {
349   AliSplineFit * fitTemp;
350   
351   ClearFits ();
352   InitFits ();
353   
354   UInt_t nChannel;
355   
356   for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++){
357     
358     // perform fit only when it is needed
359     nChannel = fDatas[iAlias].fit.GetSize ();
360     
361     for (UInt_t iSensor=0; iSensor < nChannel; iSensor++) {
362       
363       fitTemp = Fit ((TGraph*)fDatas[iAlias].graph[iSensor],
364                      fConfs[iAlias].kMinPoints,
365                      fConfs[iAlias].kIter,
366                      fConfs[iAlias].kMaxDelta,
367                      fConfs[iAlias].kFitReq); 
368       
369       if (fitTemp == 0x0)
370         AliInfo (Form ("Can't fit %s", Form (fConfs[iAlias].amanda.Data(), iSensor)));
371       
372       fDatas[iAlias].fit.AddAt (fitTemp, iSensor);
373     }
374   }
375   
376   return kTRUE;
377   
378 }
379
380 //_____________________________________________________________________________
381 AliSplineFit * AliTRDDataDCS::Fit (TGraph * graph, 
382                                    Int_t  kMinPoints, Int_t  kIter, 
383                                    Double_t  kMaxDelta, Int_t  kFitReq)
384 {
385   if (graph == 0x0) {
386     AliError ("No graph for fit");
387     return 0x0;
388   }
389   
390   AliSplineFit * spline = new AliSplineFit ();
391   spline->InitKnots (new TGraph (*graph), kMinPoints, kIter, kMaxDelta);
392   spline->SplineFit (kFitReq);
393   spline->Cleanup();   // delete also new TGraph (*graph)
394   
395   return spline;
396 }
397
398
399 //_____________________________________________________________________________
400 void AliTRDDataDCS::Print (Option_t* option) const
401 {
402   if (option[0]=='g' || option[0]=='\0'){
403     
404     if (graphsAreIni){
405       
406       for (UInt_t iAlias = 0; iAlias < fNAlias; iAlias++){
407         
408         for (Int_t iSensor = 0; iSensor < fDatas[iAlias].graph.GetSize(); iSensor++) {
409           
410           if (fDatas[iAlias].graph[iSensor] != 0x0)
411             AliInfo (Form("Graph %s contain %d point(s)", 
412                           Form(fConfs[iAlias].amanda, iSensor), 
413                           ((TGraph*)(fDatas[iAlias].graph[iSensor]))->GetN ()));
414         }
415       }
416     }else{
417       AliInfo ("Graphs don't exist");
418     }
419   }
420   
421   
422   if (option[0] == 'f' || option[0]=='\0'){
423     
424     AliInfo ("no print for fit");
425     
426   }
427   
428 }
429
430 //_____________________________________________________________________________
431 TGraph * AliTRDDataDCS::GetGraph (UInt_t iAlias, UInt_t iChannel) const
432 {
433   if (iAlias >= fNAlias) {
434     AliWarning (Form("Alias %d is not correct", iAlias));
435     return 0x0;
436   }
437   
438   if (iChannel >= (UInt_t)fDatas[iAlias].graph.GetSize()) {
439     AliWarning (Form("Alias %s is not correct", 
440                      Form(fConfs[iAlias].amanda.Data(), iChannel)));
441     return 0x0;
442   }
443   
444   return (TGraph *)fDatas[iAlias].graph[iChannel];
445 }
446
447 //_____________________________________________________________________________
448 AliSplineFit * AliTRDDataDCS::GetFit (UInt_t iAlias, UInt_t iChannel) const
449 {
450   if (iAlias >= fNAlias) {
451     AliWarning (Form("Alias %d is not correct", iAlias));
452     return 0x0;
453   }
454   
455   if (iChannel >= (UInt_t)fDatas[iAlias].fit.GetSize()) {
456     AliWarning (Form("Alias %s is not correct", 
457                      Form(fConfs[iAlias].amanda.Data(), iChannel)));
458     return 0x0;
459   }
460   
461   return (AliSplineFit *)fDatas[iAlias].fit[iChannel];
462 }
463
464 //_____________________________________________________________________________
465 TString AliTRDDataDCS::GetAmandaStr (UInt_t iAlias) const 
466 {
467   if (iAlias < fNAlias)
468     return fConfs[iAlias].amanda;
469   else return TString ();
470 }
471
472 //_____________________________________________________________________________
473 UInt_t AliTRDDataDCS::GetNChannel (UInt_t iAlias) const 
474 {
475   if (iAlias < fNAlias)
476     return fConfs[iAlias].nChannel;
477   else return 0;
478 }