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