3 #include <TSystemDirectory.h>
10 #include <TParameter.h>
11 #include "AliOADBForward.h"
12 #include <AliForwardCorrectionManager.h>
13 #include <AliCentralCorrectionManager.h>
14 #include <AliCorrectionManagerBase.h>
17 class TSystemDirectory;
18 class AliCorrectionManagerBase;
19 class AliForwardCorrectionManager;
20 class AliCentralCorrectionManager;
27 * Check if a path points to a file
31 * @return True if the path points to a regular file
33 * @ingroup pwglf_forward_scripts
35 static Bool_t IsFile(const char* path)
38 gSystem->GetPathInfo(path, stat);
40 R_ISDIR(stat.fMode) ||
41 !R_ISREG(stat.fMode)) return false;
49 gSystem->GetPathInfo(path, &id, &size, &flags, &modtime);
50 return !((flags & 0x2) == 0x2);
54 * Test if we can open a file
56 * @param name Name of file
57 * @param pattern Pattern to check against
59 * @return True on success
61 static Bool_t TestFile(const TString& name, const char* pattern=0)
63 // If this is not a root file, ignore
64 if (!name.EndsWith(".root")) return false;
66 // If this file does not contain the pattern, ignore
67 if (pattern && pattern[0] != '\0' && !name.Contains(pattern)) return false;
70 TFile* test = TFile::Open(name.Data(), "READ");
71 if (!test || test->IsZombie()) {
72 ::Warning("TestFile", "Failed to open file %s", name.Data());
80 * Scan a directory (optionally recursive) for data files to add to
81 * the chain. Only ROOT files, and files which name contain the
82 * passed pattern are considered.
84 * @param dir Directory to scan
85 * @param list List to add data to
86 * @param pattern Pattern that the file name must contain
87 * @param recursive Whether to scan recursively
89 * @ingroup pwglf_forward_scripts
92 ScanDirectory(TSystemDirectory* dir, TList* list,
93 const char* pattern, bool recursive)
95 // Get list of files, and go back to old working directory
96 TString oldDir(gSystem->WorkingDirectory());
97 TList* files = dir->GetListOfFiles();
99 gSystem->ChangeDirectory(oldDir);
101 // Sort list of files and check if we should add it
104 TSystemFile* file = 0;
105 while ((file = static_cast<TSystemFile*>(next()))) {
106 TString name(file->GetName());
108 // Ignore special links
109 if (name == "." || name == "..") continue;
111 // Check if this is a directory
112 if (file->IsDirectory()) {
114 ScanDirectory(static_cast<TSystemDirectory*>(file),list,
120 TString data(Form("%s/%s", file->GetTitle(), name.Data()));
123 if (!TestFile(data, pattern)) continue;
124 list->Add(new TObjString(data));
128 * Scan data directory for files matching pattern
130 * @param datadir Path to data directory
131 * @param pattern File name match pattern
132 * @param recursive Recurse flag
134 * @return List of file names
136 TList* Scan(const char* datadir, const char* pattern, bool recursive=false)
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));
143 Error("Scan", "%s is a file", datadir);
146 Info("Scan", "Scanning %s", path.Data());
148 TList* ret = new TList;
150 TSystemDirectory d(datadir, path.Data());
151 ScanDirectory(&d, ret, pattern, recursive);
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",
170 * @param dirName Directory name
171 * @param corrName Correction name
172 * @param methName Run number mode to set as default
173 * @param outFile Output file
174 * @param cm Correction manager to use
176 Extractor(const char* dirName,
177 const char* corrName,
178 const char* methName,
180 AliCorrectionManagerBase* cm)
181 : fDirName(dirName), fCorrName(corrName), fMethName(methName),
182 fFile(outFile), fCM(cm)
185 virtual ~Extractor() {}
189 * @return number of converted objects
191 virtual Int_t Extract(const char* prefix="$ALICE_ROOT/PWGLF/FORWARD/corrections")
194 TString dir = TString::Format("%s/%s",
195 prefix, fDirName.Data());
196 TList* l = s.Scan(dir, fCorrName);
198 Warning("Extract", "No files matching %s found in %s",
199 fCorrName.Data(), dir.Data());
206 while ((os = static_cast<TObjString*>(next()))) {
207 TString& fn = os->String();
208 if (ExtractFile(fn)) ret++;
213 * Extract from a file
215 * @param fn File name
217 * @return true on success
219 virtual Bool_t ExtractFile(const TString& fn)
226 ExtractFields(fn, sys, sNN, fld, mc);
227 if (sNN == 2750) sNN = 2760;
229 ULong_t runNo = ExtractRunNo(sys, sNN);
230 if (runNo == 0xFFFFFFFF || runNo <= 0) return false;
232 TObject* obj = ExtractObject(fn.Data());
233 if (!obj) return false;
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);
242 * Extract fields from file name
250 virtual void ExtractFields(const TString& s,
256 TString str = gSystem->BaseName(s.Data());
257 str.ReplaceAll(".root", "");
258 TObjArray* tokens = str.Tokenize("_");
261 TString& sSys = ((TObjString*)(tokens->At(1)))->String();
262 TString& sSNN = ((TObjString*)(tokens->At(2)))->String();
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;
268 sSNN.ReplaceAll("GeV", "");
269 Info("", "sSNN=%s -> ", sSNN.Data());
270 while (sSNN[0] == '0' && sSNN.Length() > 1) sSNN.Remove(0, 1);
272 Info("", "sSNN=%s sNN=%d", sSNN.Data(), sNN);
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", "-");
283 if (tokens->GetEntries() > 4) {
284 TString& sMC = ((TObjString*)(tokens->At(4)))->String();
285 mc = sMC.EqualTo("mc", TString::kIgnoreCase);
288 // Info("Extract", "%s -> %d %d %d %d", str.Data(), sys, sNN, fld, mc);
291 * Get run number corresponding to arguments
298 virtual ULong_t ExtractRunNo(UShort_t sys, UShort_t 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;
312 case 2760: run = 137123; break;
317 case 5023: run = 188246; break;
322 Warning("ExtractRunNo",
323 "Unknown energy %d for collision system %d", sNN, sys);
327 * Extract a single object from the file
329 * @param fn File name
331 * @return Object or null
333 virtual TObject* ExtractObject(const TString& fn)
335 TFile* file = TFile::Open(fn.Data(), "READ");
337 Error("ExtractObject", "Failed to open %s", fn.Data());
342 TObject* obj = file->Get(fCorrName);
344 Error("ExtractObject", "Failed to get %s from %s",
345 fCorrName.Data(), fn.Data());
355 AliCorrectionManagerBase* fCM;
358 //====================================================================
359 struct NormExtractor : public Extractor
376 NormExtractor(const char* dirName,
377 const char* corrName,
378 const char* methName)
379 : Extractor(dirName,corrName,methName,"",0),
387 * @return Number of converted oject
389 virtual Int_t Extract()
391 Fatal("Extract", "Cannot use this");
399 * @return Number of converted oject
401 virtual Bool_t ExtractFile(const TString& s)
403 Fatal("ExtractFile", "Cannot use this (%s)", s.Data());
409 * @param db Database manager
410 * @param fileName File to store in
412 * @return number of converted objects
414 virtual Int_t ExtractNorm(AliOADBForward& db, const char* fileName)
417 TString dir = TString::Format("$ALICE_ROOT/PWGLF/FORWARD/corrections/%s",
419 TList* l = s.Scan(dir, fCorrName);
421 Warning("ExtractNorm", "No files matching %s found in %s",
422 fCorrName.Data(), dir.Data());
426 fFileName = fileName;
427 // if (!Open(db, fileName)) return 0;
431 while ((os = static_cast<TObjString*>(next()))) {
432 TString& fn = os->String();
433 if (ExtractNormFile(fn, db)) ret++;
438 * Overload to store file name
443 virtual Bool_t Open(AliOADBForward& db,
444 const Char_t* fileName)
446 fFileName = fileName;
447 Info("Open", "file name set to %s", fFileName.Data());
454 * @param db Database manager
455 * @param tab Table name
456 * @param o Object to stire
457 * @param runNo Run number
461 * @return true on success
463 virtual Bool_t Store(AliOADBForward& db, const TString& tab, TObject* o,
464 ULong_t runNo, UShort_t sys, UShort_t sNN)
466 Info("Store", "file name to store in %s", fFileName.Data());
467 if (!db.Open(fFileName, Form("%s/%s", tab.Data(), fMethName.Data()),
469 Warning("Store", "Failed to open for %s/%s", tab.Data(),
473 return db.Insert(tab, o, runNo, sys, sNN, 0, false, false);
476 * Extract a histogram
478 * @param what Name part
479 * @param runNo Run number
482 * @param f File to read from
483 * @param db Database manager
485 * @return true on success
487 virtual Bool_t ExtractHist(Int_t what, ULong_t runNo,
488 UShort_t sys, UShort_t sNN,
489 TFile& f, AliOADBForward& db)
493 case kINEL: oName = "hInelNormalization"; break;
494 case kNSD: oName = "hNSDNormalization"; break;
495 case kINELGT0: oName = "hINELGT0Normalization"; break;
497 TObject* obj = f.Get(oName);
499 Warning("ExtractHist", "Object %s not found", oName.Data());
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;
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());
515 return Store(db, tName, hist, runNo, sys, sNN);
520 * @param what Name part
521 * @param runNo Run number
524 * @param f File to read from
525 * @param db Database manager
527 * @return true on success
529 virtual Bool_t ExtractNum(Int_t what, ULong_t runNo,
530 UShort_t sys, UShort_t sNN,
531 TFile& f, AliOADBForward& db)
535 case kINEL: oName = "inelTriggerEff"; break;
536 case kNSD: oName = "nsdTriggerEff"; break;
537 case kINELGT0: oName = "inelgt0TriggerEff"; break;
539 TObject* obj = f.Get(oName);
541 Warning("ExtractHist", "Object %s not found", oName.Data());
547 case kINEL: tName = "triggerEffINEL"; break;
548 case kNSD: tName = "triggerEffNSD"; break;
549 case kINELGT0: tName = "triggerEffINELGT0"; break;
551 TParameter<float>* p = static_cast<TParameter<float>*>(obj->Clone(tName));
553 return Store(db, tName, p, runNo, sys, sNN);
556 * Extract from a file
558 * @param fn File name
559 * @param db database manager
561 * @return true on success
563 virtual Bool_t ExtractNormFile(const TString& fn, AliOADBForward& db)
570 ExtractFields(fn, sys, sNN, fld, mc);
571 if (sNN == 2750) sNN = 2760;
573 ULong_t runNo = ExtractRunNo(sys, sNN);
574 if (runNo == 0xFFFFFFFF || runNo <= 0) return false;
577 TFile* f = TFile::Open(fn, "READ");
579 Error("ExtractFile", "Failed to open %s", fn.Data());
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);
592 //====================================================================
594 MakeFMDExtractor(const char* dir, const char* name)
596 return new Extractor(dir, name, "NEAR", "fmd_corrections.root",
597 &(AliForwardCorrectionManager::Instance()));
600 MakeSPDExtractor(const char* dir, const char* name)
602 return new Extractor(dir, name, "NEAR", "spd_corrections.root",
603 &(AliCentralCorrectionManager::Instance()));
607 MigrateOADB(Int_t what=0x3)
609 gROOT->Macro("$ALICE_ROOT/PWGLF/FORWARD/analysis2/scripts/LoadLibs.C");
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"),
625 gSystem->Unlink("fmd_corrections.root");
626 gSystem->Unlink("spd_corrections.root");
636 gSystem->Unlink("normalization.root");
638 NormExtractor e7("Normalization",
639 "normalizationHists",
643 e7.ExtractNorm(ndb,"normalization.root");