]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDPreprocessor.cxx
Compilation warning fixed
[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 <TFile.h>
36 #include <TProfile2D.h>
37 #include <TStopwatch.h>
38 #include <TObjString.h>
39 #include <TString.h>
40 #include <TList.h>
41 #include <TCollection.h>
42 #include <TSAXParser.h>
43 #include <fstream>
44
45 #include "AliCDBMetaData.h"
46 #include "AliLog.h"
47
48 #include "AliTRDPreprocessor.h"
49 #include "AliTRDSensorArray.h"
50 #include "AliTRDCalibraFit.h"
51 #include "AliTRDCalibraMode.h"
52 #include "AliTRDCalibPadStatus.h"
53 #include "AliTRDSaxHandler.h"
54 #include "Cal/AliTRDCalDet.h"
55 #include "Cal/AliTRDCalPad.h"
56 #include "Cal/AliTRDCalPadStatus.h"
57 #include "Cal/AliTRDCalDCS.h"
58 #include "Cal/AliTRDCalSingleChamberStatus.h"
59 #include "Cal/AliTRDCalROC.h"
60
61
62 ClassImp(AliTRDPreprocessor)
63
64 //______________________________________________________________________________________________
65 AliTRDPreprocessor::AliTRDPreprocessor(AliShuttleInterface *shuttle)
66   :AliPreprocessor("TRD", shuttle)
67   ,fVdriftHLT(0)
68 {
69   //
70   // Constructor
71   //
72
73   AddRunType("PHYSICS");
74   AddRunType("STANDALONE");
75   AddRunType("PEDESTAL");
76   AddRunType("DAQ");
77   
78 }
79
80 //______________________________________________________________________________________________
81 AliTRDPreprocessor::~AliTRDPreprocessor()
82 {
83   //
84   // Destructor
85   //
86
87 }
88
89 //______________________________________________________________________________________________
90 void AliTRDPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
91 {
92   //
93   // Initialization routine for the TRD preprocessor
94   //
95
96   AliPreprocessor::Initialize(run,startTime,endTime);
97
98 }
99
100 //______________________________________________________________________________________________
101 UInt_t AliTRDPreprocessor::Process(TMap* dcsAliasMap)
102 {
103   //
104   // Process DCS and calibration part for HLT
105   //
106
107   TString runType = GetRunType();
108   Log(Form("runtype %s\n",runType.Data()));
109   
110   // always process the configuration data
111   /*  Int_t resultDCSC = */ProcessDCSConfigData(); // for testing!
112   
113   if (runType=="PEDESTAL"){
114     if(ExtractPedestals()) return 1;
115     return 0;
116   } 
117
118   if ((runType=="PHYSICS") || (runType=="STANDALONE") || (runType=="DAQ")){
119     // DCS
120     /*if(*/ProcessDCS(dcsAliasMap)/*) return 1*/; // for testing!
121     if(runType=="PHYSICS"){
122       // HLT if On
123       //TString runPar = GetRunParameter("HLTStatus");
124       //if(runPar=="1") {
125       if(GetHLTStatus()) {
126         /*if(*/ExtractHLT()/*) return 1*/; // for testing!
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, kTRUE); 
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   Log ("         Summury of DCS :\n");
251   Log (Form("%30s %10s %10s", "dcsDatapointAlias", "Stored ?", "# graph"));
252   for (Int_t iAlias = 0; iAlias < nEntries; iAlias++) {
253     AliTRDSensorArray * oneTRDDCS = (AliTRDSensorArray *)list->At (iAlias);
254     Log (Form ("%30s %10s %4d", 
255                oneTRDDCS->GetStoreName ().Data (),
256                results[iAlias] ? "ok" : "X",
257                nGraph [iAlias]));
258   }
259   Log ("*********** End of DCS **********");
260   
261   delete results;
262   delete nGraph;
263
264   return error;
265
266 }
267
268 //______________________________________________________________________________________________
269 Bool_t AliTRDPreprocessor::ExtractPedestals()
270 {
271   //
272   // Pedestal running on LDCs at the DAQ
273   //
274
275   //
276   // The reference data are stored in:
277   // PadStatus1 for sm-00-01-02-09-10-11
278   // PadStatus2 for sm-03-04-05-12-13-14
279   // PadStatus3 for sm-06-07-08-15-16-17
280   // PadStatus0 if nothing found..means problems
281   //
282
283   Bool_t error = kFALSE;
284
285   // Init a AliTRDCalibPadStatus
286   AliTRDCalibPadStatus calPedSum = AliTRDCalibPadStatus();
287
288   AliCDBMetaData metaData;
289   metaData.SetBeamPeriod(0);
290   metaData.SetResponsible("Raphaelle Bailhache");
291   metaData.SetComment("TRD calib test");
292   
293   // Sum the contributions of the LDCs
294   TList * listpad = GetFileSources(kDAQ,"PADSTATUS");
295   if (!listpad) {
296     Log("No list found for the PEDESTRAL Run");
297     return kTRUE;
298   }
299   
300   // loop through all files from LDCs  
301   UInt_t index = 0;
302   while (listpad->At(index)!=NULL) {
303     TObjString* fileNameEntry = (TObjString*) listpad->At(index);
304     if (fileNameEntry != NULL)
305       {
306         TString fileName = GetFile(kDAQ, "PADSTATUS",
307                                    fileNameEntry->GetString().Data());
308         if(fileName.Length() ==0){
309           Log(Form("Error by retrieving the file %d for the pedestal",(Int_t)index));
310           delete listpad;
311           return kTRUE;
312         }
313         
314         TFile *f = TFile::Open(fileName);
315         AliTRDCalibPadStatus *calPed;
316         f->GetObject("calibpadstatus",calPed);
317         
318         if(calPed){
319           
320           Int_t ldc = 0; 
321
322           // analyse
323           //calPed->AnalyseHisto();
324                   
325           // Add to the calPedSum
326           for (Int_t idet=0; idet<540; idet++) {
327             AliTRDCalROC *rocMean  = calPed->GetCalRocMean(idet, kFALSE);
328             if ( rocMean )  {
329               calPedSum.SetCalRocMean(rocMean,idet);
330               ldc = (Int_t) (idet / 30);
331             }
332             AliTRDCalROC *rocRMS = calPed->GetCalRocRMS(idet, kFALSE);
333             if ( rocRMS )  {
334               calPedSum.SetCalRocRMS(rocRMS,idet);
335             }
336             AliTRDCalROC *rocMeand  = calPed->GetCalRocMeand(idet, kFALSE);
337             if ( rocMeand )  {
338               calPedSum.SetCalRocMeand(rocMeand,idet);
339             }
340             AliTRDCalROC *rocRMSd = calPed->GetCalRocRMSd(idet, kFALSE);
341             if ( rocRMSd )  {
342               calPedSum.SetCalRocRMSd(rocRMSd,idet);
343             }
344           }// det loop
345
346           if((ldc==0) || (ldc==1) || (ldc==2) || (ldc==9) || (ldc==10) || (ldc==11)) ldc = 1;
347           if((ldc==3) || (ldc==4) || (ldc==5) || (ldc==12) || (ldc==13) || (ldc==14)) ldc = 2;
348           if((ldc==6) || (ldc==7) || (ldc==8) || (ldc==15) || (ldc==16) || (ldc==17)) ldc = 3;
349         
350           // store as reference data
351           TString name("PadStatus");
352           name += ldc;
353           if(!StoreReferenceData("DAQData",(const char *)name,(TObject *) calPed,&metaData)){
354             Log(Form("Error storing AliTRDCalibPadStatus object %d as reference data",(Int_t)index));
355             error = kTRUE;
356           }
357
358         } // calPed
359       } // fileNameEntry
360     ++index;
361   }// while (list)
362
363   Log(Form("%d elements found in the list for the pedestal",(Int_t)index));
364   if(index==0){
365     delete listpad;
366     return kTRUE;
367   }
368
369   //
370   // Create pedestal 
371   //
372     
373   // Create Pad Status
374   AliTRDCalPadStatus *calPadStatus = calPedSum.CreateCalPadStatus();
375   // Create Noise 
376   //Make the AliTRDCalPad
377   AliTRDCalPad *calPad2 = calPedSum.CreateCalPad();
378   //Make the AliTRDCalDet correspondant
379   AliTRDCalDet *calDet = calPedSum.CreateCalDet();
380  
381   //
382   // Take the noise and Pad status from the previous OCDB
383   //
384
385   AliTRDCalPad *calPadPrevious=0;
386   AliCDBEntry* entry = GetFromOCDB("Calib", "PadNoise");
387   if (entry) calPadPrevious = (AliTRDCalPad*)entry->GetObject();
388   if ( calPadPrevious==NULL ) {
389      Log("AliTRDPreprocsessor: No previous TRD pad noise entry available.\n");
390      calPadPrevious = new AliTRDCalPad("PadNoise", "PadNoise");
391   }
392
393   AliTRDCalPadStatus *calPadStatusPrevious=0;
394   entry = GetFromOCDB("Calib", "PadStatus");
395   if (entry) calPadStatusPrevious = (AliTRDCalPadStatus*)entry->GetObject();
396   if ( calPadStatusPrevious==NULL ) {
397     Log("AliTRDPreprocsessor: No previous TRD pad status entry available.\n");
398     calPadStatusPrevious = new AliTRDCalPadStatus("padstatus", "padstatus");
399     for (Int_t idet=0; idet<540; ++idet)
400       {
401         AliTRDCalSingleChamberStatus *calROC = calPadStatusPrevious->GetCalROC(idet);
402         for(Int_t k = 0; k < calROC->GetNchannels(); k++){
403           calROC->SetStatus(k,AliTRDCalPadStatus::kMasked);
404         }
405       }
406   }
407
408   
409   // Loop over detectors for check
410   for (Int_t det=0; det<AliTRDgeometry::kNdet; ++det)  {
411     
412     // noise
413     AliTRDCalROC *calROCPreviousNoise = calPadPrevious->GetCalROC(det);
414     AliTRDCalROC *calROCNoise         = calPad2->GetCalROC(det);
415
416     // padstatus
417     AliTRDCalSingleChamberStatus *calROCPreviousStatus = calPadStatusPrevious->GetCalROC(det);
418     AliTRDCalSingleChamberStatus *calROCStatus         = calPadStatus->GetCalROC(det);
419     
420     
421     // loop over first half and second half chamber
422     for(Int_t half = 0; half < 2; half++){
423
424       Bool_t data         = AreThereDataPedestal(calROCStatus,(Bool_t)half);
425       //printf("There are data for the detector %d the half %d: %d\n",det,half,data);
426       if(!data){
427         // look if data in the OCDB
428         Bool_t dataPrevious = AreThereDataPedestal(calROCPreviousStatus,(Bool_t)half);
429         // if no data at all, set to default value
430         if(!dataPrevious){
431           SetDefaultStatus(*calROCStatus,(Bool_t)half);
432           SetDefaultNoise(*calROCNoise,(Bool_t)half);
433         }
434         else{
435           // if data, set to previous value
436           SetStatus(*calROCStatus,calROCPreviousStatus,(Bool_t)half);
437           SetNoise(*calROCNoise,calROCPreviousNoise,(Bool_t)half);
438         }
439       }
440     }
441   }
442   
443   //
444   // Store  
445   //  
446
447   AliCDBMetaData md3; 
448   md3.SetObjectClassName("AliTRDCalPadStatus");
449   md3.SetResponsible("Raphaelle Bailhache");
450   md3.SetBeamPeriod(1);
451   md3.SetComment("TRD calib test");
452   if(!Store("Calib","PadStatus"    ,(TObject *)calPadStatus, &md3, 0, kTRUE)){
453     Log("Error storing the pedestal");
454     delete listpad;
455     return kTRUE;
456   }
457
458   AliCDBMetaData md4; 
459   md4.SetObjectClassName("AliTRDCalPad");
460   md4.SetResponsible("Raphaelle Bailhache");
461   md4.SetBeamPeriod(1);
462   md4.SetComment("TRD calib test");
463   if(!Store("Calib","PadNoise"    ,(TObject *)calPad2, &md4, 0, kTRUE)){
464     Log("Error storing the pedestal");
465     delete listpad;
466     return kTRUE;
467   }
468   
469   AliCDBMetaData md5; 
470   md5.SetObjectClassName("AliTRDCalDet");
471   md5.SetResponsible("Raphaelle Bailhache");
472   md5.SetBeamPeriod(1);
473   md5.SetComment("TRD calib test");
474   if(!Store("Calib","DetNoise"    ,(TObject *)calDet, &md5, 0, kTRUE)){
475     Log("Error storing the pedestal");
476     delete listpad;
477     return kTRUE;
478   }  
479
480   delete listpad;
481   return error; 
482   
483 }
484 //__________________________________________________________________
485 Bool_t AliTRDPreprocessor::AreThereDataPedestal(AliTRDCalSingleChamberStatus *calROCStatus, Bool_t second){
486
487   //
488   // Data for this half chamber
489   //
490
491   Bool_t data         = kFALSE;
492   Int_t nCols         = calROCStatus->GetNcols();
493   Int_t nCol0         = 0;
494   Int_t nColE         = (Int_t) nCols/2 - 2;
495   if(second) {
496     nCol0 = nColE + 4;
497     nColE = nCols;
498   }
499   for(Int_t col = nCol0; col < nColE; col++){
500     for(Int_t row = 0; row < calROCStatus->GetNrows(); row++){
501       //printf("ismasked %d\n",(Int_t)calROCStatus->IsMasked(col,row));
502       if(!calROCStatus->GetStatus(col,row)) {
503         data = kTRUE;
504         continue;
505       }
506     }
507     if(data) continue;
508   }
509
510   return data;
511   
512 }
513 //__________________________________________________________________
514 void AliTRDPreprocessor::SetDefaultStatus(AliTRDCalSingleChamberStatus &calROCStatus, Bool_t second){
515
516   //
517   // default status for this half chamber
518   //
519
520   Int_t nCols         = calROCStatus.GetNcols();
521   Int_t nCol0         = 0;
522   Int_t nColE         = (Int_t) nCols/2;
523   if(second) {
524     nCol0 = nColE;
525     nColE = nCols;
526   }
527   for(Int_t col = nCol0; col < nColE; col++){
528     for(Int_t row = 0; row < calROCStatus.GetNrows(); row++){
529       calROCStatus.SetStatus(col,row,0);
530     }
531   }
532 }
533 //__________________________________________________________________
534 void AliTRDPreprocessor::SetStatus(AliTRDCalSingleChamberStatus &calROCStatus, AliTRDCalSingleChamberStatus *calROCStatusPrevious,Bool_t second){
535
536   //
537   // previous status for this half chamber
538   //
539
540   Int_t nCols         = calROCStatus.GetNcols();
541   Int_t nCol0         = 0;
542   Int_t nColE         = (Int_t) nCols/2;
543   if(second) {
544     nCol0 = nColE;
545     nColE = nCols;
546   }
547   for(Int_t col = nCol0; col < nColE; col++){
548     for(Int_t row = 0; row < calROCStatus.GetNrows(); row++){
549       calROCStatus.SetStatus(col,row,calROCStatusPrevious->GetStatus(col,row));
550     }
551   }
552 }
553 //__________________________________________________________________
554 void AliTRDPreprocessor::SetDefaultNoise(AliTRDCalROC &calROCNoise, Bool_t second){
555
556   //
557   // default noise for this half chamber
558   //
559
560   Int_t nCols         = calROCNoise.GetNcols();
561   Int_t nCol0         = 0;
562   Int_t nColE         = (Int_t) nCols/2;
563   if(second) {
564     nCol0 = nColE;
565     nColE = nCols;
566   }
567   for(Int_t col = nCol0; col < nColE; col++){
568     for(Int_t row = 0; row < calROCNoise.GetNrows(); row++){
569       calROCNoise.SetValue(col,row,0.12);
570     }
571   }
572 }
573 //__________________________________________________________________
574 void AliTRDPreprocessor::SetNoise(AliTRDCalROC &calROCNoise, AliTRDCalROC *calROCNoisePrevious, Bool_t second){
575
576   //
577   // previous noise for this half chamber
578   //
579
580   Int_t nCols         = calROCNoise.GetNcols();
581   Int_t nCol0         = 0;
582   Int_t nColE         = (Int_t) nCols/2;
583   if(second) {
584     nCol0 = nColE;
585     nColE = nCols;
586   }
587   for(Int_t col = nCol0; col < nColE; col++){
588     for(Int_t row = 0; row < calROCNoise.GetNrows(); row++){
589       calROCNoise.SetValue(col,row,calROCNoisePrevious->GetValue(col,row));
590     }
591   }
592 }
593 //______________________________________________________________________________________________
594 Bool_t AliTRDPreprocessor::ExtractDriftVelocityDAQ()
595 {
596   //
597   // Drift velocity DA running on monitoring servers at the DAQ
598   //
599
600   Bool_t error = kFALSE; 
601
602   // Objects for HLT and DAQ zusammen
603   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
604   AliCDBMetaData metaData;
605   metaData.SetBeamPeriod(0);
606   metaData.SetResponsible("Raphaelle Bailhache");
607   metaData.SetComment("TRD calib test");
608   // Store the infos for the detector
609   AliCDBMetaData md1; 
610   md1.SetObjectClassName("AliTRDCalDet");
611   md1.SetResponsible("Raphaelle Bailhache");
612   md1.SetBeamPeriod(0);
613   md1.SetComment("TRD calib test");
614   // Store the infos for the pads
615   AliCDBMetaData md2; 
616   md2.SetObjectClassName("AliTRDCalPad");
617   md2.SetResponsible("Raphaelle Bailhache");
618   md2.SetBeamPeriod(0);
619   md2.SetComment("TRD calib test");
620
621   // Take the file from the DAQ file exchange server
622   TList *listdaq = GetFileSources(kDAQ,"VDRIFT");
623   if (!listdaq) {
624     Log("No list found for vdrift (DAQ)");
625     return kTRUE;
626   }
627   
628   if(listdaq->GetSize() !=1){
629     Log(Form("Problem on the size of the list: %d (DAQ)",listdaq->GetSize()));
630     delete listdaq;
631     return kTRUE;
632   }
633   
634   TObjString* fileNameEntry = (TObjString*) listdaq->At(0);
635   if(fileNameEntry != NULL){
636     TString fileName = GetFile(kDAQ, "VDRIFT",
637                                fileNameEntry->GetString().Data());
638     if(fileName.Length() ==0){
639       Log("Error retrieving the file vdrift (DAQ)");
640       delete listdaq;
641       return kTRUE;
642     }
643     TFile *filedaq = TFile::Open(fileName);
644     TProfile2D *histodriftvelocity = (TProfile2D *) filedaq->Get("PH2d");
645     if (histodriftvelocity) {
646       
647       // store as reference data
648       if(!StoreReferenceData("DAQData","VdriftT0",(TObject *) histodriftvelocity,&metaData)){
649         Log("Error storing 2D Profile for vdrift from the DAQ");
650         error = kTRUE;
651       }
652       
653       // analyse
654       
655       Log("Take the PH reference data. Now we will try to fit\n");
656       calibra->SetMinEntries(2000); // If there is less than 2000
657       calibra->AnalysePH(histodriftvelocity);
658       
659       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
660         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
661       Int_t nbfit        = calibra->GetNumberFit();
662       Int_t nbE        = calibra->GetNumberEnt();
663       
664       // if enough statistics store the results
665       if ((nbtg >                  0) && 
666           (nbfit        >= 0.95*nbE)) {
667         // create the cal objects
668         TObjArray object      = calibra->GetVectorFit();
669         AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
670         TObject *objdriftvelocitypad = calibra->CreatePadObjectVdrift();
671         object              = calibra->GetVectorFit2();
672         AliTRDCalDet *objtime0det         = calibra->CreateDetObjectT0(&object,kTRUE);
673         TObject *objtime0pad         = calibra->CreatePadObjectT0();
674         calibra->ResetVectorFit();
675         // store
676         if(!Store("Calib","ChamberVdrift"    ,(TObject *) objdriftvelocitydet,&md1,0,kTRUE)){
677           Log("Error storing the calibration object for the chamber vdrift (DAQ)");
678           error = kTRUE;
679         }
680         if(!Store("Calib","ChamberT0"        ,(TObject *) objtime0det        ,&md1,0,kTRUE)){
681           Log("Error storing the calibration object for the chamber t0 (DAQ)");
682           error = kTRUE;
683         }
684         if(!Store("Calib","LocalVdrift"      ,(TObject *) objdriftvelocitypad,&md2,0,kTRUE)){
685           Log("Error storing the calibration object for the local drift velocity (DAQ)");
686           error = kTRUE;
687         }
688         if(!Store("Calib","LocalT0"          ,(TObject *) objtime0pad        ,&md2,0,kTRUE)){
689           Log("Error storing the calibration object for the local time0 (DAQ)");
690           error = kTRUE;
691         }
692       }
693       else{
694         Log("Not enough statistics for the average pulse height (DAQ)");
695       }
696     } // histo here
697   }// if fileNameEntry
698   
699   delete listdaq; 
700   return error; 
701   
702 }
703
704 //______________________________________________________________________________________________
705 Bool_t AliTRDPreprocessor::ExtractHLT()
706 {
707   //
708   // Gain, vdrift and PRF calibration running on HLT
709   // return kTRUE if NULL pointer to the list
710   //
711
712   Bool_t error = kFALSE;
713
714   // Objects for HLT and DAQ zusammen
715   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
716   AliCDBMetaData metaData;
717   metaData.SetBeamPeriod(0);
718   metaData.SetResponsible("Raphaelle Bailhache");
719   metaData.SetComment("TRD calib test");
720   // Store the infos for the detector
721   AliCDBMetaData md1; 
722   md1.SetObjectClassName("AliTRDCalDet");
723   md1.SetResponsible("Raphaelle Bailhache");
724   md1.SetBeamPeriod(0);
725   md1.SetComment("TRD calib test");
726   // Store the infos for the pads
727   AliCDBMetaData md2; 
728   md2.SetObjectClassName("AliTRDCalPad");
729   md2.SetResponsible("Raphaelle Bailhache");
730   md2.SetBeamPeriod(0);
731   md2.SetComment("TRD calib test");
732   
733   // Take the file from the HLT file exchange server
734   TList *listhlt = GetFileSources(kHLT,"GAINDRIFTPRF");
735   if (!listhlt) {
736     Log("No list found for the HLT");
737     return kTRUE;
738   }
739
740   if(listhlt->GetSize() != 1) {
741     Log(Form("Problem on the size of the list: %d (HLT)",listhlt->GetSize()));
742     delete listhlt;
743     return kTRUE;
744   }
745   
746   TObjString* fileNameEntry = (TObjString*) listhlt->At(0);
747   if(fileNameEntry != NULL){
748     TString fileName = GetFile(kHLT, "GAINDRIFTPRF",
749                                fileNameEntry->GetString().Data());
750     if(fileName.Length() ==0){
751       Log("Error retrieving the file (HLT)");
752       delete listhlt;
753       return kTRUE;
754     }
755     // Take the file
756     TFile *filehlt = TFile::Open(fileName);
757     
758     // gain
759     TH2I *histogain = (TH2I *) filehlt->Get("CH2d");
760     histogain->SetDirectory(0);
761     if (histogain) {
762       // store the reference data
763       if(!StoreReferenceData("HLTData","Gain",(TObject *) histogain,&metaData)){
764         Log("Error storing 2D histos for gain");
765         error = kTRUE;
766       }
767       // analyse
768       Log("Take the CH reference data. Now we will try to fit\n");
769       calibra->SetMinEntries(1000); // If there is less than 1000 entries in the histo: no fit
770       calibra->AnalyseCH(histogain);
771       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(0))
772         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(0));
773       Int_t nbfit       = calibra->GetNumberFit();
774       Int_t nbE         = calibra->GetNumberEnt();
775       // enough statistics
776       if ((nbtg >                  0) && 
777           (nbfit        >= 0.95*nbE)) {
778         // create the cal objects
779         TObjArray object           = calibra->GetVectorFit();
780         AliTRDCalDet *objgaindet   = calibra->CreateDetObjectGain(&object);
781         TObject *objgainpad        = calibra->CreatePadObjectGain();
782         // store them
783         if(!Store("Calib","ChamberGainFactor",(TObject *) objgaindet         ,&md1,0,kTRUE)){
784           Log("Error storing the calibration object for the chamber gain");
785           error = kTRUE;
786         }
787         if(!Store("Calib","LocalGainFactor"  ,(TObject *) objgainpad         ,&md2,0,kTRUE)){
788           Log("Error storing the calibration object for the local gain factor");
789           error = kTRUE;
790         }
791       }
792       calibra->ResetVectorFit();
793     }// if histogain
794     
795     // vdrift
796     fVdriftHLT = kFALSE;
797     TProfile2D *histodriftvelocity = (TProfile2D *) filehlt->Get("PH2d");
798     histodriftvelocity->SetDirectory(0);
799     if (histodriftvelocity) {
800       // store the reference data
801       if(!StoreReferenceData("HLTData","VdriftT0",(TObject *) histodriftvelocity,&metaData)){
802         Log("Error storing 2D Profile for average pulse height (HLT)");
803         error = kTRUE;
804       }
805       // analyse
806       Log("Take the PH reference data. Now we will try to fit\n");
807       calibra->SetMinEntries(1000*20); // If there is less than 20000
808       calibra->AnalysePH(histodriftvelocity);
809       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
810         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
811       Int_t nbfit        = calibra->GetNumberFit();
812       Int_t nbE          = calibra->GetNumberEnt();
813       // enough statistics
814       if ((nbtg >                  0) && 
815           (nbfit        >= 0.95*nbE)) {
816         // create the cal objects
817         TObjArray object  = calibra->GetVectorFit();
818         AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
819         TObject *objdriftvelocitypad      = calibra->CreatePadObjectVdrift();
820         object              = calibra->GetVectorFit2();
821         AliTRDCalDet *objtime0det  = calibra->CreateDetObjectT0(&object,kTRUE);
822         TObject *objtime0pad       = calibra->CreatePadObjectT0();
823         // store them
824         if(!Store("Calib","ChamberVdrift"    ,(TObject *) objdriftvelocitydet,&md1,0,kTRUE)){
825           Log("Error storing the calibration object for the chamber vdrift (HLT)");
826           error = kTRUE;                
827         }
828         if(!Store("Calib","ChamberT0"        ,(TObject *) objtime0det        ,&md1,0,kTRUE)){
829           Log("Error storing the calibration object for the chamber t0 (HLT)");
830           error = kTRUE;
831         }
832         if(!Store("Calib","LocalVdrift"      ,(TObject *) objdriftvelocitypad,&md2,0,kTRUE)){
833           Log("Error storing the calibration object for the local drift velocity (HLT)");
834           error = kTRUE;
835         }
836         if(!Store("Calib","LocalT0"          ,(TObject *) objtime0pad        ,&md2,0,kTRUE)){
837           Log("Error storing the calibration object for the local time0 (HLT)");
838           error = kTRUE;
839         }
840         fVdriftHLT = kTRUE;
841       }
842       calibra->ResetVectorFit();
843     }// if TProfile2D
844     
845     // prf
846     TProfile2D *histoprf = (TProfile2D *) filehlt->Get("PRF2d");
847     histoprf->SetDirectory(0);
848     if (histoprf) {
849       // store reference data
850       if(!StoreReferenceData("HLTData","PRF",(TObject *) histoprf,&metaData)){
851         Log("Error storing the 2D Profile for Pad Response Function");
852         error = kTRUE;
853       }
854       // analyse
855       Log("Take the PRF reference data. Now we will try to fit\n");
856       calibra->SetMinEntries(600); // If there is less than 20000
857       calibra->AnalysePRFMarianFit(histoprf);
858       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
859         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
860       Int_t nbfit        = calibra->GetNumberFit();
861       Int_t nbE          = calibra->GetNumberEnt();
862       // enough statistics
863       if ((nbtg >                  0) && 
864           (nbfit        >= 0.95*nbE)) {
865         // create cal pad objects 
866         TObjArray object            = calibra->GetVectorFit();
867         TObject *objPRFpad          = calibra->CreatePadObjectPRF(&object);
868         // store them
869         if(!Store("Calib","PRFWidth"         ,(TObject *) objPRFpad          ,&md2,0,kTRUE)){
870           Log("Error storing the calibration object for the Pad Response Function");
871           error = kTRUE;
872         }
873       }
874       calibra->ResetVectorFit();
875     }// if PRF
876   }// if fileNameEntry
877   
878   delete listhlt;
879   return error;
880   
881 }
882
883 //_____________________________________________________________________________
884 UInt_t AliTRDPreprocessor::ProcessDCSConfigData()
885 {
886   // 
887   // process the configuration of FEE, PTR and GTU
888   // reteive XML filei(s) from the DCS FXS
889   // parse it/them and store TObjArrays in the CDB
890   //
891   // return 0 for success, otherwise:
892   //  5 : could not get the SOR file from the FXS
893   //  6 : could not get the EOR file from the FXS
894   //  7 : 
895   //  8 : something wrong with the EOR file
896   //  9 : 
897   // 10 : EOR XML is not well-formed
898   // 11 :
899   // 12 : ERROR in XML SAX validation of EOR: something wrong with the content
900   // 13 :
901   // 14 : ERROR while creating calibration objects in the handler
902   // 15 : error while storing data in the CDB
903   //
904
905   Log("Processing the DCS config summary file.");
906
907   // get the XML files
908   Log("Requesting the 2 summaryfiles from the FXS..");
909   const char * xmlFileS = GetFile(kDCS,"CONFIGSUMMARYSOR","");
910   const char * xmlFile = GetFile(kDCS,"CONFIGSUMMARYEOR","");
911   // for the time being just request BOTH files from the FXS
912   // THEN it can be created online (otherwise the FXS would be messed up)
913   // the next step is to actually read BOTH files and store their informations
914   if (xmlFileS == NULL) {
915     Log(Form("ERROR: SOR File %s not found!",xmlFileS));
916     return 5;
917   } else {
918     Log(Form("SOR File %s found.",xmlFileS));
919   }
920   if (xmlFile == NULL) {
921     Log(Form("ERROR: EOR File %s not found!",xmlFile));
922     return 6;
923   } else {
924     Log(Form("EOR File %s found.",xmlFile));
925   }
926   
927   // test the file
928   std::ifstream fileTest;
929   fileTest.open(xmlFile, std::ios_base::binary | std::ios_base::in);
930   if (!fileTest.good() || fileTest.eof() || !fileTest.is_open()) {
931     Log(Form("ERROR: File %s not valid!",xmlFile));
932     return 8;
933   }
934   fileTest.seekg(0, std::ios_base::end);
935   if (static_cast<int>(fileTest.tellg()) < 2) {
936     Log(Form("ERROR: File %s is empty!",xmlFile));
937     return 8;
938   }
939   Log("File is tested valid.");   
940
941   // make a robust XML validation
942   TSAXParser testParser;
943   if (testParser.ParseFile(xmlFile) < 0 ) {
944     Log("ERROR: XML content is not well-formed.");
945     return 10;
946   } else {
947     Log("XML content is well-formed");   
948   }
949   
950   // create parser and parse
951   TSAXParser saxParser;
952   AliTRDSaxHandler saxHandler;
953   saxParser.ConnectToHandler("AliTRDSaxHandler", &saxHandler);
954   saxParser.ParseFile(xmlFile);
955
956   // report errors if present
957   if (saxParser.GetParseCode() == 0) {
958     Log("XML file validation OK");
959   } else {
960     Log(Form("ERROR in XML file validation. Parsecode: %s", saxParser.GetParseCode()));
961     return 12;
962   }
963   if (saxHandler.GetHandlerStatus() == 0) {
964     Log("SAX handler reports no errors.");
965   } else  {
966     Log(Form("ERROR while creating calibration objects. Error code: %s", saxHandler.GetHandlerStatus()));
967     return 14;
968   }
969
970   // get the calibration object storing the data from the handler
971   AliTRDCalDCS* fCalDCSObj = saxHandler.GetCalDCSObj();
972   fCalDCSObj->EvaluateGlobalParameters();
973   
974   // store the DCS calib data in the CDB
975   AliCDBMetaData metaData1;
976   metaData1.SetBeamPeriod(0);
977   metaData1.SetResponsible("Frederick Kramer");
978   metaData1.SetComment("DCS configuration data in one AliTRDCalDCS object.");
979   if (!Store("Calib", "DCS", fCalDCSObj, &metaData1, 0, kTRUE)) {
980     Log("problems while storing DCS config data object");
981     return 15;
982   } else {
983     Log("DCS config data object stored.");
984   }
985
986   Log("Processing of the DCS config summary file DONE.");
987
988   return 0;
989 }
990