AliPhysics  db95e02 (db95e02)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MigrateOADB.C
Go to the documentation of this file.
1 #ifndef __CINT__
2 #include <TSystem.h>
3 #include <TSystemDirectory.h>
4 #include <TError.h>
5 #include <TString.h>
6 #include <TList.h>
7 #include <TFile.h>
8 #include <TROOT.h>
9 #include <TH1.h>
10 #include <TParameter.h>
11 #include "AliOADBForward.h"
15 #else
16 class AliOADBForward;
17 class TSystemDirectory;
21 class TFile;
22 #endif
23 
24 struct Scanner
25 {
35  static Bool_t IsFile(const char* path)
36  {
37  FileStat_t stat;
38  gSystem->GetPathInfo(path, stat);
39  if (stat.fIsLink ||
40  R_ISDIR(stat.fMode) ||
41  !R_ISREG(stat.fMode)) return false;
42  return true;
43 
44 #if 0
45  Long_t id;
46  Long_t size;
47  Long_t flags;
48  Long_t modtime;
49  gSystem->GetPathInfo(path, &id, &size, &flags, &modtime);
50  return !((flags & 0x2) == 0x2);
51 #endif
52  }
61  static Bool_t TestFile(const TString& name, const char* pattern=0)
62  {
63  // If this is not a root file, ignore
64  if (!name.EndsWith(".root")) return false;
65 
66  // If this file does not contain the pattern, ignore
67  if (pattern && pattern[0] != '\0' && !name.Contains(pattern)) return false;
68 
69  Bool_t ret = true;
70  TFile* test = TFile::Open(name.Data(), "READ");
71  if (!test || test->IsZombie()) {
72  ::Warning("TestFile", "Failed to open file %s", name.Data());
73  ret = false;
74  }
75  else
76  test->Close();
77  return ret;
78  }
91  void
92  ScanDirectory(TSystemDirectory* dir, TList* list,
93  const char* pattern, bool recursive)
94  {
95  // Get list of files, and go back to old working directory
96  TString oldDir(gSystem->WorkingDirectory());
97  TList* files = dir->GetListOfFiles();
98  if (!files) return;
99  gSystem->ChangeDirectory(oldDir);
100 
101  // Sort list of files and check if we should add it
102  files->Sort();
103  TIter next(files);
104  TSystemFile* file = 0;
105  while ((file = static_cast<TSystemFile*>(next()))) {
106  TString name(file->GetName());
107 
108  // Ignore special links
109  if (name == "." || name == "..") continue;
110 
111  // Check if this is a directory
112  if (file->IsDirectory()) {
113  if (recursive)
114  ScanDirectory(static_cast<TSystemDirectory*>(file),list,
115  pattern,recursive);
116  continue;
117  }
118 
119  // Get the path
120  TString data(Form("%s/%s", file->GetTitle(), name.Data()));
121 
122  // Check the fuile
123  if (!TestFile(data, pattern)) continue;
124  list->Add(new TObjString(data));
125  }
126  }
136  TList* Scan(const char* datadir, const char* pattern, bool recursive=false)
137  {
138  // --- Get list of files --------------------------------------------
139  // Open source directory, and make sure we go back to were we were
140  TString oldDir(gSystem->WorkingDirectory());
141  TString path(gSystem->ExpandPathName(datadir));
142  if (IsFile(path)) {
143  Error("Scan", "%s is a file", datadir);
144  return 0;
145  }
146  Info("Scan", "Scanning %s", path.Data());
147 
148  TList* ret = new TList;
149  ret->SetOwner();
150  TSystemDirectory d(datadir, path.Data());
151  ScanDirectory(&d, ret, pattern, recursive);
152 
153  // Make sure we do not make an empty chain
154  if (ret->GetEntries() <= 0) {
155  Warning("Scane", "list is empty for input %s, %s",
156  datadir, pattern);
157  delete ret;
158  ret = 0;
159  }
160  return ret;
161  }
162 };
163 
164 
165 struct Extractor
166 {
176  Extractor(const char* dirName,
177  const char* corrName,
178  const char* methName,
179  const char* outFile,
181  : fDirName(dirName), fCorrName(corrName), fMethName(methName),
182  fFile(outFile), fCM(cm)
183  {
184  }
185  virtual ~Extractor() {}
191  virtual Int_t Extract(const char* prefix="$ALICE_PHYSICS/PWGLF/FORWARD/corrections")
192  {
193  Scanner s;
194  TString dir = TString::Format("%s/%s",
195  prefix, fDirName.Data());
196  TList* l = s.Scan(dir, fCorrName);
197  if (!l) {
198  Warning("Extract", "No files matching %s found in %s",
199  fCorrName.Data(), dir.Data());
200  return 0;
201  }
202 
203  TIter next(l);
204  TObjString* os = 0;
205  Int_t ret = 0;
206  while ((os = static_cast<TObjString*>(next()))) {
207  TString& fn = os->String();
208  if (ExtractFile(fn)) ret++;
209  }
210  return ret;
211  }
219  virtual Bool_t ExtractFile(const TString& fn)
220  {
221  UShort_t sys = 0;
222  UShort_t sNN = 0;
223  Short_t fld = 0;
224  Bool_t mc = false;
225 
226  ExtractFields(fn, sys, sNN, fld, mc);
227  if (sNN == 2750) sNN = 2760;
228 
229  ULong_t runNo = ExtractRunNo(sys, sNN);
230  if (runNo == 0xFFFFFFFF || runNo <= 0) return false;
231 
232  TObject* obj = ExtractObject(fn.Data());
233  if (!obj) return false;
234 
235  // Run, sys, sNN, fld, mc, sat, obj, full, meth
236  Info("", "File %s to be stored: run=%d sys=%d sNN=%d fld=%d mc=%d",
237  fn.Data(), runNo, sys, sNN, fld, mc);
238  return fCM->Store(obj, runNo, sys, sNN, fld, mc,
239  false, fFile, fMethName);
240  }
250  virtual void ExtractFields(const TString& s,
251  UShort_t& sys,
252  UShort_t& sNN,
253  Short_t& fld,
254  Bool_t& mc)
255  {
256  TString str = gSystem->BaseName(s.Data());
257  str.ReplaceAll(".root", "");
258  TObjArray* tokens = str.Tokenize("_");
259  // tokens->ls();
260 
261  TString& sSys = ((TObjString*)(tokens->At(1)))->String();
262  TString& sSNN = ((TObjString*)(tokens->At(2)))->String();
263 
264  if (sSys.EqualTo("pbpb", TString::kIgnoreCase)) sys = 2;
265  else if (sSys.EqualTo("ppb", TString::kIgnoreCase)) sys = 3;
266  else if (sSys.EqualTo("pp", TString::kIgnoreCase)) sys = 1;
267 
268  sSNN.ReplaceAll("GeV", "");
269  Info("", "sSNN=%s -> ", sSNN.Data());
270  while (sSNN[0] == '0' && sSNN.Length() > 1) sSNN.Remove(0, 1);
271  sNN = sSNN.Atoi();
272  Info("", "sSNN=%s sNN=%d", sSNN.Data(), sNN);
273 
274  if (tokens->GetEntries() > 3) {
275  TString& sFld = ((TObjString*)(tokens->At(3)))->String();
276  sFld.ReplaceAll("kG", "");
277  while (sFld[0] == '0' && sFld.Length() > 1) sFld.Remove(0, 1);
278  sFld.ReplaceAll("p", "+");
279  sFld.ReplaceAll("m", "-");
280  fld = sFld.Atoi();
281  }
282 
283  if (tokens->GetEntries() > 4) {
284  TString& sMC = ((TObjString*)(tokens->At(4)))->String();
285  mc = sMC.EqualTo("mc", TString::kIgnoreCase);
286  }
287  tokens->Delete();
288  // Info("Extract", "%s -> %d %d %d %d", str.Data(), sys, sNN, fld, mc);
289  }
299  {
300  ULong_t run = 0;
301  switch (sys) {
302  case 1: // pp
303  switch (sNN) {
304  case 900: run = 118502; break;
305  case 2760: run = 146686; break;
306  case 7000: run = 114747; break;
307  case 14000: run = 0xFFFFFFFF; break;
308  }
309  break;
310  case 2: // PbPb
311  switch (sNN) {
312  case 2760: run = 137123; break;
313  }
314  break;
315  case 3: // pPb
316  switch (sNN) {
317  case 5023: run = 188246; break;
318  }
319  break;
320  }
321  if (run == 0)
322  Warning("ExtractRunNo",
323  "Unknown energy %d for collision system %d", sNN, sys);
324  return run;
325  }
333  virtual TObject* ExtractObject(const TString& fn)
334  {
335  TFile* file = TFile::Open(fn.Data(), "READ");
336  if (!file) {
337  Error("ExtractObject", "Failed to open %s", fn.Data());
338  return 0;
339  }
340  // file->ls();
341 
342  TObject* obj = file->Get(fCorrName);
343  if (!obj) {
344  Error("ExtractObject", "Failed to get %s from %s",
345  fCorrName.Data(), fn.Data());
346  return 0;
347  }
348  file->Close();
349  return obj;
350  }
356 };
357 
358 //====================================================================
359 struct NormExtractor : public Extractor
360 {
361  enum {
365  };
376  NormExtractor(const char* dirName,
377  const char* corrName,
378  const char* methName)
379  : Extractor(dirName,corrName,methName,"",0),
380  fFileName("")
381  {
382  }
389  virtual Int_t Extract()
390  {
391  Fatal("Extract", "Cannot use this");
392  return -1;
393  }
401  virtual Bool_t ExtractFile(const TString& s)
402  {
403  Fatal("ExtractFile", "Cannot use this (%s)", s.Data());
404  return -1;
405  }
414  virtual Int_t ExtractNorm(AliOADBForward& db, const char* fileName)
415  {
416  Scanner s;
417  TString dir = TString::Format("$ALICE_PHYSICS/PWGLF/FORWARD/corrections/%s",
418  fDirName.Data());
419  TList* l = s.Scan(dir, fCorrName);
420  if (!l) {
421  Warning("ExtractNorm", "No files matching %s found in %s",
422  fCorrName.Data(), dir.Data());
423  return 0;
424  }
425 
427  // if (!Open(db, fileName)) return 0;
428  TIter next(l);
429  TObjString* os = 0;
430  Int_t ret = 0;
431  while ((os = static_cast<TObjString*>(next()))) {
432  TString& fn = os->String();
433  if (ExtractNormFile(fn, db)) ret++;
434  }
435  return ret;
436  }
442 #if 0
443  virtual Bool_t Open(AliOADBForward& db,
444  const Char_t* fileName)
445  {
446  fFileName = fileName;
447  Info("Open", "file name set to %s", fFileName.Data());
448  return true;
449  }
450 #endif
451 
463  virtual Bool_t Store(AliOADBForward& db, const TString& tab, TObject* o,
464  ULong_t runNo, UShort_t sys, UShort_t sNN)
465  {
466  Info("Store", "file name to store in %s", fFileName.Data());
467  if (!db.Open(fFileName, Form("%s/%s", tab.Data(), fMethName.Data()),
468  true, true)) {
469  Warning("Store", "Failed to open for %s/%s", tab.Data(),
470  fMethName.Data());
471  return false;
472  }
473  return db.Insert(tab, o, runNo, sys, sNN, 0, false, false);
474  }
487  virtual Bool_t ExtractHist(Int_t what, ULong_t runNo,
488  UShort_t sys, UShort_t sNN,
489  TFile& f, AliOADBForward& db)
490  {
491  TString oName;
492  switch (what) {
493  case kINEL: oName = "hInelNormalization"; break;
494  case kNSD: oName = "hNSDNormalization"; break;
495  case kINELGT0: oName = "hINELGT0Normalization"; break;
496  }
497  TObject* obj = f.Get(oName);
498  if (!obj) {
499  Warning("ExtractHist", "Object %s not found", oName.Data());
500  return false;
501  }
502 
503  TString tName;
504  TString ttName;
505  switch (what) {
506  case kINEL: tName = "normalizationINEL"; ttName = "INEL"; break;
507  case kNSD: tName = "normalizationNSD"; ttName = "NSD"; break;
508  case kINELGT0: tName = "normalizationINELGT0"; ttName = "INEL>0"; break;
509  }
510  TH1* hist = static_cast<TH1*>(obj->Clone(tName));
511  hist->SetDirectory(0);
512  hist->SetTitle(Form("Normalization for %s", ttName.Data()));
513  // obj->SetName(tName.Data());
514 
515  return Store(db, tName, hist, runNo, sys, sNN);
516  }
529  virtual Bool_t ExtractNum(Int_t what, ULong_t runNo,
530  UShort_t sys, UShort_t sNN,
531  TFile& f, AliOADBForward& db)
532  {
533  TString oName;
534  switch (what) {
535  case kINEL: oName = "inelTriggerEff"; break;
536  case kNSD: oName = "nsdTriggerEff"; break;
537  case kINELGT0: oName = "inelgt0TriggerEff"; break;
538  }
539  TObject* obj = f.Get(oName);
540  if (!obj) {
541  Warning("ExtractHist", "Object %s not found", oName.Data());
542  return false;
543  }
544 
545  TString tName;
546  switch (what) {
547  case kINEL: tName = "triggerEffINEL"; break;
548  case kNSD: tName = "triggerEffNSD"; break;
549  case kINELGT0: tName = "triggerEffINELGT0"; break;
550  }
551  TParameter<float>* p = static_cast<TParameter<float>*>(obj->Clone(tName));
552 
553  return Store(db, tName, p, runNo, sys, sNN);
554  }
563  virtual Bool_t ExtractNormFile(const TString& fn, AliOADBForward& db)
564  {
565  UShort_t sys = 0;
566  UShort_t sNN = 0;
567  Short_t fld = 0;
568  Bool_t mc = false;
569 
570  ExtractFields(fn, sys, sNN, fld, mc);
571  if (sNN == 2750) sNN = 2760;
572 
573  ULong_t runNo = ExtractRunNo(sys, sNN);
574  if (runNo == 0xFFFFFFFF || runNo <= 0) return false;
575 
576 
577  TFile* f = TFile::Open(fn, "READ");
578  if (!f) {
579  Error("ExtractFile", "Failed to open %s", fn.Data());
580  return false;
581  }
582  ExtractHist(kINEL, runNo, sys, sNN, *f, db);
583  ExtractHist(kNSD, runNo, sys, sNN, *f, db);
584  ExtractHist(kINELGT0,runNo, sys, sNN, *f, db);
585  ExtractNum(kINEL, runNo, sys, sNN, *f, db);
586  ExtractNum(kNSD, runNo, sys, sNN, *f, db);
587  ExtractNum(kINELGT0, runNo, sys, sNN, *f, db);
588  return true;
589  }
590 };
591 
592 //====================================================================
593 Extractor*
594 MakeFMDExtractor(const char* dir, const char* name)
595 {
596  return new Extractor(dir, name, "NEAR", "fmd_corrections.root",
598 }
599 Extractor*
600 MakeSPDExtractor(const char* dir, const char* name)
601 {
602  return new Extractor(dir, name, "NEAR", "spd_corrections.root",
604 }
605 
606 void
608 {
609  gROOT->Macro("$ALICE_PHYSICS/PWGLF/FORWARD/analysis2/scripts/LoadLibs.C");
610 
611  if (what & 0x1) {
612  Extractor* ee[] = {
613  MakeFMDExtractor("Acceptance", "acceptance"),
614  MakeFMDExtractor("CentralAcceptance", "centralacceptance"),
615  MakeFMDExtractor("CentralSecMap", "centralsecmap"),
616  MakeFMDExtractor("DoubleHit", "doublehit"),
617  MakeFMDExtractor("ELossFits", "elossfits"),
618  MakeFMDExtractor("MergingEfficiency", "merging"),
619  MakeFMDExtractor("SecondaryMap", "secondary"),
620  MakeFMDExtractor("VertexBias", "vertexbias"),
621  MakeSPDExtractor("CentralSecMap", "centralsecmap"),
622  MakeSPDExtractor("CentralAcceptance","centralacceptance"),
623  0 };
624 
625  gSystem->Unlink("fmd_corrections.root");
626  gSystem->Unlink("spd_corrections.root");
627 
628  Extractor** ep = ee;
629  while (*ep) {
630  (*ep)->Extract();
631  ep++;
632  }
633  }
634 
635  if (what & 0x2) {
636  gSystem->Unlink("normalization.root");
637 
638  NormExtractor e7("Normalization",
639  "normalizationHists",
640  "NEAR");
641  AliOADBForward ndb;
642  TString ntables;
643  e7.ExtractNorm(ndb,"normalization.root");
644  }
645 }
646 
Base class for correction managers.
Extractor * MakeSPDExtractor(const char *dir, const char *name)
Definition: MigrateOADB.C:600
return jsonbuilder str().c_str()
virtual Bool_t ExtractFile(const TString &fn)
Definition: MigrateOADB.C:219
AliCorrectionManagerBase * fCM
Definition: MigrateOADB.C:355
TString fileName
virtual Bool_t ExtractHist(Int_t what, ULong_t runNo, UShort_t sys, UShort_t sNN, TFile &f, AliOADBForward &db)
Definition: MigrateOADB.C:487
TSystem * gSystem
virtual Int_t Extract(const char *prefix="$ALICE_PHYSICS/PWGLF/FORWARD/corrections")
Definition: MigrateOADB.C:191
Extractor * MakeFMDExtractor(const char *dir, const char *name)
Definition: MigrateOADB.C:594
char Char_t
Definition: External.C:18
TList * list
virtual Bool_t ExtractNum(Int_t what, ULong_t runNo, UShort_t sys, UShort_t sNN, TFile &f, AliOADBForward &db)
Definition: MigrateOADB.C:529
virtual Int_t ExtractNorm(AliOADBForward &db, const char *fileName)
Definition: MigrateOADB.C:414
NormExtractor(const char *dirName, const char *corrName, const char *methName)
Definition: MigrateOADB.C:376
virtual Bool_t Store(AliOADBForward &db, const TString &tab, TObject *o, ULong_t runNo, UShort_t sys, UShort_t sNN)
Definition: MigrateOADB.C:463
TString fMethName
Definition: MigrateOADB.C:353
TString fDirName
Definition: MigrateOADB.C:351
int Int_t
Definition: External.C:63
virtual Bool_t Store(TObject *o, ULong_t runNo, UShort_t sys, UShort_t sNN, Short_t field, Bool_t mc, Bool_t sat, const char *file, const char *meth="NEAR") const
void ScanDirectory(TSystemDirectory *dir, TList *list, const char *pattern, bool recursive)
Definition: MigrateOADB.C:92
virtual ULong_t ExtractRunNo(UShort_t sys, UShort_t sNN)
Definition: MigrateOADB.C:298
unsigned long ULong_t
Definition: External.C:38
TString fFile
Definition: MigrateOADB.C:354
short Short_t
Definition: External.C:23
virtual Int_t Extract()
Definition: MigrateOADB.C:389
static Bool_t IsFile(const char *path)
Definition: MigrateOADB.C:35
virtual ~Extractor()
Definition: MigrateOADB.C:185
Extractor(const char *dirName, const char *corrName, const char *methName, const char *outFile, AliCorrectionManagerBase *cm)
Definition: MigrateOADB.C:176
void MigrateOADB(Int_t what=0x3)
Definition: MigrateOADB.C:607
TFile * file
virtual Bool_t ExtractNormFile(const TString &fn, AliOADBForward &db)
Definition: MigrateOADB.C:563
Bool_t Insert(const TString &table, TObject *o, ULong_t runNo, UShort_t sys, UShort_t sNN, Short_t field, Bool_t mc=false, Bool_t sat=false, ULong_t aliRev=0, const TString &author="")
TString fFileName
Definition: MigrateOADB.C:366
virtual void ExtractFields(const TString &s, UShort_t &sys, UShort_t &sNN, Short_t &fld, Bool_t &mc)
Definition: MigrateOADB.C:250
unsigned short UShort_t
Definition: External.C:28
TList * Scan(const char *datadir, const char *pattern, bool recursive=false)
Definition: MigrateOADB.C:136
bool Bool_t
Definition: External.C:53
static Bool_t TestFile(const TString &name, const char *pattern=0)
Definition: MigrateOADB.C:61
Bool_t Open(const TString &fileName, const TString &tables="*", Bool_t rw=false, Bool_t verb=false, Bool_t fallback=false)
virtual Bool_t ExtractFile(const TString &s)
Definition: MigrateOADB.C:401
static AliCentralCorrectionManager & Instance()
TString fCorrName
Definition: MigrateOADB.C:352
virtual TObject * ExtractObject(const TString &fn)
Definition: MigrateOADB.C:333
Definition: External.C:196
static AliForwardCorrectionManager & Instance()