ROOT logo
/**
 * @file   Tag.C
 * @author Christian Holm Christensen <cholm@nbi.dk>
 * @date   Wed Oct 15 13:28:47 2014
 * 
 * @brief  Update tags 
 */
//____________________________________________________________________
/** 
 * Tag files 
 * 
 */
void Tag() 
{
  const char* turl = gSystem->Getenv("ALIEN_JDL_OUTPUTDIR");

  gSystem->Load("libNet.so");
  //  gSystem->Load("libMonaLisa.so");
  //  new TMonaLisaWriter(0, "GridAliRoot-tag.C", 0, 0, "global");
  
  TString fESDFileName = "alien://";
  fESDFileName += turl;
  fESDFileName += "/AliESDs.root";  

  TString fGUID = 0;
  GetGUID(fGUID);

  // gEnv->Print();

  TString fAliroot, fRoot, fGeant;
  GetVersions(fAliroot,fRoot,fGeant);

  TString fPeriod, fPass, fName;
  GetProductionInfo(fPeriod, fPass, fName);

  UpdateTag(fAliroot,fRoot,fGeant,fESDFileName,fGUID,fPeriod,fPass,fName);
}

//____________________________________________________________________
/** 
 * Extract production information from path 
 * 
 * @param fPeriod  On return, the period
 * @param fPass    On return, the possible pass number 
 * @param fName    On return, the full name 
 */
void GetProductionInfo(TString &fPeriod, TString &fPass, TString &fName) 
{
  const char* turl = gSystem->Getenv("ALIEN_JDL_OUTPUTDIR");
  
  TString fS = turl;
  TObjArray *fDirs = fS.Tokenize("/");
  
  for (int iter=0; iter<fDirs->GetEntries(); iter++) {
    TString fDir = ((TObjString *) fDirs->At(iter))->String();

    if (fDir.Contains("LHC")) fPeriod = fDir;
    if (fDir.Contains("pass")) fPass = fDir;
  }
  fName = fPeriod+"."+fPass;
}
  
//____________________________________________________________________
/** 
 * Get the AliROOT, ROOT, and GEANT3 versions from the JDL packages
 * 
 * @param fAliroot On return, the AliROOT version 
 * @param froot    On return, the ROOT version 
 * @param fgeant   On return, the GEANT3 version 
 */
void GetVersions(TString &fAliroot, TString &froot, TString &fgeant) 
{
  const char* fver = gSystem->Getenv("ALIEN_JDL_PACKAGES");
  TString fS = fver;
  Int_t fFirst = fS.First("#");

  while(fFirst != -1) {
    Int_t fTotalLength = fS.Length();
    TString tmp = fS;
    TString fS1 = fS(0,fFirst);
    tmp = fS(fFirst+2,fTotalLength);
    fS = tmp;

    if(fS1.Contains("Root")) fAliroot = fS1;
    if(fS1.Contains("ROOT")) froot = fS1;
    if(fS1.Contains("GEANT")) fgeant = fS1;

    if(tmp.Contains("Root")) fAliroot = tmp;
    if(tmp.Contains("ROOT")) froot = tmp;
    if(tmp.Contains("GEANT")) fgeant = tmp;
    
    fFirst = tmp.First("#");
  }
}

//____________________________________________________________________
/** 
 * Get the global univeral identifier of the ESD file 
 * 
 * @param guid 
 */
void GetGUID(TString &guid) 
{
  ofstream myfile ("guid.txt");
  if (!myfile.is_open()) {
    Warning("GetGUID", "Couldn't open guid.txt for writing");
    return;
  }

  TFile *f = TFile::Open("AliESDs.root","read");
  if (!f || f->IsZombie() || !f->IsOpen()) {
    Warning("GetGUID", "Input file AliESDs.root not found");
    return;
  }

  guid = f->GetUUID().AsString();
  f->Close();
  Info("", "Global Unique IDentifier: %s", guid.Data());

  myfile << "AliESDs.root \t"<< guid << std::endl;
  myfile.close();
}


//____________________________________________________________________
/** 
 * Update the tags 
 * 
 * @param faliroot  AliROOT version 
 * @param froot     ROOT version
 * @param fgeant    GEANT3 version
 * @param turl      UrL we're at 
 * @param guid      Global universal identifier 
 * @param fperiod   Period
 * @param fpass     Pass
 * @param fname     Full name 
 * 
 * @return 
 */
