]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/AliCorrectionManagerBase.cxx
Updates
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliCorrectionManagerBase.cxx
1 #include "AliCorrectionManagerBase.h"
2 #include "AliOADBForward.h"
3 #include "AliForwardUtil.h"
4 #include <AliLog.h>
5 #include <TMath.h>
6 #include <iostream>
7 #include <TROOT.h>
8 #include <TSystem.h>
9 #include <TBrowser.h>
10 #include <TParameter.h>
11 #include <TFileMerger.h>
12 #include <TBits.h>
13 #include <TTree.h>
14
15 //____________________________________________________________________
16 AliCorrectionManagerBase::AliCorrectionManagerBase()
17   : fCorrections(),
18     fIsInit(false),
19     fRun(kIgnoreValue), 
20     fSys(kIgnoreValue), 
21     fSNN(kIgnoreValue), 
22     fField(kIgnoreField), 
23     fMC(false), 
24     fSatellite(false), 
25     fDB(0),
26     fDebug(false),
27     fFallBack(false)
28 {
29 }
30
31 //____________________________________________________________________
32 AliCorrectionManagerBase::AliCorrectionManagerBase(Bool_t)
33   : fCorrections(16),
34     fIsInit(false),
35     fRun(kIgnoreValue), 
36     fSys(kIgnoreValue), 
37     fSNN(kIgnoreValue), 
38     fField(kIgnoreField), 
39     fMC(false), 
40     fSatellite(false), 
41     fDB(0),
42     fDebug(false),
43     fFallBack(false)
44 {
45   fCorrections.SetOwner(false);
46   fCorrections.SetName("corrections");
47 }
48 //____________________________________________________________________
49 AliCorrectionManagerBase::AliCorrectionManagerBase(const 
50                                                    AliCorrectionManagerBase& o)
51   : TObject(o),
52     fCorrections(),
53     fIsInit(o.fIsInit),
54     fRun(o.fRun), 
55     fSys(o.fSys), 
56     fSNN(o.fSNN), 
57     fField(o.fField), 
58     fMC(o.fMC), 
59     fSatellite(o.fSatellite), 
60     fDB(o.fDB),
61     fDebug(o.fDebug),
62     fFallBack(o.fFallBack)
63 {
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);
68   }
69 }
70 //____________________________________________________________________
71 AliCorrectionManagerBase&
72 AliCorrectionManagerBase::operator=(const AliCorrectionManagerBase& o)
73 {
74   if (&o == this) return *this;
75
76   fIsInit       = o.fIsInit;
77   fRun          = o.fRun; 
78   fSys          = o.fSys; 
79   fSNN          = o.fSNN; 
80   fField        = o.fField; 
81   fMC           = o.fMC; 
82   fSatellite    = o.fSatellite;
83   fDB           = o.fDB;
84   fDebug        = o.fDebug;
85   fFallBack     = o.fFallBack;
86
87   fCorrections.Clear();
88   Int_t n = o.fCorrections.GetEntriesFast();
89   for (Int_t i = 0; i < n; i++) { 
90     fCorrections.AddAt(o.fCorrections.At(i), i);
91   }
92   return *this;
93 }
94
95 //____________________________________________________________________
96 AliCorrectionManagerBase::~AliCorrectionManagerBase()
97 {
98   // fCorrections.Delete();
99 }
100
101 #define PF(N,V,...)                                     \
102   AliForwardUtil::PrintField(N,V, ## __VA_ARGS__)
103 #define PFB(N,FLAG)                             \
104   do {                                                                  \
105     AliForwardUtil::PrintName(N);                                       \
106     std::cout << std::boolalpha << (FLAG) << std::noboolalpha << std::endl; \
107   } while(false)
108 #define PFV(N,VALUE)                                    \
109   do {                                                  \
110     AliForwardUtil::PrintName(N);                       \
111     std::cout << (VALUE) << std::endl; } while(false)
112
113 //____________________________________________________________________
114 void
115 AliCorrectionManagerBase::Print(Option_t* option) const
116 {
117   AliForwardUtil::PrintTask(*this);
118   gROOT->IncreaseDirLevel();
119   PFB("Initialized", fIsInit);
120   if (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);
127   }
128   TString opt(option);
129   opt.ToUpper();
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);
135       c->Print(option);
136     }
137     // gROOT->DecreaseDirLevel();  
138   }
139   gROOT->DecreaseDirLevel();  
140 }
141
142 //____________________________________________________________________
143 void
144 AliCorrectionManagerBase::Browse(TBrowser* b)
145 {
146   b->Add(&fCorrections);
147 }
148
149 //____________________________________________________________________
150 void
151 AliCorrectionManagerBase::SetPrefix(const TString& prefix)
152 {
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));
159   }
160 }
161
162 //____________________________________________________________________
163 Bool_t
164 AliCorrectionManagerBase::Store(TObject*     o,
165                                 ULong_t     runNo,
166                                 UShort_t    sys, 
167                                 UShort_t    sNN, 
168                                 Short_t     field, 
169                                 Bool_t      mc,
170                                 Bool_t      sat, 
171                                 const char* file,
172                                 const char* meth) const
173 {
174   Bool_t ret = false;
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;
179
180     ret = c->StoreIt(fDB, o, runNo, sys, sNN, field, mc, sat, file, meth);
181     break;
182   }
183   return ret;
184 }
185     
186 //____________________________________________________________________
187 Bool_t
188 AliCorrectionManagerBase::Append(const TString& addition, 
189                                  const TString& destination) const
190 {
191   if (addition.IsNull()) {
192     AliWarning("No addition specified");
193     return false;
194   }
195   if (destination.IsNull()) { 
196     AliWarning("No destination storage specified");
197     return false;
198   }
199   TFileMerger merger;
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());
206     return false;
207   }
208   if (destination.BeginsWith("$OADB_PATH") ||
209       destination.BeginsWith("$ALICE_ROOT"))
210     AliInfoF("Now commit %s to git", destination.Data());
211   return true;
212 }
213 //____________________________________________________________________
214 Bool_t
215 AliCorrectionManagerBase::CleanUp(const TString& destination, Bool_t verb) const
216 {
217   AliOADBForward* db = fDB;
218   if (!db) db = new AliOADBForward;
219
220   for (Int_t i = 0; i < 32; i++) {
221     const Correction* c = GetCorrection(i);
222     if (!c) continue;
223
224     if (!c->fEnabled) {
225       Info("CleanUp", "Table %s not enabled", c->GetName());
226       continue;
227     }
228     Info("CleanUp", "Will clean up table %s", c->GetName());
229     c->CleanIt(db, destination, verb);
230   }
231   return true;
232 }
233  
234   
235 //____________________________________________________________________
236 void
237 AliCorrectionManagerBase::EnableCorrections(UInt_t what)
238 {
239   for (Int_t i = 0; i < 32; i++) {
240     if (!GetCorrection(i)) continue;
241     EnableCorrection(i, (1 << i) & what);
242   }
243 }
244 //____________________________________________________________________
245 Bool_t
246 AliCorrectionManagerBase::CheckCorrections(UInt_t what, Bool_t verbose) const
247 {
248   Bool_t ret = true;
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);
253     if (!c) {
254       ret = false;
255       if (verbose) 
256         AliWarningF("No correction registered for bit %d", i);
257       continue;
258     }
259     const TObject* o = c->Get();
260     if (!o) { 
261       ret = false;
262       if (verbose) 
263         AliWarningF("Correction %s (bit %d) not initialized!", c->GetName(), i);
264       continue;
265     }
266   }
267   return ret;
268 }
269
270 //____________________________________________________________________
271 void
272 AliCorrectionManagerBase::RegisterCorrection(Int_t id, Correction* corr)
273 {
274   fCorrections.AddAtAndExpand(corr, id);
275 }
276
277 //____________________________________________________________________
278 void
279 AliCorrectionManagerBase::RegisterCorrection(Int_t id, 
280                                              const TString& tableName, 
281                                              const TString& fileName, 
282                                              TClass*        cls, 
283                                              UShort_t       fields,
284                                              Bool_t         enabled)
285 {
286   RegisterCorrection(id,new Correction(tableName,fileName,cls,fields,enabled));
287 }
288
289 //____________________________________________________________________
290 AliCorrectionManagerBase::Correction*
291 AliCorrectionManagerBase::GetCorrection(Int_t id)
292 {
293   if (id < 0 || id > fCorrections.GetEntriesFast()) return 0;
294   return static_cast<Correction*>(fCorrections.At(id));
295 }
296
297 //____________________________________________________________________
298 const AliCorrectionManagerBase::Correction*
299 AliCorrectionManagerBase::GetCorrection(Int_t id) const
300 {
301   if (id < 0 || id > fCorrections.GetEntriesFast()) return 0;
302   return static_cast<Correction*>(fCorrections.At(id));
303 }
304
305 //____________________________________________________________________
306 void 
307 AliCorrectionManagerBase::SetCorrectionFile(Int_t id, const TString& fileName)
308 {
309   Correction* c = GetCorrection(id);
310   if (!c) return;
311   c->SetFile(fileName);
312 }
313
314 //____________________________________________________________________
315 Int_t
316 AliCorrectionManagerBase::GetId(const TString& what) const
317 {
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;
322   }
323   return -1;
324 }
325
326 //____________________________________________________________________
327 void
328 AliCorrectionManagerBase::EnableCorrection(Int_t id, Bool_t enable)
329 {
330   Correction* c = GetCorrection(id);
331   if (!c) { 
332     AliWarningF("Cannot enable non-existing correction at %d", id);
333     return;
334   }
335   c->fEnabled = enable;
336 }
337
338 //____________________________________________________________________
339 Int_t
340 AliCorrectionManagerBase::GetId(const TObject* obj) const
341 {
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;
347   }
348   return -1;
349 }
350 //____________________________________________________________________
351 TObject*
352 AliCorrectionManagerBase::Get(Int_t id)
353 {
354   Correction* c = GetCorrection(id);
355   if (!c) {
356     AliWarningF("Cannot find correction with id %d", id);
357     return 0;
358   }
359   return c->Get();
360 }
361 //____________________________________________________________________
362 const TObject*
363 AliCorrectionManagerBase::Get(Int_t id) const
364 {
365   const Correction* c = GetCorrection(id);
366   if (!c) {
367     AliWarningF("Cannot find correction with id %d", id);
368     return 0;
369   }
370   return c->Get();
371 }
372
373 //____________________________________________________________________
374 Bool_t
375 AliCorrectionManagerBase::InitCorrections(ULong_t    run, 
376                                           UShort_t   sys, 
377                                           UShort_t   sNN, 
378                                           Short_t    fld, 
379                                           Bool_t     mc, 
380                                           Bool_t     sat,
381                                           Bool_t     force)
382 {
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;
386   fIsInit = true;
387
388   if (fDB) {
389     delete fDB;
390     fDB = 0;
391   }
392
393   return true;
394 }
395
396 //____________________________________________________________________
397 Bool_t
398 AliCorrectionManagerBase::CheckConditions(ULong_t    run, 
399                                           UShort_t   sys, 
400                                           UShort_t   sNN, 
401                                           Short_t    fld, 
402                                           Bool_t     mc, 
403                                           Bool_t     sat)
404 {
405   if (!fIsInit) return true;
406
407   AliInfo("We are already initialised - checking settings...");
408   Bool_t same = true;
409   if (fRun != run) {
410     same = false;
411   }
412   if (fSys != sys) { 
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);
417     same = false;
418   }
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);
424     same = false;
425   }
426   if (fField != fld) {
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);
431       same = false;
432   }
433   if (fMC != mc) {
434     AliWarningF("Initialied data type (%s) and passed "
435                 "same (%s) does not match", 
436                 (fMC ? "MC" : "real"), (mc ? "MC" : "real"));
437     same = false;
438   }
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"));
444     same = false;
445   }
446   if (!same) {
447     AliWarning("Intialised parameters and these are not the same " 
448                "- PROCEED WITH CAUTION!");
449   }
450   else
451     AliInfo("Initialized values consistent with data");
452   
453   return true;
454
455 }
456
457 //____________________________________________________________________
458 Bool_t
459 AliCorrectionManagerBase::ReadCorrection(Int_t      id,
460                                          ULong_t    run, 
461                                          UShort_t   sys, 
462                                          UShort_t   sNN, 
463                                          Short_t    fld, 
464                                          Bool_t     mc, 
465                                          Bool_t     sat)
466 {
467   if (!fDB) {
468     // We should always open the database, since we're not
469     // streamingthat object to disk.
470     fDB = new AliOADBForward;
471   }
472
473   Correction* c = GetCorrection(id);
474   if (!c->fEnabled) return true;
475   return c->ReadIt(fDB, run, sys, sNN, fld, mc, sat, fDebug, fFallBack);
476 }
477
478 //____________________________________________________________________
479 Bool_t
480 AliCorrectionManagerBase::ReadCorrections(ULong_t    run, 
481                                           UShort_t   sys, 
482                                           UShort_t   sNN, 
483                                           Short_t    fld, 
484                                           Bool_t     mc, 
485                                           Bool_t     sat)
486 {
487   if (fIsInit) return true;
488   if (fRun       == run && 
489       fSys       == sys && 
490       fField     == fld && 
491       fMC        == mc  && 
492       fSatellite == sat &&
493       TMath::Abs(fSNN - sNN) < 11) { 
494     // Already initialized for this - return
495     fIsInit = true;
496     return true;
497   }
498   if (!fDB) {
499     // We should always open the database, since we're not
500     // streamingthat object to disk.
501     fDB = new AliOADBForward;
502   }
503
504   fRun       = run;
505   fSys       = sys; 
506   fSNN       = sNN;
507   fField     = fld;
508   fMC        = mc;
509   fSatellite = sat;
510   Int_t  n   = fCorrections.GetEntriesFast();
511   Bool_t ret = true;
512   for (Int_t id = 0; id < n; id++) 
513     if (!ReadCorrection(id, run, sys, sNN, fld, mc, sat)) ret = false;
514   return ret;
515 }
516
517 //====================================================================
518 AliCorrectionManagerBase::Correction::Correction() 
519   : TNamed(), 
520     fCls(0), 
521     fClientCls(""),
522     fQueryFields(0), 
523     fEnabled(false), 
524     fLastEntry(),
525     fObject(0)
526 {}
527
528 //____________________________________________________________________
529 AliCorrectionManagerBase::Correction::Correction(const TString& tableName, 
530                                                  const TString& fileName, 
531                                                  TClass*        cls,
532                                                  UShort_t       fields,
533                                                  Bool_t         enabled) 
534   : TNamed(tableName, fileName), 
535     fCls(cls), 
536     fClientCls(cls->GetName()),
537     fQueryFields(fields), 
538     fEnabled(enabled), 
539     fLastEntry(""),
540     fObject(0)
541 {}
542
543 //____________________________________________________________________
544 AliCorrectionManagerBase::Correction::Correction(const Correction& o)
545   : TNamed(o), 
546     fCls(o.fCls), 
547     fClientCls(o.fClientCls),
548     fQueryFields(o.fQueryFields),
549     fEnabled(o.fEnabled), 
550     fLastEntry(o.fLastEntry),
551     fObject(o.fObject)
552 {}
553
554 //____________________________________________________________________
555 AliCorrectionManagerBase::Correction&
556 AliCorrectionManagerBase::Correction::operator=(const Correction& o)
557 {
558   if (&o == this) return *this;
559   SetName(o.GetName());
560   SetTitle(o.GetTitle());
561   fCls             = o.fCls;
562   //fClientCls       = o.fClientCls;
563   fQueryFields     = o.fQueryFields;
564   fEnabled         = o.fEnabled;
565   fLastEntry       = o.fLastEntry;
566   fObject          = o.fObject;
567   return *this;
568 }
569
570 //____________________________________________________________________
571 void
572 AliCorrectionManagerBase::Correction::MassageFields(ULong_t&  run,
573                                                     UShort_t& sys,
574                                                     UShort_t& sNN, 
575                                                     Short_t & fld, 
576                                                     Bool_t&   mc, 
577                                                     Bool_t&   sat) const
578 {
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;
586 }
587
588 //____________________________________________________________________
589 Bool_t
590 AliCorrectionManagerBase::Correction::OpenIt(AliOADBForward* db, 
591                                              Bool_t vrb, 
592                                              Bool_t fallback) const
593 {
594   if (!db) {
595     Warning("OpenIt", "No DB passed");
596     return false;
597   }
598
599   // Check if table is open
600   if (db->FindTable(fName, true)) return true;
601
602   //  if not try to open it 
603   if (db->Open(fTitle, fName, false, vrb, fallback)) return true;
604
605   // Give warning 
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");
610   return false;
611 }
612  
613 //____________________________________________________________________
614 Bool_t
615 AliCorrectionManagerBase::Correction::ReadIt(AliOADBForward* db, 
616                                              ULong_t         run, 
617                                              UShort_t        sys, 
618                                              UShort_t        sNN, 
619                                              Short_t         fld, 
620                                              Bool_t          mc, 
621                                              Bool_t          sat,
622                                              Bool_t          vrb,
623                                              Bool_t          fallback)
624 {
625   if (!fEnabled) {
626     AliWarningF("Correction %s not enabled", GetName());
627     return 0;
628   }
629
630   // Assume failure 
631   fObject = 0;
632
633   // Massage fields according to settings 
634   MassageFields(run, sys, sNN, fld, mc, sat);
635
636   if (!OpenIt(db, vrb, fallback)) return false;
637
638   // Query database 
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"));
647     return false;
648   }
649
650   // Ge the returned data
651   TObject* o = e->fData;
652
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());
658     return false;
659   }
660
661   // Success 
662   fObject    = o;
663   fLastEntry = e->GetTitle();
664
665   return true;
666 }
667
668 //____________________________________________________________________
669 Bool_t
670 AliCorrectionManagerBase::Correction::StoreIt(AliOADBForward* db, 
671                                               TObject*        obj,
672                                               ULong_t         run, 
673                                               UShort_t        sys, 
674                                               UShort_t        sNN, 
675                                               Short_t         fld, 
676                                               Bool_t          mc, 
677                                               Bool_t          sat,
678                                               const char*     file, 
679                                               const char*     meth) const
680 {
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();
684
685   // Check value class 
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());
689     return false;
690   }
691
692   Bool_t          local    = file || !db;
693   TString         fileName = (local ? file : fTitle.Data());
694   AliOADBForward* tdb      = (local ? new AliOADBForward : db);
695   
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());
699     return false;
700   }
701
702   // Massage fields according to settings 
703   MassageFields(run, sys, sNN, fld, mc, sat);
704   
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"));
711     return false;
712   }
713
714   if (local) { 
715     tdb->Close();
716     delete tdb;
717
718     AliInfoF("Correction object %s written to DB in %s - merge this with "
719              "%s to store for good", obj->GetName(), fileName.Data(), 
720              GetTitle());
721   }
722
723   // Success 
724   return true;
725 }
726 //____________________________________________________________________
727 TObject*
728 AliCorrectionManagerBase::Correction::Get()   
729 {
730   if (!fEnabled) {
731     AliWarningF("Correction %s not enabled", GetName());
732     return 0;
733   }
734   return fObject;
735 }
736 //____________________________________________________________________
737 const TObject*
738 AliCorrectionManagerBase::Correction::Get() const
739 {
740   if (!fEnabled) {
741     AliWarningF("Correction %s not enabled", GetName());
742     return 0;
743   }
744   return fObject;
745 }
746
747 //____________________________________________________________________
748 const TClass*
749 AliCorrectionManagerBase::Correction::TheClass() const
750 {
751   if (fCls) return fCls;
752   if (fClientCls.IsNull()) { 
753     AliErrorF("No class name set for correction %s", GetName());
754     return 0;
755   }
756   fCls = gROOT->GetClass(fClientCls);
757   if (!fCls) { 
758     AliErrorF("Couldn't get class %s for correction %s", 
759               fClientCls.Data(), GetName());
760     return 0;
761   }
762   return fCls;
763 }
764
765 //____________________________________________________________________
766 void
767 AliCorrectionManagerBase::Correction::Print(Option_t* option) const
768 {
769   AliForwardUtil::PrintTask(*this);
770   gROOT->IncreaseDirLevel();
771   PFB("Enabled", fEnabled);
772   if (!fEnabled) {
773     gROOT->DecreaseDirLevel();
774     return;
775   }
776
777   TString flds;
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);
785
786   const TClass* cl = TheClass();
787
788   PFV("Path",GetTitle());
789   PFV("Data class", cl->GetName());
790   PFV("Query fields", flds);
791   
792   if (fObject && !fLastEntry.IsNull())  PF("Entry", fLastEntry);
793   
794   TString opt(option);
795   opt.ToUpper();
796   if (opt.Contains("D") && fObject) {
797     gROOT->IncreaseDirLevel();
798     fObject->Print();
799     gROOT->DecreaseDirLevel();
800   }
801   gROOT->DecreaseDirLevel();
802 }
803
804 //____________________________________________________________________
805 void
806 AliCorrectionManagerBase::Correction::Browse(TBrowser* b)
807 {
808   b->Add(const_cast<TClass*>(fCls), "Class");
809   TString flds;
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);
817
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);
822 }
823 //____________________________________________________________________
824 Bool_t
825 AliCorrectionManagerBase::Correction::CleanIt(AliOADBForward* db,
826                                               const TString& dest,
827                                               Bool_t verb) const
828 {
829   // Open the table for this correction 
830   if (!OpenIt(db, verb , false)) {
831     Warning("CleanIt", "Failed to open table for %s", GetName());
832     return false;
833   }
834
835   // Get our table - should be here if the above succeeded
836   AliOADBForward::Table* t = db->FindTable(fName, !verb);
837   if (!t) {
838     Warning("CleanIt", "Failed to get table for %s", GetName());
839     return false;
840   }
841
842   // Get some pointers and make a bit mask of entries to copy
843   TTree* tree = t->fTree;
844   Int_t  nEnt = tree->GetEntries();
845   TBits  copy(nEnt);
846   copy.ResetAllBits(false);
847   // TString runs;
848
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 
853     tree->GetEntry(i);
854
855     // Check if we got an object 
856     AliOADBForward::Entry* e = t->fEntry;    
857     if (!e) continue;
858     
859     // Let's see it 
860     // if (verb) e->Print();
861
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;
867     Bool_t   mc  = e->fMC;
868     Bool_t   sat = e->fSatellite;
869     TString  txt = e->GetTitle();
870     MassageFields(run, sys, sNN, fld, mc, sat);
871
872     
873     Int_t r = t->GetEntry(run, AliOADBForward::kDefault, 
874                           sys, sNN, fld, mc, sat);
875     if (r < 0) { 
876       Warning("CleanIt","Uh! didn't get an entry for %s (%d)",
877               txt.Data(), i);
878       r = i;
879       // continue;
880     }
881 #if 0
882     // Here, we hard-code some fixes to remove bad entries 
883     if (fld != AliOADBForward::kInvalidField) {
884       switch (sys) {
885       case 1:
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;
889         break;
890       case 2:
891         if (fld >= 0) r = -1;
892         break;
893       }
894     }
895 #endif
896
897     Printf("%-10s (%3d %3d): %s %s", 
898            (i==r ? "copied" : "ignored"), i, r, txt.Data(), GetName());
899
900     if (r != i) continue;
901
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));
906   }
907   
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; 
914
915     // Read in next entry 
916     tree->GetEntry(i);
917
918     // Check if we got an object 
919     AliOADBForward::Entry* e = t->fEntry;    
920     if (!e) continue;
921   
922     // Let's get the entry data and store that in a new correction
923     // table
924     ULong_t  run = e->fRunNo;
925     UShort_t sys = e->fSys;
926     UShort_t sNN = e->fSNN;
927     Short_t  fld = e->fField;
928     Bool_t   mc  = e->fMC;
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());
936       continue;
937     }
938   }
939   // Printf("Runs: %s", runs.Data());
940   return true;
941 }
942
943 //
944 // EOF
945 //