1 #include "AliCorrectionManagerBase.h"
2 #include "AliOADBForward.h"
3 #include "AliForwardUtil.h"
10 #include <TParameter.h>
11 #include <TFileMerger.h>
15 //____________________________________________________________________
16 AliCorrectionManagerBase::AliCorrectionManagerBase()
31 //____________________________________________________________________
32 AliCorrectionManagerBase::AliCorrectionManagerBase(Bool_t)
45 fCorrections.SetOwner(false);
46 fCorrections.SetName("corrections");
48 //____________________________________________________________________
49 AliCorrectionManagerBase::AliCorrectionManagerBase(const
50 AliCorrectionManagerBase& o)
59 fSatellite(o.fSatellite),
62 fFallBack(o.fFallBack)
64 fCorrections.SetOwner(false);
65 Int_t n = o.fCorrections.GetEntriesFast();
66 for (Int_t i = 0; i < n; i++) {
67 fCorrections.AddAt(o.fCorrections.At(i), i);
70 //____________________________________________________________________
71 AliCorrectionManagerBase&
72 AliCorrectionManagerBase::operator=(const AliCorrectionManagerBase& o)
74 if (&o == this) return *this;
82 fSatellite = o.fSatellite;
85 fFallBack = o.fFallBack;
88 Int_t n = o.fCorrections.GetEntriesFast();
89 for (Int_t i = 0; i < n; i++) {
90 fCorrections.AddAt(o.fCorrections.At(i), i);
95 //____________________________________________________________________
96 AliCorrectionManagerBase::~AliCorrectionManagerBase()
98 // fCorrections.Delete();
101 #define PF(N,V,...) \
102 AliForwardUtil::PrintField(N,V, ## __VA_ARGS__)
103 #define PFB(N,FLAG) \
105 AliForwardUtil::PrintName(N); \
106 std::cout << std::boolalpha << (FLAG) << std::noboolalpha << std::endl; \
108 #define PFV(N,VALUE) \
110 AliForwardUtil::PrintName(N); \
111 std::cout << (VALUE) << std::endl; } while(false)
113 //____________________________________________________________________
115 AliCorrectionManagerBase::Print(Option_t* option) const
117 AliForwardUtil::PrintTask(*this);
118 gROOT->IncreaseDirLevel();
119 PFB("Initialized", fIsInit);
121 PFV("Run number", fRun);
122 PFV("Collision system", AliForwardUtil::CollisionSystemString(fSys));
123 PFV("Sqrt(s_NN)",AliForwardUtil::CenterOfMassEnergyString(fSNN));
124 PFV("Magnetic field", AliForwardUtil::MagneticFieldString(fField));
125 PFB("For simulations", fMC);
126 PFB("For satellites", fSatellite);
130 if (opt.Contains("R")) {
131 // gROOT->IncreaseDirLevel();
132 Int_t n = fCorrections.GetEntriesFast();
133 for (Int_t id = 0; id < n; id++) {
134 const Correction* c = GetCorrection(id);
137 // gROOT->DecreaseDirLevel();
139 gROOT->DecreaseDirLevel();
142 //____________________________________________________________________
144 AliCorrectionManagerBase::Browse(TBrowser* b)
146 b->Add(&fCorrections);
149 //____________________________________________________________________
151 AliCorrectionManagerBase::SetPrefix(const TString& prefix)
153 Int_t n = fCorrections.GetEntriesFast();
154 for (Int_t id = 0; id < n; id++) {
155 Correction* c = GetCorrection(id);
156 const char* old = c->GetTitle();
157 TString oldf(gSystem->BaseName(old));
158 c->SetFile(gSystem->ConcatFileName(prefix, oldf));
162 //____________________________________________________________________
164 AliCorrectionManagerBase::Store(TObject* o,
172 const char* meth) const
175 Int_t n = fCorrections.GetEntriesFast();
176 for (Int_t id = 0; id < n; id++) {
177 const Correction* c = GetCorrection(id);
178 if (!o->IsA()->InheritsFrom(c->fCls)) continue;
180 ret = c->StoreIt(fDB, o, runNo, sys, sNN, field, mc, sat, file, meth);
186 //____________________________________________________________________
188 AliCorrectionManagerBase::Append(const TString& addition,
189 const TString& destination) const
191 if (addition.IsNull()) {
192 AliWarning("No addition specified");
195 if (destination.IsNull()) {
196 AliWarning("No destination storage specified");
200 merger.SetPrintLevel(1);
201 merger.OutputFile(destination, "UPDATE");
202 merger.AddFile(addition);
203 if (!merger.PartialMerge()) {
204 AliInfoF("Failed to merge %s with %s",
205 addition.Data(), destination.Data());
208 if (destination.BeginsWith("$OADB_PATH") ||
209 destination.BeginsWith("$ALICE_ROOT"))
210 AliInfoF("Now commit %s to git", destination.Data());
213 //____________________________________________________________________
215 AliCorrectionManagerBase::CleanUp(const TString& destination, Bool_t verb) const
217 AliOADBForward* db = fDB;
218 if (!db) db = new AliOADBForward;
220 for (Int_t i = 0; i < 32; i++) {
221 const Correction* c = GetCorrection(i);
225 Info("CleanUp", "Table %s not enabled", c->GetName());
228 Info("CleanUp", "Will clean up table %s", c->GetName());
229 c->CleanIt(db, destination, verb);
235 //____________________________________________________________________
237 AliCorrectionManagerBase::EnableCorrections(UInt_t what)
239 for (Int_t i = 0; i < 32; i++) {
240 if (!GetCorrection(i)) continue;
241 EnableCorrection(i, (1 << i) & what);
244 //____________________________________________________________________
246 AliCorrectionManagerBase::CheckCorrections(UInt_t what, Bool_t verbose) const
249 for (Int_t i = 0; i < 32; i++) {
250 Bool_t enabled = (1 << i) & what;
251 if (!enabled) continue;
252 const Correction* c = GetCorrection(i);
256 AliWarningF("No correction registered for bit %d", i);
259 const TObject* o = c->Get();
263 AliWarningF("Correction %s (bit %d) not initialized!", c->GetName(), i);
270 //____________________________________________________________________
272 AliCorrectionManagerBase::RegisterCorrection(Int_t id, Correction* corr)
274 fCorrections.AddAtAndExpand(corr, id);
277 //____________________________________________________________________
279 AliCorrectionManagerBase::RegisterCorrection(Int_t id,
280 const TString& tableName,
281 const TString& fileName,
286 RegisterCorrection(id,new Correction(tableName,fileName,cls,fields,enabled));
289 //____________________________________________________________________
290 AliCorrectionManagerBase::Correction*
291 AliCorrectionManagerBase::GetCorrection(Int_t id)
293 if (id < 0 || id > fCorrections.GetEntriesFast()) return 0;
294 return static_cast<Correction*>(fCorrections.At(id));
297 //____________________________________________________________________
298 const AliCorrectionManagerBase::Correction*
299 AliCorrectionManagerBase::GetCorrection(Int_t id) const
301 if (id < 0 || id > fCorrections.GetEntriesFast()) return 0;
302 return static_cast<Correction*>(fCorrections.At(id));
305 //____________________________________________________________________
307 AliCorrectionManagerBase::SetCorrectionFile(Int_t id, const TString& fileName)
309 Correction* c = GetCorrection(id);
311 c->SetFile(fileName);
314 //____________________________________________________________________
316 AliCorrectionManagerBase::GetId(const TString& what) const
318 Int_t n = fCorrections.GetEntriesFast();
319 for (Int_t id = 0; id < n; id++) {
320 const Correction* c = GetCorrection(id);
321 if (what.EqualTo(c->GetName(), TString::kIgnoreCase)) return id;
326 //____________________________________________________________________
328 AliCorrectionManagerBase::EnableCorrection(Int_t id, Bool_t enable)
330 Correction* c = GetCorrection(id);
332 AliWarningF("Cannot enable non-existing correction at %d", id);
335 c->fEnabled = enable;
338 //____________________________________________________________________
340 AliCorrectionManagerBase::GetId(const TObject* obj) const
342 Int_t n = fCorrections.GetEntriesFast();
343 TClass* ocl = obj->IsA();
344 for (Int_t id = 0; id < n; id++) {
345 const Correction* c = GetCorrection(id);
346 if (ocl->InheritsFrom(c->fCls)) return id;
350 //____________________________________________________________________
352 AliCorrectionManagerBase::Get(Int_t id)
354 Correction* c = GetCorrection(id);
356 AliWarningF("Cannot find correction with id %d", id);
361 //____________________________________________________________________
363 AliCorrectionManagerBase::Get(Int_t id) const
365 const Correction* c = GetCorrection(id);
367 AliWarningF("Cannot find correction with id %d", id);
373 //____________________________________________________________________
375 AliCorrectionManagerBase::InitCorrections(ULong_t run,
383 if (force) fIsInit = false;
384 if (!CheckConditions(run, sys, sNN, fld, mc, sat)) return false;
385 if (!ReadCorrections(run, sys, sNN, fld, mc, sat)) return false;
396 //____________________________________________________________________
398 AliCorrectionManagerBase::CheckConditions(ULong_t run,
405 if (!fIsInit) return true;
407 AliInfo("We are already initialised - checking settings...");
413 AliWarningF("Initialised collision system %s (%d) and "
414 "passed same %s (%d) does not match",
415 AliForwardUtil::CollisionSystemString(fSys), fSys,
416 AliForwardUtil::CollisionSystemString(sys), sys);
419 if (TMath::Abs(fSNN - sNN) >= 10) {
420 AliWarningF("Initialised center of mass energy per nuclean "
421 "%s (%d) and passed same %s (%d) does not match",
422 AliForwardUtil::CenterOfMassEnergyString(fSNN), fSNN,
423 AliForwardUtil::CenterOfMassEnergyString(sNN), sNN);
427 AliWarningF("Initialied L3 magnetic field %s (%d) and passed "
428 "same %s (%d) does not match",
429 AliForwardUtil::MagneticFieldString(fField), fField,
430 AliForwardUtil::MagneticFieldString(fld), fld);
434 AliWarningF("Initialied data type (%s) and passed "
435 "same (%s) does not match",
436 (fMC ? "MC" : "real"), (mc ? "MC" : "real"));
439 if (fSatellite != sat) {
440 AliWarningF("Initialied collision ip type (%s) and passed "
441 "same (%s) does not match",
442 (fSatellite ? "satellite" : "nominal"),
443 (sat ? "satellite" : "nominal"));
447 AliWarning("Intialised parameters and these are not the same "
448 "- PROCEED WITH CAUTION!");
451 AliInfo("Initialized values consistent with data");
457 //____________________________________________________________________
459 AliCorrectionManagerBase::ReadCorrection(Int_t id,
468 // We should always open the database, since we're not
469 // streamingthat object to disk.
470 fDB = new AliOADBForward;
473 Correction* c = GetCorrection(id);
474 if (!c->fEnabled) return true;
475 return c->ReadIt(fDB, run, sys, sNN, fld, mc, sat, fDebug, fFallBack);
478 //____________________________________________________________________
480 AliCorrectionManagerBase::ReadCorrections(ULong_t run,
487 if (fIsInit) return true;
493 TMath::Abs(fSNN - sNN) < 11) {
494 // Already initialized for this - return
499 // We should always open the database, since we're not
500 // streamingthat object to disk.
501 fDB = new AliOADBForward;
510 Int_t n = fCorrections.GetEntriesFast();
512 for (Int_t id = 0; id < n; id++)
513 if (!ReadCorrection(id, run, sys, sNN, fld, mc, sat)) ret = false;
517 //====================================================================
518 AliCorrectionManagerBase::Correction::Correction()
528 //____________________________________________________________________
529 AliCorrectionManagerBase::Correction::Correction(const TString& tableName,
530 const TString& fileName,
534 : TNamed(tableName, fileName),
536 fClientCls(cls->GetName()),
537 fQueryFields(fields),
543 //____________________________________________________________________
544 AliCorrectionManagerBase::Correction::Correction(const Correction& o)
547 fClientCls(o.fClientCls),
548 fQueryFields(o.fQueryFields),
549 fEnabled(o.fEnabled),
550 fLastEntry(o.fLastEntry),
554 //____________________________________________________________________
555 AliCorrectionManagerBase::Correction&
556 AliCorrectionManagerBase::Correction::operator=(const Correction& o)
558 if (&o == this) return *this;
559 SetName(o.GetName());
560 SetTitle(o.GetTitle());
562 //fClientCls = o.fClientCls;
563 fQueryFields = o.fQueryFields;
564 fEnabled = o.fEnabled;
565 fLastEntry = o.fLastEntry;
570 //____________________________________________________________________
572 AliCorrectionManagerBase::Correction::MassageFields(ULong_t& run,
579 // Massage fields according to settings
580 if (!(fQueryFields & kRun)) run = kIgnoreValue;
581 if (!(fQueryFields & kSys)) sys = kIgnoreValue;
582 if (!(fQueryFields & kSNN)) sNN = kIgnoreValue;
583 if (!(fQueryFields & kField)) fld = AliOADBForward::kInvalidField;
584 if (!(fQueryFields & kMC)) mc = false;
585 if (!(fQueryFields & kSatellite)) sat = false;
588 //____________________________________________________________________
590 AliCorrectionManagerBase::Correction::OpenIt(AliOADBForward* db,
592 Bool_t fallback) const
595 Warning("OpenIt", "No DB passed");
599 // Check if table is open
600 if (db->FindTable(fName, true)) return true;
602 // if not try to open it
603 if (db->Open(fTitle, fName, false, vrb, fallback)) return true;
606 AliWarningF("Failed to open table %s from %s", GetName(), GetTitle());
607 AliWarningF("content of %s for %s:",
608 gSystem->WorkingDirectory(), GetName());
609 gSystem->Exec("pwd; ls -l");
613 //____________________________________________________________________
615 AliCorrectionManagerBase::Correction::ReadIt(AliOADBForward* db,
626 AliWarningF("Correction %s not enabled", GetName());
633 // Massage fields according to settings
634 MassageFields(run, sys, sNN, fld, mc, sat);
636 if (!OpenIt(db, vrb, fallback)) return false;
639 AliOADBForward::Entry* e = db->Get(fName, run, AliOADBForward::kDefault,
640 sys, sNN, fld, mc, sat);
641 // Check return value
642 if (!e || !e->fData) {
643 AliWarningF("Failed to get %s from database in %s with "
644 "run=%lu sys=%hu sNN=%hu fld=%hd %s %s",
645 GetName(), GetTitle(), run, sys, sNN, fld,
646 (mc ? "MC" : "real"), (sat ? "satellite" : "nominal"));
650 // Ge the returned data
651 TObject* o = e->fData;
653 const TClass* cl = TheClass();
654 // Check return class
655 if (!o->IsA()->InheritsFrom(cl)) {
656 AliWarningF("%p is not pointer to a %s object but a %s",
657 o, fCls->GetName(), o->ClassName());
663 fLastEntry = e->GetTitle();
668 //____________________________________________________________________
670 AliCorrectionManagerBase::Correction::StoreIt(AliOADBForward* db,
679 const char* meth) const
681 // Info("StoreIt", "Storing run=%lu sys=%hy sNN=%d fld=%d mc=%d sat=%d",
682 // run, sys, sNN, fld, mc, sat);
683 const TClass* cl = TheClass();
686 if (!obj->IsA()->InheritsFrom(cl)) {
687 AliWarningF("%p is not pointer to a %s object but a %s",
688 obj, cl->GetName(), obj->ClassName());
692 Bool_t local = file || !db;
693 TString fileName = (local ? file : fTitle.Data());
694 AliOADBForward* tdb = (local ? new AliOADBForward : db);
696 // Try to open the table read/write
697 if (!tdb->Open(fileName, Form("%s/%s", GetName(), meth), true, false)) {
698 AliWarningF("Failed to open table %s in %s", GetName(), fileName.Data());
702 // Massage fields according to settings
703 MassageFields(run, sys, sNN, fld, mc, sat);
705 // Try to insert the object
706 if (!tdb->Insert(fName, obj, run, sys, sNN, fld, mc, sat)) {
707 AliWarningF("Failed to insert into %s off database in %s with "
708 "run=%lu sys=%hu sNN=%hu fld=%hd %s %s",
709 GetName(), GetTitle(), run, sys, sNN, fld,
710 (mc ? "MC" : "real"), (sat ? "satellite" : "nominal"));
718 AliInfoF("Correction object %s written to DB in %s - merge this with "
719 "%s to store for good", obj->GetName(), fileName.Data(),
726 //____________________________________________________________________
728 AliCorrectionManagerBase::Correction::Get()
731 AliWarningF("Correction %s not enabled", GetName());
736 //____________________________________________________________________
738 AliCorrectionManagerBase::Correction::Get() const
741 AliWarningF("Correction %s not enabled", GetName());
747 //____________________________________________________________________
749 AliCorrectionManagerBase::Correction::TheClass() const
751 if (fCls) return fCls;
752 if (fClientCls.IsNull()) {
753 AliErrorF("No class name set for correction %s", GetName());
756 fCls = gROOT->GetClass(fClientCls);
758 AliErrorF("Couldn't get class %s for correction %s",
759 fClientCls.Data(), GetName());
765 //____________________________________________________________________
767 AliCorrectionManagerBase::Correction::Print(Option_t* option) const
769 AliForwardUtil::PrintTask(*this);
770 gROOT->IncreaseDirLevel();
771 PFB("Enabled", fEnabled);
773 gROOT->DecreaseDirLevel();
778 if (fQueryFields & kRun) flds.Append("run");
779 if (fQueryFields & kSys) flds.Append("|sys");
780 if (fQueryFields & kSNN) flds.Append("|sNN");
781 if (fQueryFields & kField) flds.Append("|field");
782 if (fQueryFields & kMC) flds.Append("|MC");
783 if (fQueryFields & kSatellite) flds.Append("|Satellite");
784 if (flds.BeginsWith("|")) flds.Remove(0,1);
786 const TClass* cl = TheClass();
788 PFV("Path",GetTitle());
789 PFV("Data class", cl->GetName());
790 PFV("Query fields", flds);
792 if (fObject && !fLastEntry.IsNull()) PF("Entry", fLastEntry);
796 if (opt.Contains("D") && fObject) {
797 gROOT->IncreaseDirLevel();
799 gROOT->DecreaseDirLevel();
801 gROOT->DecreaseDirLevel();
804 //____________________________________________________________________
806 AliCorrectionManagerBase::Correction::Browse(TBrowser* b)
808 b->Add(const_cast<TClass*>(fCls), "Class");
810 if (fQueryFields & kRun) flds.Append("run");
811 if (fQueryFields & kSys) flds.Append("|sys");
812 if (fQueryFields & kSNN) flds.Append("|sNN");
813 if (fQueryFields & kField) flds.Append("|field");
814 if (fQueryFields & kMC) flds.Append("|MC");
815 if (fQueryFields & kSatellite) flds.Append("|Satellite");
816 if (flds.BeginsWith("|")) flds.Remove(0,1);
818 b->Add(new TObjString(flds), "Query fields");
819 b->Add(new TParameter<bool>("Enabled", fEnabled));
820 b->Add(new TObjString(fLastEntry), "Entry");
821 if (fObject) b->Add(fObject);
823 //____________________________________________________________________
825 AliCorrectionManagerBase::Correction::CleanIt(AliOADBForward* db,
829 // Open the table for this correction
830 if (!OpenIt(db, verb , false)) {
831 Warning("CleanIt", "Failed to open table for %s", GetName());
835 // Get our table - should be here if the above succeeded
836 AliOADBForward::Table* t = db->FindTable(fName, !verb);
838 Warning("CleanIt", "Failed to get table for %s", GetName());
842 // Get some pointers and make a bit mask of entries to copy
843 TTree* tree = t->fTree;
844 Int_t nEnt = tree->GetEntries();
846 copy.ResetAllBits(false);
849 // Loop over all entries
850 Info("CleanIt", "Looping over %d entries in tree", nEnt);
851 for (Int_t i = 0; i < nEnt; i++) {
852 // Read in next entry
855 // Check if we got an object
856 AliOADBForward::Entry* e = t->fEntry;
860 // if (verb) e->Print();
862 // Now query the DB with this entry's fields
863 ULong_t run = e->fRunNo;
864 UShort_t sys = e->fSys;
865 UShort_t sNN = e->fSNN;
866 Short_t fld = e->fField;
868 Bool_t sat = e->fSatellite;
869 TString txt = e->GetTitle();
870 MassageFields(run, sys, sNN, fld, mc, sat);
873 Int_t r = t->GetEntry(run, AliOADBForward::kDefault,
874 sys, sNN, fld, mc, sat);
876 Warning("CleanIt","Uh! didn't get an entry for %s (%d)",
882 // Here, we hard-code some fixes to remove bad entries
883 if (fld != AliOADBForward::kInvalidField) {
886 if (sNN == 900 && fld <= 0) r = -1;
887 else if (sNN == 7000 && fld >= 0) r = -1;
888 else if (sNN == 2760 && fld >= 0) r = -1;
891 if (fld >= 0) r = -1;
897 Printf("%-10s (%3d %3d): %s %s",
898 (i==r ? "copied" : "ignored"), i, r, txt.Data(), GetName());
900 if (r != i) continue;
902 // If the entry found by the query and this entry is the same,
903 // then we should keep it
904 copy.SetBitNumber(i,true);
905 // runs.Append(Form("%7lu", run));
908 // Now loop over the entries of the tree again, this time
909 // checking if we should copy or not.
910 // Loop over all entries
911 for (Int_t i = 0; i < nEnt; i++) {
912 // If not marked for copy, continue to the next.
913 if (!copy.TestBitNumber(i)) continue;
915 // Read in next entry
918 // Check if we got an object
919 AliOADBForward::Entry* e = t->fEntry;
922 // Let's get the entry data and store that in a new correction
924 ULong_t run = e->fRunNo;
925 UShort_t sys = e->fSys;
926 UShort_t sNN = e->fSNN;
927 Short_t fld = e->fField;
929 Bool_t sat = e->fSatellite;
930 TObject* obj = e->fData;
931 TString txt = e->GetTitle();
932 Printf("Storing %3d: %s %s", i, txt.Data(), GetName());
933 if (!StoreIt(0, obj, run, sys, sNN, fld, mc, sat, dest.Data(),
934 AliOADBForward::Mode2String(t->fMode))) {
935 Warning("CleanIt", "Failed to write new entry to %s", dest.Data());
939 // Printf("Runs: %s", runs.Data());