ROOT logo
// $Id$
/**
 * @file makeGRPObject.C
 * @brief Creation of the GRP configuration object for HLT 
 *
 * <pre>
 * Usage: aliroot -b -q -l makeGRPObject.C'("arguments", "uri", \
 *                                          rangemin, rangemax)'
 * 
 * 
 * Usage: aliroot -b -q -l makeGRPObject.C'(l3Current, l3Polarity,         \
 *                                          dipoleCurrent, dipolePolarity, \
 *                                          "uri", rangemin, rangemax)'
 * </pre>
 *
 * Create the GRP OCDB object for the magnetic field settings.
 * The magnetic field in AliRoot is controlled by the AliMagF class and
 * the TGeoGlobalMagField container. AliMagF contains the field maps
 * for both the L3 and dipole magnets.
 * 
 * The following conventions for magnetic field polarity are known:
 * 1) kConvMap2005: used for the field mapping in 2005
 *    positive L3  current -> negative Bz
 *    positive Dip current -> positive Bx 
 * 2) kConvMapDCS2008: defined by the microswitches/cabling of power
 *                     converters as of 2008 - 1st half 2009
 *    positive L3  current -> positive Bz
 *    positive Dip current -> positive Bx
 * 3) kConvLHC : defined by LHC
 *    positive L3  current -> positive Bz
 *    positive Dip current -> negative Bx
 * 
 * For current data taking, only the last convention is relevant.
 *
 * Negative polarity corresponds to the value '1' in the GRP, while
 * positive to '0'.
 *
 * Parameters: <br>
 *      arguments      off, default, l3=current, l3=off,
 *                     dipole=current, dipole=off
 *      uri   (opt)    the OCDB URI, default $ALICE_ROOT/OCDB
 *      rangemin (opt) default 0
 *      rangemax (opt) default 999999999
 *
 * @author Matthias.Richter@ift.uib.no
 * @ingroup alihlt_tutorial
 */
void makeGRPObject(float l3Current,
		   int   l3Polarity,
		   float dipoleCurrent,
		   int   dipolePolarity,
		   const char* cdbUri=NULL,
		   int runmin=0,
		   int runmax=999999999,
		   bool bVerbose=true)
{
  AliCDBManager* man = AliCDBManager::Instance();
  if (!man) {
    cerr << "can not get AliCDBManager" << end;
    exit;
  }
  TString storage;
  if (!man->IsDefaultStorageSet()) {
    if (cdbUri) {
      storage=cdbUri;
      if (storage.Contains("://")==0) {
	storage="local://"; storage+=cdbUri;
      }
    } else {
      storage="local://$ALICE_ROOT/OCDB";
    }
    man->SetDefaultStorage(storage);
  } else {
    storage = man->GetDefaultStorage()->GetURI();
  }

  // generate GRP object
  AliGRPObject* grpObj=new AliGRPObject;
  float cmsEnergy=14000;
  grpObj->SetBeamEnergy(cmsEnergy/0.120); // LHC convention
  grpObj->SetBeamType("p-p");
  grpObj->SetL3Current(l3Current,(AliGRPObject::Stats)0);
  grpObj->SetDipoleCurrent(dipoleCurrent,(AliGRPObject::Stats)0);  
  grpObj->SetL3Polarity(l3Polarity);  
  grpObj->SetDipolePolarity(dipolePolarity);
  grpObj->SetPolarityConventionLHC();                    // LHC convention +/+ current -> -/- field main components

  if (bVerbose) {
    AliGRPManager grpman;
    grpman.SetGRPEntry(grpObj);
    grpman.SetMagField();
  }

  // write object to OCDB
  AliCDBPath cdbPath("GRP/GRP/Data");
  AliCDBId cdbId(cdbPath, runmin, runmax);
  AliCDBMetaData cdbMetaData;
  cdbMetaData.SetResponsible("ALICE HLT");
  cdbMetaData.SetComment("Automatically produced GRP entry (AliHLTSimulation) for the magnetic field initialization of HLT components");
  man->Put(grpObj, cdbId, &cdbMetaData);
}

void makeGRPObject(const char* arguments="", 
		   const char* cdbUri=NULL,
		   int runmin=0,
		   int runmax=999999999)

