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