]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/TPCbase/AliTPCPreprocessor.cxx
doxy: TPC/TPCbase converted
[u/mrichter/AliRoot.git] / TPC / TPCbase / AliTPCPreprocessor.cxx
1 /// \class AliTPCPreprocessor
2
3 /**************************************************************************
4  * Copyright(c) 2007, ALICE Experiment at CERN, All rights reserved.      *
5  *                                                                        *
6  * Author: The ALICE Off-line Project.                                    *
7  * Contributors are mentioned in the code where appropriate.              *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          *
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18
19 #include "AliTPCPreprocessor.h"
20 #include "AliShuttleInterface.h"
21
22 #include "AliCDBMetaData.h"
23 #include "AliDCSValue.h"
24 #include "AliLog.h"
25 #include "AliTPCSensorTempArray.h"
26 #include "AliTPCROC.h"
27 #include "AliTPCCalROC.h"
28 #include "AliTPCCalPad.h"
29 #include "AliTPCCalibPedestal.h"
30 #include "AliTPCCalibPulser.h"
31 #include "AliTPCCalibCE.h"
32 #include "AliTPCCalibRaw.h"
33 #include "AliTPCdataQA.h"
34 #include "ARVersion.h"
35 #include "TFile.h"
36 #include "TTree.h"
37 #include "TGraph.h"
38 #include "TEnv.h"
39 #include "TParameter.h"
40
41 #include <TTimeStamp.h>
42
43 const Int_t kValCutTemp = 100;               ///< discard temperatures > 100 degrees
44 const Int_t kDiffCutTemp = 5;                ///< discard temperature differences > 5 degrees
45 const Double_t kHighVoltageDifference = 1e-4; ///< don't record High Voltage points
46                                              // differing by less than 1e-4 from
47                                              // previous point.
48 const Double_t kGasCompositionDifference = 1e-3; ///< don't record Gas Composition points
49                                              // differing by less than 1e-3 from
50                                              // previous point
51 const TString kPedestalRunType = "PEDESTAL";  ///< pedestal run identifier
52 const TString kPulserRunType = "PULSER";     ///< pulser run identifier
53 const TString kPhysicsRunType = "PHYSICS";   ///< physics run identifier
54 const TString kCosmicRunType = "COSMIC";     ///< cosmic run identifier
55 const TString kLaserRunType = "LASER";       ///< laser run identifier
56 const TString kDaqRunType = "DAQ";           ///< DAQ run identifier
57 const TString kStandaloneRunType = "STANDALONE";  ///< STANDALONE run identifier
58 const TString kAmandaTemp = "TPC_PT_%d_TEMPERATURE"; ///< Amanda string for temperature entries
59 const TString kAmandaDDL = "DDL%d";   ///< Amanda string for list of active DDLs
60 const Int_t  kNumDDL = 216;           ///< number of TPC DDLs
61 const Int_t  kFirstDDL = 768;         ///< identifier of first DDL
62 //const Double_t kFitFraction = 0.7;                 // Fraction of DCS sensor fits required
63 const Double_t kFitFraction = -1.0;          ///< Don't require minimum number of fits in commissioning run
64 const Int_t   kNumPressureSensors = 3;    ///< number of pressure sensors
65 const char* kPressureSensorNames[kNumPressureSensors] = {
66                    "CavernAtmosPressure",
67                    "CavernAtmosPressure2",
68                    "SurfaceAtmosPressure" };
69 const Int_t  kMinCESectors = 32;      ///< minimum number of sectors (per side)
70                                       // to accept CE calibration
71
72
73 //
74 // This class is the SHUTTLE preprocessor for the TPC detector.
75 //
76
77 /// \cond CLASSIMP
78 ClassImp(AliTPCPreprocessor)
79 /// \endcond
80
81 //______________________________________________________________________________________________
82 AliTPCPreprocessor::AliTPCPreprocessor(AliShuttleInterface* shuttle) :
83   AliPreprocessor("TPC",shuttle),
84   fConfEnv(0), fTemp(0), fHighVoltage(0), fHighVoltageStat(0), fGoofie(0),
85   fPressure(0), fGasComposition(0), fConfigOK(kTRUE), fROC(0)
86 {
87   // constructor
88   fROC = AliTPCROC::Instance();
89
90   // define run types to be processed
91
92   AddRunType(kPedestalRunType);
93   AddRunType(kPulserRunType);
94   AddRunType(kPhysicsRunType);
95   AddRunType(kCosmicRunType);
96   AddRunType(kLaserRunType);
97   AddRunType(kDaqRunType);
98   AddRunType(kStandaloneRunType);
99 }
100 //______________________________________________________________________________________________
101  AliTPCPreprocessor::AliTPCPreprocessor(const AliTPCPreprocessor&  ) :
102    AliPreprocessor("TPC",0),
103    fConfEnv(0), fTemp(0), fHighVoltage(0), fHighVoltageStat(0), fGoofie(0),
104    fPressure(0), fGasComposition(0), fConfigOK(kTRUE), fROC(0)
105  {
106
107    Fatal("AliTPCPreprocessor", "copy constructor not implemented");
108 //
109 // //  fTemp = new AliTPCSensorTempArray(*(org.fTemp));
110  }
111
112 //______________________________________________________________________________________________
113 AliTPCPreprocessor::~AliTPCPreprocessor()
114 {
115   /// destructor
116
117   delete fTemp;
118   delete fHighVoltage;
119   delete fHighVoltageStat;
120   delete fGoofie;
121   delete fGasComposition;
122   delete fPressure;
123 }
124 //______________________________________________________________________________________________
125 AliTPCPreprocessor& AliTPCPreprocessor::operator = (const AliTPCPreprocessor& )
126 {
127   Fatal("operator =", "assignment operator not implemented");
128   return *this;
129 }
130
131
132 //______________________________________________________________________________________________
133 void AliTPCPreprocessor::Initialize(Int_t run, UInt_t startTime,
134         UInt_t endTime)
135 {
136
137   AliPreprocessor::Initialize(run, startTime, endTime);
138
139         AliInfo(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
140                 TTimeStamp((time_t)startTime,0).AsString(),
141                 TTimeStamp((time_t)endTime,0).AsString()));
142
143   // Preprocessor configuration
144
145         AliCDBEntry* entry = GetFromOCDB("Config", "Preprocessor");
146         if (entry) fConfEnv = (TEnv*) entry->GetObject();
147         if ( fConfEnv==0 ) {
148            Log("AliTPCPreprocsessor: Preprocessor Config OCDB entry missing.\n");
149            fConfigOK = kFALSE;
150            return;
151         }
152
153   // Temperature sensors
154
155        TTree *confTree = 0;
156
157        TString tempConf = fConfEnv->GetValue("Temperature","ON");
158        tempConf.ToUpper();
159        if (tempConf != "OFF" ) {
160         entry = GetFromOCDB("Config", "Temperature");
161         if (entry) confTree = (TTree*) entry->GetObject();
162         if ( confTree==0 ) {
163            Log("AliTPCPreprocsessor: Temperature Config OCDB entry missing.\n");
164            fConfigOK = kFALSE;
165            return;
166         }
167         fTemp = new AliTPCSensorTempArray(startTime, endTime, confTree, kAmandaTemp);
168         fTemp->SetValCut(kValCutTemp);
169         fTemp->SetDiffCut(kDiffCutTemp);
170        }
171
172   // High voltage measurements
173
174       TString hvConf = fConfEnv->GetValue("HighVoltage","ON");
175       hvConf.ToUpper();
176       if (hvConf != "OFF" ) {
177         confTree=0;
178         entry=0;
179         entry = GetFromOCDB("Config", "HighVoltage");
180         if (entry) confTree = (TTree*) entry->GetObject();
181         if ( confTree==0 ) {
182            Log("AliTPCPreprocsessor: High Voltage Config OCDB entry missing.\n");
183            fConfigOK = kFALSE;
184            return;
185         }
186         time_t timeStart = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
187         time_t timeEnd = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
188         fHighVoltage = new AliDCSSensorArray (UInt_t(timeStart),
189                                             UInt_t(timeEnd), confTree);
190       }
191
192    // High voltage status values
193
194       TString hvStatConf = fConfEnv->GetValue("HighVoltageStat","ON");
195       hvStatConf.ToUpper();
196       if (hvStatConf != "OFF" ) {
197         confTree=0;
198         entry=0;
199         entry = GetFromOCDB("Config", "HighVoltageStat");
200         if (entry) confTree = (TTree*) entry->GetObject();
201         if ( confTree==0 ) {
202            Log("AliTPCPreprocsessor: High Voltage Status Config OCDB entry missing.\n");
203            fConfigOK = kFALSE;
204            return;
205         }
206         fHighVoltageStat = new AliDCSSensorArray(startTime, endTime, confTree);
207       }
208
209    // Goofie values
210
211       TString goofieConf = fConfEnv->GetValue("Goofie","ON");
212       goofieConf.ToUpper();
213       if (goofieConf != "OFF" ) {
214         confTree=0;
215         entry=0;
216         entry = GetFromOCDB("Config", "Goofie");
217         if (entry) confTree = (TTree*) entry->GetObject();
218         if ( confTree==0 ) {
219            Log("AliTPCPreprocsessor: Goofie Config OCDB entry missing.\n");
220            fConfigOK = kFALSE;
221            return;
222         }
223         fGoofie = new AliDCSSensorArray(startTime, endTime, confTree);
224       }
225
226
227   // Gas composition measurements
228
229       TString gasConf = fConfEnv->GetValue("GasComposition","OFF");
230       gasConf.ToUpper();
231       if (gasConf != "OFF" ) {
232         confTree=0;
233         entry=0;
234         entry = GetFromOCDB("Config", "GasComposition");
235         if (entry) confTree = (TTree*) entry->GetObject();
236         if ( confTree==0 ) {
237            Log("AliTPCPreprocsessor: Gas Composition Config OCDB entry missing.\n");
238            fConfigOK = kFALSE;
239            return;
240         }
241         time_t timeStart = (time_t)(((TString)GetRunParameter("DAQ_time_start")).Atoi());
242         time_t timeEnd = (time_t)(((TString)GetRunParameter("DAQ_time_end")).Atoi());
243         fGasComposition = new AliDCSSensorArray (UInt_t(timeStart),
244                                             UInt_t(timeEnd), confTree);
245       }
246    // Pressure values
247
248        TString runType = GetRunType();
249
250        if( runType == kPhysicsRunType ||
251         runType == kLaserRunType ) {
252        TString pressureConf = fConfEnv->GetValue("Pressure","ON");
253        pressureConf.ToUpper();
254        if (pressureConf != "OFF" ) {
255          TClonesArray * array = new TClonesArray("AliDCSSensor",kNumPressureSensors);
256          for(Int_t j = 0; j < kNumPressureSensors; j++) {
257            AliDCSSensor * sens = new ((*array)[j])AliDCSSensor;
258            sens->SetStringID(kPressureSensorNames[j]);
259          }
260          fPressure = new AliDCSSensorArray(startTime, endTime, array);
261        }
262      }
263 }
264
265 //______________________________________________________________________________________________
266 UInt_t AliTPCPreprocessor::Process(TMap* dcsAliasMap)
267 {
268   /// Fills data into TPC calibrations objects
269
270   // Amanda servers provide information directly through dcsAliasMap
271
272
273   if (!fConfigOK) return 9;
274   UInt_t result = 0;
275   TObjArray *resultArray = new TObjArray();
276   TString errorHandling = fConfEnv->GetValue("ErrorHandling","ON");
277   errorHandling.ToUpper();
278   TObject * status;
279
280   UInt_t dcsResult=0;
281   if (!dcsAliasMap) {
282      dcsResult=1;
283   } else {
284      if (dcsAliasMap->GetEntries() == 0 ) dcsResult=1;
285   }
286   status = new TParameter<int>("dcsResult",dcsResult);
287   resultArray->Add(status);
288
289
290   TString runType = GetRunType();
291
292   if ( dcsResult == 0 ) {
293
294   // Temperature sensors are processed by AliTPCCalTemp
295
296     TString tempConf = fConfEnv->GetValue("Temperature","ON");
297     tempConf.ToUpper();
298     if (tempConf != "OFF" ) {
299       UInt_t tempResult = MapTemperature(dcsAliasMap);
300       if ( tempConf != "TRY") result+=tempResult;
301       status = new TParameter<int>("tempResult",tempResult);
302       resultArray->Add(status);
303     }
304
305   // High Voltage recordings
306
307
308     TString hvConf = fConfEnv->GetValue("HighVoltage","ON");
309     hvConf.ToUpper();
310     if (hvConf != "OFF" ) {
311      UInt_t hvResult = MapHighVoltage(dcsAliasMap);
312      if (hvConf != "TRY") result+=hvResult;
313      status = new TParameter<int>("hvResult",hvResult);
314      resultArray->Add(status);
315     }
316
317     // Goofie values
318
319
320     TString goofieConf = fConfEnv->GetValue("Goofie","ON");
321     goofieConf.ToUpper();
322     if (goofieConf != "OFF" ) {
323      UInt_t goofieResult = MapGoofie(dcsAliasMap);
324      if (goofieConf != "TRY") result+=goofieResult;
325      status = new TParameter<int>("goofieResult",goofieResult);
326      resultArray->Add(status);
327     }
328
329
330     // Gas composition recordings
331
332
333     TString gasConf = fConfEnv->GetValue("GasComposition","OFF");
334     gasConf.ToUpper();
335     if (gasConf != "OFF" ) {
336      UInt_t gasResult = MapGasComposition(dcsAliasMap);
337      if (gasConf != "TRY") result+=gasResult;
338      status = new TParameter<int>("gasResult",gasResult);
339      resultArray->Add(status);
340     }
341
342     // Pressure values
343
344     if( runType == kPhysicsRunType || 
345       runType == kLaserRunType || runType == kStandaloneRunType ) {    
346
347       TString pressureConf = fConfEnv->GetValue("Pressure","ON");
348       pressureConf.ToUpper();
349       if (pressureConf != "OFF" ) {
350        UInt_t pressureResult = MapPressure(dcsAliasMap);
351        status = new TParameter<int>("pressureResult",pressureResult);
352        resultArray->Add(status);
353       }
354     }
355   }
356   // Other calibration information will be retrieved through FXS files
357   //  examples:
358   //    TList* fileSourcesDAQ = GetFile(AliShuttleInterface::kDAQ, "pedestals");
359   //    const char* fileNamePed = GetFile(AliShuttleInterface::kDAQ, "pedestals", "LDC1");
360   //
361   //    TList* fileSourcesHLT = GetFile(AliShuttleInterface::kHLT, "calib");
362   //    const char* fileNameHLT = GetFile(AliShuttleInterface::kHLT, "calib", "LDC1");
363
364   // pedestal entries
365
366   if(runType == kPedestalRunType) {
367     Int_t numSources = 1;
368     Int_t pedestalSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
369     TString source = fConfEnv->GetValue("Pedestal","DAQ");
370     source.ToUpper();
371     if (source != "OFF" ) {
372      if ( source == "HLT") pedestalSource[0] = AliShuttleInterface::kHLT;
373      if (!GetHLTStatus()) pedestalSource[0] = AliShuttleInterface::kDAQ;
374      if (source == "HLTDAQ" ) {
375          numSources=2;
376          pedestalSource[0] = AliShuttleInterface::kHLT;
377          pedestalSource[1] = AliShuttleInterface::kDAQ;
378      }
379      if (source == "DAQHLT" ) numSources=2;
380      UInt_t pedestalResult=0;
381      for (Int_t i=0; i<numSources; i++ ) {
382        pedestalResult = ExtractPedestals(pedestalSource[i]);
383        if ( pedestalResult == 0 ) break;
384      }
385      result += pedestalResult;
386      status = new TParameter<int>("pedestalResult",pedestalResult);
387      resultArray->Add(status);
388     }
389   }
390
391   // pulser trigger processing
392
393   if(runType == kPulserRunType) {
394     Int_t numSources = 1;
395     Int_t pulserSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
396     TString source = fConfEnv->GetValue("Pulser","DAQ");
397     source.ToUpper();
398     if ( source != "OFF") {
399      if ( source == "HLT") pulserSource[0] = AliShuttleInterface::kHLT;
400      if (!GetHLTStatus()) pulserSource[0] = AliShuttleInterface::kDAQ;
401      if (source == "HLTDAQ" ) {
402          numSources=2;
403          pulserSource[0] = AliShuttleInterface::kHLT;
404          pulserSource[1] = AliShuttleInterface::kDAQ;
405      }
406      if (source == "DAQHLT" ) numSources=2;
407      if (source == "TRY" ) numSources=1;
408      UInt_t pulserResult=0;
409      for (Int_t i=0; i<numSources; i++ ) {
410        pulserResult = ExtractPulser(pulserSource[i]);
411        if ( pulserResult == 0 ) break;
412      }
413      if (source != "TRY") result += pulserResult;
414      status = new TParameter<int>("pulserResult",pulserResult);
415      resultArray->Add(status);
416     }
417   }
418
419
420 // raw calibration processing
421
422   if(runType == kPhysicsRunType) {
423     Int_t numSources = 1;
424     Int_t rawSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
425     TString source = fConfEnv->GetValue("Raw","DAQ");
426     source.ToUpper();
427     if ( source != "OFF") {
428      if ( source == "HLT") rawSource[0] = AliShuttleInterface::kHLT;
429      if (!GetHLTStatus()) rawSource[0] = AliShuttleInterface::kDAQ;
430      if (source == "HLTDAQ" ) {
431          numSources=2;
432          rawSource[0] = AliShuttleInterface::kHLT;
433          rawSource[1] = AliShuttleInterface::kDAQ;
434      }
435      if (source == "DAQHLT" ) numSources=2;
436      if (source == "TRY" ) numSources=1;
437      UInt_t rawResult=0;
438      for (Int_t i=0; i<numSources; i++ ) {
439        rawResult = ExtractRaw(rawSource[i]);
440        if ( rawResult == 0 ) break;
441      }
442      if (source != "TRY" )result += rawResult;
443      status = new TParameter<int>("rawResult",rawResult);
444      resultArray->Add(status);
445     }
446   }
447
448
449   // Altro configuration
450
451
452   TString altroConf = fConfEnv->GetValue("AltroConf","ON");
453   altroConf.ToUpper();
454   if (altroConf != "OFF" ) {
455    UInt_t altroResult = ExtractAltro(AliShuttleInterface::kDCS,dcsAliasMap);
456    if (altroConf != "TRY" ) result+=altroResult;
457    status = new TParameter<int>("altroResult",altroResult);
458    resultArray->Add(status);
459   }
460
461
462   // Central Electrode processing
463
464   if( runType == kPhysicsRunType ||
465       runType == kLaserRunType ) {
466
467     Int_t numSources = 1;
468     Int_t ceSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
469     TString source = fConfEnv->GetValue("CE","DAQ");
470     source.ToUpper();
471     if ( source != "OFF" ) {
472      if ( source == "HLT") ceSource[0] = AliShuttleInterface::kHLT;
473      if (!GetHLTStatus()) ceSource[0] = AliShuttleInterface::kDAQ;
474      if (source == "HLTDAQ" ) {
475         numSources=2;
476         ceSource[0] = AliShuttleInterface::kHLT;
477         ceSource[1] = AliShuttleInterface::kDAQ;
478      }
479      if (source == "DAQHLT" ) numSources=2;
480      if (source == "TRY" ) numSources=1;
481      UInt_t ceResult=0;
482      for (Int_t i=0; i<numSources; i++ ) {
483        ceResult = ExtractCE(ceSource[i]);
484        if ( ceResult == 0 ) break;
485      }
486
487    // only flag error if CE result is missing from LASER runs
488    //    -- for PHYSICS run do CE processing if data available
489
490      if ( runType == kLaserRunType && source != "TRY"  && ceResult<10 ) result += ceResult;
491      status = new TParameter<int>("ceResult",ceResult);
492      resultArray->Add(status);
493
494     numSources = 1;
495     Int_t qaSource[2] = {AliShuttleInterface::kDAQ,AliShuttleInterface::kHLT} ;
496     source = fConfEnv->GetValue("QA","DAQ");
497     source.ToUpper();
498     if ( source != "OFF" ) {
499      if ( source == "HLT") qaSource[0] = AliShuttleInterface::kHLT;
500      if (!GetHLTStatus()) qaSource[0] = AliShuttleInterface::kDAQ;
501      if (source == "HLTDAQ" ) {
502         numSources=2;
503         qaSource[0] = AliShuttleInterface::kHLT;
504         qaSource[1] = AliShuttleInterface::kDAQ;
505      }
506      if (source == "DAQHLT" ) numSources=2;
507      if (source == "TRY" ) numSources=1;
508      UInt_t qaResult=0;
509      for (Int_t i=0; i<numSources; i++ ) {
510        qaResult = ExtractQA(qaSource[i]);
511        if ( qaResult == 0 ) break;
512      }
513 //     result += qaResult;
514      if ( qaResult !=0 ) Log ("ExtractQA failed, no QA entry available.");
515      status = new TParameter<int>("qaResult",qaResult);
516      resultArray->Add(status);
517     }
518    }
519   }
520
521 // Store component status to OCDB
522
523   AliCDBMetaData metaData;
524   metaData.SetBeamPeriod(0);
525   metaData.SetResponsible("Haavard Helstrup");
526   metaData.SetAliRootVersion(ALIROOT_BRANCH);
527   metaData.SetComment("Preprocessor AliTPC status.");
528   Bool_t storeOK = Store("Calib", "PreprocStatus", resultArray, &metaData, 0,  kFALSE);
529   if (!storeOK) Log ("Unable to store preprocessor status entry");
530
531   resultArray->Delete();
532   delete resultArray;
533
534   if (errorHandling == "OFF" ) return 0;
535   return result;
536
537 }
538 //______________________________________________________________________________________________
539 UInt_t AliTPCPreprocessor::MapTemperature(TMap* dcsAliasMap)
540 {
541
542    /// extract DCS temperature maps. Perform fits to save space
543
544   UInt_t result=0;
545   TMap *map = fTemp->ExtractDCS(dcsAliasMap);
546   if (map) {
547     fTemp->MakeSplineFit(map);
548     Double_t fitFraction = 1.0*fTemp->NumFits()/fTemp->NumSensors();
549     if (fitFraction > kFitFraction ) {
550       AliInfo(Form("Temperature values extracted, fits performed.\n"));
551     } else {
552       Log ("Too few temperature maps fitted. \n");
553       result = 9;
554     }
555   } else {
556     Log("No temperature map extracted. \n");
557     result=9;
558   }
559   delete map;
560   // Now store the final CDB file
561
562   if ( result == 0 ) {
563         AliCDBMetaData metaData;
564         metaData.SetBeamPeriod(0);
565         metaData.SetResponsible("Haavard Helstrup");
566         metaData.SetAliRootVersion(ALIROOT_BRANCH);
567         metaData.SetComment("Preprocessor AliTPC data base entries.");
568
569         Bool_t storeOK = Store("Calib", "Temperature", fTemp, &metaData, 0, kFALSE);
570         if ( !storeOK )  result=1;
571
572    }
573
574    return result;
575
576 }
577 //______________________________________________________________________________________________
578 UInt_t AliTPCPreprocessor::MapPressure(TMap* dcsAliasMap)
579 {
580
581    /// extract DCS pressure maps. Perform fits to save space
582
583   UInt_t result=0;
584   TMap *map = fPressure->ExtractDCS(dcsAliasMap);
585   if (map) {
586     fPressure->MakeSplineFit(map);
587     Double_t fitFraction = 1.0*fPressure->NumFits()/fPressure->NumSensors();
588     if (fitFraction > kFitFraction ) {
589       AliInfo(Form("Pressure values extracted, fits performed.\n"));
590     } else {
591       Log ("Too few pressure maps fitted. \n");
592       result = 9;
593     }
594   } else {
595     Log("No pressure map extracted. \n");
596     result=9;
597   }
598   delete map;
599   return result;
600 }
601
602 //______________________________________________________________________________________________
603 UInt_t AliTPCPreprocessor::MapHighVoltage(TMap* dcsAliasMap)
604 {
605
606    /// extract DCS HV maps. Perform fits to save space
607
608   UInt_t result=0;
609   TMap *map = fHighVoltage->ExtractDCS(dcsAliasMap);
610   if (map) {
611     fHighVoltage->ClearFit();
612     fHighVoltage->RemoveGraphDuplicates(kHighVoltageDifference);
613                // don't keep new point if too similar to previous one
614     fHighVoltage->SetGraph(map);
615   } else {
616     Log("No high voltage recordings extracted. \n");
617     result=9;
618   }
619   delete map;
620
621   TString hvStatConf = fConfEnv->GetValue("HighVoltageStat","ON");
622   hvStatConf.ToUpper();
623   if (hvStatConf != "OFF" ) {
624     TMap *map2 = fHighVoltageStat->ExtractDCS(dcsAliasMap);
625     if (map2) {
626       fHighVoltageStat->ClearFit();
627       fHighVoltageStat->SetGraph(map2);
628     } else {
629        Log("No high voltage status recordings extracted. \n");
630       result=9;
631     }
632     delete map2;
633
634     // add status maps to high voltage sensor array
635
636     fHighVoltage->AddSensors(fHighVoltageStat);
637    }
638   // Now store the final CDB file
639
640   if ( result == 0 ) {
641         AliCDBMetaData metaData;
642         metaData.SetBeamPeriod(0);
643         metaData.SetResponsible("Haavard Helstrup");
644         metaData.SetAliRootVersion(ALIROOT_BRANCH);
645         metaData.SetComment("Preprocessor AliTPC data base entries.");
646
647         Bool_t storeOK = Store("Calib", "HighVoltage", fHighVoltage, &metaData, 0, kFALSE);
648         if ( !storeOK )  result=1;
649
650    }
651
652    return result;
653
654 }
655
656 //______________________________________________________________________________________________
657 UInt_t AliTPCPreprocessor::MapGoofie(TMap* dcsAliasMap)
658 {
659
660    /// extract DCS Goofie maps. Do not perform fits (low update rate)
661
662   UInt_t result=0;
663
664   TMap *map = fGoofie->ExtractDCS(dcsAliasMap);
665   if (map) {
666     fGoofie->ClearFit();
667     fGoofie->SetGraph(map);
668   } else {
669     Log("No Goofie recordings extracted. \n");
670     result=9;
671   }
672   delete map;
673
674   // Now store the final CDB file
675
676   if ( result == 0 ) {
677         AliCDBMetaData metaData;
678         metaData.SetBeamPeriod(0);
679         metaData.SetResponsible("Haavard Helstrup");
680         metaData.SetAliRootVersion(ALIROOT_BRANCH);
681         metaData.SetComment("Preprocessor AliTPC data base entries.");
682
683         Bool_t storeOK = Store("Calib", "Goofie", fGoofie, &metaData, 0, kFALSE);
684         if ( !storeOK )  result=1;
685
686    }
687
688    return result;
689
690 }
691
692 //______________________________________________________________________________________________
693 UInt_t AliTPCPreprocessor::MapGasComposition(TMap* dcsAliasMap)
694 {
695
696    /// extract DCS HV maps. Perform fits to save space
697
698   UInt_t result=0;
699   TMap *map = fGasComposition->ExtractDCS(dcsAliasMap);
700   if (map) {
701     fGasComposition->ClearFit();
702     fGasComposition->RemoveGraphDuplicates(kGasCompositionDifference);
703                // don't keep new point if too similar to previous one
704     fGasComposition->SetGraph(map);
705   } else {
706     Log("No gas composition recordings extracted. \n");
707     result=9;
708   }
709   delete map;
710
711   // Now store the final CDB file
712
713   if ( result == 0 ) {
714         AliCDBMetaData metaData;
715         metaData.SetBeamPeriod(0);
716         metaData.SetResponsible("Haavard Helstrup");
717         metaData.SetAliRootVersion(ALIROOT_BRANCH);
718         metaData.SetComment("Preprocessor AliTPC data base entries.");
719
720         Bool_t storeOK = Store("Calib", "GasComposition", fGasComposition, &metaData, 0, kFALSE);
721         if ( !storeOK )  result=1;
722
723    }
724
725    return result;
726
727 }
728
729
730 //______________________________________________________________________________________________
731
732 UInt_t AliTPCPreprocessor::ExtractPedestals(Int_t sourceFXS)
733 {
734  /// Read pedestal file from file exchage server
735  /// Keep original entry from OCDB in case no new pedestals are available
736
737  AliTPCCalPad *calPadPedOCDB=0;
738  AliCDBEntry* entry = GetFromOCDB("Calib", "Pedestals");
739  if (entry) calPadPedOCDB = (AliTPCCalPad*)entry->GetObject();
740  if ( calPadPedOCDB==NULL ) {
741      Log("AliTPCPreprocsessor: No previous TPC pedestal entry available.\n");
742  }
743
744  AliTPCCalPad *calPadRMSOCDB=0;
745  entry = GetFromOCDB("Calib", "PadNoise");
746  if (entry) calPadRMSOCDB = (AliTPCCalPad*)entry->GetObject();
747  if ( calPadRMSOCDB==NULL ) {
748      Log("AliTPCPreprocsessor: No previous TPC noise entry available.\n");
749  }
750
751   AliTPCCalPad* calPadPed = new AliTPCCalPad("PedestalsMean","PedestalsMean");
752   AliTPCCalPad* calPadRMS = new AliTPCCalPad("PedestalsRMS","PedestalsRMS");
753
754
755  UInt_t result=0;
756
757  Int_t nSectors = fROC->GetNSectors();
758  TVectorD foundSectorsPed(nSectors);
759  foundSectorsPed=0;
760  TVectorD foundSectorsRMS(nSectors);
761  foundSectorsRMS=0;
762
763  TList* list = GetFileSources(sourceFXS,"pedestals");
764
765  if (list && list->GetEntries()>0) {
766
767 //  loop through all files from LDCs
768
769     Bool_t changed=false;
770     UInt_t index = 0;
771     while (list->At(index)!=NULL) {
772      TObjString* fileNameEntry = (TObjString*) list->At(index);
773      if (fileNameEntry!=NULL) {
774         TString fileName = GetFile(sourceFXS, "pedestals",
775                                          fileNameEntry->GetString().Data());
776         TFile *f = TFile::Open(fileName);
777         if (!f) {
778           Log ("Error opening pedestal file.");
779           result =2;
780           break;
781         }
782         AliTPCCalibPedestal *calPed;
783         f->GetObject("tpcCalibPedestal",calPed);
784         if ( !calPed ) {
785           Log ("No pedestal calibration object in file.");
786           result = 2;
787           break;
788         }
789
790         //  replace entries for the sectors available in the present file
791
792         changed=true;
793         for (Int_t sector=0; sector<nSectors; sector++) {
794            AliTPCCalROC *rocPed=calPed->GetCalRocPedestal(sector, kFALSE);
795            if ( rocPed ) {
796                AliTPCCalROC* roc=calPadPed->GetCalROC(sector);
797                roc->Add(rocPed,1);
798                foundSectorsPed[sector]++;
799            }
800            AliTPCCalROC *rocRMS=calPed->GetCalRocRMS(sector, kFALSE);
801            if ( rocRMS )  {
802                AliTPCCalROC* roc=calPadRMS->GetCalROC(sector);
803                roc->Add(rocRMS,1);
804                foundSectorsRMS[sector]++;
805            }
806         }
807         delete calPed;
808         f->Close();
809       }
810      ++index;
811     }  // while(list)
812
813 //
814 //  Check if calibration is complete -- otherwise take from old OCDB entry
815
816 // inner sectors
817     for (Int_t sector=0; sector<nSectors/2; sector++) {
818        if (foundSectorsPed[sector] < 1 ) {
819           if (calPadPedOCDB) {
820             AliTPCCalROC *rocOCDB=calPadPedOCDB->GetCalROC(sector);
821             calPadPed->SetCalROC(rocOCDB,sector);
822           } else {
823             const int mess_length=100;
824             char message[mess_length];
825             snprintf(message,mess_length,"Missing pedestals for sector %d - also not available from previous OCDB entry.\n",
826                      sector);
827             Log (message);
828             result = 2;
829           }
830        }
831        if (foundSectorsRMS[sector] < 1 ) {
832           if (calPadRMSOCDB) {
833             AliTPCCalROC *rocOCDB=calPadRMSOCDB->GetCalROC(sector);
834             calPadRMS->SetCalROC(rocOCDB,sector);
835           } else {
836             const int mess_length=100;
837             char message[mess_length];
838             snprintf(message,mess_length,"Missing pedestal RMS for sector %d - also not available from previous OCDB entry.\n",
839                      sector);
840             Log (message);
841             result = 2;
842           }
843        }
844     }
845
846 // outer sectors -- two updates needed
847
848     for (Int_t sector=nSectors/2; sector<nSectors; sector++) {
849        if (foundSectorsPed[sector] < 2 ) {
850           if (calPadPedOCDB) {
851             AliTPCCalROC *rocOCDB=calPadPedOCDB->GetCalROC(sector);
852             calPadPed->SetCalROC(rocOCDB,sector);
853           } else {
854             const int mess_length=100;
855             char message[mess_length];
856             snprintf(message,mess_length,"Missing pedestals for sector %d - also not available from previous OCDB entry.\n",
857                      sector);
858             Log (message);
859             result = 2;
860           }
861
862        }
863        if (foundSectorsRMS[sector] < 2 ) {
864           if (calPadRMSOCDB) {
865             AliTPCCalROC *rocOCDB=calPadRMSOCDB->GetCalROC(sector);
866             calPadRMS->SetCalROC(rocOCDB,sector);
867           } else {
868             const int mess_length=100;
869             char message[mess_length];
870             snprintf(message,mess_length,"Missing pedestal RMS for sector %d - also not available from previous OCDB entry.\n",
871                      sector);
872             Log (message);
873             result = 2;
874           }
875
876        }
877     }
878
879
880 //
881 //  Store updated pedestal entry to OCDB
882 //
883     if (changed) {
884      AliCDBMetaData metaData;
885      metaData.SetBeamPeriod(0);
886      metaData.SetResponsible("Haavard Helstrup");
887      metaData.SetAliRootVersion(ALIROOT_BRANCH);
888      metaData.SetComment("Preprocessor AliTPC data base entries.");
889
890      Bool_t storeOK = Store("Calib", "Pedestals", calPadPed, &metaData, 0, kTRUE);
891      if ( !storeOK ) ++result;
892      storeOK = Store("Calib", "PadNoise", calPadRMS, &metaData, 0, kTRUE);
893      if ( !storeOK ) ++result;
894     }
895   } else {
896     Log ("Error: no entries in pedestal file list!");
897     result = 1;
898   }
899
900   delete calPadPed;
901   delete calPadRMS;
902   delete calPadPedOCDB;
903   delete calPadRMSOCDB;
904
905   return result;
906 }
907
908 //______________________________________________________________________________________________
909
910
911 UInt_t AliTPCPreprocessor::ExtractPulser(Int_t sourceFXS)
912 {
913  /// Read pulser calibration file from file exchage server
914  /// Keep original entry from OCDB in case no new pulser calibration is available
915
916  TObjArray *pulserObjects = new TObjArray;
917  TObjArray *pulserObjectsOCDB=0;
918
919  AliCDBEntry* entry = GetFromOCDB("Calib", "Pulser");
920  if (entry) pulserObjectsOCDB = (TObjArray*)entry->GetObject();
921  if ( pulserObjectsOCDB==NULL ) {
922      Log("AliTPCPreprocsessor: No previous TPC pulser entry available.\n");
923  }
924
925  AliTPCCalPad *pulserTmean = new AliTPCCalPad("PulserTmean","PulserTmean");
926  pulserObjects->Add(pulserTmean);
927
928  AliTPCCalPad *pulserTrms = new AliTPCCalPad("PulserTrms","PulserTrms");
929  pulserObjects->Add(pulserTrms);
930
931  AliTPCCalPad *pulserQmean = new AliTPCCalPad("PulserQmean","PulserQmean");
932  pulserObjects->Add(pulserQmean);
933
934
935
936  UInt_t result=0;
937
938  Int_t nSectors = fROC->GetNSectors();
939  TVectorD foundTmean(nSectors);
940  foundTmean=0;
941  TVectorD foundTrms(nSectors);
942  foundTrms=0;
943  TVectorD foundQmean(nSectors);
944  foundQmean=0;
945
946
947  TList* list = GetFileSources(sourceFXS,"pulser");
948
949  if (list && list->GetEntries()>0) {
950
951 //  loop through all files from LDCs
952
953     Bool_t changed=false;
954     UInt_t index = 0;
955     while (list->At(index)!=NULL) {
956      TObjString* fileNameEntry = (TObjString*) list->At(index);
957      if (fileNameEntry!=NULL) {
958         TString fileName = GetFile(sourceFXS, "pulser",
959                                          fileNameEntry->GetString().Data());
960         TFile *f = TFile::Open(fileName);
961         if (!f) {
962           Log ("Error opening pulser file.");
963           result =2;
964           break;
965         }
966         AliTPCCalibPulser *calPulser;
967         f->GetObject("tpcCalibPulser",calPulser);
968         if ( !calPulser ) {
969           Log ("No pulser calibration object in file.");
970           result = 2;
971           break;
972         }
973
974         //  replace entries for the sectors available in the present file
975
976         changed=true;
977         for (Int_t sector=0; sector<nSectors; sector++) {
978            AliTPCCalROC *rocTmean=calPulser->GetCalRocT0(sector);
979            if ( rocTmean ) {
980                AliTPCCalROC* roc=pulserTmean->GetCalROC(sector);
981                roc->Add(rocTmean,1);
982                foundTmean[sector]++;
983            }
984            AliTPCCalROC *rocTrms=calPulser->GetCalRocRMS(sector);
985            if ( rocTrms ) {
986                AliTPCCalROC* roc=pulserTrms->GetCalROC(sector);
987                roc->Add(rocTrms,1);
988                foundTrms[sector]++;
989            }
990            AliTPCCalROC *rocQmean=calPulser->GetCalRocQ(sector);
991            if ( rocQmean ) {
992                AliTPCCalROC* roc=pulserQmean->GetCalROC(sector);
993                roc->Add(rocQmean,1);
994                foundQmean[sector]++;
995            }
996         }
997        delete calPulser;
998        f->Close();
999       }
1000      ++index;
1001     }  // while(list)
1002
1003
1004
1005
1006 if (pulserObjectsOCDB) {
1007   AliTPCCalPad* pulserTmeanOCDB = (AliTPCCalPad*)pulserObjectsOCDB->FindObject("PulserTmean");
1008   AliTPCCalPad* pulserTrmsOCDB  = (AliTPCCalPad*)pulserObjectsOCDB->FindObject("PulserTrms");
1009   AliTPCCalPad* pulserQmeanOCDB = (AliTPCCalPad*)pulserObjectsOCDB->FindObject("PulserQmean");
1010
1011 //
1012 //  Check if calibration is complete -- otherwise take from old OCDB entry
1013
1014 // inner sectors
1015     for (Int_t sector=0; sector<nSectors/2; sector++) {
1016        if (foundTmean[sector] < 1 ) {
1017          if (pulserTmeanOCDB) {
1018           AliTPCCalROC* rocOCDB = pulserTmeanOCDB->GetCalROC(sector);
1019           if ( rocOCDB ) pulserTmean->SetCalROC(rocOCDB,sector);
1020          }
1021        }
1022        if (foundTrms[sector] < 1 ) {
1023          if (pulserTrmsOCDB) {
1024           AliTPCCalROC* rocOCDB = pulserTrmsOCDB->GetCalROC(sector);
1025           if ( rocOCDB ) pulserTrms->SetCalROC(rocOCDB,sector);
1026          }
1027        }
1028        if (foundQmean[sector] < 1 ) {
1029          if (pulserQmeanOCDB) {
1030           AliTPCCalROC* rocOCDB = pulserQmeanOCDB->GetCalROC(sector);
1031           if ( rocOCDB ) pulserQmean->SetCalROC(rocOCDB,sector);
1032          }
1033        }
1034     }
1035
1036 // outer sectors -- two updates needed
1037
1038     for (Int_t sector=0; sector<nSectors/2; sector++) {
1039        if (foundTmean[sector] < 2 ) {
1040          if (pulserTmeanOCDB) {
1041           AliTPCCalROC* rocOCDB = pulserTmeanOCDB->GetCalROC(sector);
1042           if ( rocOCDB ) pulserTmean->SetCalROC(rocOCDB,sector);
1043          }
1044        }
1045        if (foundTrms[sector] < 2 ) {
1046          if (pulserTrmsOCDB) {
1047           AliTPCCalROC* rocOCDB = pulserTrmsOCDB->GetCalROC(sector);
1048           if ( rocOCDB ) pulserTrms->SetCalROC(rocOCDB,sector);
1049          }
1050        }
1051        if (foundQmean[sector] < 2 ) {
1052          if (pulserQmeanOCDB) {
1053           AliTPCCalROC* rocOCDB = pulserQmeanOCDB->GetCalROC(sector);
1054           if ( rocOCDB ) pulserQmean->SetCalROC(rocOCDB,sector);
1055          }
1056        }
1057
1058     }
1059 }
1060
1061 //
1062 //  Store updated pedestal entry to OCDB
1063 //
1064     if (changed) {
1065      AliCDBMetaData metaData;
1066      metaData.SetBeamPeriod(0);
1067      metaData.SetResponsible("Haavard Helstrup");
1068      metaData.SetAliRootVersion(ALIROOT_BRANCH);
1069      metaData.SetComment("Preprocessor AliTPC data base entries.");
1070
1071      Bool_t storeOK = Store("Calib", "Pulser", pulserObjects, &metaData, 0, kTRUE);
1072      if ( !storeOK ) ++result;
1073     }
1074   } else {
1075     Log ("Error: no entries in pulser file list!");
1076     result = 1;
1077   }
1078   pulserObjects->Delete();
1079   delete pulserObjects;
1080   if (pulserObjectsOCDB) pulserObjectsOCDB->Delete();
1081   delete pulserObjectsOCDB;
1082
1083   return result;
1084 }
1085
1086 //______________________________________________________________________________________________
1087
1088
1089 UInt_t AliTPCPreprocessor::ExtractRaw(Int_t sourceFXS)
1090 {
1091  /// Read Raw calibration file from file exchage server
1092
1093  UInt_t result=0;
1094  TObjArray* rawArray = new TObjArray;
1095
1096  TList* list = GetFileSources(sourceFXS,"tpcCalibRaw");
1097
1098  if (list && list->GetEntries()>0) {
1099
1100 //  loop through all files
1101
1102     UInt_t index = 0;
1103     while (list->At(index)!=NULL) {
1104      TObjString* fileNameEntry = (TObjString*) list->At(index);
1105      if (fileNameEntry!=NULL) {
1106         TString fileName = GetFile(sourceFXS, "tpcCalibRaw",
1107                                          fileNameEntry->GetString().Data());
1108         TFile *f = TFile::Open(fileName);
1109         if (!f) {
1110           Log ("Error opening raw file.");
1111           result =2;
1112           break;
1113         }
1114         AliTPCCalibRaw *calRaw;
1115         f->GetObject("tpcCalibRaw",calRaw);
1116         if ( !calRaw ) {
1117           Log ("No raw calibration object in file.");
1118           result = 2;
1119           break;
1120         }
1121        rawArray->Add(calRaw);
1122        f->Close();
1123       }
1124      ++index;
1125     }  // while(list)
1126 //
1127 //  Store updated pedestal entry to OCDB
1128 //
1129      AliCDBMetaData metaData;
1130      metaData.SetBeamPeriod(0);
1131      metaData.SetResponsible("Haavard Helstrup");
1132      metaData.SetAliRootVersion(ALIROOT_BRANCH);
1133      metaData.SetComment("Preprocessor AliTPC data base entries.");
1134
1135      Bool_t storeOK = Store("Calib", "Raw", rawArray, &metaData, 0, kTRUE);
1136      if ( !storeOK ) ++result;
1137   } else {
1138     Log ("Error: no entries in raw file list!");
1139     result = 1;
1140   }
1141
1142   rawArray->Delete();
1143   delete rawArray;
1144
1145   return result;
1146 }
1147 //______________________________________________________________________________________________
1148
1149 UInt_t AliTPCPreprocessor::ExtractCE(Int_t sourceFXS)
1150 {
1151  /// Read Central Electrode file from file exchage server
1152
1153   AliTPCCalPad *ceTmean=0;
1154   AliTPCCalPad *ceTrms=0;
1155   AliTPCCalPad *ceQmean=0;
1156   TObjArray    *rocTtime=0;
1157   TObjArray    *rocQtime=0;
1158
1159   TObjArray    *ceObjects= new TObjArray;
1160
1161
1162   Int_t nSectors = fROC->GetNSectors();
1163
1164   ceTmean = new AliTPCCalPad("CETmean","CETmean");
1165   ceObjects->Add(ceTmean);
1166
1167   ceTrms = new AliTPCCalPad("CETrms","CETrms");
1168   ceObjects->Add(ceTrms);
1169
1170   ceQmean = new AliTPCCalPad("CEQmean","CEQmean");
1171   ceObjects->Add(ceQmean);
1172
1173   rocTtime = new TObjArray(nSectors+2);   // also make room for A and C side average
1174   rocTtime->SetName("rocTtime");
1175   ceObjects->Add(rocTtime);
1176
1177   rocQtime = new TObjArray(nSectors);
1178   rocQtime->SetName("rocQtime");
1179   ceObjects->Add(rocQtime);
1180
1181   //=== new part
1182   TObjArray *arrFitGraphs=new TObjArray;
1183   arrFitGraphs->SetName("ceFitsDrift");
1184   ceObjects->Add(arrFitGraphs);
1185
1186 // Temperature maps
1187
1188   if (fTemp) {
1189     AliTPCSensorTempArray *tempMap = new AliTPCSensorTempArray(*fTemp);
1190     tempMap->SetNameTitle("TempMap","TempMap");
1191     ceObjects->Add(tempMap);
1192   }
1193
1194 // Pressure maps
1195
1196   if (fPressure) {
1197     AliDCSSensor *sensor=0, *sensorCopy=0;
1198     for (Int_t isensor=0; isensor<kNumPressureSensors; ++isensor ) {
1199       sensor = fPressure->GetSensor(kPressureSensorNames[isensor]);
1200       if (sensor) {
1201         sensorCopy = new AliDCSSensor(*sensor);
1202         sensorCopy->SetNameTitle(kPressureSensorNames[isensor],kPressureSensorNames[isensor]);
1203         ceObjects->Add(sensorCopy);
1204       }
1205     }
1206   }
1207
1208   UInt_t result=0;
1209
1210   TList* list = GetFileSources(sourceFXS,"CE");
1211
1212   if (list && list->GetEntries()>0) {
1213
1214 //  loop through all files from LDCs
1215
1216     UInt_t index = 0;
1217     while (list->At(index)!=NULL) {
1218       TObjString* fileNameEntry = (TObjString*) list->At(index);
1219       if (fileNameEntry!=NULL) {
1220         TString fileName = GetFile(sourceFXS, "CE",
1221                                    fileNameEntry->GetString().Data());
1222         AliTPCCalibCE *calCE=AliTPCCalibCE::ReadFromFile(fileName.Data());
1223
1224         if (!calCE) {
1225           Log ("No valid calibCE object.");
1226           result=2;
1227           break;
1228         }
1229         //  replace entries for the sectors available in the present file
1230
1231         for (Int_t sector=0; sector<nSectors; sector++) {
1232           AliTPCCalROC *rocTmean=calCE->GetCalRocT0(sector);
1233           if ( rocTmean )  ceTmean->SetCalROC(rocTmean,sector);
1234           AliTPCCalROC *rocTrms=calCE->GetCalRocRMS(sector);
1235           if ( rocTrms )  ceTrms->SetCalROC(rocTrms,sector);
1236           AliTPCCalROC *rocQmean=calCE->GetCalRocQ(sector);
1237           if ( rocQmean )  ceQmean->SetCalROC(rocQmean,sector);
1238           TGraph *grT=calCE->MakeGraphTimeCE(sector,0,2); // T time graph
1239           if ( grT ) rocTtime->AddAt(grT,sector);
1240           TGraph *grQ=calCE->MakeGraphTimeCE(sector,0,3); // Q time graph
1241           if ( grQ ) rocQtime->AddAt(grQ,sector);
1242         }
1243
1244         TGraph *grT=calCE->MakeGraphTimeCE(-1,0,2); // A side average
1245         if ( grT ) {
1246           rocTtime->AddAt(grT,nSectors);
1247         } else {
1248           result=10;
1249         }
1250         grT=calCE->MakeGraphTimeCE(-2,0,2); // C side average
1251         if ( grT ) {
1252           rocTtime->AddAt(grT,nSectors+1);
1253         } else {
1254           result=10;
1255         }
1256
1257         delete calCE;
1258       }
1259       ++index;
1260     }  // while(list)
1261 //
1262 //   Check number of calibrated sectors per side
1263 //
1264     Int_t aside=0, cside=0;
1265     for (Int_t ind=0; ind<nSectors/4; ind++ ) {
1266         TGraph *grT=(TGraph*)rocTtime->At(ind);
1267         if (grT) aside++;
1268         grT=(TGraph*)rocTtime->At(ind+nSectors/2);
1269         if (grT) aside++;
1270         grT=(TGraph*)rocTtime->At(ind+nSectors/4);
1271         if (grT) cside++;
1272         grT=(TGraph*)rocTtime->At(ind+3*nSectors/4);
1273         if (grT) cside++;
1274      }
1275      if ( (aside<kMinCESectors) && (cside<kMinCESectors) ) {
1276         Log (Form("ExtractCE: Too few fitted sectors: Aside =%d, Cside=%d\n",
1277              aside, cside)) ;
1278         result=10;
1279      }
1280
1281     //
1282     //=== New CE part
1283     //    if it is validated this part needs to be modified again
1284     //    currently its only processed if there is a valid standard CE object
1285     //
1286     list = GetFileSources(sourceFXS,"CEnew");
1287
1288     if (result==0 && list && list->GetEntries()>0) {
1289
1290 //  loop through all files from LDCs
1291
1292       UInt_t index2 = 0;
1293       while (list->At(index2)!=NULL) {
1294         TObjString* fileNameEntry = (TObjString*) list->At(index2);
1295         if (fileNameEntry!=NULL) {
1296           TString fileName = GetFile(sourceFXS, "CEnew",
1297                                      fileNameEntry->GetString().Data());
1298           AliTPCCalibCE *calCE=AliTPCCalibCE::ReadFromFile(fileName.Data());
1299
1300           if (!calCE) {
1301             Log ("No valid new calibCE object.");
1302 //             result=2;
1303             break;
1304           }
1305
1306           TIter nextObj(calCE->GetArrFitGraphs());
1307           TObject *obj=0x0;
1308           while ( (obj=nextObj()) ){
1309             arrFitGraphs->Add(obj->Clone());
1310           }
1311           delete calCE;
1312         }
1313         ++index2;
1314       }
1315     }
1316
1317 //
1318 //  Store updated pedestal entry to OCDB
1319 //
1320     AliCDBMetaData metaData;
1321     metaData.SetBeamPeriod(0);
1322     metaData.SetResponsible("Haavard Helstrup");
1323     metaData.SetAliRootVersion(ALIROOT_BRANCH);
1324     metaData.SetComment("Preprocessor AliTPC data base entries.");
1325
1326     if ( result == 0 ) {
1327       Bool_t storeOK = Store("Calib", "CE", ceObjects, &metaData, 0, kTRUE);
1328       if ( !storeOK ) ++result;
1329     } else {
1330       Log ("Warning: Average time graphs not available - no OCDB entry written");
1331     }
1332   } else {
1333     Log ("Error: no CE entries available from FXS!");
1334     result = 1;
1335   }
1336
1337   ceObjects->Delete();
1338   delete ceObjects;
1339
1340   return result;
1341 }
1342 //______________________________________________________________________________________________
1343
1344 UInt_t AliTPCPreprocessor::ExtractQA(Int_t sourceFXS)
1345 {
1346  /// Read Quality Assurance file from file exchage server
1347
1348  UInt_t result=0;
1349
1350  TList* list = GetFileSources(sourceFXS,"QA");
1351
1352  if (list && list->GetEntries()>0) {
1353
1354 //  only one QA object should be available!
1355
1356     AliTPCdataQA *calQA;
1357
1358     UInt_t nentries = list->GetEntries();
1359     UInt_t index=0;
1360     if ( nentries > 1) Log ( "More than one QA entry. First one processed");
1361     TObjString* fileNameEntry = (TObjString*) list->At(index);
1362     if (fileNameEntry!=NULL) {
1363         TString fileName = GetFile(sourceFXS, "QA",
1364                                          fileNameEntry->GetString().Data());
1365         TFile *f = TFile::Open(fileName);
1366         if (!f) {
1367           Log ("Error opening QA file.");
1368           result =2;
1369         } else {
1370           f->GetObject("tpcCalibQA",calQA);
1371           if ( calQA ) {
1372 //
1373 //  Store updated pedestal entry to OCDB
1374 //
1375            AliCDBMetaData metaData;
1376            metaData.SetBeamPeriod(0);
1377            metaData.SetResponsible("Haavard Helstrup");
1378            metaData.SetAliRootVersion(ALIROOT_BRANCH);
1379            metaData.SetComment("Preprocessor AliTPC data base entries.");
1380
1381            Bool_t storeOK = Store("Calib", "QA", calQA, &metaData, 0, kFALSE);
1382            if ( !storeOK ) ++result;
1383
1384            delete calQA;
1385           }
1386         }
1387     } else {
1388     Log ("Error: no QA files on FXS!");
1389     result = 2;
1390     }
1391   } else {
1392     Log ("Error: no QA entries in FXS list!");
1393     result = 1;
1394   }
1395   return result;
1396 }
1397
1398 //______________________________________________________________________________________________
1399
1400
1401 UInt_t AliTPCPreprocessor::ExtractAltro(Int_t sourceFXS, TMap* dcsMap)
1402 {
1403  /// Read Altro configuration file from file exchage server
1404  /// Keep original entry from OCDB in case no new pulser calibration is available
1405
1406  TObjArray    *altroObjects=0;
1407  AliTPCCalPad *acqStart=0;
1408  AliTPCCalPad *zsThr=0;
1409  AliTPCCalPad *acqStop=0;
1410  AliTPCCalPad *FPED=0;
1411  AliTPCCalPad *masked=0;
1412  AliTPCCalPad *k1=0, *k2=0, *k3=0;
1413  AliTPCCalPad *l1=0, *l2=0, *l3=0;
1414  TMap *mapRCUconfig=0;
1415
1416  AliCDBEntry* entry = GetFromOCDB("Calib", "AltroConfig");
1417  if (entry) altroObjects = (TObjArray*)entry->GetObject();
1418  if ( altroObjects==NULL ) {
1419      Log("AliTPCPreprocsessor: No previous TPC altro calibration entry available.\n");
1420      altroObjects = new TObjArray;
1421  }
1422
1423  acqStart = (AliTPCCalPad*)altroObjects->FindObject("AcqStart");
1424  if ( !acqStart ) {
1425     acqStart = new AliTPCCalPad("AcqStart","AcqStart");
1426     altroObjects->Add(acqStart);
1427  }
1428  zsThr = (AliTPCCalPad*)altroObjects->FindObject("ZsThr");
1429  if ( !zsThr )  {
1430     zsThr = new AliTPCCalPad("ZsThr","ZsThr");
1431     altroObjects->Add(zsThr);
1432  }
1433  FPED = (AliTPCCalPad*)altroObjects->FindObject("FPED");
1434  if ( !FPED )  {
1435     FPED = new AliTPCCalPad("FPED","FPED");
1436     altroObjects->Add(FPED);
1437  }
1438  acqStop = (AliTPCCalPad*)altroObjects->FindObject("AcqStop");
1439  if ( !acqStop ) {
1440     acqStop = new AliTPCCalPad("AcqStop","AcqStop");
1441     altroObjects->Add(acqStop);
1442  }
1443  masked = (AliTPCCalPad*)altroObjects->FindObject("Masked");
1444  if ( !masked )  {
1445     masked = new AliTPCCalPad("Masked","Masked");
1446     altroObjects->Add(masked);
1447  }
1448  k1 = (AliTPCCalPad*)altroObjects->FindObject("K1");
1449  if ( !k1 )  {
1450     k1 = new AliTPCCalPad("K1","K1");
1451     altroObjects->Add(k1);
1452  }
1453  k2 = (AliTPCCalPad*)altroObjects->FindObject("K2");
1454  if ( !k2 )  {
1455     k2 = new AliTPCCalPad("K2","K2");
1456     altroObjects->Add(k2);
1457  }
1458  k3 = (AliTPCCalPad*)altroObjects->FindObject("K3");
1459  if ( !k3 )  {
1460     k3 = new AliTPCCalPad("K3","K3");
1461     altroObjects->Add(k3);
1462  }
1463  l1 = (AliTPCCalPad*)altroObjects->FindObject("L1");
1464  if ( !l1 )  {
1465     l1 = new AliTPCCalPad("L1","L1");
1466     altroObjects->Add(l1);
1467  }
1468  l2 = (AliTPCCalPad*)altroObjects->FindObject("L2");
1469  if ( !l2 )  {
1470     l2 = new AliTPCCalPad("L2","L2");
1471     altroObjects->Add(l2);
1472  }
1473  l3 = (AliTPCCalPad*)altroObjects->FindObject("L3");
1474  if ( !l3 )  {
1475     l3 = new AliTPCCalPad("L3","L3");
1476     altroObjects->Add(l3);
1477  }
1478  mapRCUconfig = (TMap*)altroObjects->FindObject("RCUconfig");
1479  if (!mapRCUconfig) {
1480     mapRCUconfig = new TMap();
1481     mapRCUconfig->SetName("RCUconfig");
1482     altroObjects->Add(mapRCUconfig);
1483  }
1484
1485
1486  UInt_t result=0;
1487  TString idFXS[2]={"AltroConfigA","AltroConfigC"};
1488
1489  Int_t nSectors = fROC->GetNSectors();
1490  Bool_t changed=false;
1491  if (altroObjects == 0 ) altroObjects = new TObjArray;
1492
1493 // extract list of active DDLs
1494
1495  if (dcsMap) {
1496   Bool_t found;
1497   TString arrDDL(kNumDDL);
1498   arrDDL.Append('x',kNumDDL);
1499   for ( Int_t iDDL = 0; iDDL<kNumDDL; iDDL++ ) {
1500     TString stringID = Form (kAmandaDDL.Data(),iDDL+kFirstDDL);
1501     TPair *pair = (TPair*)dcsMap->FindObject(stringID.Data());
1502     found = false;
1503     if ( pair ) {
1504         TObjArray *valueSet=(TObjArray*)pair->Value();
1505         if ( valueSet) {
1506           AliDCSValue *val = (AliDCSValue*)valueSet->At(0);
1507           if (val) {
1508               found = val->GetBool();
1509               if (found){
1510                arrDDL[iDDL] = '1';
1511               } else {
1512                arrDDL[iDDL] = '0';
1513               }
1514           }
1515         }
1516     }
1517   }
1518   TObjString *ddlArray = new TObjString;
1519   ddlArray->SetString(arrDDL);
1520   TMap *activeDDL = new TMap;
1521   activeDDL->SetName("DDLArray");
1522   TObjString *key = new TObjString("DDLArray");
1523   activeDDL->Add(key,ddlArray);
1524   altroObjects->Add(activeDDL);
1525   changed=true;
1526  } else {
1527    Log ("ExtractAltro: No DCS map available. Active DDL list cannot be obtained.");
1528    result = 3;
1529  }
1530
1531 // extract Altro configuration files
1532
1533  for ( Int_t id=0; id<2; id++) {
1534    TList* list = GetFileSources(sourceFXS,idFXS[id].Data());
1535
1536    if (list && list->GetEntries()>0) {
1537
1538 //  loop through all files from LDCs
1539
1540     UInt_t index = 0;
1541     while (list->At(index)!=NULL) {
1542      TObjString* fileNameEntry = (TObjString*) list->At(index);
1543      if (fileNameEntry!=NULL) {
1544         TString fileName = GetFile(sourceFXS, idFXS[id].Data(),
1545                                          fileNameEntry->GetString().Data());
1546         TFile *f = TFile::Open(fileName);
1547         if (!f) {
1548           const int mess_length=40;
1549           char message[mess_length];
1550           snprintf(message,mess_length,"Error opening Altro configuration file, id = %d",id);
1551           Log (message);
1552           result =2;
1553           break;
1554         }
1555         TObjArray *altroFXS;
1556         f->GetObject("AltroConfig",altroFXS);
1557         if ( !altroFXS ) {
1558           Log ("No Altro configuration object in file.");
1559           result = 2;
1560           break;
1561         }
1562
1563         //  replace entries for the sectors available in the present file
1564         AliTPCCalPad *acqStartFXS=(AliTPCCalPad*)altroFXS->FindObject("AcqStart");
1565         AliTPCCalPad *zsThrFXS=(AliTPCCalPad*)altroFXS->FindObject("ZsThr");
1566         AliTPCCalPad *acqStopFXS=(AliTPCCalPad*)altroFXS->FindObject("AcqStop");
1567         AliTPCCalPad *FPEDFXS=(AliTPCCalPad*)altroFXS->FindObject("FPED");
1568         AliTPCCalPad *maskedFXS=(AliTPCCalPad*)altroFXS->FindObject("Masked");
1569         AliTPCCalPad *k1FXS=(AliTPCCalPad*)altroFXS->FindObject("K1");
1570         AliTPCCalPad *k2FXS=(AliTPCCalPad*)altroFXS->FindObject("K2");
1571         AliTPCCalPad *k3FXS=(AliTPCCalPad*)altroFXS->FindObject("K3");
1572         AliTPCCalPad *l1FXS=(AliTPCCalPad*)altroFXS->FindObject("L1");
1573         AliTPCCalPad *l2FXS=(AliTPCCalPad*)altroFXS->FindObject("L2");
1574         AliTPCCalPad *l3FXS=(AliTPCCalPad*)altroFXS->FindObject("L3");
1575         TMap *mapRCUconfigFXS = (TMap*)altroFXS->FindObject("RCUconfig");
1576         TIterator *mapFXSiter = mapRCUconfigFXS->MakeIterator();
1577
1578         changed=true;
1579         for (Int_t sector=0; sector<nSectors; sector++) {
1580
1581            if (acqStartFXS) {
1582               AliTPCCalROC *rocAcqStart=acqStartFXS->GetCalROC(sector);
1583               if ( rocAcqStart )  acqStart->SetCalROC(rocAcqStart,sector);
1584            }
1585            if (zsThrFXS ) {
1586               AliTPCCalROC *rocZsThr=zsThrFXS->GetCalROC(sector);
1587               if ( rocZsThr )  zsThr->SetCalROC(rocZsThr,sector);
1588            }
1589            if (acqStopFXS) {
1590               AliTPCCalROC *rocAcqStop=acqStopFXS->GetCalROC(sector);
1591               if ( rocAcqStop )  acqStop->SetCalROC(rocAcqStop,sector);
1592            }
1593            if (FPEDFXS ) {
1594               AliTPCCalROC *rocFPED=FPEDFXS->GetCalROC(sector);
1595               if ( rocFPED )  FPED->SetCalROC(rocFPED,sector);
1596            }
1597            if (maskedFXS) {
1598               AliTPCCalROC *rocMasked=maskedFXS->GetCalROC(sector);
1599               if ( rocMasked )  masked->SetCalROC(rocMasked,sector);
1600            }
1601            if (k1FXS) {
1602               AliTPCCalROC *rocK1=k1FXS->GetCalROC(sector);
1603               if ( rocK1 )  k1->SetCalROC(rocK1,sector);
1604            }
1605            if (k2FXS) {
1606               AliTPCCalROC *rocK2=k2FXS->GetCalROC(sector);
1607               if ( rocK2 )  k2->SetCalROC(rocK2,sector);
1608            }
1609            if (k3FXS) {
1610               AliTPCCalROC *rocK3=k3FXS->GetCalROC(sector);
1611               if ( rocK3 )  k3->SetCalROC(rocK3,sector);
1612            }
1613            if (l1FXS) {
1614               AliTPCCalROC *rocL1=l1FXS->GetCalROC(sector);
1615               if ( rocL1 )  l1->SetCalROC(rocL1,sector);
1616            }
1617            if (l2FXS) {
1618               AliTPCCalROC *rocL2=l2FXS->GetCalROC(sector);
1619               if ( rocL2 )  l2->SetCalROC(rocL2,sector);
1620            }
1621            if (l3FXS) {
1622               AliTPCCalROC *rocL3=l3FXS->GetCalROC(sector);
1623               if ( rocL3 )  l3->SetCalROC(rocL3,sector);
1624            }
1625          }
1626          if (mapRCUconfigFXS) {
1627           Int_t mapEntries = mapRCUconfigFXS->GetEntries();
1628           TObjString* keyFXS;
1629           TVectorF* vecFXS;
1630           TVectorF* vec;              // nSectors = 72  (total number of inner/outer sectors)
1631           for (Int_t i=0; i<mapEntries; ++i) {
1632             keyFXS=(TObjString*)mapFXSiter->Next();
1633             vecFXS=(TVectorF*)mapRCUconfigFXS->GetValue(keyFXS);
1634             vec=(TVectorF*)mapRCUconfig->GetValue(keyFXS);
1635             if (!vec) {
1636               vec = new TVectorF(3*nSectors);
1637               *vec = -1;
1638               mapRCUconfig->Add(keyFXS,vec);
1639             }
1640             if (vec->GetNoElements() != 3*nSectors ) {
1641               vec->ResizeTo(3*nSectors);
1642             }
1643             if (id==0) {                        // A side
1644               vec->SetSub(0,vecFXS->GetSub(0,nSectors/2-1));
1645               vec->SetSub(nSectors,vecFXS->GetSub(nSectors,2*nSectors-1));
1646             } else {                             // C side
1647               vec->SetSub(nSectors/2,vecFXS->GetSub(nSectors/2,nSectors-1));
1648               vec->SetSub(2*nSectors,vecFXS->GetSub(2*nSectors,3*nSectors-1));
1649             }
1650           }
1651         }
1652        delete altroFXS;
1653        f->Close();
1654       }
1655      ++index;
1656      }  // while(list)
1657     } else {
1658       Log ("Error: no entries in AltroConfig file list!");
1659       result = 1;
1660     }
1661
1662    }   // for - id
1663 //
1664 //  Store updated pedestal entry to OCDB
1665 //
1666     if (changed) {
1667      AliCDBMetaData metaData;
1668      metaData.SetBeamPeriod(0);
1669      metaData.SetResponsible("Haavard Helstrup");
1670      metaData.SetAliRootVersion(ALIROOT_BRANCH);
1671      metaData.SetComment("Preprocessor AliTPC data base entries.");
1672
1673      Bool_t storeOK = Store("Calib", "AltroConfig", altroObjects, &metaData, 0, kFALSE);
1674      if ( !storeOK ) ++result;
1675     }
1676
1677   altroObjects->Delete();
1678   delete altroObjects;
1679
1680   return result;
1681 }