ROOT logo
/* $Id$ */

// Script to create calibration parameters and store them into CDB
// Two sets of calibration parameters can be created:
// 1) equal parameters
// 2) randomly distributed parameters for decalibrated detector silumations

#if !defined(__CINT__)
#include "TControlBar.h"
#include "TString.h"
#include "TRandom.h"
#include "TH2F.h"
#include "TCanvas.h"

#include "AliRun.h"
#include "AliPHOSCalibData.h"
#include "AliCDBMetaData.h"
#include "AliCDBId.h"
#include "AliCDBEntry.h"
#include "AliCDBManager.h"
#include "AliCDBStorage.h"
#endif

static const Int_t nMod =  5;
static const Int_t nCol = 56;
static const Int_t nRow = 64;

void AliPHOSSetCDB()
{
  TControlBar *menu = new TControlBar("vertical","PHOS CDB");
  menu->AddButton("Help to run PHOS CDB","Help()",
		  "Explains how to use PHOS CDB menus");
  menu->AddButton("Create ideal calibration","SetCC(0)",
		  "Set equal CC");
  menu->AddButton("Create full decalibration","SetCC(1)",
		  "Set random decalibration CC");
  menu->AddButton("Create residual decalibration","SetCC(2)",
		  "Set residual decalibration calibration coefficients");

  menu->AddButton("Read ideal calibration","GetCC(0)",
		  "Read equal calibration coefficients");
  menu->AddButton("Read full decalibration","GetCC(1)",
		  "Read random decalibration calibration coefficients");
  menu->AddButton("Read residual calibration","GetCC(2)",
		  "Read residial calibration coefficients");
  menu->AddButton("Exit","gApplication->Terminate(0)","Quit aliroot session");
  menu->Show();
}

//-----------------------------------------------------------------------
void Help()
{
  TString string="\nSet calibration parameters and write them into ALICE OCDB:";
  string += "\n\tPress button \"Equal CC\" to create equal calibration coefficients;";
  string += "\n\tPress button \"Full decalibration\" to create \n\t random calibration coefficients with 20\% spread \n\t to imitate fully decalibrated detector;";
  string += "\n\tPress button \"Residual decalibration\" to create \n\t random calibration coefficients with +-2\% spread\n\t to imitate decalibrated detector after initial calibration.\n";
  printf("%s",string.Data());
}

//------------------------------------------------------------------------
void SetCC(Int_t flag=0)
{
  // Writing calibration coefficients into the Calibration DB
  // Arguments:
  //   flag=0: all calibration coefficients are equal
  //   flag=1: decalibration coefficients
  //   flag=2: calibration coefficients equal to inverse decalibration ones
  // Author: Boris Polishchuk (Boris.Polichtchouk at cern.ch)

  TString DBFolder;
  Int_t firstRun   =  0;
  Int_t lastRun    =  0;
  Int_t beamPeriod =  1;
  char* objFormat  = "";

  AliPHOSCalibData* cdb = 0;

  if      (flag == 0) {
    // Ideal calibration with all channels at nominal value 0.005
    DBFolder  ="local://InitCalibDB";
    firstRun  =  0;
    lastRun   =  999999;
    objFormat = "PHOS ideal pedestals and ADC gain factors (5x64x56)";
    cdb = new AliPHOSCalibData();
    cdb->CreateNew();
  }

  else if (flag == 1) {
    // Full decalibration is +-10% of the nominal value
    DBFolder  ="local://FullDecalibDB";
    firstRun  =  0;
    lastRun   =  999999;
    objFormat = "PHOS fully decalibrated calibration coefficients (5x64x56)";
 
    cdb = new AliPHOSCalibData();    
    cdb->RandomEmc(0.045,0.055);
    cdb->RandomCpv(0.0008,0.0016);
  }
  
  else if (flag == 2) {
    // Residual decalibration is +-1% of the nominal value
    DBFolder  ="local://ResidualCalibDB";
    firstRun  =  0;
    lastRun   =  999999;
    objFormat = "PHOS residual calibration coefficients (5x64x56)";
    
    cdb = new AliPHOSCalibData();    
    cdb->RandomEmc(0.00495,0.00505);
    cdb->RandomCpv(0.00115,0.00125);
  }
  
  //Store calibration data into database
  
  AliCDBMetaData md;
  md.SetComment(objFormat);
  md.SetBeamPeriod(beamPeriod);
  md.SetResponsible("Boris Polichtchouk");
  
  AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
  if(gSystem->Getenv("STORAGE")){
    cout << "Setting specific storage" << endl;
    AliCDBManager::Instance()->SetSpecificStorage("PHOS/*",DBFolder.Data());
  }

  cdb->WriteEmc(firstRun,lastRun,&md);
  cdb->WriteCpv(firstRun,lastRun,&md);
  cdb->WriteEmcBadChannelsMap(firstRun,lastRun,&md);

}