Bool_t UpdateTag(TString faliroot, 
		 TString froot, 
		 TString fgeant, 
		 TString turl, 
		 TString guid,
		 TString fperiod, 
		 TString fpass, 
		 TString fname) 
{
  Info("", "Updating tags (%s,%s,%s,%s,%s,%s,%s,%s",
       faliroot.Data(), froot.Data(), fgeant.Data(), 
       turl.Data(), guid.Data(), fperiod.Data(), 
       fpass.Data(),fname.Data());

  const TString tagPattern = "tag.root";

  // --- Open the working directory ----------------------------------
  TSystemDirectory dir(".", gSystem->pwd());
  TIter            next(dir.GetListOfFiles());
  TSystemFile*     file = 0;

  // --- Add all files matching *pattern* to the chain ---------------
  while ((file = static_cast<TSystemFile*>(next()))) {
    TString name(file->GetName());
    if (!name.Contains(tagPattern)) continue;
    
    // --- Open file matching pattern --------------------------------
    TFile*      f     = TFile::Open(name,"read") ;
    if (!f) { 
      continue;
    }
    Info("", "Updating tags in %s", name.Data());

    // --- Find the tree ---------------------------------------------
    AliRunTag*  tag   = 0x0;
    AliFileTag* flTag = 0x0;
    TTree*      fTree = (TTree *)f->Get("T");
    if (!fTree) { 
      f->Close(); 
      continue; 
    }
    fTree->SetBranchAddress("AliTAG",&tag);
   
    // --- Defining new tag objects ----------------------------------
    AliRunTag* newTag = 0x0;
    TTree      ttag("T","A Tree with event tags");
    TBranch*   btag = ttag.Branch("AliTAG", &newTag);
    btag->SetCompressionLevel(9);
    // --- disassociate the tree with underlying directory -----------
    ttag.SetDirectory(0);
      
    Printf(">>>>> Found %d entries....",fTree->GetEntries());

    for (Int_t iTagFiles = 0; iTagFiles < fTree->GetEntries(); iTagFiles++) {
      fTree->GetEntry(0);
      newTag = new AliRunTag(*tag);
      newTag->SetAlirootVersion(faliroot);
      newTag->SetRootVersion(froot);
      newTag->SetGeant3Version(fgeant);
      newTag->SetLHCPeriod(fperiod);
      newTag->SetReconstructionPass(fpass);
      newTag->SetProductionName(fname);
      Printf("Found %d file tags",newTag->GetNFiles());
      for(Int_t j = 0; j < newTag->GetNFiles(); j++) {
	flTag = (AliFileTag *) newTag->GetFileTag(j);
	flTag->SetTURL(turl);
	flTag->SetGUID(guid);
      }
      ttag.Fill();

      delete tag;
      delete newTag;
    }//tag file loop 
    
    // --- Close the input file --------------------------------------
    f->Close();

    // --- Overwrite the file ----------------------------------------
    TFile* ftag = TFile::Open(name, "recreate");
    ftag->cd();
    ttag.Write();
    ftag->Close();
    Info("", "Overwrote %s with new tags", name.Data());
  }//directory loop
  return kTRUE;
}

