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