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