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