{
  TString args=arguments;
  bool bHelp=args.IsNull();
  TObjArray* pTokens=args.Tokenize(" ");
  float l3Current=0;
  int l3Polarity=0;
  float dipoleCurrent=0;
  int dipolePolarity=0;
  if (pTokens) {
    for (int i=0; i<pTokens->GetEntriesFast(); i++) {
      TString arg=((TObjString*)pTokens->At(i))->GetString();
      if (arg.CompareTo("off", TString::kIgnoreCase)==0) {
	  l3Current=0;
	  l3Polarity=0;
	  dipoleCurrent=0;
	  dipolePolarity=0;
      } else if (arg.CompareTo("default", TString::kIgnoreCase)==0) {
	  l3Current=30000;
	  l3Polarity=0;  // positive for positive field
	  dipoleCurrent=6000;
	  dipolePolarity=1; // negative for positive field
      } else if (arg.BeginsWith("l3=")) {
	arg.ReplaceAll("l3=", "");
	if (arg.CompareTo("off", TString::kIgnoreCase)==0) {
	  l3Current=0;
	  l3Polarity=0;
	} else if (arg.IsFloat()) {
	  l3Current=arg.Atof();
	  l3Polarity=l3Current<0 ? 1:0;
	  l3Current=TMath::Abs(l3Current);
	} else {
	  cerr << "Invalid parameter for key 'l3=', allowed is 'off' or current" << endl;
	}
	
      } else if (arg.BeginsWith("dipole=")) {
	arg.ReplaceAll("dipole=", "");
	if (arg.CompareTo("off", TString::kIgnoreCase)==0) {
	  dipoleCurrent=0;
	  dipolePolarity=0;
	} else if (arg.IsFloat()) {
	  dipoleCurrent=arg.Atof();
	  dipolePolarity=dipoleCurrent<0 ? 1:0;
	  dipoleCurrent=TMath::Abs(dipoleCurrent);
	} else {
	  cerr << "Invalid parameter for key 'dipole=', allowed is 'off' or current" << endl;
	}
      } else {
	if (arg.CompareTo("--help", TString::kIgnoreCase) &&
	    arg.CompareTo("-h", TString::kIgnoreCase)) {
	  cerr << "Unknown argument: " << arg << endl;
	}
	bHelp=true;
      }
    }
    delete pTokens;
  }

  if (!bHelp) {
    makeGRPObject(l3Current, l3Polarity, dipoleCurrent, dipolePolarity, cdbUri, runmin, runmax);
  } else {
    cout << "========================================================================" << endl;
    cout << "usage: aliroot -b -q -l makeGRPObject.C'(\"arguments\", \"uri\", rangemin, rangemax)'" << endl << endl;
    cout << "  arguments      off, default, l3=<current>, l3=off," << endl;
    cout << "                 dipole=<current>, dipole=off" << endl;
    cout << "  uri   (opt)    the OCDB URI, default $ALICE_ROOT/OCDB   " << endl;
    cout << "  rangemin (opt) default 0" << endl;
    cout << "  rangemax (opt) default 999999999" << endl << endl;
    cout << "alternative usage: aliroot -b -q -l makeGRPObject.C'(l3Current, l3Polarity, \\" << endl;
    cout << "                                         dipoleCurrent, dipolePolarity, \\" << endl;
    cout << "                                         \"uri\", rangemin, rangemax)'" << endl << endl;
    cout << "========================================================================" << endl;
  }
}
 makeGRPObject.C:1
 makeGRPObject.C:2
 makeGRPObject.C:3
 makeGRPObject.C:4
 makeGRPObject.C:5
 makeGRPObject.C:6
 makeGRPObject.C:7
 makeGRPObject.C:8
 makeGRPObject.C:9
 makeGRPObject.C:10
 makeGRPObject.C:11
 makeGRPObject.C:12
 makeGRPObject.C:13
 makeGRPObject.C:14
 makeGRPObject.C:15
 makeGRPObject.C:16
 makeGRPObject.C:17
 makeGRPObject.C:18
 makeGRPObject.C:19
 makeGRPObject.C:20
 makeGRPObject.C:21
 makeGRPObject.C:22
 makeGRPObject.C:23
 makeGRPObject.C:24
 makeGRPObject.C:25
 makeGRPObject.C:26
 makeGRPObject.C:27
 makeGRPObject.C:28
 makeGRPObject.C:29
 makeGRPObject.C:30
 makeGRPObject.C:31
 makeGRPObject.C:32
 makeGRPObject.C:33
 makeGRPObject.C:34
 makeGRPObject.C:35
 makeGRPObject.C:36
 makeGRPObject.C:37
 makeGRPObject.C:38
 makeGRPObject.C:39
 makeGRPObject.C:40
 makeGRPObject.C:41
 makeGRPObject.C:42
 makeGRPObject.C:43
 makeGRPObject.C:44
 makeGRPObject.C:45
 makeGRPObject.C:46
 makeGRPObject.C:47
 makeGRPObject.C:48
 makeGRPObject.C:49
 makeGRPObject.C:50
 makeGRPObject.C:51
 makeGRPObject.C:52
 makeGRPObject.C:53
 makeGRPObject.C:54
 makeGRPObject.C:55
 makeGRPObject.C:56
 makeGRPObject.C:57
 makeGRPObject.C:58
 makeGRPObject.C:59
 makeGRPObject.C:60
 makeGRPObject.C:61
 makeGRPObject.C:62
 makeGRPObject.C:63
 makeGRPObject.C:64
 makeGRPObject.C:65
 makeGRPObject.C:66
 makeGRPObject.C:67
 makeGRPObject.C:68
 makeGRPObject.C:69
 makeGRPObject.C:70
 makeGRPObject.C:71
 makeGRPObject.C:72
 makeGRPObject.C:73
 makeGRPObject.C:74
 makeGRPObject.C:75
 makeGRPObject.C:76
 makeGRPObject.C:77
 makeGRPObject.C:78
 makeGRPObject.C:79
 makeGRPObject.C:80
 makeGRPObject.C:81
 makeGRPObject.C:82
 makeGRPObject.C:83
 makeGRPObject.C:84
 makeGRPObject.C:85
 makeGRPObject.C:86
 makeGRPObject.C:87
 makeGRPObject.C:88
 makeGRPObject.C:89
 makeGRPObject.C:90
 makeGRPObject.C:91
 makeGRPObject.C:92
 makeGRPObject.C:93
 makeGRPObject.C:94
 makeGRPObject.C:95
 makeGRPObject.C:96
 makeGRPObject.C:97
 makeGRPObject.C:98
 makeGRPObject.C:99
 makeGRPObject.C:100
 makeGRPObject.C:101
 makeGRPObject.C:102
 makeGRPObject.C:103
 makeGRPObject.C:104
 makeGRPObject.C:105
 makeGRPObject.C:106
 makeGRPObject.C:107
 makeGRPObject.C:108
 makeGRPObject.C:109
 makeGRPObject.C:110
 makeGRPObject.C:111
 makeGRPObject.C:112
 makeGRPObject.C:113
 makeGRPObject.C:114
 makeGRPObject.C:115
 makeGRPObject.C:116
 makeGRPObject.C:117
 makeGRPObject.C:118
 makeGRPObject.C:119
 makeGRPObject.C:120
 makeGRPObject.C:121
 makeGRPObject.C:122
 makeGRPObject.C:123
 makeGRPObject.C:124
 makeGRPObject.C:125
 makeGRPObject.C:126
 makeGRPObject.C:127
 makeGRPObject.C:128
 makeGRPObject.C:129
 makeGRPObject.C:130
 makeGRPObject.C:131
 makeGRPObject.C:132
 makeGRPObject.C:133
 makeGRPObject.C:134
 makeGRPObject.C:135
 makeGRPObject.C:136
 makeGRPObject.C:137
 makeGRPObject.C:138
 makeGRPObject.C:139
 makeGRPObject.C:140
 makeGRPObject.C:141
 makeGRPObject.C:142
 makeGRPObject.C:143
 makeGRPObject.C:144
 makeGRPObject.C:145
 makeGRPObject.C:146
 makeGRPObject.C:147
 makeGRPObject.C:148
 makeGRPObject.C:149
 makeGRPObject.C:150
 makeGRPObject.C:151
 makeGRPObject.C:152
 makeGRPObject.C:153
 makeGRPObject.C:154
 makeGRPObject.C:155
 makeGRPObject.C:156
 makeGRPObject.C:157
 makeGRPObject.C:158
 makeGRPObject.C:159
 makeGRPObject.C:160
 makeGRPObject.C:161
 makeGRPObject.C:162
 makeGRPObject.C:163
 makeGRPObject.C:164
 makeGRPObject.C:165
 makeGRPObject.C:166
 makeGRPObject.C:167
 makeGRPObject.C:168
 makeGRPObject.C:169
 makeGRPObject.C:170
 makeGRPObject.C:171
 makeGRPObject.C:172
 makeGRPObject.C:173
 makeGRPObject.C:174
 makeGRPObject.C:175
 makeGRPObject.C:176
 makeGRPObject.C:177
 makeGRPObject.C:178
 makeGRPObject.C:179
 makeGRPObject.C:180
 makeGRPObject.C:181