]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDCPreprocessor.cxx
Fix for the problem during PbPb run of Nov 2010 (Indra)
[u/mrichter/AliRoot.git] / ZDC / AliZDCPreprocessor.cxx
1 // --- ROOT system
2 #include <TFile.h>
3 #include <TClonesArray.h>
4 #include <TList.h>
5 #include <TObjString.h>
6 #include <TTimeStamp.h>
7
8 #include "AliZDCPreprocessor.h"
9 #include "AliCDBManager.h"
10 #include "AliCDBEntry.h"
11 #include "AliCDBMetaData.h"
12 #include "AliDCSValue.h"
13 #include "AliAlignObj.h"
14 #include "AliAlignObjParams.h"
15 #include "AliLog.h"
16 #include "AliZDCDataDCS.h"
17 #include "AliZDCChMap.h"
18 #include "AliZDCPedestals.h"
19 #include "AliZDCLaserCalib.h"
20 #include "AliZDCEnCalib.h"
21 #include "AliZDCTowerCalib.h"
22 #include "AliZDCMBCalib.h"
23
24 /////////////////////////////////////////////////////////////////////
25 //                                                                 //
26 // Class implementing Shuttle ZDC pre-processor.                   //
27 // It takes data from DCS and DAQ and writes calibration objects   //
28 // in the OCDB and reference values/histos in the ReferenceData.   //
29 //                                                                 //
30 /////////////////////////////////////////////////////////////////////
31
32 // ******************************************************************
33 //    RETURN CODES:
34 // return 0 : everything OK
35 // return 1 : no DCS input data Map
36 // return 2 : error storing DCS data in RefData 
37 // return 3 : error storing alignment object in OCDB
38 // return 4 : error in ZDCMapping.dat file retrieved from DAQ FXS (not existing|empty|corrupted)
39 // return 5 : error storing mapping obj. in OCDB
40 // return 6 : error storing energy calibration obj. in OCDB
41 // return 7 : error storing tower inter-calibration obj. in OCDB
42 // return 8 : error in ZDCEnergyCalib.dat file retrieved from DAQ FXS 
43 // return 9 : error in ZDCTowerCalib.dat file retrieved from DAQ FXS 
44 // return 10: error in ZDCPedestal.dat file retrieved from DAQ FXS 
45 // return 11: error storing pedestal calibration obj. in OCDB
46 // return 12: error in ZDCPedHisto.root file retrieved from DAQ FXS 
47 // return 13: error storing pedestal histos in RefData 
48 // return 14: error in ZDCLaserCalib.dat file retrieved from DAQ FXS 
49 // return 15: error storing laser calibration obj. in OCDB
50 // return 16: error in ZDCLaserHisto.root file retrieved from DAQ FXS 
51 // return 17: error storing laser histos in RefData 
52 // return 18: error in ZDCMBCalib.root file retrieved from DAQ FXS 
53 // return 19: error storing MB calibration obj. in OCDB
54 // ******************************************************************
55
56 ClassImp(AliZDCPreprocessor)
57
58 //______________________________________________________________________________________________
59 AliZDCPreprocessor::AliZDCPreprocessor(AliShuttleInterface* shuttle) :
60   AliPreprocessor("ZDC", shuttle),
61   fData(0)
62 {
63   // constructor
64   // May 2009 - run types updated according to
65   // http://alice-ecs.web.cern.ch/alice-ecs/runtypes_3.36.html
66   AddRunType("STANDALONE_PEDESTAL");
67   AddRunType("STANDALONE_LASER");
68   AddRunType("STANDALONE_COSMIC");
69   AddRunType("CALIBRATION_EMD");
70   AddRunType("CALIBRATION_MB");
71   AddRunType("CALIBRATION_CENTRAL");
72   AddRunType("CALIBRATION_SEMICENTRAL");
73   AddRunType("CALIBRATION_BC");
74   AddRunType("PHYSICS");
75 }
76
77
78 //______________________________________________________________________________________________
79 AliZDCPreprocessor::~AliZDCPreprocessor()
80 {
81   // destructor
82 }
83
84
85 //______________________________________________________________________________________________
86 void AliZDCPreprocessor::Initialize(Int_t run, UInt_t startTime, UInt_t endTime)
87 {
88   // Creates AliZDCDataDCS object
89
90   AliPreprocessor::Initialize(run, startTime, endTime);
91
92   AliDebug(2,Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s \n\tStartTime DCS Query %s \n\tEndTime DCS Query %s", run,
93                 TTimeStamp(startTime).AsString(),
94                 TTimeStamp(endTime).AsString(), ((TTimeStamp)GetStartTimeDCSQuery()).AsString(), ((TTimeStamp)GetEndTimeDCSQuery()).AsString()));
95
96   fRun = run;
97   fStartTime = startTime;
98   fEndTime = endTime;
99
100   fData = new AliZDCDataDCS(fRun, fStartTime, fEndTime, GetStartTimeDCSQuery(), GetEndTimeDCSQuery());
101 }
102
103 //_____________________________________________________________________________
104 Bool_t AliZDCPreprocessor::ProcessDCS(){
105
106   // tells whether DCS should be processed or not
107
108   TString runType = GetRunType();
109   Log(Form("RunType %s",runType.Data()));
110
111   if (runType=="STANDALONE_COSMIC" || runType=="STANDALONE_PEDESTAL"){
112     return kFALSE;
113   }
114
115   return kTRUE;
116 }
117
118 //______________________________________________________________________________________________
119 UInt_t AliZDCPreprocessor::ProcessDCSData(TMap* dcsAliasMap)
120 {
121   
122   // Fills data into a AliZDCDataDCS object
123   if(!dcsAliasMap){
124     Log(" No DCS map found: ZDC exiting from Shuttle");
125     if(fData){
126       delete fData;
127       fData = 0;
128     }
129     return 1;
130   }
131
132   Log(Form("Processing data from DCS"));
133    
134   // The processing of the DCS input data is forwarded to AliZDCDataDCS
135   //dcsAliasMap->Print(""); 
136   Bool_t resDCSProcess = fData->ProcessData(*dcsAliasMap);
137   if(resDCSProcess==kFALSE){
138     Log(" Problems in processing DCS DP");
139     return 1;
140   }  
141   
142   // ------------------------------------------------------
143   // Change introduced 26/9/09 in order NOT to process the
144   // HV DP since some of them are never found in amanda DB
145   // ------------------------------------------------------
146   // Store DCS data as reference
147   AliCDBMetaData metadata;
148   metadata.SetResponsible("Chiara Oppedisano");
149   metadata.SetComment("DCS DP TMap for ZDC");
150   Bool_t resDCSRef = kTRUE;
151   resDCSRef = StoreReferenceData("DCS","Data", dcsAliasMap, &metadata);
152   
153   if(resDCSRef==kFALSE) return 2;
154
155   // --- Writing ZDC table positions into alignment object
156   TClonesArray *array = new TClonesArray("AliAlignObjParams",10);
157   TClonesArray &alobj = *array;
158   AliAlignObjParams a;
159   Double_t dx=0., dz=0., dpsi=0., dtheta=0., dphi=0.;
160   // Vertical table position in mm from DCS
161   Double_t dyZN1 = (Double_t) (fData->GetAlignData(0)/10.);
162   Double_t dyZP1 = (Double_t) (fData->GetAlignData(1)/10.);
163   Double_t dyZN2 = (Double_t) (fData->GetAlignData(2)/10.);
164   Double_t dyZP2 = (Double_t) (fData->GetAlignData(3)/10.);
165   //
166   const char *n1ZDC="ZDC/NeutronZDC_C";  
167   const char *p1ZDC="ZDC/ProtonZDC_C";
168   const char *n2ZDC="ZDC/NeutronZDC_A";
169   const char *p2ZDC="ZDC/ProtonZDC_A";
170   //
171   UShort_t iIndex=0;
172   AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer;
173   UShort_t volid = AliGeomManager::LayerToVolUID(iLayer,iIndex);
174   //
175   new(alobj[0]) AliAlignObjParams(n1ZDC, volid, dx, dyZN1, dz, dpsi, dtheta, dphi, kTRUE);
176   new(alobj[1]) AliAlignObjParams(p1ZDC, volid, dx, dyZP1, dz, dpsi, dtheta, dphi, kTRUE);
177   new(alobj[2]) AliAlignObjParams(n2ZDC, volid, dx, dyZN2, dz, dpsi, dtheta, dphi, kTRUE);
178   new(alobj[3]) AliAlignObjParams(p2ZDC, volid, dx, dyZP2, dz, dpsi, dtheta, dphi, kTRUE);
179  
180   // save in CDB storage
181   AliCDBMetaData mdDCS;
182   mdDCS.SetResponsible("Chiara Oppedisano");
183   mdDCS.SetComment("Alignment object for ZDC");
184   Bool_t resultAl = Store("Align","Data", array, &mdDCS, 0, kFALSE);
185   if(resultAl==kFALSE)  return 3;
186   
187   return 0;
188 }
189
190 //______________________________________________________________________________________________
191 UInt_t AliZDCPreprocessor::ProcessChMap()
192
193   const int kNModules=10, kNch=48, kNScch=32, kNtdcch=32;
194   
195   // Reading the file for mapping from FXS
196   TList* daqSource = GetFileSources(kDAQ, "MAPPING");
197   if(!daqSource){
198     AliError(Form("No sources for file ZDCChMapping.dat in run %d ", fRun));
199     return 4;
200   }
201   if(daqSource->GetEntries()==0) return 4;
202   Log("\t List of DAQ sources for MAPPING id: "); daqSource->Print();
203   //
204   TIter iter(daqSource);
205   TObjString* source = 0;
206   Int_t isou = 0;
207   Int_t modMap[kNModules][3], adcMap[kNch][6], scMap[kNScch][6], tdcMap[kNtdcch][4]; 
208   //
209   while((source = dynamic_cast<TObjString*> (iter.Next()))){
210      TString fileName = GetFile(kDAQ, "MAPPING", source->GetName());
211      Log(Form("\t Getting file #%d: ZDCChMapping.dat from %s\n",++isou, source->GetName()));
212
213      if(fileName.Length() <= 0){
214        Log(Form("No file from source %s!", source->GetName()));
215        return 4;
216      }
217      // --- Reading file with calibration data
218      //const char* fname = fileName.Data();
219      if(fileName){
220        FILE *file;
221        if((file = fopen(fileName,"r")) == NULL){
222          printf("Cannot open file %s \n",fileName.Data());
223          return 4;
224        }
225        Log(Form("File %s connected to process data for ADC mapping", fileName.Data()));
226        //
227        for(Int_t j=0; j<kNch; j++){       
228            for(Int_t k=0; k<6; k++){
229              int read = fscanf(file,"%d",&adcMap[j][k]);
230              if(read == 0) AliDebug(3," Failing in reading data from mapping file");
231            }
232        }
233        for(Int_t j=kNch; j<kNch+kNScch; j++){     
234            for(Int_t k=0; k<6; k++){
235              int read = fscanf(file,"%d",&scMap[j-kNch][k]);
236              if(read == 0) AliDebug(3," Failing in reading data from mapping file");
237            }
238        }
239        for(Int_t j=kNch+kNScch; j<kNch+kNScch+kNtdcch; j++){      
240            for(Int_t k=0; k<4; k++){
241              int read = fscanf(file,"%d",&tdcMap[j-kNch-kNScch][k]);
242              if(read == 0) AliDebug(3," Failing in reading data from mapping file");
243            }
244        }
245        for(Int_t j=kNch+kNScch+kNtdcch; j<kNch+kNScch+kNtdcch+kNModules; j++){    
246            for(Int_t k=0; k<3; k++){
247              int read = fscanf(file,"%d",&modMap[j-kNch-kNScch-kNtdcch][k]);
248              if(read == 0) AliDebug(3," Failing in reading data from mapping file");
249            }
250        }
251        fclose(file);
252      }
253      else{
254        Log(Form("File %s not found", fileName.Data()));
255        return 4;
256      }
257   }
258   
259   // Store the currently read map ONLY IF it is different
260   // from the entry in the OCDB
261   Bool_t adcMapUpdated=kFALSE, scMapUpdated=kFALSE, tdcMapUpdated=kFALSE;
262   Bool_t updateOCDB = kFALSE;
263   
264   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","ChMap");
265   if(!cdbEntry){
266     Log(" No existing CDB entry for ADC mapping");
267     updateOCDB = kTRUE;
268   }
269   else{
270     AliZDCChMap *chMap = (AliZDCChMap*) cdbEntry->GetObject();
271     for(Int_t i=0; i<kNch; i++){
272       if(  (adcMap[i][1] != chMap->GetADCModule(i)) 
273         || (adcMap[i][2] != chMap->GetADCChannel(i)) 
274         || (adcMap[i][3] != chMap->GetADCSignalCode(i)) 
275         || (adcMap[i][4] != chMap->GetDetector(i)) 
276         || (adcMap[i][5] != chMap->GetSector(i))) 
277          adcMapUpdated = kTRUE;
278     }
279     for(Int_t i=0; i<kNScch; i++){
280       if(  (scMap[i][2] != chMap->GetScChannel(i)) 
281         || (scMap[i][3] != chMap->GetScSignalCode(i)) )
282          scMapUpdated = kTRUE;
283     }
284     for(Int_t i=0; i<kNtdcch; i++){
285       if(  (tdcMap[i][2] != chMap->GetTDCChannel(i)) 
286         || (tdcMap[i][3] != chMap->GetTDCSignalCode(i)))
287          tdcMapUpdated = kTRUE;
288     }
289   }
290   if(adcMapUpdated || scMapUpdated || tdcMapUpdated) updateOCDB = kTRUE;
291   //
292   Bool_t resChMapStore = kTRUE;
293   if(updateOCDB==kTRUE){
294     Log(" A new entry ZDC/Calib/ChMap will be created");
295     //
296     // --- Initializing mapping calibration object
297     AliZDCChMap *mapCalib = new AliZDCChMap("ZDC");
298     // Writing channel map in the OCDB
299     for(Int_t k=0; k<kNModules; k++){
300       mapCalib->SetModuleMap(k, modMap[k][0], modMap[k][1], modMap[k][2]);
301     }
302     for(Int_t k=0; k<kNch; k++){
303       mapCalib->SetADCModule(k,adcMap[k][1]);
304       mapCalib->SetADCChannel(k,adcMap[k][2]);
305       mapCalib->SetADCSignalCode(k,adcMap[k][3]);
306       mapCalib->SetDetector(k,adcMap[k][4]);
307       mapCalib->SetSector(k,adcMap[k][5]);
308     }
309     for(Int_t k=0; k<kNScch; k++){
310        mapCalib->SetScChannel(k, scMap[k][2]);
311        mapCalib->SetScSignalCode(k, scMap[k][3]);
312        mapCalib->SetScDetector(k, scMap[k][4]);
313        mapCalib->SetScSector(k, scMap[k][5]);
314     }
315     for(Int_t k=0; k<kNtdcch; k++){
316        mapCalib->SetTDCChannel(k, tdcMap[k][2]);
317        mapCalib->SetTDCSignalCode(k, tdcMap[k][3]);
318     }
319     //
320     //mapCalib->Print("");
321     // 
322     AliCDBMetaData metaData;
323     metaData.SetBeamPeriod(0);
324     metaData.SetResponsible("Chiara Oppedisano");
325     metaData.SetComment("AliZDCChMap object created by ZDC preprocessor");  
326     //
327     resChMapStore = Store("Calib","ChMap",mapCalib, &metaData, 0, kTRUE);
328     printf("  Mapping object stored in OCDB\n");
329   }
330   else{
331     Log(" ZDC/Calib/ChMap entry in OCDB is valid and won't be updated");
332     resChMapStore = kTRUE;
333   }
334   
335   delete daqSource; daqSource=0;
336   
337   if(resChMapStore==kFALSE) return 5;
338   else return 0;
339
340 }
341
342 //______________________________________________________________________________________________
343 UInt_t AliZDCPreprocessor::ProcessppData()
344 {
345    Bool_t resEnCal=kTRUE, resTowCal=kTRUE;
346   
347    // *********** Energy calibration
348    // --- Cheking if there is already the entry in the OCDB
349    AliCDBEntry *cdbEnEntry = GetFromOCDB("Calib", "EnergyCalib");
350    if(!cdbEnEntry){   
351      Log(Form(" ZDC/Calib/EnergyCalib entry will be created"));
352      // --- Initializing calibration object
353      AliCDBMetaData metaData;
354      metaData.SetBeamPeriod(0);
355      metaData.SetResponsible("Chiara Oppedisano");
356      //
357      AliZDCEnCalib *eCalib = new AliZDCEnCalib("ZDC");
358      for(Int_t j=0; j<6; j++) eCalib->SetEnCalib(j,1.);
359      metaData.SetComment("AliZDCEnCalib object");  
360      //eCalib->Print("");
361      resEnCal = Store("Calib", "EnergyCalib", eCalib, &metaData, 0, kTRUE);
362    }
363    else{ 
364      // if entry exists it is still valid (=1 for all runs!)
365      Log(Form(" Valid ZDC/Calib/EnergyCalib object already existing in OCDB!!!"));
366      resEnCal = kTRUE;
367    }
368    
369    if(resEnCal==kFALSE) return 6;
370
371    //
372    // *********** Tower inter-calibration
373    // --- Cheking if there is already the entry in the OCDB
374    AliCDBEntry *cdbTowEntry = GetFromOCDB("Calib", "TowerCalib");
375    if(!cdbTowEntry){   
376      AliZDCTowerCalib *towCalib = new AliZDCTowerCalib("ZDC");
377      for(Int_t j=0; j<5; j++){  
378         towCalib->SetZN1EqualCoeff(j, 1.);
379         towCalib->SetZP1EqualCoeff(j, 1.);
380         towCalib->SetZN2EqualCoeff(j, 1.);
381         towCalib->SetZP2EqualCoeff(j, 1.);  
382      }
383      //towCalib->Print("");
384      // 
385      AliCDBMetaData metaData;
386      metaData.SetBeamPeriod(0);
387      metaData.SetResponsible("Chiara Oppedisano");
388      metaData.SetComment("AliZDCTowerCalib object");  
389      //
390      resTowCal = Store("Calib", "TowerCalib", towCalib, &metaData, 0, kTRUE);
391    }
392    else{ 
393      // if entry exists it is still valid (=1 for all runs!)
394      Log(Form(" Valid ZDC/Calib/TowerCalib object already existing in OCDB!!!"));
395      resTowCal = kTRUE;
396    }
397    
398    if(resTowCal==kFALSE) return 7;
399    
400    return 0;
401 }
402
403 //______________________________________________________________________________________________
404 UInt_t AliZDCPreprocessor::ProcessCalibData(Float_t beamEnergy)
405 {
406   TList* daqSources = GetFileSources(kDAQ, "EMDENERGYCALIB");
407   if(!daqSources){
408     AliError(Form("No sources for CALIBRATION_EMD run %d !", fRun));
409     return 8;
410   }
411   Log("\t List of DAQ sources for EMDENERGYCALIB id: "); daqSources->Print();
412   //
413   TIter iter2(daqSources);
414   TObjString* source = 0;
415   Int_t i=0;
416   Bool_t resEnCal=kTRUE, resTowCal=kTRUE;
417   
418   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
419     TString stringEMDFileName = GetFile(kDAQ, "EMDENERGYCALIB", source->GetName());
420     if(stringEMDFileName.Length() <= 0){
421       Log(Form("No file from source %s!", source->GetName()));
422       return 8;
423     }
424     const char* emdFileName = stringEMDFileName.Data();
425     Log(Form("\t Getting file #%d: %s from %s\n",++i,emdFileName,source->GetName()));
426     //
427     // --- Initializing energy calibration object
428     AliZDCEnCalib *eCalib = new AliZDCEnCalib("ZDC");
429     // --- Reading file with calibration data
430     if(emdFileName){
431       FILE *file;
432       if((file = fopen(emdFileName,"r")) == NULL){
433         printf("Cannot open file %s \n",emdFileName);
434         return 8;
435       }
436       Log(Form("File %s connected to process data from EM dissociation events", emdFileName));
437       //
438       Float_t fitValEMD[6];
439       for(Int_t j=0; j<6; j++){        
440         if(j<6){
441           int iread = fscanf(file,"%f",&fitValEMD[j]);
442           if(iread==0) AliDebug(3," Failing reading data from EMD calibration data file");
443           if(fitValEMD[j]!=1.) eCalib->SetEnCalib(j, beamEnergy/fitValEMD[j]);
444           else eCalib->SetEnCalib(j, fitValEMD[j]);
445         }
446       }
447       //
448       fclose(file);
449     }
450     else{
451       Log(Form("File %s not found", emdFileName));
452       return 8;
453     }
454     //eCalib->Print("");
455     // 
456     AliCDBMetaData metaData;
457     metaData.SetBeamPeriod(0);
458     metaData.SetResponsible("Chiara Oppedisano");
459     metaData.SetComment("Filling AliZDCEnCalib object");  
460     //
461     resEnCal = Store("Calib","EnergyCalib",eCalib, &metaData, 0, kTRUE);
462     if(resEnCal==kFALSE) return 6;
463   }
464   delete daqSources; daqSources = 0;
465   
466   TList* daqSourcesH = GetFileSources(kDAQ, "EMDTOWERCALIB");
467   if(!daqSourcesH){
468     AliError(Form("No sources for CALIBRATION_EMD run %d !", fRun));
469     return 9;
470   }
471   Log("\t List of DAQ sources for EMDTOWERCALIB id: "); daqSourcesH->Print();
472   //
473   TIter iter2H(daqSourcesH);
474   TObjString* sourceH = 0;
475   Int_t iH=0;
476   while((sourceH = dynamic_cast<TObjString*> (iter2H.Next()))){
477     TString stringtowEMDFileName = GetFile(kDAQ, "EMDTOWERCALIB", sourceH->GetName());
478     if(stringtowEMDFileName.Length() <= 0){
479       Log(Form("No file from source %s!", sourceH->GetName()));
480       return 9;
481     }
482     const char * towEMDFileName = stringtowEMDFileName.Data();
483     Log(Form("\t Getting file #%d: %s from source %s\n",++iH,towEMDFileName,sourceH->GetName()));
484     // --- Initializing energy calibration object
485     AliZDCTowerCalib *towCalib = new AliZDCTowerCalib("ZDC");
486     // --- Reading file with calibration data
487     if(towEMDFileName){
488       FILE *file;
489       if((file = fopen(towEMDFileName,"r")) == NULL){
490         printf("Cannot open file %s \n",towEMDFileName);
491         return 9;
492       }
493       //
494       Float_t equalCoeff[4][5];
495       for(Int_t j=0; j<4; j++){        
496          for(Int_t k=0; k<5; k++){
497            int leggi = fscanf(file,"%f",&equalCoeff[j][k]);
498            if(leggi==0) AliDebug(3," Failing reading data from EMD calibration file");
499            if(j==0)      towCalib->SetZN1EqualCoeff(k, equalCoeff[j][k]);
500            else if(j==1) towCalib->SetZP1EqualCoeff(k, equalCoeff[j][k]);
501            else if(j==2) towCalib->SetZN2EqualCoeff(k, equalCoeff[j][k]);
502            else if(j==3) towCalib->SetZP2EqualCoeff(k, equalCoeff[j][k]);  
503          }
504       }
505       //
506       fclose(file);
507     }
508     else{
509       Log(Form("File %s not found", towEMDFileName));
510       return 9;
511     }
512     //towCalib->Print("");
513     // 
514     AliCDBMetaData metaData;
515     metaData.SetBeamPeriod(0);
516     metaData.SetResponsible("Chiara Oppedisano");
517     metaData.SetComment("Filling AliZDCTowerCalib object");  
518     //
519     resTowCal = Store("Calib","TowerCalib",towCalib, &metaData, 0, kTRUE);
520     if(resTowCal==kFALSE) return 7;
521   }
522   delete daqSourcesH; daqSourcesH = 0;
523   
524      
525   return 0;
526 }
527
528 //______________________________________________________________________________________________
529 UInt_t AliZDCPreprocessor::ProcessPedestalData()
530 {
531   TList* daqSources = GetFileSources(kDAQ, "PEDESTALDATA");
532   if(!daqSources){
533     Log(Form("No source for STANDALONE_PEDESTAL run %d !", fRun));
534     return 10;
535   }
536   if(daqSources->GetEntries()==0) return 10;
537   Log("\t List of DAQ sources for PEDESTALDATA id: "); daqSources->Print();
538   //
539   TIter iter(daqSources);
540   TObjString* source;
541   Int_t i=0;
542   Bool_t resPedCal=kTRUE, resPedHist=kTRUE;
543   
544   while((source = dynamic_cast<TObjString*> (iter.Next()))){
545      TString stringPedFileName = GetFile(kDAQ, "PEDESTALDATA", source->GetName());
546      if(stringPedFileName.Length() <= 0){
547         Log(Form("No PEDESTALDATA file from source %s!", source->GetName()));
548         return 10;
549      }
550      const char* pedFileName = stringPedFileName.Data();
551      Log(Form("\t Getting file #%d: %s from %s\n",++i,pedFileName,source->GetName()));
552      //
553      // --- Initializing pedestal calibration object
554      AliZDCPedestals *pedCalib = new AliZDCPedestals("ZDC");
555      // --- Reading file with pedestal calibration data
556      // no. ADCch = (22 signal ch. + 2 reference PMs) * 2 gain chain = 48
557      const Int_t knZDCch = 48;
558      FILE *file;
559      if((file = fopen(pedFileName,"r")) == NULL){
560        printf("Cannot open file %s \n",pedFileName);
561        return 10;
562      }
563      Log(Form("File %s connected to process pedestal data", pedFileName));
564      Float_t pedVal[(3*knZDCch)][2];
565      for(Int_t k=0; k<(3*knZDCch); k++){
566         for(Int_t j=0; j<2; j++){
567            int aleggi = fscanf(file,"%f",&pedVal[k][j]);
568            if(aleggi==0) AliDebug(3," Failing reading data from pedestal file");
569            //if(j==1) printf("pedVal[%d] -> %f, %f \n",k,pedVal[k][0],pedVal[k][1]);
570         }
571         if(k<knZDCch){
572           pedCalib->SetMeanPed(k,pedVal[k][0]);
573           pedCalib->SetMeanPedWidth(k,pedVal[k][1]);
574         }
575         else if(k>=knZDCch && k<(2*knZDCch)){
576           pedCalib->SetOOTPed(k-knZDCch,pedVal[k][0]);
577           pedCalib->SetOOTPedWidth(k-knZDCch,pedVal[k][1]);
578         }
579         else if(k>=(2*knZDCch) && k<(3*knZDCch)){
580           pedCalib->SetPedCorrCoeff(k-(2*knZDCch),pedVal[k][0],pedVal[k][1]);
581         }
582      }
583      fclose(file);
584      //pedCalib->Print("");
585      // 
586      AliCDBMetaData metaData;
587      metaData.SetBeamPeriod(0);
588      metaData.SetResponsible("Chiara Oppedisano");
589      metaData.SetComment("Filling AliZDCPedestals object");  
590      //
591      resPedCal = Store("Calib","Pedestals",pedCalib, &metaData, 0, kTRUE);
592      if(resPedCal==kFALSE) return 11;
593   }
594   delete daqSources; daqSources = 0;
595   
596   TList* daqSourceH = GetFileSources(kDAQ, "PEDESTALHISTOS");
597   if(!daqSourceH){
598     Log(Form("No source for PEDESTALHISTOS id run %d !", fRun));
599     return 12;
600   }
601   Log("\t List of DAQ sources for PEDESTALHISTOS id: "); daqSourceH->Print();
602   //
603   TIter iterH(daqSourceH);
604   TObjString* sourceH = 0;
605   Int_t iH=0;
606   while((sourceH = dynamic_cast<TObjString*> (iterH.Next()))){
607      TString stringPedFileName = GetFile(kDAQ, "PEDESTALHISTOS", sourceH->GetName());
608      if(stringPedFileName.Length() <= 0){
609         Log(Form("No PEDESTALHISTOS file from source %s!", sourceH->GetName()));
610         return 12;
611      }
612      const char* pedFileName = stringPedFileName.Data();
613      Log(Form("\t Getting file #%d: %s from %s\n",++iH, pedFileName, sourceH->GetName()));
614      resPedHist = StoreReferenceFile(pedFileName, "pedestalReference.root");
615      if(resPedHist==kFALSE) return 13;
616   }
617   delete daqSourceH; daqSourceH=0;
618   
619   return 0;
620 }
621
622 //______________________________________________________________________________________________
623 UInt_t AliZDCPreprocessor::ProcessLaserData()
624 {
625   TList* daqSources = GetFileSources(kDAQ, "LASERDATA");
626   if(!daqSources){
627     AliError(Form("No sources for STANDALONE_LASER run %d !", fRun));
628     return 14;
629   }
630   if(daqSources->GetEntries()==0) return 14;
631   Log("\t List of DAQ sources for LASERDATA id: "); daqSources->Print();
632   //
633   TIter iter2(daqSources);
634   TObjString* source = 0;
635   Int_t i=0;
636   Bool_t resLaserCal=kTRUE, resLaserHist=kTRUE;
637   
638   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
639      TString stringLaserFileName = GetFile(kDAQ, "LASERDATA", source->GetName());
640      if(stringLaserFileName.Length() <= 0){
641        Log(Form("No LASER file from source %s!", source->GetName()));
642        return 14;
643      }
644      const char* laserFileName = stringLaserFileName.Data();
645      Log(Form("\t Getting file #%d: %s from %s\n",++i,laserFileName,source->GetName()));
646      //
647      // --- Initializing pedestal calibration object
648      AliZDCLaserCalib *lCalib = new AliZDCLaserCalib("ZDC");
649      // --- Reading file with pedestal calibration data
650      if(laserFileName){
651        FILE *file;
652        if((file = fopen(laserFileName,"r")) == NULL){
653          printf("Cannot open file %s \n",laserFileName);
654          return 14;
655        }
656        Log(Form("File %s connected to process data from LASER events", laserFileName));
657        //
658        Float_t ivalRead[22][4]; 
659        for(Int_t j=0; j<22; j++){
660           for(Int_t k=0; k<4; k++){
661             int aleggi = fscanf(file,"%f",&ivalRead[j][k]);
662             if(aleggi==0) AliDebug(3," Failng reading data from laser file");
663             //printf(" %d %1.0f  ",k, ivalRead[j][k]);
664           }
665           lCalib->SetDetector(j, (Int_t) ivalRead[j][0]);
666           lCalib->SetSector(j, (Int_t) ivalRead[j][1]);
667           lCalib->SetfPMValue(j, ivalRead[j][2]);
668           lCalib->SetfPMWidth(j, ivalRead[j][3]);
669        }
670        fclose(file);
671      }
672      else{
673        Log(Form("File %s not found", laserFileName));
674        return 14;
675      }
676      //lCalib->Print("");
677      // 
678      AliCDBMetaData metaData;
679      metaData.SetBeamPeriod(0);
680      metaData.SetResponsible("Chiara Oppedisano");
681      metaData.SetComment("Filling AliZDCLaserCalib object");  
682      //
683      resLaserCal = Store("Calib","LaserCalib",lCalib, &metaData, 0, kTRUE);
684      if(resLaserCal==kFALSE) return 15;
685   }
686   delete daqSources; daqSources = 0;
687
688   TList* daqSourceH = GetFileSources(kDAQ, "LASERHISTOS");
689   if(!daqSourceH){
690     AliError(Form("No sources for STANDALONE_LASER run %d !", fRun));
691     return 16;
692   }
693   Log("\t List of DAQ sources for LASERHISTOS id: "); daqSourceH->Print();
694   //
695   TIter iter2H(daqSourceH);
696   TObjString* sourceH = 0;
697   Int_t iH=0;
698   while((sourceH = dynamic_cast<TObjString*> (iter2H.Next()))){
699      Log(Form("\t Getting file #%d\n",++iH));
700      TString stringLaserFileName = GetFile(kDAQ, "LASERHISTOS", sourceH->GetName());
701      if(stringLaserFileName.Length() <= 0){
702        Log(Form("No LASER file from source %s!", sourceH->GetName()));
703        return 16;
704      }
705      resLaserHist = StoreReferenceFile(stringLaserFileName.Data(), "laserReference.root");
706      //
707      if(resLaserHist==kFALSE) return 17;
708   }
709   delete daqSourceH; daqSourceH = 0;
710   
711   return 0;
712 }
713
714
715 //______________________________________________________________________________________________
716 UInt_t AliZDCPreprocessor::ProcessMBCalibData()
717 {
718   TList* daqSources = GetFileSources(kDAQ, "MBCALIB");
719   if(!daqSources){
720     AliError(Form("No sources for CALIBRATION_MB run %d !", fRun));
721     return 18;
722   }
723   if(daqSources->GetEntries()==0) return 18;
724   Log("\t List of DAQ sources for MBCALIB id: "); daqSources->Print();
725   //
726   TIter iter2(daqSources);
727   TObjString* source = 0;
728   Int_t i=0;
729   Bool_t resMBCal=kTRUE;
730   
731   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
732      TString stringMBFileName = GetFile(kDAQ, "MBCALIB", source->GetName());
733      if(stringMBFileName.Length() <= 0){
734        Log(Form("No MBCALIB file from source %s!", source->GetName()));
735        return 18;
736      }
737      const char* mbFileName = stringMBFileName.Data();
738      Log(Form("\t Getting file #%d: %s from %s\n",++i,mbFileName,source->GetName()));
739      //
740      // --- Initializing calibration object
741      AliZDCMBCalib *mbCalib = new AliZDCMBCalib("ZDC");
742      // --- Reading file with calibration data
743      if(mbFileName){
744        TFile * fileHistos = TFile::Open(mbFileName);
745        Log(Form("File %s connected to process data from CALIBRATION_MB events", mbFileName));
746        //
747        fileHistos->cd();
748        TH2F *hZDCvsZEM = (TH2F*)  fileHistos->Get("hZDCvsZEM");
749        TH2F *hZDCCvsZEM = (TH2F*) fileHistos->Get("hZDCCvsZEM");
750        TH2F *hZDCAvsZEM = (TH2F*) fileHistos->Get("hZDCAvsZEM");
751        //
752        mbCalib->SetZDCvsZEM(hZDCvsZEM);
753        mbCalib->SetZDCCvsZEM(hZDCCvsZEM);
754        mbCalib->SetZDCAvsZEM(hZDCAvsZEM);
755        //
756        //fileHistos->Close();
757      }
758      else{
759        Log(Form("File %s not found", mbFileName));
760        return 14;
761      }
762      // 
763      AliCDBMetaData metaData;
764      metaData.SetBeamPeriod(0);
765      metaData.SetResponsible("Chiara Oppedisano");
766      metaData.SetComment("Filling AliZDCMBCalib object");  
767      //
768      //mbCalib->Dump();
769      //
770      resMBCal = Store("Calib","MBCalib",mbCalib, &metaData, 0, kTRUE);
771        printf(" here 1000\n");
772      if(resMBCal==kFALSE) return 19;
773   }
774   delete daqSources; daqSources = 0;
775   
776   return 0;
777 }
778
779 //______________________________________________________________________________________________
780 UInt_t AliZDCPreprocessor::Process(TMap* dcsAliasMap)
781 {
782  UInt_t resDCS = 0;
783  UInt_t resChMap=0;
784  UInt_t resEnergyCalib=0, resPedestalCalib=0, resLaserCalib=0, resMBCalib=0;
785
786  // ************************* Process DCS data ****************************
787  if(ProcessDCS()) resDCS = ProcessDCSData(dcsAliasMap);
788   
789  // ********************************* From DAQ ************************************
790
791  const char* beamType = GetRunParameter("beamType");
792  TString runType = GetRunType();
793  Float_t beamEnergy = (Float_t)(((TString)GetRunParameter("beamEnergy")).Atof()); 
794  printf("\t **** AliZDCPreprocessor -> runType %s, beamType %s,  beamEnergy %1.0f ****\n",
795         runType.Data(),beamType,beamEnergy);
796
797  // ******************************************
798  // ADC channel mapping
799  // ******************************************
800  resChMap = ProcessChMap();
801  
802  // ******************************************
803  // Calibration param. for p-p data (all = 1)
804  // ******************************************
805  // NO ENERGY CALIBRATION -> coefficients set to 1.
806  // Temp -> also inter-calibration coefficients are set to 1.
807  if((strcmp(beamType,"p-p")==0) || (strcmp(beamType,"P-P")==0)) resEnergyCalib = ProcessppData();
808  
809  // *****************************************************
810  // CALIBRATION_EMD -> Energy calibration and equalization
811  // *****************************************************
812  if((strcmp(beamType,"A-A")==0) && (runType.CompareTo("CALIBRATION_EMD")==0)) 
813    resEnergyCalib =  ProcessCalibData(beamEnergy);
814  
815  // *****************************************************
816  // STANDALONE_PEDESTALS -> Pedestal subtraction 
817  // *****************************************************
818  if(runType.CompareTo("STANDALONE_PEDESTAL")==0) resPedestalCalib = ProcessPedestalData();
819  
820  // *****************************************************
821  // STANDALONE_LASER -> Signal stability and ageing 
822  // *****************************************************
823  else if(runType.CompareTo("STANDALONE_LASER")==0) resLaserCalib = ProcessLaserData();
824
825  // *****************************************************
826  // CALIBRATION_MB -> Signal stability and ageing 
827  // *****************************************************
828  else if(runType.CompareTo("CALIBRATION_MB")==0) resMBCalib = ProcessMBCalibData();
829  
830
831  if(resDCS!=0)                return resDCS;
832  else if(resChMap!=0)         return resChMap;
833  else if(resEnergyCalib!=0)   return resEnergyCalib;
834  else if(resPedestalCalib!=0) return resPedestalCalib;
835  else if(resLaserCalib!=0)    return resLaserCalib;
836  else if(resMBCalib!=0)       return resMBCalib;
837  
838  return 0;
839   
840 }