]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDCPreprocessor.cxx
Energy calibration coefficient in GeV
[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 "AliZDCCalib.h"
21
22 /////////////////////////////////////////////////////////////////////
23 //                                                                 //
24 // Class implementing ZDC pre-processor.                           //
25 // It takes data from DCS and passes it to the class AliZDCDataDCS //
26 // The class is then written to the CDB.                           //
27 //                                                                 //
28 /////////////////////////////////////////////////////////////////////
29
30 ClassImp(AliZDCPreprocessor)
31
32 //______________________________________________________________________________________________
33 AliZDCPreprocessor::AliZDCPreprocessor(AliShuttleInterface* shuttle) :
34   AliPreprocessor("ZDC", shuttle),
35   fData(0)
36 {
37   // constructor
38   AddRunType("STANDALONE_PEDESTAL");
39   AddRunType("STANDALONE_LASER");
40   AddRunType("STANDALONE_EMD");
41   AddRunType("STANDALONE_COSMIC");
42   AddRunType("STANDALONE_BC");
43   AddRunType("PHYSICS");
44 }
45
46
47 //______________________________________________________________________________________________
48 AliZDCPreprocessor::~AliZDCPreprocessor()
49 {
50   // destructor
51 }
52
53
54 //______________________________________________________________________________________________
55 void AliZDCPreprocessor::Initialize(Int_t run, UInt_t startTime,
56         UInt_t endTime)
57 {
58   // Creates AliZDCDataDCS object
59
60   AliPreprocessor::Initialize(run, startTime, endTime);
61
62         Log(Form("\n\tRun %d \n\tStartTime %s \n\tEndTime %s", run,
63                 TTimeStamp(startTime).AsString(),
64                 TTimeStamp(endTime).AsString()));
65
66         fRun = run;
67         fStartTime = startTime;
68         fEndTime = endTime;
69
70         fData = new AliZDCDataDCS(fRun, fStartTime, fEndTime);
71 }
72
73 //______________________________________________________________________________________________
74 UInt_t AliZDCPreprocessor::ProcessChMap(TString runType)
75
76   // Reading the file for mapping from FXS
77   TList* daqSource = GetFileSources(kDAQ, runType.Data());
78   if(!daqSource){
79     AliError(Form("No sources run %d for run type %s!", fRun, runType.Data()));
80     return 1;
81   }
82   Log("\t List of sources "); daqSource->Print();
83   //
84   TIter iter(daqSource);
85   TObjString* source = 0;
86   Int_t isou=0;
87   Int_t res=999;
88   Int_t readMap[48][6]; 
89   //
90   while((source = dynamic_cast<TObjString*> (iter.Next()))){
91      Log(Form("\n\t Getting file #%d\n",++isou));
92      TString fileName = "ZDCChMapping.dat";
93
94      if(fileName.Length() <= 0){
95        Log(Form("No file from source %s!", source->GetName()));
96        return 1;
97      }
98      // --- Reading file with calibration data
99      //const char* fname = fileName.Data();
100      if(fileName){
101        FILE *file;
102        if((file = fopen(fileName,"r")) == NULL){
103          printf("Cannot open file %s \n",fileName.Data());
104          return 1;
105        }
106        Log(Form("File %s connected to process data for ADC mapping", fileName.Data()));
107        //
108        for(Int_t j=0; j<48; j++){         
109            for(Int_t k=0; k<6; k++){
110              fscanf(file,"%d",&readMap[j][k]);
111            }
112        }
113        fclose(file);
114      }
115      else{
116        Log(Form("File %s not found", fileName.Data()));
117        return 1;
118      }
119   }
120   delete daqSource; daqSource=0;
121   
122   // Store the currently read map ONLY IF it is different
123   // from the entry in the OCDB
124   Bool_t updateOCDB = kFALSE;
125   
126   AliCDBEntry *cdbEntry = GetFromOCDB("Calib","ChMap");
127   if(!cdbEntry){
128     Log("\t AliZDCPreprocessor -> WARNING! No CDB entry for ch. mapping\n");
129     updateOCDB = kTRUE;
130   }
131   else{
132     AliZDCChMap *chMap = (AliZDCChMap*) cdbEntry->GetObject();
133     for(Int_t i=0; i<48; i++){
134       if(  (readMap[i][1] == chMap->GetADCModule(i)) 
135         && (readMap[i][2] == chMap->GetADCChannel(i)) 
136         && (readMap[i][4] == chMap->GetDetector(i)) 
137         && (readMap[i][5] == chMap->GetSector(i))){
138          updateOCDB = kFALSE;
139       }
140       else updateOCDB = kTRUE;
141     }
142   }
143   //
144   if(updateOCDB==kTRUE){
145     Log("\t AliZDCPreprocessor -> A new entry ZDC/Calib/ChMap will be created");
146     //
147     // --- Initializing mapping calibration object
148     AliZDCChMap *mapCalib = new AliZDCChMap("ZDC");
149     // Writing channel map in the OCDB
150     for(Int_t k=0; k<48; k++){
151       mapCalib->SetADCModule(k,readMap[k][1]);
152       mapCalib->SetADCChannel(k,readMap[k][2]);
153       mapCalib->SetDetector(k,readMap[k][4]);
154       mapCalib->SetSector(k,readMap[k][5]);
155     }
156     //mapCalib->Print("");
157     // 
158     AliCDBMetaData metaData;
159     metaData.SetBeamPeriod(0);
160     metaData.SetResponsible("Chiara Oppedisano");
161     metaData.SetComment("Filling AliZDCChMap object");  
162     //
163     res = Store("Calib","ChMap",mapCalib, &metaData, 0, 1);
164   }
165   else{
166     Log("\t AliZDCPreprocessor -> ZDC/Calib/ChMap entry in OCDB is valid and won't be updated\n");
167     res = kTRUE;
168   }
169
170   
171   return res;
172
173 }
174
175 //______________________________________________________________________________________________
176 UInt_t AliZDCPreprocessor::Process(TMap* dcsAliasMap)
177 {
178   // *************** From DCS ******************
179   // Fills data into a AliZDCDataDCS object
180   if(!dcsAliasMap) return 1;
181   printf("Processing data from DCS\n");
182   
183   // The processing of the DCS input data is forwarded to AliZDCDataDCS
184   Float_t dcsValues[28]; // DCSAliases=28
185   fData->SetCalibData(dcsValues);
186   fData->ProcessData(*dcsAliasMap);
187   // Store DCS data for reference
188   AliCDBMetaData metadata;
189   metadata.SetResponsible("Chiara Oppedisano");
190   metadata.SetComment("DCS data for ZDC");
191   Bool_t resDCSRef = kTRUE;
192   resDCSRef = StoreReferenceData("DCS","Data",fData,&metadata);
193   dcsAliasMap->Print("");
194
195   // --- Writing ZDC table positions into alignment object
196   TClonesArray *array = new TClonesArray("AliAlignObjParams",10);
197   TClonesArray &alobj = *array;
198   AliAlignObjParams a;
199   Double_t dx=0., dz=0., dpsi=0., dtheta=0., dphi=0.;
200   // Vertical table position in mm from DCS
201   Double_t dyZN1 = (Double_t) (dcsValues[0]/10.);
202   Double_t dyZP1 = (Double_t) (dcsValues[1]/10.);
203   Double_t dyZN2 = (Double_t) (dcsValues[2]/10.);
204   Double_t dyZP2 = (Double_t) (dcsValues[3]/10.);
205   //
206   
207   const char *n1ZDC="ZDC/NeutronZDC_C";  
208   const char *p1ZDC="ZDC/ProtonZDC_C";
209   const char *n2ZDC="ZDC/NeutronZDC_A";
210   const char *p2ZDC="ZDC/ProtonZDC_A";
211   //
212   UShort_t iIndex=0;
213   AliGeomManager::ELayerID iLayer = AliGeomManager::kInvalidLayer;
214   UShort_t volid = AliGeomManager::LayerToVolUID(iLayer,iIndex);
215   //
216   new(alobj[0]) AliAlignObjParams(n1ZDC, volid, dx, dyZN1, dz, dpsi, dtheta, dphi, kTRUE);
217   new(alobj[1]) AliAlignObjParams(p1ZDC, volid, dx, dyZP1, dz, dpsi, dtheta, dphi, kTRUE);
218   new(alobj[2]) AliAlignObjParams(n2ZDC, volid, dx, dyZN2, dz, dpsi, dtheta, dphi, kTRUE);
219   new(alobj[3]) AliAlignObjParams(p2ZDC, volid, dx, dyZP2, dz, dpsi, dtheta, dphi, kTRUE);
220   
221   // save in CDB storage
222   AliCDBMetaData md;
223   md.SetResponsible("Chiara Oppedisano");
224   md.SetComment("Alignment object for ZDC");
225   Bool_t resultAl = kTRUE;
226   resultAl = Store("Align","Data", array, &md, 0, 0);
227   
228 // *************** From DAQ ******************
229 Bool_t resChMap=kTRUE, resPedCal=kTRUE, resLaserCal=kTRUE, resECal=kTRUE;
230 // 
231 const char* beamType = GetRunParameter("beamType");
232 TString runType = GetRunType();
233 printf("\n\t AliZDCPreprocessor -> beamType %s\n",beamType);
234 printf("\t AliZDCPreprocessor -> runType  %s\n\n",runType.Data());
235
236 if(strcmp(beamType,"p-p")==0){
237    
238    // --- Cheking if there is already the entry in the OCDB
239    AliCDBEntry *cdbEntry = GetFromOCDB("Calib", "EMDCalib");
240    if(!cdbEntry){   
241      printf("\t AliZDCPreprocessor -> ZDC/Calib/EMDCalib entry will be created\n");
242      // --- Initializing calibration object
243      AliZDCCalib *eCalib = new AliZDCCalib("ZDC");
244      //
245      for(Int_t j=0; j<6; j++) eCalib->SetEnCalib(j,1.);
246      for(Int_t j=0; j<5; j++){  
247         eCalib->SetZN1EqualCoeff(j, 1.);
248         eCalib->SetZP1EqualCoeff(j, 1.);
249         eCalib->SetZN2EqualCoeff(j, 1.);
250         eCalib->SetZP2EqualCoeff(j, 1.);  
251      }
252      //eCalib->Print("");
253      // 
254      AliCDBMetaData metaData;
255      metaData.SetBeamPeriod(0);
256      metaData.SetResponsible("Chiara Oppedisano");
257      metaData.SetComment("AliZDCCalib object");  
258      //
259      resECal = Store("Calib","EMDCalib",eCalib, &metaData, 0, 1);
260    }
261    else{
262      printf("\t AliZDCPreprocessor -> ZDC/Calib/EMDCalib object already existing in OCDB!!!\n");
263      resECal = kTRUE;
264    }
265 }
266 // ******************************************
267 //   ZDC ADC channel mapping
268 // ******************************************
269 resChMap = ProcessChMap(runType);
270 // 
271 // *****************************************************
272 // [a] PEDESTALS -> Pedestal subtraction
273 // *****************************************************
274 // 
275 if(runType=="STANDALONE_PEDESTAL"){
276   TList* daqSources = GetFileSources(kDAQ, "PEDESTALS");
277   if(!daqSources){
278     Log(Form("No source for STANDALONE_PEDESTAL run %d !", fRun));
279     return 1;
280   }
281   Log("\t List of sources for STANDALONE_PEDESTAL");
282   daqSources->Print();
283   //
284   TIter iter(daqSources);
285   TObjString* source = 0;
286   Int_t i=0;
287   while((source = dynamic_cast<TObjString*> (iter.Next()))){
288        Log(Form("\n\t Getting file #%d\n",++i));
289        TString stringPedFileName = GetFile(kDAQ, "PEDESTALS", source->GetName());
290        if(stringPedFileName.Length() <= 0){
291           Log(Form("No PEDESTAL file from source %s!", source->GetName()));
292           return 1;
293        }
294        // --- Initializing pedestal calibration object
295        AliZDCPedestals *pedCalib = new AliZDCPedestals("ZDC");
296        // --- Reading file with pedestal calibration data
297        const char* pedFileName = stringPedFileName.Data();
298        // no. ADCch = (22 signal ch. + 2 reference PMs) * 2 gain chain = 48
299        const Int_t knZDCch = 48;
300        if(pedFileName){
301          FILE *file;
302          if((file = fopen(pedFileName,"r")) == NULL){
303            printf("Cannot open file %s \n",pedFileName);
304            return 1;
305          }
306          Log(Form("File %s connected to process pedestal data", pedFileName));
307          Float_t pedVal[(2*knZDCch)][2];
308          for(Int_t k=0; k<(2*knZDCch); k++){
309             for(Int_t j=0; j<2; j++){
310                fscanf(file,"%f",&pedVal[k][j]);
311                //if(j==1) printf("pedVal[%d] -> %f, %f \n",k,pedVal[k][0],pedVal[k][1]);
312             }
313             if(k<knZDCch){
314               pedCalib->SetMeanPed(k,pedVal[k][0]);
315               pedCalib->SetMeanPedWidth(i,pedVal[k][1]);
316             }
317             else if(k>=knZDCch && k<(2*knZDCch)){
318               pedCalib->SetOOTPed(k-knZDCch,pedVal[k][0]);
319               pedCalib->SetOOTPedWidth(k-knZDCch,pedVal[k][1]);
320             }
321             else if(k>=(2*knZDCch) && k<(3*knZDCch)){
322               pedCalib->SetPedCorrCoeff(k-(2*knZDCch),pedVal[k][0],pedVal[k][1]);
323             }
324          }
325          fclose(file);
326        }
327        else{
328           Log(Form("File %s not found", pedFileName));
329           return 1;
330        }
331        //
332       //pedCalib->Print("");
333       // 
334       AliCDBMetaData metaData;
335       metaData.SetBeamPeriod(0);
336       metaData.SetResponsible("Chiara Oppedisano");
337       metaData.SetComment("Filling AliZDCPedestals object");  
338       //
339       resPedCal = Store("Calib","Pedestals",pedCalib, &metaData, 0, 1);
340   }
341   delete daqSources; daqSources = 0;
342 }
343 // *****************************************************
344 // [b] STANDALONE_LASER EVENTS -> Signal stability
345 // *****************************************************
346 else if(runType=="STANDALONE_LASER"){
347   TList* daqSources = GetFileSources(kDAQ, "LASER");
348   if(!daqSources){
349     AliError(Form("No sources for STANDALONE_LASER run %d !", fRun));
350     return 1;
351   }
352   Log("\t List of sources for STANDALONE_LASER");
353   daqSources->Print();
354   //
355   TIter iter2(daqSources);
356   TObjString* source = 0;
357   Int_t i=0;
358   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
359        Log(Form("\n\t Getting file #%d\n",++i));
360        TString stringLASERFileName = GetFile(kDAQ, "LASER", source->GetName());
361        if(stringLASERFileName.Length() <= 0){
362          Log(Form("No LASER file from source %s!", source->GetName()));
363          return 1;
364        }
365        // --- Initializing pedestal calibration object
366        AliZDCLaserCalib *lCalib = new AliZDCLaserCalib("ZDC");
367        // --- Reading file with pedestal calibration data
368        const char* laserFileName = stringLASERFileName.Data();
369        if(laserFileName){
370          FILE *file;
371          if((file = fopen(laserFileName,"r")) == NULL){
372            printf("Cannot open file %s \n",laserFileName);
373            return 1;
374          }
375          Log(Form("File %s connected to process data from LASER events", laserFileName));
376          //
377          Float_t ivalRead[22][4]; 
378          for(Int_t j=0; j<22; j++){
379             for(Int_t k=0; k<4; k++){
380               fscanf(file,"%f",&ivalRead[j][k]);
381               //printf(" %d %1.0f  ",k, ivalRead[j][k]);
382             }
383             lCalib->SetDetector(j, (Int_t) ivalRead[j][0]);
384             lCalib->SetSector(j, (Int_t) ivalRead[j][1]);
385             lCalib->SetfPMValue(j, ivalRead[j][2]);
386             lCalib->SetfPMWidth(j, ivalRead[j][3]);
387          }
388          fclose(file);
389        }
390        else{
391          Log(Form("File %s not found", laserFileName));
392          return 1;
393        }
394        //lCalib->Print("");
395        // 
396        AliCDBMetaData metaData;
397        metaData.SetBeamPeriod(0);
398        metaData.SetResponsible("Chiara Oppedisano");
399        metaData.SetComment("Filling AliZDCLaserCalib object");  
400        //
401        resLaserCal = Store("Calib","LaserCalib",lCalib, &metaData, 0, 1);
402   }
403   
404 }
405 // *****************************************************
406 // [c] EMD EVENTS -> Energy calibration and equalization
407 // *****************************************************
408 else if(runType=="STANDALONE_EMD" && strcmp(beamType,"Pb-Pb")==0){
409   TList* daqSources = GetFileSources(kDAQ, "EMDCALIB");
410   if(!daqSources){
411     AliError(Form("No sources for STANDALONE_EMD run %d !", fRun));
412     return 1;
413   }
414   Log("\t List of sources for STANDALONE_EMD");
415   daqSources->Print();
416   //
417   TIter iter2(daqSources);
418   TObjString* source = 0;
419   Int_t i=0;
420   while((source = dynamic_cast<TObjString*> (iter2.Next()))){
421       Log(Form("\n\t Getting file #%d\n",++i));
422       TString stringEMDFileName = GetFile(kDAQ, "EMDCALIB", source->GetName());
423       if(stringEMDFileName.Length() <= 0){
424         Log(Form("No EMDCALIB file from source %s!", source->GetName()));
425         return 1;
426       }
427       // --- Initializing pedestal calibration object
428       AliZDCCalib *eCalib = new AliZDCCalib("ZDC");
429       // --- Reading file with pedestal calibration data
430       const char* emdFileName = stringEMDFileName.Data();
431       if(emdFileName){
432         FILE *file;
433         if((file = fopen(emdFileName,"r")) == NULL){
434           printf("Cannot open file %s \n",emdFileName);
435           return 1;
436         }
437         Log(Form("File %s connected to process data from EM dissociation events", emdFileName));
438         //
439         Float_t fitValEMD[6]; Float_t equalCoeff[4][5];
440         Float_t calibVal[4];
441         for(Int_t j=0; j<10; j++){         
442           if(j<6){
443             fscanf(file,"%f",&fitValEMD[j]);
444             if(j<4){
445               calibVal[j] = 2760./fitValEMD[j];
446               eCalib->SetEnCalib(j,calibVal[j]);
447             }
448             else eCalib->SetEnCalib(j,fitValEMD[j]);
449           }
450           else{
451             for(Int_t k=0; k<5; k++){
452                fscanf(file,"%f",&equalCoeff[j][k]);
453                if(j==6)      eCalib->SetZN1EqualCoeff(k, equalCoeff[j][k]);
454                else if(j==7) eCalib->SetZP1EqualCoeff(k, equalCoeff[j][k]);
455                else if(j==8) eCalib->SetZN2EqualCoeff(k, equalCoeff[j][k]);
456                else if(j==9) eCalib->SetZP2EqualCoeff(k, equalCoeff[j][k]);  
457             }
458           }
459         }
460         fclose(file);
461       }
462       else{
463         Log(Form("File %s not found", emdFileName));
464         return 1;
465       }
466       //eCalib->Print("");
467       // 
468       AliCDBMetaData metaData;
469       metaData.SetBeamPeriod(0);
470       metaData.SetResponsible("Chiara Oppedisano");
471       metaData.SetComment("Filling AliZDCCalib object");  
472       //
473       resECal = Store("Calib","EMDCalib",eCalib, &metaData, 0, 1);
474   }
475 }
476
477
478   // note that the parameters are returned as character strings!
479   const char* nEvents = GetRunParameter("totalEvents");
480   if(nEvents) Log(Form("Number of events for run %d: %s",fRun, nEvents));
481   else Log(Form("Number of events not put in logbook!"));
482  
483   UInt_t result = 0;
484   if(resDCSRef==kFALSE || resultAl==kFALSE || resPedCal==kFALSE ||
485      resLaserCal==kFALSE || resECal==kFALSE || resChMap==kFALSE){
486     if(resDCSRef == kFALSE)        result = 1;
487     else if(resultAl == kFALSE)    result = 2;
488     else if(resChMap == kFALSE)    result = 3;
489     else if(resPedCal == kFALSE)   result = 4;
490     else if(resLaserCal == kFALSE) result = 5;
491     else if(resECal == kFALSE)     result = 6;
492   }
493   
494   return result;
495   
496 }