]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFLvHvDataPoints.cxx
Remove old raw reader class
[u/mrichter/AliRoot.git] / TOF / AliTOFLvHvDataPoints.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 $Log: AliTOFLvHvDataPoints.cxx,v $
18 */  
19
20 // AliTOFLvHvDataPoints class
21 // main aim to introduce the aliases for the TOF LV and HV DCS
22 // data points to be then
23 // stored in the OCDB, and to process them. 
24 // Process() method called by TOF preprocessor
25
26 #include "TString.h"
27 #include "TTimeStamp.h"
28 #include "TMap.h"
29 #include "TMath.h"
30 #include "TH1C.h"
31
32 #include "AliDCSValue.h"
33 #include "AliLog.h"
34 #include "AliBitPacking.h"
35
36 #include "AliTOFGeometry.h"
37 #include "AliTOFDCSmaps.h"
38 #include "AliTOFLvHvDataPoints.h"
39
40 class AliCDBMetaData;
41 class TDatime;
42
43 ClassImp(AliTOFLvHvDataPoints)
44
45 //---------------------------------------------------------------
46 AliTOFLvHvDataPoints::AliTOFLvHvDataPoints():
47   TObject(),
48   fRun(0),
49   fStartTime(0),
50   fEndTime(0),
51   fStartTimeDCSQuery(0),
52   fEndTimeDCSQuery(0),
53   fIsProcessed(kFALSE),
54   fFDR(kFALSE),
55   fNumberOfLVdataPoints(0),
56   fNumberOfHVdataPoints(0),
57   fNumberOfHVandLVmaps(0),
58   fStartingLVmap(0x0),
59   fStartingHVmap(0x0),
60   fHisto(0x0),
61   fNSecondsBeforeEOR(60)
62 {
63   // main constructor 
64
65 }
66
67 //---------------------------------------------------------------
68 AliTOFLvHvDataPoints::AliTOFLvHvDataPoints(Int_t nRun, UInt_t startTime, UInt_t endTime, UInt_t startTimeDCSQuery, UInt_t endTimeDCSQuery):
69   TObject(),
70   fRun(nRun),
71   fStartTime(startTime),
72   fEndTime(endTime),
73   fStartTimeDCSQuery(startTimeDCSQuery),
74   fEndTimeDCSQuery(endTimeDCSQuery),
75   fIsProcessed(kFALSE),
76   fFDR(kFALSE),
77   fNumberOfLVdataPoints(0),
78   fNumberOfHVdataPoints(0),
79   fNumberOfHVandLVmaps(0),
80   fStartingLVmap(new AliTOFDCSmaps()),
81   fStartingHVmap(new AliTOFDCSmaps()),
82   fHisto(new TH1C("histo","",kNpads,-0.5,kNpads-0.5)),
83   fNSecondsBeforeEOR(60)
84 {
85
86   // constructor with arguments
87
88   AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s \n\tStartTime DCS Query %s \n\tEndTime DCS Query %s", nRun,
89                TTimeStamp(startTime).AsString(),
90                TTimeStamp(endTime).AsString(), 
91                TTimeStamp(startTimeDCSQuery).AsString(), 
92                TTimeStamp(endTimeDCSQuery).AsString()));
93
94   Init();
95
96 }
97
98 //---------------------------------------------------------------
99
100 AliTOFLvHvDataPoints::AliTOFLvHvDataPoints(const AliTOFLvHvDataPoints & data):
101   TObject(data), 
102   fRun(data.fRun),
103   fStartTime(data.fStartTime),
104   fEndTime(data.fEndTime),
105   fStartTimeDCSQuery(data.fStartTimeDCSQuery),
106   fEndTimeDCSQuery(data.fEndTimeDCSQuery),
107   fIsProcessed(data.fIsProcessed),
108   fFDR(data.fFDR),
109   fNumberOfLVdataPoints(data.fNumberOfLVdataPoints),
110   fNumberOfHVdataPoints(data.fNumberOfHVdataPoints),
111   fNumberOfHVandLVmaps(data.fNumberOfHVandLVmaps),
112   fStartingLVmap(data.fStartingLVmap),
113   fStartingHVmap(data.fStartingHVmap),
114   fHisto(data.fHisto),
115   fNSecondsBeforeEOR(data.fNSecondsBeforeEOR)
116 {
117
118   // copy constructor
119
120   for(int i=0;i<kNddl;i++)
121     fAliasNamesXLVmap[i]=data.fAliasNamesXLVmap[i];
122     
123   for(int i=0;i<kNsectors;i++)
124     for(int j=0;j<kNplates;j++)
125       fAliasNamesXHVmap[i][j]=data.fAliasNamesXHVmap[i][j];
126  
127 }
128 //---------------------------------------------------------------
129
130 AliTOFLvHvDataPoints& AliTOFLvHvDataPoints:: operator=(const AliTOFLvHvDataPoints & data) { 
131
132   // assignment operator
133
134   if (this == &data)
135     return *this;
136
137   TObject::operator=(data);
138   fRun=data.GetRun();
139   fStartTime=data.GetStartTime();
140   fEndTime=data.GetEndTime();
141   fStartTimeDCSQuery=data.GetStartTimeDCSQuery();
142   fEndTimeDCSQuery=data.GetEndTimeDCSQuery();
143
144   fNumberOfLVdataPoints=data.fNumberOfLVdataPoints;
145   fNumberOfHVdataPoints=data.fNumberOfHVdataPoints;
146   fNumberOfHVandLVmaps=data.fNumberOfHVandLVmaps;
147
148   fStartingLVmap=data.fStartingLVmap;
149   fStartingHVmap=data.fStartingHVmap;
150
151   for(int i=0;i<kNddl;i++)
152     fAliasNamesXLVmap[i]=data.fAliasNamesXLVmap[i];
153
154   for(int i=0;i<kNsectors;i++)
155     for(int j=0;j<kNplates;j++)
156       fAliasNamesXHVmap[i][j]=data.fAliasNamesXHVmap[i][j];
157
158   fHisto=data.fHisto;
159
160   fNSecondsBeforeEOR=data.fNSecondsBeforeEOR;
161
162   return *this;
163 }
164 //---------------------------------------------------------------
165 AliTOFLvHvDataPoints::~AliTOFLvHvDataPoints() {
166
167   // destructor
168
169   delete fStartingLVmap;
170   delete fStartingHVmap;
171
172 }
173
174 //---------------------------------------------------------------
175 Bool_t AliTOFLvHvDataPoints::ProcessData(TMap& aliasMap) {
176   //
177   // method to process the data
178   //
179
180   if(!(fAliasNamesXHVmap[0][0]) || !(fAliasNamesXLVmap[0])) Init();
181
182   AliInfo(Form(" Start Time = %i",fStartTime));
183   AliInfo(Form(" End Time = %i",fEndTime));
184   AliInfo(Form(" Start Time DCS Query= %i",fStartTimeDCSQuery));
185   AliInfo(Form(" End Time DCS Query= %i",fEndTimeDCSQuery));
186
187   if (fEndTime==fStartTime){
188     AliError(Form(" Run with null time length: start time = %d = end time = %d",fStartTime,fEndTime));
189     return kFALSE;
190   }
191
192
193   if (!ReadLVDataPoints(aliasMap)) return kFALSE;
194   AliDebug(1,Form(" Number of LV dp value changes = %d",fNumberOfLVdataPoints));
195
196   if (!ReadHVDataPoints(aliasMap)) return kFALSE;
197   AliDebug(1,Form(" Number of HV dp value changes = %d",fNumberOfHVdataPoints));
198
199   if (!MergeLVmap()) return kFALSE;
200
201   if (!MergeHVmap()) return kFALSE;
202
203   if (!MergeMaps()) return kFALSE;
204
205   fIsProcessed=kTRUE;
206
207   return kTRUE;
208
209 }
210
211 //---------------------------------------------------------------
212 Bool_t AliTOFLvHvDataPoints::MergeMaps() {
213   //
214   // Merge together LV and HV maps
215   //
216
217   Int_t timeMaps[kNmaxDataPoints];
218   for (Int_t ii=0; ii<kNmaxDataPoints; ii++) timeMaps[ii]=0;
219
220   for (Int_t ii=0; ii<fNumberOfHVdataPoints; ii++) {
221     AliDebug(1,Form(" fNumberOfHVandLVmaps = %d (HV=%d, LV=%d) - time=%d",fNumberOfHVandLVmaps,fNumberOfHVdataPoints,fNumberOfLVdataPoints,timeMaps[fNumberOfHVandLVmaps]));
222     timeMaps[fNumberOfHVandLVmaps++]=fHVDataPoints[ii]->GetTime();
223   }
224
225   AliDebug(1,Form(" fNumberOfHVandLVmaps = %d (HV=%d) ",fNumberOfHVandLVmaps,fNumberOfHVdataPoints));
226
227   Bool_t check = kTRUE;
228   for (Int_t jj=0; jj<fNumberOfLVdataPoints; jj++) {
229     check = kTRUE;
230     for (Int_t ii=0; ii<fNumberOfHVdataPoints; ii++)
231       check=check&&(fLVDataPoints[jj]->GetTime()!=timeMaps[ii]);
232
233     if (check) {
234       AliDebug(1,Form(" fNumberOfHVandLVmaps = %d (HV=%d, LV=%d) - time=%d",fNumberOfHVandLVmaps,fNumberOfHVdataPoints,fNumberOfLVdataPoints,timeMaps[fNumberOfHVandLVmaps]));
235       timeMaps[fNumberOfHVandLVmaps++]=fLVDataPoints[jj]->GetTime();
236     }
237   }
238
239   AliInfo(Form(" TOF HV dps = %d; TOF LV dps = %d; TOF HVandLV dps %d",
240                fNumberOfHVdataPoints, fNumberOfLVdataPoints, fNumberOfHVandLVmaps));
241
242
243   Int_t *controller = new Int_t[fNumberOfHVandLVmaps];
244   for (Int_t ii=0; ii<fNumberOfHVandLVmaps; ii++) controller[ii]=-1;
245   TMath::Sort(fNumberOfHVandLVmaps,timeMaps,controller,kFALSE); // increasing order
246
247   for (Int_t ii=0; ii<fNumberOfHVandLVmaps; ii++)
248     AliDebug(1, Form(" Time Order - time[controller[%d]] = %d", ii, timeMaps[controller[ii]]));
249
250   Short_t array[kNpads];
251   for (Int_t iPad=0; iPad<kNpads; iPad++) array[iPad]=-1;
252   Int_t time = 0;
253
254   // HVandLV status map during run
255   for (Int_t index=0; index<fNumberOfHVandLVmaps; index++) {
256     time = timeMaps[controller[index]];
257
258     AliDebug(2, Form(" Time Order - time[controller[%d]] = %d", index, timeMaps[controller[index]]));
259
260     for (Int_t ii=0; ii<fNumberOfHVdataPoints; ii++)
261       for (Int_t jj=0; jj<fNumberOfLVdataPoints; jj++) {
262
263         AliDebug(2,Form(" time=%d --- HVdp_time[%d]=%d, LVdp_time[%d]=%d",
264                         time,
265                         ii,fHVDataPoints[ii]->GetTime(),
266                         jj,fLVDataPoints[jj]->GetTime()));
267
268         if ( (fHVDataPoints[ii]->GetTime()==time && fLVDataPoints[jj]->GetTime()<=time) ||
269              (fLVDataPoints[jj]->GetTime()==time && fHVDataPoints[ii]->GetTime()<=time) ) {
270
271           AliDebug(2,Form(" HVdp_time[%d]=%d, LVdp_time[%d]=%d",
272                           ii,fHVDataPoints[ii]->GetTime(),
273                           jj,fLVDataPoints[jj]->GetTime()));
274           for (Int_t iPad=0; iPad<kNpads; iPad++)
275             array[iPad] = fHVDataPoints[ii]->GetCellValue(iPad)*fLVDataPoints[jj]->GetCellValue(iPad);
276           AliTOFDCSmaps *object = new AliTOFDCSmaps(time,array);
277           fMap[index]= object;
278           break;
279
280         }
281         else if ( fHVDataPoints[ii]->GetTime()==time && fLVDataPoints[jj]->GetTime()>time ) {
282
283           AliDebug(2,Form(" HVdp_time[%d]=%d, (no LVdp)",ii,fHVDataPoints[ii]->GetTime()));
284           for (Int_t iPad=0; iPad<kNpads; iPad++)
285             array[iPad] = fHVDataPoints[ii]->GetCellValue(iPad);
286           AliTOFDCSmaps *object = new AliTOFDCSmaps(time,array);
287           fMap[index]= object;
288           break;
289
290         } else if ( fLVDataPoints[jj]->GetTime()==time && fHVDataPoints[ii]->GetTime()>time ) {
291
292           AliDebug(2,Form(" LVdp_time[%d]=%d, (no HVdp)",jj,fLVDataPoints[jj]->GetTime()));
293           for (Int_t iPad=0; iPad<kNpads; iPad++)
294             array[iPad] = fLVDataPoints[jj]->GetCellValue(iPad);
295           AliTOFDCSmaps *object = new AliTOFDCSmaps(time,array);
296           fMap[index]= object;
297           break;
298
299         }
300
301       }
302
303   }
304
305   delete [] controller;
306
307   for (Int_t ii=0; ii<fNumberOfHVandLVmaps; ii++)
308     AliDebug(1,Form(" fMap[%d]->GetTime() = %d ",ii,fMap[ii]->GetTime()));
309
310   return kTRUE;
311
312 }
313
314 //---------------------------------------------------------------
315 Bool_t AliTOFLvHvDataPoints::MergeHVmap() {
316   //
317   // Create HV maps from HV dps
318   //
319
320   Bool_t check= kFALSE;
321
322   if (fNumberOfHVdataPoints==0)
323     return check;
324   else {
325
326     // first map construction
327     for (Int_t iPad=0; iPad<kNpads; iPad++) {
328       if (fHVDataPoints[0]->GetCellValue(iPad)==-1)
329         fHVDataPoints[0]->SetCellValue(iPad,fStartingHVmap->GetCellValue(iPad));
330       //else
331       //fHVDataPoints[0]->SetCellValue(iPad,fHVDataPoints[0]->GetCellValue(iPad)*fStartingHVmap->GetCellValue(iPad));
332
333       if (iPad%(96*91)==0)
334         AliDebug(2,Form("HVdp0: channel=%6d -> %1d",iPad,fHVDataPoints[0]->GetCellValue(iPad)));
335     }
336
337     // other maps construction
338     for (Int_t ii=1; ii<fNumberOfHVdataPoints; ii++) {
339       for (Int_t iPad=0; iPad<kNpads; iPad++) {
340         if (fHVDataPoints[ii]->GetCellValue(iPad)==-1)
341           fHVDataPoints[ii]->SetCellValue(iPad,fHVDataPoints[ii-1]->GetCellValue(iPad));
342
343         if (iPad%(96*91)==0)
344           AliDebug(2,Form("HVdp%d: channel=%6d -> %1d",ii,iPad,fHVDataPoints[ii]->GetCellValue(iPad)));
345       }
346     }
347
348     check=kTRUE;
349   }
350
351   return kTRUE;
352
353 }
354
355 //---------------------------------------------------------------
356 Bool_t AliTOFLvHvDataPoints::MergeLVmap() {
357   //
358   // Create LV maps from LV dps
359   //
360
361   Bool_t check= kFALSE;
362
363   if (fNumberOfLVdataPoints==0)
364     return check;
365   else {
366
367     // first map construction
368     for (Int_t iPad=0; iPad<kNpads; iPad++) {
369       if (fLVDataPoints[0]->GetCellValue(iPad)==-1)
370         fLVDataPoints[0]->SetCellValue(iPad,fStartingLVmap->GetCellValue(iPad));
371       //else
372       //fLVDataPoints[0]->SetCellValue(iPad,fLVDataPoints[0]->GetCellValue(iPad)*fStartingLVmap->GetCellValue(iPad));
373
374       if (iPad%(96*91)==0)
375         AliDebug(2,Form("LVdp0: channel=%6d -> %1d",iPad,fLVDataPoints[0]->GetCellValue(iPad)));
376     }
377
378     // other maps construction
379     for (Int_t ii=1; ii<fNumberOfLVdataPoints; ii++) {
380       for (Int_t iPad=0; iPad<kNpads; iPad++) {
381         if (fLVDataPoints[ii]->GetCellValue(iPad)==-1)
382           fLVDataPoints[ii]->SetCellValue(iPad,fLVDataPoints[ii-1]->GetCellValue(iPad));
383
384         if (iPad%(96*91)==0)
385           AliDebug(2,Form("LVdp%d: channel=%6d -> %1d",ii,iPad,fLVDataPoints[ii]->GetCellValue(iPad)));
386       }
387     }
388
389     check=kTRUE;
390   }
391
392   return check;
393
394 }
395
396 //---------------------------------------------------------------
397 Bool_t AliTOFLvHvDataPoints::ReadHVDataPoints(TMap& aliasMap) {
398   //
399   // Read HV dps
400   //
401
402   TObjArray *aliasArr;
403   AliDCSValue* aValue;
404   AliDCSValue* aValuePrev;
405   Int_t val = 0;
406   Int_t time = 0;
407   Int_t nEntries = 0;
408
409   Short_t dummy[kNpads];
410
411   for (Int_t iBin=0; iBin<kNpads; iBin++) dummy[iBin]=-1;
412   // starting loop on aliases
413   for (int i=0; i<kNsectors; i++)
414     for (int j=0; j<kNplates; j++) {
415       aliasArr = (TObjArray*) aliasMap.GetValue(fAliasNamesXHVmap[i][j].Data());
416       if (!aliasArr) {
417         AliError(Form("Alias %s not found!", fAliasNamesXHVmap[i][j].Data()));
418         if (!fFDR)
419           return kFALSE;    // returning only in case we are not in a FDR run
420         else
421           continue;
422       }
423
424       nEntries = aliasArr->GetEntries();
425     
426       if (nEntries<2) AliDebug(1, Form(" NB: number of values for dp %s %d (less than 2)",fAliasNamesXHVmap[i][j].Data(),nEntries));
427
428       if (nEntries==0) {
429         AliError(Form("Alias %s has no entries! Nothing will be stored",
430                       fAliasNamesXHVmap[i][j].Data()));
431         continue;
432       }
433       else {
434
435         // read the first value
436         for (Int_t iBin=0; iBin<kNpads; iBin++) dummy[iBin]=-1;
437         aValue = (AliDCSValue*) aliasArr->At(0);
438         val = aValue->GetInt();
439         time = aValue->GetTimeStamp();
440         AliDebug(1, Form(" at t=%d, 1st value for dp %s = %d", time, fAliasNamesXHVmap[i][j].Data(), val));
441         FillHVarrayPerDataPoint(i,j,val,dummy);
442         AliTOFDCSmaps *object0 = new AliTOFDCSmaps(time,dummy);
443         if (InsertHVDataPoint(object0)!=0) return kTRUE; // to be verified
444
445         // read the values from the second one
446         for (Int_t iEntry=1; iEntry<nEntries; iEntry++) {
447           for (Int_t iBin=0; iBin<kNpads; iBin++) dummy[iBin]=-1;
448           aValue = (AliDCSValue*) aliasArr->At(iEntry);
449           val = aValue->GetInt();
450           time = aValue->GetTimeStamp();
451           AliDebug(2, Form(" at t=%d, %dth value for dp %s = %d", time, iEntry+1, fAliasNamesXHVmap[i][j].Data(), val));
452           aValuePrev = (AliDCSValue*) aliasArr->At(iEntry-1);
453           if (aValuePrev->GetInt()!=val) {
454             AliDebug(1, Form(" CHANGE - at t=%d, %dth value for dp %s = %d", time, iEntry+1, fAliasNamesXHVmap[i][j].Data(), val));
455             FillHVarrayPerDataPoint(i,j,val,dummy);
456             AliTOFDCSmaps *object = new AliTOFDCSmaps(time,dummy);
457             if (InsertHVDataPoint(object)!=0) return kTRUE; // to be verified
458           }
459
460         }
461       }
462     }
463
464   if (fNumberOfHVdataPoints==0) {
465     AliInfo("Valid HV dps not found. By default all HV TOF channels (except the ones in the PHOS holes) switched ON, at SOR.");
466     if (InsertHVDataPoint(fStartingHVmap)!=0) return kTRUE; // to be verified
467   }
468
469   return kTRUE;
470
471 }
472
473 //---------------------------------------------------------------
474 Bool_t AliTOFLvHvDataPoints::ReadLVDataPoints(TMap& aliasMap) {
475   //
476   // Read LV dps
477   //
478
479   TObjArray *aliasArr;
480   AliDCSValue* aValue;
481   AliDCSValue* aValuePrev;
482   Int_t val = 0;
483   Int_t time = 0;
484   Int_t nEntries = 0;
485
486   Short_t dummy[kNpads];
487
488   for (Int_t iBin=0; iBin<kNpads; iBin++) dummy[iBin]=-1;
489   // starting loop on aliases
490   for (int i=0; i<kNddl; i++) {
491     aliasArr = (TObjArray*) aliasMap.GetValue(fAliasNamesXLVmap[i].Data());
492     if (!aliasArr) {
493       AliError(Form("Alias %s not found!", fAliasNamesXLVmap[i].Data()));
494       if (!fFDR)
495         return kFALSE;    // returning only in case we are not in a FDR run
496       else
497         continue;
498     }
499
500     nEntries = aliasArr->GetEntries();
501     
502     if (nEntries<2) AliDebug(1, Form(" NB: number of values for dp %s %d (less than 2)",fAliasNamesXLVmap[i].Data(),nEntries));
503     
504     if (nEntries==0) {
505       AliError(Form("Alias %s has no entries! Nothing will be stored",
506                     fAliasNamesXLVmap[i].Data()));
507       continue;
508     }
509     else {
510
511       // read the first value
512       for (Int_t iBin=0; iBin<kNpads; iBin++) dummy[iBin]=-1;
513       aValue = (AliDCSValue*) aliasArr->At(0);
514       val = aValue->GetInt();
515       time = aValue->GetTimeStamp();
516       AliDebug(1, Form(" at t=%d, 1st value for dp %s = %d", time, fAliasNamesXLVmap[i].Data(), val));
517       FillLVarrayPerDataPoint(i,val,dummy);
518       AliTOFDCSmaps *object0 = new AliTOFDCSmaps(time,dummy);
519       if (InsertLVDataPoint(object0)!=0) return kTRUE; // to be verified
520
521       // read the values from the second one
522       for (Int_t iEntry=1; iEntry<nEntries; iEntry++) {
523         for (Int_t iBin=0; iBin<kNpads; iBin++) dummy[iBin]=-1;
524         aValue = (AliDCSValue*) aliasArr->At(iEntry);
525         val = aValue->GetInt();
526         time = aValue->GetTimeStamp();
527         AliDebug(2, Form(" at t=%d, %dth value for dp %s = %d", time, iEntry+1, fAliasNamesXLVmap[i].Data(), val));
528         aValuePrev = (AliDCSValue*) aliasArr->At(iEntry-1);
529         if (aValuePrev->GetInt()!=val) {
530           AliDebug(1, Form(" CHANGE - at t=%d, %dth value for dp %s = %d", time, iEntry+1, fAliasNamesXLVmap[i].Data(), val));
531           FillLVarrayPerDataPoint(i,val,dummy);
532           AliTOFDCSmaps *object = new AliTOFDCSmaps(time,dummy);
533           if (InsertLVDataPoint(object)!=0) return kTRUE; // to be verified
534         }
535
536       }
537     }
538   }
539
540   if (fNumberOfLVdataPoints==0) {
541     AliInfo("Valid LV dps not found. By default all LV TOF channels switched ON, at SOR.");
542     if (InsertLVDataPoint(fStartingLVmap)!=0) return kTRUE; // to be verified
543   }
544
545   return kTRUE;
546
547 }
548
549 //---------------------------------------------------------------
550 Int_t AliTOFLvHvDataPoints::InsertHVDataPoint(AliTOFDCSmaps *object)
551 {
552   //
553   // Insert HV dp in the HV dps array.
554   // The HV dps array is sorted according to increasing dp timeStamp value
555   //
556
557   if (fNumberOfHVdataPoints==kNmaxDataPoints) {
558     AliError("Too many HV data points!");
559     return 1;
560   }
561
562   if (fNumberOfHVdataPoints==0) {
563     fHVDataPoints[fNumberOfHVdataPoints++] = object;
564     return 0;
565   }
566
567   for (Int_t index=0; index<fNumberOfHVdataPoints; index++) {
568     if (object->GetTime()==fHVDataPoints[index]->GetTime()) {
569       fHVDataPoints[index]->Update(object);
570       return 0;
571     }
572   }
573
574   Int_t ii = FindHVdpIndex(object->GetTime());
575   memmove(fHVDataPoints+ii+1 ,fHVDataPoints+ii,(fNumberOfHVdataPoints-ii)*sizeof(AliTOFDCSmaps*));
576   fHVDataPoints[ii] = object;
577   fNumberOfHVdataPoints++;
578   
579   return 0;
580
581 }
582
583 //_________________________________________________________________________
584 Int_t AliTOFLvHvDataPoints::FindHVdpIndex(Int_t z) const {
585   //
586   // This function returns the index of the nearest HV DP in time
587   //
588
589   if (fNumberOfHVdataPoints==0) return 0;
590   if (z <= fHVDataPoints[0]->GetTime()) return 0;
591   if (z > fHVDataPoints[fNumberOfHVdataPoints-1]->GetTime()) return fNumberOfHVdataPoints;
592   Int_t b = 0, e = fNumberOfHVdataPoints-1, m = (b+e)/2;
593   for (; b<e; m=(b+e)/2) {
594     if (z > fHVDataPoints[m]->GetTime()) b=m+1;
595     else e=m;
596   }
597
598   return m;
599
600 }
601
602 //---------------------------------------------------------------
603 Int_t AliTOFLvHvDataPoints::InsertLVDataPoint(AliTOFDCSmaps *object)
604 {
605   //
606   // Insert LV dp in the LV dps array.
607   // The LV dps array is sorted according to increasing dp timeStamp value
608   //
609
610   if (fNumberOfLVdataPoints==kNmaxDataPoints) {
611     AliError("Too many LV data points!");
612     return 1;
613   }
614
615   if (fNumberOfLVdataPoints==0) {
616     fLVDataPoints[fNumberOfLVdataPoints++] = object;
617     return 0;
618   }
619
620   for (Int_t index=0; index<fNumberOfLVdataPoints; index++) {
621     if (object->GetTime()==fLVDataPoints[index]->GetTime()) {
622       fLVDataPoints[index]->Update(object);
623       return 0;
624     }
625   }
626
627   Int_t ii = FindLVdpIndex(object->GetTime());
628   memmove(fLVDataPoints+ii+1 ,fLVDataPoints+ii,(fNumberOfLVdataPoints-ii)*sizeof(AliTOFDCSmaps*));
629   fLVDataPoints[ii] = object;
630   fNumberOfLVdataPoints++;
631   
632   return 0;
633
634 }
635
636 //_________________________________________________________________________
637 Int_t AliTOFLvHvDataPoints::FindLVdpIndex(Int_t z) const {
638   //
639   // This function returns the index of the nearest LV DP in time
640   //
641
642   if (fNumberOfLVdataPoints==0) return 0;
643   if (z <= fLVDataPoints[0]->GetTime()) return 0;
644   if (z > fLVDataPoints[fNumberOfLVdataPoints-1]->GetTime()) return fNumberOfLVdataPoints;
645   Int_t b = 0, e = fNumberOfLVdataPoints-1, m = (b+e)/2;
646   for (; b<e; m=(b+e)/2) {
647     if (z > fLVDataPoints[m]->GetTime()) b=m+1;
648     else e=m;
649   }
650
651   return m;
652
653 }
654
655 //---------------------------------------------------------------
656 void AliTOFLvHvDataPoints::Init(){
657   //
658   // Initialize aliases and DCS data
659   //
660
661   TString sindex;
662   for(int i=0;i<kNsectors;i++)
663     for(int j=0;j<kNplates;j++) {
664       fAliasNamesXHVmap[i][j] = "TOF_HVSTATUS_";
665       sindex.Form("SM%02dMOD%1d",i,j);
666       fAliasNamesXHVmap[i][j] += sindex;
667     }
668
669
670   for(int i=0;i<kNddl;i++) {
671     fAliasNamesXLVmap[i] = "TOF_FEACSTATUS_";
672     sindex.Form("%02d",i);
673     fAliasNamesXLVmap[i] += sindex;
674   }
675
676   fStartingLVmap->SetTime(0);
677   for (Int_t iPad=0; iPad<kNpads; iPad++)
678     fStartingLVmap->SetCellValue(iPad,1);
679
680   fStartingHVmap->SetTime(0);
681   for (Int_t iPad=0; iPad<kNpads; iPad++) {
682     Int_t vol[5] = {-1,-1,-1,-1,-1};
683     AliTOFGeometry::GetVolumeIndices(iPad,vol);
684     if ( (vol[0]==13 || vol[0]==14 || vol[0]==15) &&
685          vol[1]==2)
686       fStartingHVmap->SetCellValue(iPad,0);
687     else
688       fStartingHVmap->SetCellValue(iPad,1);
689   }
690
691 }
692
693 //---------------------------------------------------------------
694 void AliTOFLvHvDataPoints::FillHVarrayPerDataPoint(Int_t sector, Int_t plate, UInt_t baseWord, Short_t *array) const
695 {
696   //
697   // Set the status of the TOF pads connected to the HV dp
698   // labelled by sector and plate numbers
699   //
700
701   Int_t det[5] = {sector, plate, -1, -1, -1};
702   UInt_t checkBit = 0;
703
704   Int_t channel = -1;
705
706   for (Int_t iStrip=0; iStrip<AliTOFGeometry::NStrip(plate); iStrip++) {
707     checkBit = AliBitPacking::UnpackWord(baseWord,iStrip,iStrip);
708     for (Int_t iPadZ=0; iPadZ<AliTOFGeometry::NpadZ(); iPadZ++)
709       for (Int_t iPadX=0; iPadX<AliTOFGeometry::NpadX(); iPadX++) {
710         det[2] = iStrip;
711         det[3] = iPadZ;
712         det[4] = iPadX;
713         channel = AliTOFGeometry::GetIndex(det);
714         array[channel]=checkBit;
715     }
716   }
717
718
719 }
720
721 //---------------------------------------------------------------
722 void AliTOFLvHvDataPoints::FillLVarrayPerDataPoint(Int_t nDDL, UInt_t baseWord, Short_t *array) const 
723 {
724   //
725   // Set the status of the TOF pads connected to the LV dp
726   // labelled by TOF crate number
727   //
728
729   Int_t det[5] = {nDDL/4, -1, -1, -1, -1};
730   UInt_t checkBit = 0;
731
732   Int_t iStripXsm[6] = {-1,-1,-1,-1,-1,-1};
733   Int_t firstPadX = -1;
734   Int_t lastPadX = -1;
735   Int_t plate = -1;
736   Int_t strip = -1;
737
738   Int_t channel = -1;
739
740   for (Int_t nFEAC=0; nFEAC<8; nFEAC++) {
741     checkBit = AliBitPacking::UnpackWord(baseWord,nFEAC,nFEAC);
742     firstPadX = -1;
743     lastPadX = -1;
744     GetStripsConnectedToFEAC(nDDL, nFEAC, iStripXsm, firstPadX,lastPadX);
745     for (Int_t index=0; index<6; index++) {
746       if (iStripXsm[index]==-1) continue;
747
748       for (Int_t iPadZ=0; iPadZ<AliTOFGeometry::NpadZ(); iPadZ++)
749         for (Int_t iPadX=firstPadX; iPadX<=lastPadX; iPadX++) {
750           AliTOFGeometry::GetStripAndModule(iStripXsm[index],plate,strip);
751           det[1] = plate;
752           det[2] = strip;
753           det[3] = iPadZ;
754           det[4] = iPadX;
755           channel = AliTOFGeometry::GetIndex(det);
756           array[channel]=checkBit;
757         }
758     }
759   }
760
761
762 }
763
764 //---------------------------------------------------------------
765 void AliTOFLvHvDataPoints::GetStripsConnectedToFEAC(Int_t nDDL, Int_t nFEAC, Int_t *iStrip, Int_t &firstPadX, Int_t &lastPadX) const
766 {
767   //
768   // FEAC-strip mapping:
769   // return the strips and first PadX numbers
770   // connected to the FEAC number nFEAC in the crate number nDDL
771   //
772
773   for (Int_t ii=0; ii<6; ii++) iStrip[ii]=-1;
774
775   if (nDDL<0 || nDDL>=kNddl || nFEAC<0 || nFEAC>=8) return;
776
777   switch (nDDL%4) {
778   case 0:
779     firstPadX =  0;
780     lastPadX  = AliTOFGeometry::NpadX()/2-1;
781
782     if (nFEAC<=2)
783       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC;
784     else if (nFEAC==3)
785       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=ii+6*nFEAC;
786     else if (nFEAC==4)
787       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC-1;
788     else if (nFEAC==5)
789       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=ii+6*nFEAC-1;
790     else if (nFEAC==6)
791       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC-2;
792     else if (nFEAC==7)
793       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=ii+6*nFEAC-2;
794
795     break; 
796   case 1:
797     firstPadX = AliTOFGeometry::NpadX()/2;
798     lastPadX  = AliTOFGeometry::NpadX()-1;
799
800     if (nFEAC<=2)
801       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC;
802     else if (nFEAC==3)
803       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=ii+6*nFEAC;
804     else if (nFEAC==4)
805       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC-1;
806     else if (nFEAC==5)
807       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC-1;
808     else if (nFEAC==6)
809       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=ii+6*nFEAC-1;
810     else if (nFEAC==7)
811       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=ii+6*nFEAC-2;
812
813     break;
814   case 2:
815     firstPadX = AliTOFGeometry::NpadX()/2;
816     lastPadX  = AliTOFGeometry::NpadX()-1;
817
818     if (nFEAC<=2)
819       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC);
820     else if (nFEAC==3)
821       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=90-(ii+6*nFEAC);
822     else if (nFEAC==4)
823       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC-1);
824     else if (nFEAC==5)
825       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=90-(ii+6*nFEAC-1);
826     else if (nFEAC==6)
827       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC-2);
828     else if (nFEAC==7)
829       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=90-(ii+6*nFEAC-2);
830
831     break;
832   case 3:
833     firstPadX =  0;
834     lastPadX  = AliTOFGeometry::NpadX()/2-1;
835
836     if (nFEAC<=2)
837       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC);
838     else if (nFEAC==3)
839       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=90-(ii+6*nFEAC);
840     else if (nFEAC==4)
841       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC-1);
842     else if (nFEAC==5)
843       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC-1);
844     else if (nFEAC==6)
845       for (Int_t ii=0; ii<5; ii++) iStrip[ii]=90-(ii+6*nFEAC-1);
846     else if (nFEAC==7)
847       for (Int_t ii=0; ii<6; ii++) iStrip[ii]=90-(ii+6*nFEAC-2);
848
849     break;
850  }
851
852 }
853
854 //---------------------------------------------------------------
855 void AliTOFLvHvDataPoints::Draw(const Option_t* /*option*/)
856 {
857   //
858   // Draw all histos and graphs
859   //
860
861   if(!fIsProcessed) return;
862   /*
863   TCanvas *ch;
864   TString canvasHistoName="Histos";
865   ch = new TCanvas(canvasHistoName,canvasHistoName,20,20,600,600);
866   ch->cd();
867   */
868   // to be implemented
869
870 }
871
872
873 //---------------------------------------------------------------
874 void AliTOFLvHvDataPoints::DrawHVandLVMap(Int_t index)
875 {
876   //
877   // Draw HV+LV map labelled as index
878   //
879
880   if(!fIsProcessed) return;
881
882   if (index>=fNumberOfHVandLVmaps) return;
883
884   AliTOFDCSmaps *mappa=(AliTOFDCSmaps*)GetHVandLVmap(index);
885
886   char title[100];
887   if (index==0) sprintf(title,"HVandLV map at time %d (%dst map)",mappa->GetTime(),index+1);
888   else if (index==1) sprintf(title,"HVandLV map at time %d (%dnd map)",mappa->GetTime(),index+1);
889   else if (index==2) sprintf(title,"HVandLV map at time %d (%drd map)",mappa->GetTime(),index+1);
890   else if (index>=3) sprintf(title,"HVandLV map at time %d (%dth map)",mappa->GetTime(),index+1);
891   fHisto->Delete();
892   fHisto = new TH1C("histo","",kNpads,-0.5,kNpads-0.5);
893   //fHisto->Clear();
894   fHisto->SetTitle(title);
895
896   for (Int_t ii=0; ii<kNpads; ii++)
897     fHisto->SetBinContent(ii+1,mappa->GetCellValue(ii));
898
899   fHisto->Draw();
900
901 }
902
903 //---------------------------------------------------------------
904 void AliTOFLvHvDataPoints::DrawLVMap(Int_t index)
905 {
906   //
907   // Draw LV map labelled as index
908   //
909
910   if(!fIsProcessed) return;
911
912   if (index>=fNumberOfLVdataPoints) return;
913
914   AliTOFDCSmaps *mappa=(AliTOFDCSmaps*)GetLVmap(index);
915
916   char title[100];
917   if (index==0) sprintf(title,"LV map at time %d (%dst map)",mappa->GetTime(),index+1);
918   else if (index==1) sprintf(title,"LV map at time %d (%dnd map)",mappa->GetTime(),index+1);
919   else if (index==2) sprintf(title,"LV map at time %d (%drd map)",mappa->GetTime(),index+1);
920   else if (index>=3) sprintf(title,"LV map at time %d (%dth map)",mappa->GetTime(),index+1);
921   fHisto->Delete();
922   fHisto = new TH1C("histo","",kNpads,-0.5,kNpads-0.5);
923   //fHisto->Clear();
924   fHisto->SetTitle(title);
925
926   for (Int_t ii=0; ii<kNpads; ii++)
927     fHisto->SetBinContent(ii+1,mappa->GetCellValue(ii));
928
929   fHisto->Draw();
930
931 }
932
933 //---------------------------------------------------------------
934 void AliTOFLvHvDataPoints::DrawHVMap(Int_t index)
935 {
936   //
937   // Draw HV map labelled as index
938   //
939
940   if(!fIsProcessed) return;
941
942   if (index>=fNumberOfHVdataPoints) return;
943
944   AliTOFDCSmaps *mappa=(AliTOFDCSmaps*)GetHVmap(index);
945
946   char title[100];
947   if (index==0) sprintf(title,"HV map at time %d (%dst map)",mappa->GetTime(),index+1);
948   else if (index==1) sprintf(title,"HV map at time %d (%dnd map)",mappa->GetTime(),index+1);
949   else if (index==2) sprintf(title,"HV map at time %d (%drd map)",mappa->GetTime(),index+1);
950   else if (index>=3) sprintf(title,"HV map at time %d (%dth map)",mappa->GetTime(),index+1);
951   fHisto->Delete();
952   fHisto = new TH1C("histo","",kNpads,-0.5,kNpads-0.5);
953   //fHisto->Clear();
954   fHisto->SetTitle(title);
955
956   for (Int_t ii=0; ii<kNpads; ii++)
957     fHisto->SetBinContent(ii+1,mappa->GetCellValue(ii));
958
959   fHisto->Draw();
960
961 }
962
963 //---------------------------------------------------------------
964 AliTOFDCSmaps *AliTOFLvHvDataPoints::GetHVandLVmapAtEOR()
965 {
966   //
967   // Returns HVandLV status map at EOR.
968   // If the end-of-run has been caused by TOF self,
969   // the second-last value of HVandLV status map
970   // will be taken into account, i.e. the value immediately before
971   // the change that caused EOR.
972   // This last condition is true
973   // if the time interval between the last map value and the EOR
974   // is less than 60s.
975   // If the run ended correctly, last map value will be take into account.
976   // If nothing changed during the run, the SOR map will be take into account.
977   //
978
979   AliTOFDCSmaps * lvANDhvMap = 0;
980
981   if (fNumberOfHVandLVmaps==1) { // nothing changed during the run
982
983     AliInfo(Form("Nothing changed in the TOF channels LV and HV status during the run number %d",fRun));
984
985     lvANDhvMap = fMap[fNumberOfHVandLVmaps-1];
986
987   }
988   else {
989
990     if ( (fEndTimeDCSQuery-fMap[fNumberOfHVandLVmaps-1]->GetTime()<=fNSecondsBeforeEOR) ||
991          (fEndTime-fMap[fNumberOfHVandLVmaps-1]->GetTime()<=fNSecondsBeforeEOR) ) {
992       lvANDhvMap = (AliTOFDCSmaps*)fMap[fNumberOfHVandLVmaps-2];
993       AliInfo(Form("Probably, TOF caused the EOR (EOR-t(last map))<%d).For run number %d the second-last HVandLV map will be take into account.",fNSecondsBeforeEOR,fRun));
994     }
995     else {
996       lvANDhvMap = (AliTOFDCSmaps*)fMap[fNumberOfHVandLVmaps-1];
997       AliInfo(Form("The run number %d ended correctly. The last HVandLV map will be take into account.",fRun));
998     }
999
1000   }
1001
1002   return lvANDhvMap;
1003
1004 }