// 
// EOF
// 
 Tag.C:1
 Tag.C:2
 Tag.C:3
 Tag.C:4
 Tag.C:5
 Tag.C:6
 Tag.C:7
 Tag.C:8
 Tag.C:9
 Tag.C:10
 Tag.C:11
 Tag.C:12
 Tag.C:13
 Tag.C:14
 Tag.C:15
 Tag.C:16
 Tag.C:17
 Tag.C:18
 Tag.C:19
 Tag.C:20
 Tag.C:21
 Tag.C:22
 Tag.C:23
 Tag.C:24
 Tag.C:25
 Tag.C:26
 Tag.C:27
 Tag.C:28
 Tag.C:29
 Tag.C:30
 Tag.C:31
 Tag.C:32
 Tag.C:33
 Tag.C:34
 Tag.C:35
 Tag.C:36
 Tag.C:37
 Tag.C:38
 Tag.C:39
 Tag.C:40
 Tag.C:41
 Tag.C:42
 Tag.C:43
 Tag.C:44
 Tag.C:45
 Tag.C:46
 Tag.C:47
 Tag.C:48
 Tag.C:49
 Tag.C:50
 Tag.C:51
 Tag.C:52
 Tag.C:53
 Tag.C:54
 Tag.C:55
 Tag.C:56
 Tag.C:57
 Tag.C:58
 Tag.C:59
 Tag.C:60
 Tag.C:61
 Tag.C:62
 Tag.C:63
 Tag.C:64
 Tag.C:65
 Tag.C:66
 Tag.C:67
 Tag.C:68
 Tag.C:69
 Tag.C:70
 Tag.C:71
 Tag.C:72
 Tag.C:73
 Tag.C:74
 Tag.C:75
 Tag.C:76
 Tag.C:77
 Tag.C:78
 Tag.C:79
 Tag.C:80
 Tag.C:81
 Tag.C:82
 Tag.C:83
 Tag.C:84
 Tag.C:85
 Tag.C:86
 Tag.C:87
 Tag.C:88
 Tag.C:89
 Tag.C:90
 Tag.C:91
 Tag.C:92
 Tag.C:93
 Tag.C:94
 Tag.C:95
 Tag.C:96
 Tag.C:97
 Tag.C:98
 Tag.C:99
 Tag.C:100
 Tag.C:101
 Tag.C:102
 Tag.C:103
 Tag.C:104
 Tag.C:105
 Tag.C:106
 Tag.C:107
 Tag.C:108
 Tag.C:109
 Tag.C:110
 Tag.C:111
 Tag.C:112
 Tag.C:113
 Tag.C:114
 Tag.C:115
 Tag.C:116
 Tag.C:117
 Tag.C:118
 Tag.C:119
 Tag.C:120
 Tag.C:121
 Tag.C:122
 Tag.C:123
 Tag.C:124
 Tag.C:125
 Tag.C:126
 Tag.C:127
 Tag.C:128
 Tag.C:129
 Tag.C:130
 Tag.C:131
 Tag.C:132
 Tag.C:133
 Tag.C:134
 Tag.C:135
 Tag.C:136
 Tag.C:137
 Tag.C:138
 Tag.C:139
 Tag.C:140
 Tag.C:141
 Tag.C:142
 Tag.C:143
 Tag.C:144
 Tag.C:145
 Tag.C:146
 Tag.C:147
 Tag.C:148
 Tag.C:149
 Tag.C:150
 Tag.C:151
 Tag.C:152
 Tag.C:153
 Tag.C:154
 Tag.C:155
 Tag.C:156
 Tag.C:157
 Tag.C:158
 Tag.C:159
 Tag.C:160
 Tag.C:161
 Tag.C:162
 Tag.C:163
 Tag.C:164
 Tag.C:165
 Tag.C:166
 Tag.C:167
 Tag.C:168
 Tag.C:169
 Tag.C:170
 Tag.C:171
 Tag.C:172
 Tag.C:173
 Tag.C:174
 Tag.C:175
 Tag.C:176
 Tag.C:177
 Tag.C:178
 Tag.C:179
 Tag.C:180
 Tag.C:181
 Tag.C:182
 Tag.C:183
 Tag.C:184
 Tag.C:185
 Tag.C:186
 Tag.C:187
 Tag.C:188
 Tag.C:189
 Tag.C:190
 Tag.C:191
 Tag.C:192
 Tag.C:193
 Tag.C:194
 Tag.C:195
 Tag.C:196
 Tag.C:197
 Tag.C:198
 Tag.C:199
 Tag.C:200
 Tag.C:201
 Tag.C:202
 Tag.C:203
 Tag.C:204
 Tag.C:205
 Tag.C:206
 Tag.C:207
 Tag.C:208
 Tag.C:209
 Tag.C:210
 Tag.C:211
 Tag.C:212
 Tag.C:213
 Tag.C:214
 Tag.C:215
 Tag.C:216
 Tag.C:217
 Tag.C:218
 Tag.C:219
 Tag.C:220
 Tag.C:221
 Tag.C:222
 Tag.C:223
 Tag.C:224
 Tag.C:225
 Tag.C:226
 Tag.C:227
 Tag.C:228
 Tag.C:229
 Tag.C:230