]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGLF/FORWARD/analysis2/corrs/MigrateOADB.C
Merge branch master into TRDdev
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / corrs / MigrateOADB.C
CommitLineData
8449e3e0 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"
bfab35d9 12#include <AliForwardCorrectionManager.h>
13#include <AliCentralCorrectionManager.h>
14#include <AliCorrectionManagerBase.h>
8449e3e0 15#else
16class AliOADBForward;
17class TSystemDirectory;
bfab35d9 18class AliCorrectionManagerBase;
19class AliForwardCorrectionManager;
20class AliCentralCorrectionManager;
8449e3e0 21class TFile;
22#endif
23
24struct Scanner
25{
26 /**
27 * Check if a path points to a file
28 *
29 * @param path Path
30 *
31 * @return True if the path points to a regular file
32 *
33 * @ingroup pwglf_forward_scripts
34 */
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 }
53 /**
54 * Test if we can open a file
55 *
56 * @param name Name of file
57 * @param pattern Pattern to check against
58 *
59 * @return True on success
60 */
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 }
79 /**
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.
83 *
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
88 *
89 * @ingroup pwglf_forward_scripts
90 */
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 }
127 /**
128 * Scan data directory for files matching pattern
129 *
130 * @param datadir Path to data directory
131 * @param pattern File name match pattern
132 * @param recursive Recurse flag
133 *
134 * @return List of file names
135 */
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
165struct Extractor
166{
167 /**
168 * Constructor
169 *
170 * @param dirName Directory name
171 * @param corrName Correction name
172 * @param methName Run number mode to set as default
c8b1a7db 173 * @param outFile Output file
174 * @param cm Correction manager to use
8449e3e0 175 */
176 Extractor(const char* dirName,
177 const char* corrName,
178 const char* methName,
179 const char* outFile,
180 AliCorrectionManagerBase* cm)
181 : fDirName(dirName), fCorrName(corrName), fMethName(methName),
182 fFile(outFile), fCM(cm)
183 {
184 }
185 virtual ~Extractor() {}
186 /**
187 * Extract files
188 *
8449e3e0 189 * @return number of converted objects
190 */
191 virtual Int_t Extract()
192 {
193 Scanner s;
194 TString dir = TString::Format("$ALICE_ROOT/PWGLF/FORWARD/corrections/%s",
195 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 }
212 /**
213 * Extract from a file
214 *
215 * @param fn File name
8449e3e0 216 *
217 * @return true on success
218 */
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 }
241 /**
242 * Extract fields from file name
243 *
244 * @param s File name
245 * @param sys System
246 * @param sNN Energy
247 * @param fld Field
248 * @param mc MC flag
249 */
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 }
290 /**
291 * Get run number corresponding to arguments
292 *
293 * @param sys System
294 * @param sNN Energy
295 *
296 * @return run number
297 */
298 virtual ULong_t ExtractRunNo(UShort_t sys, UShort_t sNN)
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 }
326 /**
327 * Extract a single object from the file
328 *
329 * @param fn File name
330 *
331 * @return Object or null
332 */
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 }
351 TString fDirName;
352 TString fCorrName;
353 TString fMethName;
354 TString fFile;
355 AliCorrectionManagerBase* fCM;
356};
357
358//====================================================================
359struct NormExtractor : public Extractor
360{
361 enum {
362 kINEL,
363 kNSD,
364 kINELGT0
365 };
366 TString fFileName;
367 /**
368 * Constructor
369 *
370 * @param dirName
371 * @param corrName
372 * @param methName
373 *
374 * @return
375 */
376 NormExtractor(const char* dirName,
377 const char* corrName,
378 const char* methName)
bfab35d9 379 : Extractor(dirName,corrName,methName,"",0),
8449e3e0 380 fFileName("")
381 {
382 }
c8b1a7db 383 /**
384 * Extract
385 *
386 *
387 * @return Number of converted oject
388 */
8449e3e0 389 virtual Int_t Extract()
390 {
391 Fatal("Extract", "Cannot use this");
392 return -1;
393 }
c8b1a7db 394 /**
395 * Extract
396 *
397 * @param s Source
398 *
399 * @return Number of converted oject
400 */
8449e3e0 401 virtual Bool_t ExtractFile(const TString& s)
402 {
403 Fatal("ExtractFile", "Cannot use this (%s)", s.Data());
404 return -1;
405 }
406 /**
407 * Extract files
408 *
409 * @param db Database manager
410 * @param fileName File to store in
411 *
412 * @return number of converted objects
413 */
414 virtual Int_t ExtractNorm(AliOADBForward& db, const char* fileName)
415 {
416 Scanner s;
417 TString dir = TString::Format("$ALICE_ROOT/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
426 fFileName = fileName;
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 }
437 /**
438 * Overload to store file name
439 *
440 * @return true
441 */
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 /**
452 * Store object in DB
453 *
454 * @param db Database manager
455 * @param tab Table name
456 * @param o Object to stire
457 * @param runNo Run number
458 * @param sys System
459 * @param sNN Energy
460 *
461 * @return true on success
462 */
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 }
475 /**
476 * Extract a histogram
477 *
478 * @param what Name part
479 * @param runNo Run number
480 * @param sys System
481 * @param sNN Energy
482 * @param f File to read from
483 * @param db Database manager
484 *
485 * @return true on success
486 */
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);
bfab35d9 512 hist->SetTitle(Form("Normalization for %s", ttName.Data()));
8449e3e0 513 // obj->SetName(tName.Data());
514
515 return Store(db, tName, hist, runNo, sys, sNN);
516 }
517 /**
518 * Extract a number
519 *
520 * @param what Name part
521 * @param runNo Run number
522 * @param sys System
523 * @param sNN Energy
524 * @param f File to read from
525 * @param db Database manager
526 *
527 * @return true on success
528 */
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 }
555 /**
556 * Extract from a file
557 *
558 * @param fn File name
559 * @param db database manager
560 *
561 * @return true on success
562 */
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//====================================================================
593Extractor*
594MakeFMDExtractor(const char* dir, const char* name)
595{
596 return new Extractor(dir, name, "NEAR", "fmd_corrections.root",
bfab35d9 597 &(AliForwardCorrectionManager::Instance()));
8449e3e0 598}
599Extractor*
600MakeSPDExtractor(const char* dir, const char* name)
601{
602 return new Extractor(dir, name, "NEAR", "spd_corrections.root",
bfab35d9 603 &(AliCentralCorrectionManager::Instance()));
8449e3e0 604}
605
606void
607MigrateOADB(Int_t what=0x3)
608{
609 gROOT->Macro("$ALICE_ROOT/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