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