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