//------------------------------------------------------------------------
void GetCC(Int_t flag=0)
{
  // Read calibration coefficients into the Calibration DB
  // Arguments:
  //   flag=0: all calibration coefficients are equal
  //   flag=1: decalibration coefficients
  //   flag=2: calibration coefficients equal to inverse decalibration ones
  // Author: Yuri.Kharlov at cern.ch

  gStyle->SetPalette(1);
  gStyle->SetOptStat(0);
  TString DBFolder;
  Int_t runNumber;

  if      (flag == 0) {
    DBFolder  ="local://InitCalibDB";
    runNumber = 0;
  }
  else if (flag == 1) {
    DBFolder  ="local://FullDecalibDB";
    runNumber = 0;
  }
  else if (flag == 2) {
    DBFolder  ="local://ResidualCalibDB";
    runNumber = 0;
  }

  AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT/OCDB");
  if(gSystem->Getenv("STORAGE")){
    cout << "Setting specific storage" << endl;
    AliCDBManager::Instance()->SetSpecificStorage("PHOS/*",DBFolder.Data());
  }

  AliPHOSCalibData* clb  = new AliPHOSCalibData(runNumber);

  TH2::AddDirectory(kFALSE);

  TH2F* hGain[5];

  TCanvas *cGain = new TCanvas("cGain","PHOS EMC Gain factors",10,10,700,500);
  cGain->Divide(3,2);

  for (Int_t module=1; module<=nMod; module++) {
    TString nameGain="hGain";
    nameGain+=module;
    TString titleGain="Gain factors in module ";
    titleGain+=module;
    hGain[module-1] = new TH2F(nameGain.Data(),titleGain.Data(),
			    nCol,1.,1.*nCol,nRow,1.,1.*nRow);

    for (Int_t column=1; column<=nCol; column++) {
      for (Int_t row=1; row<=nRow; row++) {
        Float_t gain = clb->GetADCchannelEmc (module,column,row);
	hGain[module-1]->SetBinContent(column,row,gain);
      }
    }
    cGain->cd(module);
    hGain[module-1]->SetMinimum(0);
    hGain[module-1]->Draw("colz");
  }
  cGain->Print("gains.eps");
}
 AliPHOSSetCDB.C:1
 AliPHOSSetCDB.C:2
 AliPHOSSetCDB.C:3
 AliPHOSSetCDB.C:4
 AliPHOSSetCDB.C:5
 AliPHOSSetCDB.C:6
 AliPHOSSetCDB.C:7
 AliPHOSSetCDB.C:8
 AliPHOSSetCDB.C:9
 AliPHOSSetCDB.C:10
 AliPHOSSetCDB.C:11
 AliPHOSSetCDB.C:12
 AliPHOSSetCDB.C:13
 AliPHOSSetCDB.C:14
 AliPHOSSetCDB.C:15
 AliPHOSSetCDB.C:16
 AliPHOSSetCDB.C:17
 AliPHOSSetCDB.C:18
 AliPHOSSetCDB.C:19
 AliPHOSSetCDB.C:20
 AliPHOSSetCDB.C:21
 AliPHOSSetCDB.C:22
 AliPHOSSetCDB.C:23
 AliPHOSSetCDB.C:24
 AliPHOSSetCDB.C:25
 AliPHOSSetCDB.C:26
 AliPHOSSetCDB.C:27
 AliPHOSSetCDB.C:28
 AliPHOSSetCDB.C:29
 AliPHOSSetCDB.C:30
 AliPHOSSetCDB.C:31
 AliPHOSSetCDB.C:32
 AliPHOSSetCDB.C:33
 AliPHOSSetCDB.C:34
 AliPHOSSetCDB.C:35
 AliPHOSSetCDB.C:36
 AliPHOSSetCDB.C:37
 AliPHOSSetCDB.C:38
 AliPHOSSetCDB.C:39
 AliPHOSSetCDB.C:40
 AliPHOSSetCDB.C:41
 AliPHOSSetCDB.C:42
 AliPHOSSetCDB.C:43
 AliPHOSSetCDB.C:44
 AliPHOSSetCDB.C:45
 AliPHOSSetCDB.C:46
 AliPHOSSetCDB.C:47
 AliPHOSSetCDB.C:48
 AliPHOSSetCDB.C:49
 AliPHOSSetCDB.C:50
 AliPHOSSetCDB.C:51
 AliPHOSSetCDB.C:52
 AliPHOSSetCDB.C:53
 AliPHOSSetCDB.C:54
 AliPHOSSetCDB.C:55
 AliPHOSSetCDB.C:56
 AliPHOSSetCDB.C:57
 AliPHOSSetCDB.C:58
 AliPHOSSetCDB.C:59
 AliPHOSSetCDB.C:60
 AliPHOSSetCDB.C:61
 AliPHOSSetCDB.C:62
 AliPHOSSetCDB.C:63
 AliPHOSSetCDB.C:64
 AliPHOSSetCDB.C:65
 AliPHOSSetCDB.C:66
 AliPHOSSetCDB.C:67
 AliPHOSSetCDB.C:68
 AliPHOSSetCDB.C:69
 AliPHOSSetCDB.C:70
 AliPHOSSetCDB.C:71
 AliPHOSSetCDB.C:72
 AliPHOSSetCDB.C:73
 AliPHOSSetCDB.C:74
 AliPHOSSetCDB.C:75
 AliPHOSSetCDB.C:76
 AliPHOSSetCDB.C:77
 AliPHOSSetCDB.C:78
 AliPHOSSetCDB.C:79
 AliPHOSSetCDB.C:80
 AliPHOSSetCDB.C:81
 AliPHOSSetCDB.C:82
 AliPHOSSetCDB.C:83
 AliPHOSSetCDB.C:84
 AliPHOSSetCDB.C:85
 AliPHOSSetCDB.C:86
 AliPHOSSetCDB.C:87
 AliPHOSSetCDB.C:88
 AliPHOSSetCDB.C:89
 AliPHOSSetCDB.C:90
 AliPHOSSetCDB.C:91
 AliPHOSSetCDB.C:92
 AliPHOSSetCDB.C:93
 AliPHOSSetCDB.C:94
 AliPHOSSetCDB.C:95
 AliPHOSSetCDB.C:96
 AliPHOSSetCDB.C:97
 AliPHOSSetCDB.C:98
 AliPHOSSetCDB.C:99
 AliPHOSSetCDB.C:100
 AliPHOSSetCDB.C:101
 AliPHOSSetCDB.C:102
 AliPHOSSetCDB.C:103
 AliPHOSSetCDB.C:104
 AliPHOSSetCDB.C:105
 AliPHOSSetCDB.C:106
 AliPHOSSetCDB.C:107
 AliPHOSSetCDB.C:108
 AliPHOSSetCDB.C:109
 AliPHOSSetCDB.C:110
 AliPHOSSetCDB.C:111
 AliPHOSSetCDB.C:112
 AliPHOSSetCDB.C:113
 AliPHOSSetCDB.C:114
 AliPHOSSetCDB.C:115
 AliPHOSSetCDB.C:116
 AliPHOSSetCDB.C:117
 AliPHOSSetCDB.C:118
 AliPHOSSetCDB.C:119
 AliPHOSSetCDB.C:120
 AliPHOSSetCDB.C:121
 AliPHOSSetCDB.C:122
 AliPHOSSetCDB.C:123
 AliPHOSSetCDB.C:124
 AliPHOSSetCDB.C:125
 AliPHOSSetCDB.C:126
 AliPHOSSetCDB.C:127
 AliPHOSSetCDB.C:128
 AliPHOSSetCDB.C:129
 AliPHOSSetCDB.C:130
 AliPHOSSetCDB.C:131
 AliPHOSSetCDB.C:132
 AliPHOSSetCDB.C:133
 AliPHOSSetCDB.C:134
 AliPHOSSetCDB.C:135
 AliPHOSSetCDB.C:136
 AliPHOSSetCDB.C:137
 AliPHOSSetCDB.C:138
 AliPHOSSetCDB.C:139
 AliPHOSSetCDB.C:140
 AliPHOSSetCDB.C:141
 AliPHOSSetCDB.C:142
 AliPHOSSetCDB.C:143
 AliPHOSSetCDB.C:144
 AliPHOSSetCDB.C:145
 AliPHOSSetCDB.C:146
 AliPHOSSetCDB.C:147
 AliPHOSSetCDB.C:148
 AliPHOSSetCDB.C:149
 AliPHOSSetCDB.C:150
 AliPHOSSetCDB.C:151
 AliPHOSSetCDB.C:152
 AliPHOSSetCDB.C:153
 AliPHOSSetCDB.C:154
 AliPHOSSetCDB.C:155
 AliPHOSSetCDB.C:156
 AliPHOSSetCDB.C:157
 AliPHOSSetCDB.C:158
 AliPHOSSetCDB.C:159
 AliPHOSSetCDB.C:160
 AliPHOSSetCDB.C:161
 AliPHOSSetCDB.C:162
 AliPHOSSetCDB.C:163
 AliPHOSSetCDB.C:164
 AliPHOSSetCDB.C:165
 AliPHOSSetCDB.C:166
 AliPHOSSetCDB.C:167
 AliPHOSSetCDB.C:168
 AliPHOSSetCDB.C:169
 AliPHOSSetCDB.C:170
 AliPHOSSetCDB.C:171
 AliPHOSSetCDB.C:172
 AliPHOSSetCDB.C:173
 AliPHOSSetCDB.C:174
 AliPHOSSetCDB.C:175
 AliPHOSSetCDB.C:176
 AliPHOSSetCDB.C:177
 AliPHOSSetCDB.C:178
 AliPHOSSetCDB.C:179
 AliPHOSSetCDB.C:180
 AliPHOSSetCDB.C:181
 AliPHOSSetCDB.C:182
 AliPHOSSetCDB.C:183
 AliPHOSSetCDB.C:184
 AliPHOSSetCDB.C:185
 AliPHOSSetCDB.C:186
 AliPHOSSetCDB.C:187
 AliPHOSSetCDB.C:188
 AliPHOSSetCDB.C:189
 AliPHOSSetCDB.C:190
 AliPHOSSetCDB.C:191
 AliPHOSSetCDB.C:192
 AliPHOSSetCDB.C:193
 AliPHOSSetCDB.C:194