]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDPreprocessor.cxx
Modifications by Thomas
[u/mrichter/AliRoot.git] / TRD / AliTRDPreprocessor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ////////////////////////////////////////////////////////////////////////////
19 //                                                                        //
20 // This class is a first implementation for the TRD.                      //
21 // It takes data from HLT and computes the parameters                     //
22 // and stores both reference data and online calibration                  //
23 // parameters in the CDB                                                  //
24 // It alsotakes DCS data, does spline fits                                //
25 // and stores both reference data and spline fits results                 //
26 // in the CDB                                                             //
27 //                                                                        //
28 // Authors:                                                               //
29 //   R. Bailhache (R.Bailhache@gsi.de)                                    //
30 //   W. Monange   (w.monange@gsi.de)                                      //
31 //   F. Kramer    (kramer@ikf.uni-frankfurt.de)                           //
32 //                                                                        //
33 ////////////////////////////////////////////////////////////////////////////
34
35 #include <fstream>
36
37 #include <TFile.h>
38 #include <TProfile2D.h>
39 #include <TObjString.h>
40 #include <TString.h>
41 #include <TList.h>
42 #include <TSAXParser.h>
43
44 #include "AliCDBMetaData.h"
45 #include "AliLog.h"
46
47 #include "AliTRDPreprocessor.h"
48 #include "AliTRDSensorArray.h"
49 #include "AliTRDCalibraFit.h"
50 #include "AliTRDCalibraMode.h"
51 #include "AliTRDCalibPadStatus.h"
52 #include "AliTRDSaxHandler.h"
53 #include "AliTRDgeometry.h"
54 #include "Cal/AliTRDCalPad.h"
55 #include "Cal/AliTRDCalPadStatus.h"
56 #include "Cal/AliTRDCalDCS.h"
57 #include "Cal/AliTRDCalSingleChamberStatus.h"
58 #include "Cal/AliTRDCalROC.h"
59
60 ClassImp(AliTRDPreprocessor)
61
62 //______________________________________________________________________________________________
63 AliTRDPreprocessor::AliTRDPreprocessor(AliShuttleInterface *shuttle)
64   :AliPreprocessor("TRD", shuttle)
65   ,fVdriftHLT(0)
66 {
67   //
68   // Constructor
69   //
70
71   AddRunType("PHYSICS");
72   AddRunType("STANDALONE");
73   AddRunType("PEDESTAL");
74   AddRunType("DAQ");
75   
76 }
77
78 //______________________________________________________________________________________________
79 AliTRDPreprocessor::~AliTRDPreprocessor()
80 {
81   //
82   // Destructor
83   //
84
85 }
86
87 //______________________________________________________________________________________________
88 void AliTRDPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
89 {
90   //
91   // Initialization routine for the TRD preprocessor
92   //
93
94   AliPreprocessor::Initialize(run,startTime,endTime);
95
96 }
97
98 //______________________________________________________________________________________________
99 UInt_t AliTRDPreprocessor::Process(TMap* dcsAliasMap)
100 {
101   //
102   // Process DCS and calibration part for HLT
103   //
104
105   TString runType = GetRunType();
106   Log(Form("runtype %s\n",runType.Data()));
107   
108   // always process the configuration data
109   Int_t DCSConfigReturn = ProcessDCSConfigData();
110   if(DCSConfigReturn) return DCSConfigReturn; 
111   
112   if (runType=="PEDESTAL"){
113     if(ExtractPedestals()) return 1;
114     return 0;
115   } 
116
117   if ((runType=="PHYSICS") || (runType=="STANDALONE") || (runType=="DAQ")){
118     // DCS
119     if(ProcessDCS(dcsAliasMap)) return 1; 
120     if(runType=="PHYSICS"){
121       // HLT if On
122       //TString runPar = GetRunParameter("HLTStatus");
123       //if(runPar=="1") {
124       if(GetHLTStatus()) {
125         if(ExtractHLT()) return 1; // for testing!
126       } 
127       // DAQ if HLT failed
128       if(!fVdriftHLT) {
129         if(ExtractDriftVelocityDAQ()) return 1; // for testing!
130       }
131     }
132   }
133   
134   return 0;  
135   
136 }
137 //______________________________________________________________________________
138 Bool_t AliTRDPreprocessor::ProcessDCS()
139 {
140   //
141   // Default process DCS method
142   //
143
144   TString runType = GetRunType();
145   if ((runType == "PHYSICS") || (runType == "STANDALONE")) {
146     return kTRUE;
147   }
148   return kFALSE;
149
150 }
151
152 //______________________________________________________________________________
153 Bool_t AliTRDPreprocessor::ProcessDCS(TMap *dcsAliasMap)
154 {
155   //
156   // Process DCS method
157   //
158
159   Bool_t error = kFALSE;
160
161   AliCDBMetaData metaData;
162   metaData.SetBeamPeriod(0);
163   metaData.SetResponsible("Wilfried Monange/Raphaelle Bailhache");
164   metaData.SetComment("TRD calib test");
165
166   Log("****** DCS ******\n");
167         
168   TObjArray * list=AliTRDSensorArray::GetList ();
169         
170   if (list == 0x0) {
171     Log ("Error during AliTRDSensorArray::GetList");
172     Log ("DCS will not be processing");
173     return kTRUE;
174   }
175
176   Int_t nEntries = list->GetEntries ();
177   Log (Form ("%d alias loaded", nEntries));
178                 
179   Bool_t * results=new Bool_t [nEntries];
180   Int_t  * nGraph=new Int_t [nEntries];
181                 
182   for (Int_t iAlias = 0; iAlias < nEntries; iAlias++) {
183                         
184     AliTRDSensorArray * oneTRDDCS = (AliTRDSensorArray *)list->At (iAlias);
185                         
186     oneTRDDCS->SetStartTime (TTimeStamp (fStartTime));
187     oneTRDDCS->SetEndTime (TTimeStamp (fEndTime));
188                         
189     Log(Form("Processing DCS : \"%s\"", oneTRDDCS->GetStoreName ().Data ()));
190                         
191     TMap * map;
192
193     map=oneTRDDCS->ExtractDCS (dcsAliasMap);
194                 
195     nGraph [iAlias] = map->GetEntries ();
196                 
197     if (nGraph [iAlias] == 0) {
198       Log("No TGraph for this dcsDatapointAlias : not stored");
199       results [iAlias] = kFALSE;
200       error  = kTRUE;
201       continue;
202     }
203                 
204     oneTRDDCS->SetGraph(map);
205     results[iAlias]=Store("Calib", oneTRDDCS->GetStoreName().Data(), oneTRDDCS, &metaData, 0, kTRUE); 
206     delete map;         
207
208     //results [iAlias] = StoreReferenceData("Calib", oneTRDDCS->GetStoreName ().Data (), oneTRDDCS, &metaData); 
209
210     if (!results[iAlias]) {
211       AliError("Problem during StoreRef DCS");
212       error=kTRUE;
213     }
214
215     //BEGIN TEST (should not be removed ...)
216     /*
217     oneTRDDCS->ClearGraph();
218     oneTRDDCS->ClearFit();
219     oneTRDDCS->SetDiffCut2 (0.1);
220     map=oneTRDDCS->ExtractDCS (dcsAliasMap);
221     oneTRDDCS->SetGraph (map);
222     Store("Calib", ("cut_"+oneTRDDCS->GetStoreName()).Data(), oneTRDDCS, &metaData, 0, kTRUE); 
223     delete map;
224
225
226     if(iAlias==1 || iAlias==19) continue;
227     
228     oneTRDDCS->ClearGraph();
229     oneTRDDCS->ClearFit();
230     oneTRDDCS->SetDiffCut2(0);
231     map=oneTRDDCS->ExtractDCS(dcsAliasMap);
232     oneTRDDCS->MakeSplineFit(map);
233     Store("Calib", ("fit_"+oneTRDDCS->GetStoreName()).Data() , oneTRDDCS, &metaData, 0, kTRUE); 
234     delete map;
235
236      
237     oneTRDDCS->ClearGraph(); 
238     oneTRDDCS->ClearFit();
239     oneTRDDCS->SetDiffCut2 (0.1);
240     map=oneTRDDCS->ExtractDCS (dcsAliasMap);
241     oneTRDDCS->MakeSplineFit(map);
242     Store("Calib", ("cutfit_"+oneTRDDCS->GetStoreName()).Data() , oneTRDDCS, &metaData, 0, kTRUE); 
243     delete map;
244     */    
245     //END TEST
246
247   }
248                 
249   Log ("         Summury of DCS :\n");
250   Log (Form("%30s %10s %10s", "dcsDatapointAlias", "Stored ?", "# graph"));
251   for (Int_t iAlias = 0; iAlias < nEntries; iAlias++) {
252     AliTRDSensorArray * oneTRDDCS = (AliTRDSensorArray *)list->At (iAlias);
253     Log (Form ("%30s %10s %4d", 
254                oneTRDDCS->GetStoreName ().Data (),
255                results[iAlias] ? "ok" : "X",
256                nGraph [iAlias]));
257   }
258   Log ("*********** End of DCS **********");
259   
260   delete results;
261   delete nGraph;
262
263   return error;
264
265 }
266
267 //______________________________________________________________________________________________
268 Bool_t AliTRDPreprocessor::ExtractPedestals()
269 {
270   //
271   // Pedestal running on LDCs at the DAQ
272   //
273
274   //
275   // The reference data are stored in:
276   // PadStatus1 for sm-00-01-02-09-10-11
277   // PadStatus2 for sm-03-04-05-12-13-14
278   // PadStatus3 for sm-06-07-08-15-16-17
279   // PadStatus0 if nothing found..means problems
280   //
281
282   Bool_t error = kFALSE;
283
284   // Init a AliTRDCalibPadStatus
285   AliTRDCalibPadStatus calPedSum = AliTRDCalibPadStatus();
286
287   AliCDBMetaData metaData;
288   metaData.SetBeamPeriod(0);
289   metaData.SetResponsible("Raphaelle Bailhache");
290   metaData.SetComment("TRD calib test");
291   
292   // Sum the contributions of the LDCs
293   TList * listpad = GetFileSources(kDAQ,"PADSTATUS");
294   if (!listpad) {
295     Log("No list found for the PEDESTRAL Run");
296     return kTRUE;
297   }
298   
299   // loop through all files from LDCs  
300   UInt_t index = 0;
301   while (listpad->At(index)!=NULL) {
302     TObjString* fileNameEntry = (TObjString*) listpad->At(index);
303     if (fileNameEntry != NULL)
304       {
305         TString fileName = GetFile(kDAQ, "PADSTATUS",
306                                    fileNameEntry->GetString().Data());
307         if(fileName.Length() ==0){
308           Log(Form("Error by retrieving the file %d for the pedestal",(Int_t)index));
309           delete listpad;
310           return kTRUE;
311         }
312         
313         TFile *f = TFile::Open(fileName);
314         AliTRDCalibPadStatus *calPed;
315         f->GetObject("calibpadstatus",calPed);
316         
317         if(calPed){
318           
319           Int_t ldc = 0; 
320
321           // analyse
322           //calPed->AnalyseHisto();
323                   
324           // Add to the calPedSum
325           for (Int_t idet=0; idet<540; idet++) {
326             AliTRDCalROC *rocMean  = calPed->GetCalRocMean(idet, kFALSE);
327             if ( rocMean )  {
328               calPedSum.SetCalRocMean(rocMean,idet);
329               ldc = (Int_t) (idet / 30);
330             }
331             AliTRDCalROC *rocRMS = calPed->GetCalRocRMS(idet, kFALSE);
332             if ( rocRMS )  {
333               calPedSum.SetCalRocRMS(rocRMS,idet);
334             }
335             AliTRDCalROC *rocMeand  = calPed->GetCalRocMeand(idet, kFALSE);
336             if ( rocMeand )  {
337               calPedSum.SetCalRocMeand(rocMeand,idet);
338             }
339             AliTRDCalROC *rocRMSd = calPed->GetCalRocRMSd(idet, kFALSE);
340             if ( rocRMSd )  {
341               calPedSum.SetCalRocRMSd(rocRMSd,idet);
342             }
343           }// det loop
344
345           if((ldc==0) || (ldc==1) || (ldc==2)) ldc = 1;
346           if((ldc==3) || (ldc==4) || (ldc==5)) ldc = 2;
347           if((ldc==6) || (ldc==7) || (ldc==8)) ldc = 3;
348           if((ldc==9) || (ldc==10) || (ldc==11)) ldc = 4;
349           if((ldc==12) || (ldc==13) || (ldc==14)) ldc = 5;
350           if((ldc==15) || (ldc==16) || (ldc==17)) ldc = 6;
351         
352           // store as reference data
353           TString name("PadStatus");
354           name += ldc;
355           if(!StoreReferenceData("DAQData",(const char *)name,(TObject *) calPed,&metaData)){
356             Log(Form("Error storing AliTRDCalibPadStatus object %d as reference data",(Int_t)index));
357             error = kTRUE;
358           }
359
360         } // calPed
361       } // fileNameEntry
362     ++index;
363   }// while (list)
364
365   Log(Form("%d elements found in the list for the pedestal",(Int_t)index));
366   if(index==0){
367     delete listpad;
368     return kTRUE;
369   }
370
371   //
372   // Create pedestal 
373   //
374     
375   // Create Pad Status
376   AliTRDCalPadStatus *calPadStatus = calPedSum.CreateCalPadStatus();
377   // Create Noise 
378   //Make the AliTRDCalPad
379   AliTRDCalPad *calPad2 = calPedSum.CreateCalPad();
380   //Make the AliTRDCalDet correspondant
381   AliTRDCalDet *calDet = calPedSum.CreateCalDet();
382  
383   //
384   // Take the noise and Pad status from the previous OCDB
385   //
386
387   AliTRDCalPad *calPadPrevious=0;
388   AliCDBEntry* entry = GetFromOCDB("Calib", "PadNoise");
389   if (entry) calPadPrevious = (AliTRDCalPad*)entry->GetObject();
390   if ( calPadPrevious==NULL ) {
391      Log("AliTRDPreprocsessor: No previous TRD pad noise entry available.\n");
392      calPadPrevious = new AliTRDCalPad("PadNoise", "PadNoise");
393   }
394
395   AliTRDCalPadStatus *calPadStatusPrevious=0;
396   entry = GetFromOCDB("Calib", "PadStatus");
397   if (entry) calPadStatusPrevious = (AliTRDCalPadStatus*)entry->GetObject();
398   if ( calPadStatusPrevious==NULL ) {
399     Log("AliTRDPreprocsessor: No previous TRD pad status entry available.\n");
400     calPadStatusPrevious = new AliTRDCalPadStatus("padstatus", "padstatus");
401     for (Int_t idet=0; idet<540; ++idet)
402       {
403         AliTRDCalSingleChamberStatus *calROC = calPadStatusPrevious->GetCalROC(idet);
404         for(Int_t k = 0; k < calROC->GetNchannels(); k++){
405           calROC->SetStatus(k,AliTRDCalPadStatus::kMasked);
406         }
407       }
408   }
409
410   
411   // Loop over detectors for check
412   for (Int_t det=0; det<AliTRDgeometry::kNdet; ++det)  {
413     
414     // noise
415     AliTRDCalROC *calROCPreviousNoise = calPadPrevious->GetCalROC(det);
416     AliTRDCalROC *calROCNoise         = calPad2->GetCalROC(det);
417
418     // padstatus
419     AliTRDCalSingleChamberStatus *calROCPreviousStatus = calPadStatusPrevious->GetCalROC(det);
420     AliTRDCalSingleChamberStatus *calROCStatus         = calPadStatus->GetCalROC(det);
421     
422     
423     // loop over first half and second half chamber
424     for(Int_t half = 0; half < 2; half++){
425
426       Bool_t data         = AreThereDataPedestal(calROCStatus,(Bool_t)half);
427       //printf("There are data for the detector %d the half %d: %d\n",det,half,data);
428       if(!data){
429         // look if data in the OCDB
430         Bool_t dataPrevious = AreThereDataPedestal(calROCPreviousStatus,(Bool_t)half);
431         // if no data at all, set to default value
432         if(!dataPrevious){
433           SetDefaultStatus(*calROCStatus,(Bool_t)half);
434           SetDefaultNoise(*calROCNoise,(Bool_t)half);
435         }
436         else{
437           // if data, set to previous value
438           SetStatus(*calROCStatus,calROCPreviousStatus,(Bool_t)half);
439           SetNoise(*calROCNoise,calROCPreviousNoise,(Bool_t)half);
440         }
441       }
442     }
443   }
444   
445   //
446   // Store  
447   //  
448
449   AliCDBMetaData md3; 
450   md3.SetObjectClassName("AliTRDCalPadStatus");
451   md3.SetResponsible("Raphaelle Bailhache");
452   md3.SetBeamPeriod(1);
453   md3.SetComment("TRD calib test");
454   if(!Store("Calib","PadStatus"    ,(TObject *)calPadStatus, &md3, 0, kTRUE)){
455     Log("Error storing the pedestal");
456     delete listpad;
457     return kTRUE;
458   }
459
460   AliCDBMetaData md4; 
461   md4.SetObjectClassName("AliTRDCalPad");
462   md4.SetResponsible("Raphaelle Bailhache");
463   md4.SetBeamPeriod(1);
464   md4.SetComment("TRD calib test");
465   if(!Store("Calib","PadNoise"    ,(TObject *)calPad2, &md4, 0, kTRUE)){
466     Log("Error storing the pedestal");
467     delete listpad;
468     return kTRUE;
469   }
470   
471   AliCDBMetaData md5; 
472   md5.SetObjectClassName("AliTRDCalDet");
473   md5.SetResponsible("Raphaelle Bailhache");
474   md5.SetBeamPeriod(1);
475   md5.SetComment("TRD calib test");
476   if(!Store("Calib","DetNoise"    ,(TObject *)calDet, &md5, 0, kTRUE)){
477     Log("Error storing the pedestal");
478     delete listpad;
479     return kTRUE;
480   }  
481
482   delete listpad;
483   return error; 
484   
485 }
486
487 //__________________________________________________________________
488 Bool_t AliTRDPreprocessor::AreThereDataPedestal(AliTRDCalSingleChamberStatus * const calROCStatus
489                                               , Bool_t second)
490 {
491
492   //
493   // Data for this half chamber
494   //
495
496   Bool_t data         = kFALSE;
497   Int_t nCols         = calROCStatus->GetNcols();
498   Int_t nCol0         = 0;
499   Int_t nColE         = (Int_t) nCols/2 - 2;
500   if(second) {
501     nCol0 = nColE + 4;
502     nColE = nCols;
503   }
504
505   Int_t totalnumberofpads = 0;
506   Int_t totalnumberofdata = 0; 
507
508   for(Int_t col = nCol0; col < nColE; col++){
509     for(Int_t row = 0; row < calROCStatus->GetNrows(); row++){
510       totalnumberofpads++;
511       //printf("ismasked %d\n",(Int_t)calROCStatus->IsMasked(col,row));
512       if(!calROCStatus->GetStatus(col,row)) {
513         data = kTRUE;
514         totalnumberofdata++;
515       }
516     }
517   }
518   if(totalnumberofdata < (Int_t)(totalnumberofpads/2)) data = kFALSE;
519
520   return data;
521   
522 }
523 //__________________________________________________________________
524 void AliTRDPreprocessor::SetDefaultStatus(AliTRDCalSingleChamberStatus &calROCStatus, Bool_t second){
525
526   //
527   // default status for this half chamber
528   //
529
530   Int_t nCols         = calROCStatus.GetNcols();
531   Int_t nCol0         = 0;
532   Int_t nColE         = (Int_t) nCols/2;
533   if(second) {
534     nCol0 = nColE;
535     nColE = nCols;
536   }
537   for(Int_t col = nCol0; col < nColE; col++){
538     for(Int_t row = 0; row < calROCStatus.GetNrows(); row++){
539       calROCStatus.SetStatus(col,row,0);
540     }
541   }
542 }
543 //__________________________________________________________________
544 void AliTRDPreprocessor::SetStatus(AliTRDCalSingleChamberStatus &calROCStatus, AliTRDCalSingleChamberStatus *calROCStatusPrevious,Bool_t second){
545
546   //
547   // previous status for this half chamber
548   //
549
550   Int_t nCols         = calROCStatus.GetNcols();
551   Int_t nCol0         = 0;
552   Int_t nColE         = (Int_t) nCols/2;
553   if(second) {
554     nCol0 = nColE;
555     nColE = nCols;
556   }
557   for(Int_t col = nCol0; col < nColE; col++){
558     for(Int_t row = 0; row < calROCStatus.GetNrows(); row++){
559       calROCStatus.SetStatus(col,row,calROCStatusPrevious->GetStatus(col,row));
560     }
561   }
562 }
563 //__________________________________________________________________
564 void AliTRDPreprocessor::SetDefaultNoise(AliTRDCalROC &calROCNoise, Bool_t second){
565
566   //
567   // default noise for this half chamber
568   //
569
570   Int_t nCols         = calROCNoise.GetNcols();
571   Int_t nCol0         = 0;
572   Int_t nColE         = (Int_t) nCols/2;
573   if(second) {
574     nCol0 = nColE;
575     nColE = nCols;
576   }
577   for(Int_t col = nCol0; col < nColE; col++){
578     for(Int_t row = 0; row < calROCNoise.GetNrows(); row++){
579       calROCNoise.SetValue(col,row,0.12);
580     }
581   }
582 }
583 //__________________________________________________________________
584 void AliTRDPreprocessor::SetNoise(AliTRDCalROC &calROCNoise, AliTRDCalROC *calROCNoisePrevious, Bool_t second){
585
586   //
587   // previous noise for this half chamber
588   //
589
590   Int_t nCols         = calROCNoise.GetNcols();
591   Int_t nCol0         = 0;
592   Int_t nColE         = (Int_t) nCols/2;
593   if(second) {
594     nCol0 = nColE;
595     nColE = nCols;
596   }
597   for(Int_t col = nCol0; col < nColE; col++){
598     for(Int_t row = 0; row < calROCNoise.GetNrows(); row++){
599       calROCNoise.SetValue(col,row,calROCNoisePrevious->GetValue(col,row));
600     }
601   }
602 }
603 //______________________________________________________________________________________________
604 Bool_t AliTRDPreprocessor::ExtractDriftVelocityDAQ()
605 {
606   //
607   // Drift velocity DA running on monitoring servers at the DAQ
608   //
609
610   Bool_t error = kFALSE; 
611
612   // Objects for HLT and DAQ zusammen
613   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
614   AliCDBMetaData metaData;
615   metaData.SetBeamPeriod(0);
616   metaData.SetResponsible("Raphaelle Bailhache");
617   metaData.SetComment("TRD calib test");
618   // Store the infos for the detector
619   AliCDBMetaData md1; 
620   md1.SetObjectClassName("AliTRDCalDet");
621   md1.SetResponsible("Raphaelle Bailhache");
622   md1.SetBeamPeriod(0);
623   md1.SetComment("TRD calib test");
624   // Store the infos for the pads
625   AliCDBMetaData md2; 
626   md2.SetObjectClassName("AliTRDCalPad");
627   md2.SetResponsible("Raphaelle Bailhache");
628   md2.SetBeamPeriod(0);
629   md2.SetComment("TRD calib test");
630
631   // Take the file from the DAQ file exchange server
632   TList *listdaq = GetFileSources(kDAQ,"VDRIFT");
633   if (!listdaq) {
634     Log("No list found for vdrift (DAQ)");
635     return kTRUE;
636   }
637   
638   if(listdaq->GetSize() !=1){
639     Log(Form("Problem on the size of the list: %d (DAQ)",listdaq->GetSize()));
640     delete listdaq;
641     return kTRUE;
642   }
643   
644   TObjString* fileNameEntry = (TObjString*) listdaq->At(0);
645   if(fileNameEntry != NULL){
646     TString fileName = GetFile(kDAQ, "VDRIFT",
647                                fileNameEntry->GetString().Data());
648     if(fileName.Length() ==0){
649       Log("Error retrieving the file vdrift (DAQ)");
650       delete listdaq;
651       return kTRUE;
652     }
653     TFile *filedaq = TFile::Open(fileName);
654     TProfile2D *histodriftvelocity = (TProfile2D *) filedaq->Get("PH2d");
655     if (histodriftvelocity) {
656       
657       // store as reference data
658       if(!StoreReferenceData("DAQData","VdriftT0",(TObject *) histodriftvelocity,&metaData)){
659         Log("Error storing 2D Profile for vdrift from the DAQ");
660         error = kTRUE;
661       }
662       
663       // analyse
664       
665       Log("Take the PH reference data. Now we will try to fit\n");
666       calibra->SetMinEntries(2000); // If there is less than 2000
667       calibra->AnalysePH(histodriftvelocity);
668       
669       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
670         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
671       Int_t nbfit        = calibra->GetNumberFit();
672       Int_t nbE        = calibra->GetNumberEnt();
673       
674       // if enough statistics store the results
675       if ((nbtg >                  0) && 
676           (nbfit        >= 0.5*nbE)) {
677         // create the cal objects
678         calibra->PutMeanValueOtherVectorFit(1,kTRUE);
679         calibra->PutMeanValueOtherVectorFit2(1,kTRUE);
680         TObjArray object      = calibra->GetVectorFit();
681         AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
682         TObject *objdriftvelocitypad = calibra->CreatePadObjectVdrift();
683         object              = calibra->GetVectorFit2();
684         AliTRDCalDet *objtime0det         = calibra->CreateDetObjectT0(&object,kTRUE);
685         TObject *objtime0pad         = calibra->CreatePadObjectT0();
686         calibra->ResetVectorFit();
687         // store
688         if(!Store("Calib","ChamberVdrift"    ,(TObject *) objdriftvelocitydet,&md1,0,kTRUE)){
689           Log("Error storing the calibration object for the chamber vdrift (DAQ)");
690           error = kTRUE;
691         }
692         if(!Store("Calib","ChamberT0"        ,(TObject *) objtime0det        ,&md1,0,kTRUE)){
693           Log("Error storing the calibration object for the chamber t0 (DAQ)");
694           error = kTRUE;
695         }
696         if(!Store("Calib","LocalVdrift"      ,(TObject *) objdriftvelocitypad,&md2,0,kTRUE)){
697           Log("Error storing the calibration object for the local drift velocity (DAQ)");
698           error = kTRUE;
699         }
700         if(!Store("Calib","LocalT0"          ,(TObject *) objtime0pad        ,&md2,0,kTRUE)){
701           Log("Error storing the calibration object for the local time0 (DAQ)");
702           error = kTRUE;
703         }
704       }
705       else{
706         Log("Not enough statistics for the average pulse height (DAQ)");
707       }
708     } // histo here
709   }// if fileNameEntry
710   
711   delete listdaq; 
712   return error; 
713   
714 }
715
716 //______________________________________________________________________________________________
717 Bool_t AliTRDPreprocessor::ExtractHLT()
718 {
719   //
720   // Gain, vdrift and PRF calibration running on HLT
721   // return kTRUE if NULL pointer to the list
722   //
723
724   Bool_t error = kFALSE;
725
726   // Objects for HLT and DAQ zusammen
727   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
728   AliCDBMetaData metaData;
729   metaData.SetBeamPeriod(0);
730   metaData.SetResponsible("Raphaelle Bailhache");
731   metaData.SetComment("TRD calib test");
732   // Store the infos for the detector
733   AliCDBMetaData md1; 
734   md1.SetObjectClassName("AliTRDCalDet");
735   md1.SetResponsible("Raphaelle Bailhache");
736   md1.SetBeamPeriod(0);
737   md1.SetComment("TRD calib test");
738   // Store the infos for the pads
739   AliCDBMetaData md2; 
740   md2.SetObjectClassName("AliTRDCalPad");
741   md2.SetResponsible("Raphaelle Bailhache");
742   md2.SetBeamPeriod(0);
743   md2.SetComment("TRD calib test");
744   
745   // Take the file from the HLT file exchange server
746   TList *listhlt = GetFileSources(kHLT,"GAINDRIFTPRF");
747   if (!listhlt) {
748     Log("No list found for the HLT");
749     return kTRUE;
750   }
751
752   if(listhlt->GetSize() != 1) {
753     Log(Form("Problem on the size of the list: %d (HLT)",listhlt->GetSize()));
754     delete listhlt;
755     return kTRUE;
756   }
757   
758   TObjString* fileNameEntry = (TObjString*) listhlt->At(0);
759   if(fileNameEntry != NULL){
760     TString fileName = GetFile(kHLT, "GAINDRIFTPRF",
761                                fileNameEntry->GetString().Data());
762     if(fileName.Length() ==0){
763       Log("Error retrieving the file (HLT)");
764       delete listhlt;
765       return kTRUE;
766     }
767     // Take the file
768     TFile *filehlt = TFile::Open(fileName);
769     
770     // gain
771     TH2I *histogain = (TH2I *) filehlt->Get("CH2d");
772     histogain->SetDirectory(0);
773     if (histogain) {
774       // store the reference data
775       if(!StoreReferenceData("HLTData","Gain",(TObject *) histogain,&metaData)){
776         Log("Error storing 2D histos for gain");
777         error = kTRUE;
778       }
779       // analyse
780       Log("Take the CH reference data. Now we will try to fit\n");
781       calibra->SetMinEntries(800); // If there is less than 1000 entries in the histo: no fit
782       calibra->AnalyseCH(histogain);
783       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(0))
784         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(0));
785       Int_t nbfit       = calibra->GetNumberFit();
786       Int_t nbE         = calibra->GetNumberEnt();
787       // enough statistics
788       if ((nbtg >                  0) && 
789           (nbfit        >= 0.5*nbE)) {
790         // create the cal objects
791         calibra->PutMeanValueOtherVectorFit(1,kTRUE);
792         TObjArray object           = calibra->GetVectorFit();
793         AliTRDCalDet *objgaindet   = calibra->CreateDetObjectGain(&object);
794         TObject *objgainpad        = calibra->CreatePadObjectGain();
795         // store them
796         if(!Store("Calib","ChamberGainFactor",(TObject *) objgaindet         ,&md1,0,kTRUE)){
797           Log("Error storing the calibration object for the chamber gain");
798           error = kTRUE;
799         }
800         if(!Store("Calib","LocalGainFactor"  ,(TObject *) objgainpad         ,&md2,0,kTRUE)){
801           Log("Error storing the calibration object for the local gain factor");
802           error = kTRUE;
803         }
804       }
805       calibra->ResetVectorFit();
806     }// if histogain
807     
808     // vdrift
809     fVdriftHLT = kFALSE;
810     TProfile2D *histodriftvelocity = (TProfile2D *) filehlt->Get("PH2d");
811     histodriftvelocity->SetDirectory(0);
812     if (histodriftvelocity) {
813       // store the reference data
814       if(!StoreReferenceData("HLTData","VdriftT0",(TObject *) histodriftvelocity,&metaData)){
815         Log("Error storing 2D Profile for average pulse height (HLT)");
816         error = kTRUE;
817       }
818       // analyse
819       Log("Take the PH reference data. Now we will try to fit\n");
820       calibra->SetMinEntries(800*20); // If there is less than 20000
821       calibra->AnalysePH(histodriftvelocity);
822       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
823         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
824       Int_t nbfit        = calibra->GetNumberFit();
825       Int_t nbE          = calibra->GetNumberEnt();
826       // enough statistics
827       if ((nbtg >                  0) && 
828           (nbfit        >= 0.5*nbE)) {
829         // create the cal objects
830         calibra->PutMeanValueOtherVectorFit(1,kTRUE);
831         calibra->PutMeanValueOtherVectorFit2(1,kTRUE);
832         TObjArray object  = calibra->GetVectorFit();
833         AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
834         TObject *objdriftvelocitypad      = calibra->CreatePadObjectVdrift();
835         object              = calibra->GetVectorFit2();
836         AliTRDCalDet *objtime0det  = calibra->CreateDetObjectT0(&object,kTRUE);
837         TObject *objtime0pad       = calibra->CreatePadObjectT0();
838         // store them
839         if(!Store("Calib","ChamberVdrift"    ,(TObject *) objdriftvelocitydet,&md1,0,kTRUE)){
840           Log("Error storing the calibration object for the chamber vdrift (HLT)");
841           error = kTRUE;                
842         }
843         if(!Store("Calib","ChamberT0"        ,(TObject *) objtime0det        ,&md1,0,kTRUE)){
844           Log("Error storing the calibration object for the chamber t0 (HLT)");
845           error = kTRUE;
846         }
847         if(!Store("Calib","LocalVdrift"      ,(TObject *) objdriftvelocitypad,&md2,0,kTRUE)){
848           Log("Error storing the calibration object for the local drift velocity (HLT)");
849           error = kTRUE;
850         }
851         if(!Store("Calib","LocalT0"          ,(TObject *) objtime0pad        ,&md2,0,kTRUE)){
852           Log("Error storing the calibration object for the local time0 (HLT)");
853           error = kTRUE;
854         }
855         fVdriftHLT = kTRUE;
856       }
857       calibra->ResetVectorFit();
858     }// if TProfile2D
859     
860     // prf
861     TProfile2D *histoprf = (TProfile2D *) filehlt->Get("PRF2d");
862     histoprf->SetDirectory(0);
863     if (histoprf) {
864       // store reference data
865       if(!StoreReferenceData("HLTData","PRF",(TObject *) histoprf,&metaData)){
866         Log("Error storing the 2D Profile for Pad Response Function");
867         error = kTRUE;
868       }
869       // analyse
870       Log("Take the PRF reference data. Now we will try to fit\n");
871       calibra->SetMinEntries(600); // If there is less than 20000
872       calibra->AnalysePRFMarianFit(histoprf);
873       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
874         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
875       Int_t nbfit        = calibra->GetNumberFit();
876       Int_t nbE          = calibra->GetNumberEnt();
877       // enough statistics
878       if ((nbtg >                  0) && 
879           (nbfit        >= 0.95*nbE)) {
880         // create cal pad objects 
881         TObjArray object            = calibra->GetVectorFit();
882         TObject *objPRFpad          = calibra->CreatePadObjectPRF(&object);
883         // store them
884         if(!Store("Calib","PRFWidth"         ,(TObject *) objPRFpad          ,&md2,0,kTRUE)){
885           Log("Error storing the calibration object for the Pad Response Function");
886           error = kTRUE;
887         }
888       }
889       calibra->ResetVectorFit();
890     }// if PRF
891   }// if fileNameEntry
892   
893   delete listhlt;
894   return error;
895   
896 }
897
898 //_____________________________________________________________________________
899 UInt_t AliTRDPreprocessor::ProcessDCSConfigData()
900 {
901   // 
902   // process the configuration of FEE, PTR and GTU
903   // reteive XML filei(s) from the DCS FXS
904   // parse it/them and store TObjArrays in the CDB
905   //
906   // return 0 for success, otherwise:
907   //  5 : Could not get the SOR and EOR file from the FXS
908   //  8 : Something wrong with the SOR file
909   //  9 : Something wrong with the EOR file
910   // 10 : SOR XML is not well-formed
911   // 11 : EOR XML is not well-formed
912   // 12 : ERROR in XML SAX validation: something wrong with the content
913   // 14 : ERROR while creating calibration objects in the handler
914   // 15 : ERROR while storing data in the CDB
915   //
916
917   Log("Processing the DCS config summary files.");
918
919   // get the XML files
920   Log("Requesting the 2 summaryfiles from the FXS..");
921   const char *xmlFileS = GetFile(kDCS,"CONFIGSUMMARYSOR","");
922   const char *xmlFileE = GetFile(kDCS,"CONFIGSUMMARYEOR","");
923   // Request EOR and SOR files from the fxs, if both are not found exit
924
925   if (xmlFileS == NULL && xmlFileE == NULL) {
926     Log(Form("ERROR: SOR and EOR Files %s and %s not found!",xmlFileS,xmlFileE));
927     return 5;
928   } 
929
930   Bool_t fileExistE = true, fileExistS = true;
931
932   if (xmlFileS == NULL) {
933     Log(Form("Warning: SOR File %s not found!",xmlFileS));
934     fileExistE = false;
935   } else if (xmlFileE == NULL) {
936     Log(Form("Warning: EOR File %s not found!",xmlFileE));
937     fileExistS = false;
938   } else {
939     Log(Form("Both Files (%s and %s) found.",xmlFileS,xmlFileE));
940   }
941
942   if (fileExistS) {
943     // test the files
944     std::ifstream fileTestS;
945     fileTestS.open(xmlFileS, std::ios_base::binary | std::ios_base::in);
946     if (!fileTestS.good() || fileTestS.eof() || !fileTestS.is_open()) {
947       Log(Form("ERROR: File %s not valid!",xmlFileS));
948       return 8;
949     }
950     fileTestS.seekg(0, std::ios_base::end);
951     if (static_cast<int>(fileTestS.tellg()) < 2) {
952       Log(Form("ERROR: File %s is empty!",xmlFileS));
953       return 8;
954     }
955   }
956
957   if (fileExistE) {
958     std::ifstream fileTestE;
959     fileTestE.open(xmlFileE, std::ios_base::binary | std::ios_base::in);
960     if (!fileTestE.good() || fileTestE.eof() || !fileTestE.is_open()) {
961       Log(Form("ERROR: File %s not valid!",xmlFileE));
962       return 9;
963     }
964     fileTestE.seekg(0, std::ios_base::end);
965     if (static_cast<int>(fileTestE.tellg()) < 2) {
966       Log(Form("ERROR: File %s is empty!",xmlFileE));
967       return 9;
968     }
969   }
970
971   Log("Tested files are valid.");   
972
973   // make a robust XML validation
974   TSAXParser testParser;
975   if (fileExistS && testParser.ParseFile(xmlFileS) < 0 ) {
976     Log("ERROR: XML content (SOR) is not well-formed.");
977     return 10;
978   } else if (fileExistE && testParser.ParseFile(xmlFileE) < 0 ) {
979     Log("ERROR: XML content (EOR) is not well-formed.");
980     return 11;
981   }
982   Log("XML contents are well-formed.");
983
984   // create parser and parse
985   TSAXParser saxParserS, saxParserE;
986   AliTRDSaxHandler saxHandlerS, saxHandlerE;
987
988   if (fileExistS) {
989     saxParserS.ConnectToHandler("AliTRDSaxHandler", &saxHandlerS);
990     saxParserS.ParseFile(xmlFileS);
991   }
992   if (fileExistE) {
993     saxParserE.ConnectToHandler("AliTRDSaxHandler", &saxHandlerE);
994     saxParserE.ParseFile(xmlFileE);
995   }
996
997   // report errors if present 
998   if (((fileExistS && saxParserS.GetParseCode() == 0) || !fileExistS) && (!fileExistE || (fileExistE && saxParserE.GetParseCode() == 0))) {
999     Log("XML file validation OK.");
1000   } else {
1001     if (fileExistS) {
1002       Log(Form("ERROR in XML file validation. SOR Parse Code: %s", saxParserS.GetParseCode()));
1003     }
1004     if (fileExistE) {
1005       Log(Form("ERROR in XML file validation. EOR Parse Code: %s", saxParserE.GetParseCode()));
1006     }
1007     return 12;
1008   }
1009   if (((fileExistS && saxHandlerS.GetHandlerStatus() == 0) || !fileExistS) && (!fileExistE || (saxHandlerE.GetHandlerStatus() == 0))) {
1010     Log("SAX handler reports no errors.");
1011   } else  {
1012     if (fileExistS) {
1013       Log(Form("ERROR while creating calibration objects. SOR Error code: %s", saxHandlerS.GetHandlerStatus()));
1014     }
1015     if (fileExistE) {
1016       Log(Form("ERROR while creating calibration objects. EOR Error code: %s", saxHandlerE.GetHandlerStatus()));
1017     }
1018     return 14;
1019   }
1020
1021   // put both objects in one TObjArray to store them
1022   TObjArray* fCalObjArray = new TObjArray(2);
1023   fCalObjArray->SetOwner();
1024
1025   // get the calibration object storing the data from the handler
1026   if (fileExistS) {
1027     AliTRDCalDCS* fCalDCSObjSOR = saxHandlerS.GetCalDCSObj();
1028     fCalDCSObjSOR->EvaluateGlobalParameters();
1029     fCalObjArray->AddAt(fCalDCSObjSOR,0);
1030   }
1031
1032   if (fileExistE) {
1033     AliTRDCalDCS* fCalDCSObjEOR = saxHandlerE.GetCalDCSObj();
1034     fCalDCSObjEOR->EvaluateGlobalParameters();
1035     fCalObjArray->AddAt(fCalDCSObjEOR,1);
1036   }
1037
1038   // store the DCS calib data in the CDB
1039   AliCDBMetaData metaData1;
1040   metaData1.SetBeamPeriod(0);
1041   metaData1.SetResponsible("Frederick Kramer");
1042   metaData1.SetComment("DCS configuration data in two AliTRDCalDCS objects in one TObjArray (0:SOR, 1:EOR).");
1043   if (!Store("Calib", "DCS", fCalObjArray, &metaData1, 0, kTRUE)) {
1044     Log("problems while storing DCS config data object");
1045     return 15;
1046   } else {
1047     Log("DCS config data object stored.");
1048   }
1049
1050   //delete fCalObjArray;
1051
1052   Log("Processing of the DCS config summary file DONE.");  
1053   return 0;
1054 }