]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGLF/FORWARD/analysis2/AliOADBForward.h
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / AliOADBForward.h
CommitLineData
8449e3e0 1// -*- mode: C++ -*-
2#ifndef ALIOADBFORWARD_H
3#define ALIOADBFORWARD_H
4#include <TNamed.h>
5#include <TString.h>
6#include <TMap.h>
7class TFile;
8class TTree;
9class TBrowser;
10class TList;
11
12/**
13 * Container/handler of Forward calibration objects.
14 */
15class AliOADBForward : public TObject
16{
17public:
18 //=== Options ======================================================
19 /**
20 * Options for selecting the entries according to the run number
21 * given in the query.
22 */
23 enum ERunSelectMode {
24 kDefault = 0,
25 /**
26 * Select only entries from exactly the run given.
27 */
28 kExact = 1,
29 /**
30 * Select the entry with the largest run number
31 */
32 kNewest = 2,
33 /**
34 * Select entries from the run nearest (possibly constrained to be
35 * older or newer) to the run given in the query
36 */
37 kNear = 3,
38 /**
39 * Select entries from runs that are older than the run given in
40 * the query. The oldest entry with the largest run number
41 * smaller than the given run is selected.
42 */
43 kOlder = 4,
44 /**
45 * Select entries from runs that are newer than the run given.
46 * Select the entries with the smallest run number which is larger
47 * than the given run.
48 */
49 kNewer = 5
50 };
c8b1a7db 51 /**
52 * Return the mode as a string
53 *
54 * @param mode Mode
55 *
56 * @return Stringified mode
57 */
8449e3e0 58 static const char* Mode2String(ERunSelectMode mode);
c8b1a7db 59 /**
60 * Parse a string to get the mode
61 *
62 * @param str Input string
63 *
64 * @return Mode
65 */
8449e3e0 66 static ERunSelectMode String2Mode(const TString& str);
c8b1a7db 67 /**
68 * Return mode as an integer
69 *
70 * @param mode Mode
71 *
72 * @return Mode identifier
73 */
8449e3e0 74 static ERunSelectMode Int2Mode(Int_t mode);
75 /**
76 * Various flags
77 *
78 */
79 enum {
80 /**
81 * The maximum distance to the given run when selecting in kNear
82 * mode. Currently this is set to a large number to allow
83 * selection of any entry. This should change
84 */
2a50d35b 85 kMaxNearDistance = 1000000,
86 kInvalidField = 999
8449e3e0 87 };
88 //=== Entry ========================================================
89 /**
90 * An entry in the FORWARD OADB database tables
91 *
92 */
93 class Entry : public TObject
94 {
95 public:
96 /**
97 * Constructor
98 *
99 * @param runNo Run number
100 * @param sys Collision system (1:pp, 2:pbpb, 3:ppb)
101 * @param sNN Center of mass energy (GeV)
102 * @param field L3 magnetic field (kG)
103 * @param mc True if for MC only
c8b1a7db 104 * @param sat For satellite events
8449e3e0 105 * @param o Correction object
106 */
107 Entry(ULong_t runNo = 0,
108 UShort_t sys = 0,
109 UShort_t sNN = 0,
110 Short_t field = 0,
111 Bool_t mc = false,
112 Bool_t sat = false,
113 TObject* o = 0);
114 /**
115 * Copy constructor
116 *
117 * @param o Object to copy from
118 */
119 Entry(const Entry& o);
120 /**
121 * Destructor
122 */
123 virtual ~Entry() {}
124 /**
125 * Assignment operator
126 *
127 * @param o Object to assign from
128 *
129 * @return Reference to this object
130 */
131 Entry& operator=(const Entry& o);
132 /**
133 * Get the title of the entry
134 *
135 * @return stringified fields
136 */
137 const char* GetTitle() const;
138 /**
139 * Print this entry
140 *
141 * @param option Not used
142 */
143 void Print(Option_t* option="") const; //*MENU*
144 ULong_t fRunNo; // Run number
145 UShort_t fSys; // Collision system (1: pp, 2: pbpb, 3: ppb)
146 UShort_t fSNN; // Center of mass energy
147 Short_t fField; // L3 magnetic field
148 Bool_t fMC; // True if only for MC
149 Bool_t fSatellite; // Satelitte events
150 TObject* fData; // Correction object
151 UInt_t fTimestamp; // When the object was stored
152 ULong_t fAliROOTRevision; // Revision of AliROOT used
153 TString fAuthor; // Author of calibration/correction
154
155 ClassDef(Entry,2); // Entry in PWGLF/Forward OADB
156 };
157
158 //=== Table ========================================================
159 /**
160 * A table on the Forward OADB - the underlying storage is a TTree
161 * containing Entry objects.
162 *
163 */
164 class Table : public TObject
165 {
166 public:
167 /**
168 * Constructor
169 *
170 * @param tree Tree
171 * @param isNew Whether to make the branch
c8b1a7db 172 * @param mode How to select on the run number
8449e3e0 173 *
174 * @return
175 */
176 Table(TTree* tree, Bool_t isNew, ERunSelectMode mode=kNear);
177 /**
178 * Copy constructor
179 *
180 * @param other Object to copy from
181 */
182 Table(const Table& other);
183 /**
184 * Destructor.
185 *
186 * Closes the corresponding file
187 */
188 ~Table();
189 /**
190 * Assignemt operator
191 *
192 * @param other Object to assign form
193 *
194 * @return Reference to this object
195 */
196 Table& operator=(const Table& other);
197 /**
198 * Set the verbosity
199 *
200 * @param verb
201 */
202 void SetVerbose(Bool_t verb=true) { fVerbose = verb; }
2a50d35b 203 /**
204 * Set wheter to enable fall-back queries
205 *
206 * @param use If true, enable fall-back queries
207 */
208 void SetEnableFallBack(Bool_t use=true) { fFallBack = use; }
8449e3e0 209 // -----------------------------------------------------------------
210 /**
211 * Get the name of the tree
212 *
213 * @return Table name
214 */
215 const Char_t* GetTableName() const;
216 /**
217 * Overload TObject::GetName
218 *
219 * @return Name
220 */
221 const Char_t* GetName() const;
222
223 // -----------------------------------------------------------------
224 /**
225 * @{
226 * @name Open/close/flush
227 */
228 /**
229 * Flush to disk
230 *
231 * @return true on success
232 */
233 Bool_t Update();
234 /**
235 * Close connection
236 *
237 * @return true on success
238 */
239 Bool_t Close();
240 /* @} */
241 // -----------------------------------------------------------------
242 /**
243 * @{
244 * @name Queries
245 */
246 /**
247 * Query the tree
248 *
c8b1a7db 249 * @param runNo Run number
250 * @param mode Run selection mode
8449e3e0 251 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
252 * @param sNN Center of mass energy (GeV)
253 * @param fld L3 magnetic field (kG)
254 * @param mc For MC only
255 * @param sat For satellite events
256 * @param mode How to select on the run number
257 *
258 * @return Found entry number or negative number in case of problems
259 */
260 Int_t Query(ULong_t runNo = 0,
261 ERunSelectMode mode = kNear,
262 UShort_t sys = 0,
263 UShort_t sNN = 0,
2a50d35b 264 Short_t fld = kInvalidField,
8449e3e0 265 Bool_t mc = false,
266 Bool_t sat = false) const;
267 /**
268 * Run a query with pre-build conditions
269 *
270 * @param q query string
271 * @param runNo The given run number
272 * @param mode Run selection mode
273 *
274 * @return Entry number of selected entry
275 */
276 Int_t Query(ULong_t runNo,
277 ERunSelectMode mode,
278 const TString& q) const;
279 /**
280 * Insert a new entry into the tree
281 *
282 * @param o Object to write
283 * @param runNo Run number
284 * @param sys Collision system (1: pp, 2:PbPb, 3:pPb)
285 * @param sNN Center of mass energy (GeV)
286 * @param field L3 magnetic field (kG)
287 * @param mc If true, only for MC
c8b1a7db 288 * @param sat For satellite interactions
8449e3e0 289 * @param aliRev AliROOT revision
c8b1a7db 290 * @param author Creater of this correction
8449e3e0 291 *
292 * @return true on success
293 */
294 Bool_t Insert(TObject* o,
295 ULong_t runNo,
296 UShort_t sys,
297 UShort_t sNN,
298 Short_t field,
299 Bool_t mc=false,
300 Bool_t sat=false,
301 ULong_t aliRev=0,
302 const TString& author="");
6953b59b 303 /**
304 * Query the tree for an object. The strategy is as follows.
305 *
306 * - First query with all fields
307 * - If this returns a single entry, return that.
308 * - If not, then ignore the run number (if given)
309 * - If this returns a single entry, return that
310 * - If not, and fall-back is enabled, then
311 * - Ignore the collision energy (if given)
312 * - If this returns a single entry, return that.
313 * - If not, ignore all passed values
314 * - If this returns a single entry, return that.
315 * - Otherwise, give up and return -1
316 * - Otherwise, give up and return -1
317 *
318 * This allow us to specify default objects for a period, and for
319 * collision system, energy, and field setting.
320 *
321 * @param run Run number
322 * @param mode Run selection mode
323 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
324 * @param sNN Center of mass energy (GeV)
325 * @param fld L3 magnetic field (kG)
326 * @param mc For MC only
327 * @param sat For satellite interactions
328 *
329 * @return Found entry number, or -1
330 */
331 Int_t GetEntry(ULong_t run = 0,
332 ERunSelectMode mode = kNear,
333 UShort_t sys = 0,
334 UShort_t sNN = 0,
335 Short_t fld = 0,
336 Bool_t mc = false,
337 Bool_t sat = false) const;
8449e3e0 338 /**
339 * Query the tree for an object. The strategy is as follows.
340 *
341 * - First query with all fields
342 * - If this returns a single entry, return that.
343 * - If not, then ignore the run number (if given)
344 * - If this returns a single entry, return that
2a50d35b 345 * - If not, and fall-back is enabled, then
346 * - Ignore the collision energy (if given)
347 * - If this returns a single entry, return that.
348 * - If not, ignore all passed values
349 * - If this returns a single entry, return that.
350 * - Otherwise, give up and return null
8449e3e0 351 * - Otherwise, give up and return null
352 *
353 * This allow us to specify default objects for a period, and for
354 * collision system, energy, and field setting.
355 *
356 * @param run Run number
c8b1a7db 357 * @param mode Run selection mode
8449e3e0 358 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
359 * @param sNN Center of mass energy (GeV)
360 * @param fld L3 magnetic field (kG)
361 * @param mc For MC only
c8b1a7db 362 * @param sat For satellite interactions
8449e3e0 363 *
364 * @return Found entry, or null
365 */
2a50d35b 366 Entry* Get(ULong_t run = 0,
367 ERunSelectMode mode = kNear,
368 UShort_t sys = 0,
369 UShort_t sNN = 0,
370 Short_t fld = 0,
371 Bool_t mc = false,
372 Bool_t sat = false) const;
8449e3e0 373 /**
374 * Query the tree for an object. The strategy is as follows.
375 *
376 * - First query with all fields
377 * - If this returns a single entry, return that.
378 * - If not, then ignore the run number (if given)
379 * - If this returns a single entry, return that
380 * - Otherwise, give up and return null
381 *
382 * This allow us to specify default objects for a period, and for
383 * collision system, energy, and field setting.
384 *
385 * @param run Run number
c8b1a7db 386 * @param mode Run selection mode
8449e3e0 387 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
388 * @param sNN Center of mass energy (GeV)
389 * @param fld L3 magnetic field (kG)
390 * @param mc For MC only
c8b1a7db 391 * @param sat For satellite interactions
8449e3e0 392 *
393 * @return Found data, or null
394 */
2a50d35b 395 TObject* GetData(ULong_t run = 0,
396 ERunSelectMode mode = kNear,
397 UShort_t sys = 0,
398 UShort_t sNN = 0,
399 Short_t fld = 0,
400 Bool_t mc = false,
401 Bool_t sat = false) const;
8449e3e0 402 /* @} */
403 /**
404 * Print the contents of the tree
405 *
406 * @param option Passed to TTree::Scan
407 */
408 void Print(Option_t* option="") const; //*MENU*
409 /**
410 * Browse this table
411 */
412 void Browse(TBrowser* b);
413 /**
414 * Check if the tree was opened.
415 *
416 * @param rw If true, also check if the file is read/write
417 *
418 * @return true if everything is dandy
419 */
420 Bool_t IsOpen(Bool_t rw=false) const;
421
422 TTree* fTree; // Our tree
423 Entry* fEntry; // Entry cache
424 Bool_t fVerbose; // To be verbose or not
2a50d35b 425 ERunSelectMode fMode; // Run query mode
426 Bool_t fFallBack; // Enable fall-back
8449e3e0 427
428 ClassDef(Table,1);
429 };
430 // === Interface ===================================================
431 /**
432 * Constructor
8449e3e0 433 */
434 AliOADBForward();
435 /**
436 * Destructor
437 *
438 */
439 ~AliOADBForward();
440 // --- Open/close/update -------------------------------------------
441 /**
442 * @{
443 * @name Input/output
444 */
445 /**
446 * Open a file containing tables. Note, this member function can be
447 * called multiple times to open tables in different files.
448 *
449 * If a table is already associated with this handler, it will not
450 * be re-associated.
451 *
452 * @param fileName Path to file to get/write tables from/in
453 * @param rw if true, open read+write, otherwise read-only
454 * @param tables Tables to open
455 * @param verb Verbosity flag
c8b1a7db 456 * @param fallback If true allow for fall-backs
8449e3e0 457 *
458 * @return true on success
459 */
460 Bool_t Open(const TString& fileName,
461 const TString& tables = "*",
462 Bool_t rw = false,
2a50d35b 463 Bool_t verb = false,
464 Bool_t fallback= false);
8449e3e0 465 /**
466 * Open a file containing tables. Note, this member function can be
467 * called multiple times to open tables in different files.
468 *
469 * If a table is already associated with this handler, it will not
470 * be re-associated.
471 *
472 * @param file File to get/write tables from/in
473 * @param rw if true, open read+write, otherwise read-only
474 * @param tables Tables to open
475 * @param verb Verbosity flag
c8b1a7db 476 * @param fallback If true allow for fall-backs
8449e3e0 477 *
478 * @return true on success
479 */
480 Bool_t Open(TFile* file,
481 const TString& tables,
482 Bool_t rw = false,
2a50d35b 483 Bool_t verb = false,
484 Bool_t fallback= false);
8449e3e0 485 /**
486 * Close this database
487 *
488 *
489 * @return true on success
490 */
491 Bool_t Close();
492 /**
493 * Flush to disk
494 *
495 * @return true on success
496 */
497 Bool_t Update();
498 /* @} */
499 // --- Queries -----------------------------------------------------
500 /**
501 * @{
502 * @name Queries
503 */
504 /**
505 * Query the table for an object. The strategy is as follows.
506 *
507 * - First query with all fields
508 * - If this returns a single entry, return that.
509 * - If not, then ignore the run number (if given)
510 * - If this returns a single entry, return that
511 * - Otherwise, give up and return null
512 *
513 * This allow us to specify default objects for a period, and for
514 * collision system, energy, and field setting.
515 *
c8b1a7db 516 * @param table Table name
8449e3e0 517 * @param run Run number
c8b1a7db 518 * @param mode Run selection mode
8449e3e0 519 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
520 * @param sNN Center of mass energy (GeV)
521 * @param fld L3 magnetic field (kG)
c8b1a7db 522 * @param mc For MC only
523 * @param sat For satellite interactions
8449e3e0 524 *
525 * @return Found entry, or null
526 */
527 Entry* Get(const TString& table,
528 ULong_t run = 0,
529 ERunSelectMode mode = kNear,
530 UShort_t sys = 0,
531 UShort_t sNN = 0,
532 Short_t fld = 0,
533 Bool_t mc = false,
534 Bool_t sat = false) const;
535 /**
536 * Query the table for an object. The strategy is as follows.
537 *
538 * - First query with all fields
539 * - If this returns a single entry, return that.
540 * - If not, then ignore the run number (if given)
541 * - If this returns a single entry, return that
542 * - Otherwise, give up and return null
543 *
544 * This allow us to specify default objects for a period, and for
545 * collision system, energy, and field setting.
546 *
c8b1a7db 547 * @param table Table name
8449e3e0 548 * @param run Run number
c8b1a7db 549 * @param mode Run selection mode
8449e3e0 550 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
551 * @param sNN Center of mass energy (GeV)
552 * @param fld L3 magnetic field (kG)
553 * @param mc For MC only
c8b1a7db 554 * @param sat For satellite interactions
8449e3e0 555 *
556 * @return Found data, or null
557 */
558 TObject* GetData(const TString& table,
2a50d35b 559 ULong_t run = 0,
560 ERunSelectMode mode = kNear,
561 UShort_t sys = 0,
562 UShort_t sNN = 0,
563 Short_t fld = 0,
564 Bool_t mc = false,
565 Bool_t sat = false) const;
8449e3e0 566 // --- Insert ------------------------------------------------------
567 /**
568 * Insert a new entry into the table
569 *
c8b1a7db 570 * @param table Table name
8449e3e0 571 * @param o Object to write
572 * @param runNo Run number
573 * @param sys Collision system (1: pp, 2:PbPb, 3:pPb)
574 * @param sNN Center of mass energy (GeV)
575 * @param field L3 magnetic field (kG)
576 * @param mc If true, only for MC
c8b1a7db 577 * @param sat For satellite interactions
8449e3e0 578 * @param aliRev AliROOT revision
c8b1a7db 579 * @param author Creater of this correction
8449e3e0 580 *
581 * @return true on success
582 */
583 Bool_t Insert(const TString& table,
584 TObject* o,
585 ULong_t runNo,
586 UShort_t sys,
587 UShort_t sNN,
588 Short_t field,
589 Bool_t mc=false,
590 Bool_t sat=false,
591 ULong_t aliRev=0,
592 const TString& author="");
77f97e3f
CHC
593 /**
594 * Copy one entry to another entry
595 *
596 * @param table Table name
597 * @param oldRunNo Old run number
598 * @param oldSys Old collision system
599 * @param oldSNN Old center of mass energy
600 * @param oldField Old L3 magnetic field strength
601 * @param newRunNo New run number
602 * @param newSys New collision system
603 * @param newSNN New center of mass energy
604 * @param newField New L3 magnetic field strength
605 * @param mc True for MC only queries
606 * @param sat True for including satellite queries
607 *
608 * @return true on success
609 */
610 Bool_t CopyEntry(const TString& table,
611 ULong_t oldRunNo,
612 UShort_t oldSys,
613 UShort_t oldSNN,
614 Short_t oldField,
615 ULong_t newRunNo,
616 UShort_t newSys,
617 UShort_t newSNN,
618 Short_t newField,
619 Bool_t mc,
620 Bool_t sat);
8449e3e0 621 /* @} */
622 /**
623 * Print the content of all tables
624 *
625 * @param option Passed on to tables
626 */
627 void Print(const Option_t* option="") const; //*MENU*
628 /**
629 * Browse this database
630 *
631 * @param b Browser
632 */
633 void Browse(TBrowser* b);
634 /**
635 * Find a table by name
636 *
637 * @param name Name of table
638 * @param quite Do not print warning if not found
639 * @return Table or null
640 */
641 Table* FindTable(const TString& name, Bool_t quite=false) const;
642 /**
643 * Get all tables
644 *
645 *
646 * @return Map of all tables
647 */
648 const TMap& GetTables() const { return fTables; }
649protected:
650 /**
651 * Get a list of associated files
652 *
653 * @param files On return, contains list of files
654 *
655 * @return Number of associated files found
656 */
657 Int_t GetFiles(TList& files) const;
658 /**
659 * Get a table (TTree) from a file
660 *
661 * @param file File to look in
662 * @param rw If true, open read/write, false read-only
663 * @param name Name of the table
664 * @param mode Default mode for new table, or override mode
665 * for existing tables if not default
666 *
667 * @return Table or (if rw=true) possibly newly created table
668 */
669 Table* GetTableFromFile(TFile* file, Bool_t rw,
670 const TString& name,
671 const TString& mode) const;
6953b59b 672
673 /**
674 * Get a table (TTree) from a file and attach
675 *
676 * @param file File to look in
677 * @param rw If true, open read/write, false read-only
678 * @param name Name of the table
679 * @param mode Default mode for new table, or override mode
680 * for existing tables if not default
681 *
682 * @return Table or (if rw=true) possibly newly created table
683 */
684 void OpenTable(TFile* file, Bool_t rw, const TString& name,
685 const TString& mode, Bool_t verb, Bool_t fallback);
8449e3e0 686 /**
687 * Helper function to append to query string
688 *
689 * @param q String to attach to
690 * @param s What to attach
691 * @param andNotOr If true, assume @b and, otherwise @b or
692 */
693 static void AppendToQuery(TString& q, const TString& s, Bool_t andNotOr=true);
694 /**
695 * Helper function to build a query string
696 *
697 * @param sys Collision system (1:pp, 2: PbPb, 3:pPb)
698 * @param sNN Collision energy in GeV
699 * @param fld L3-Field strength and polarity
700 * @param mc For Monte-carlo
701 * @param sat For satelitte collisions
702 *
703 * @return Query string
704 */
705 static TString Conditions(UShort_t sys = 0,
706 UShort_t sNN = 0,
2a50d35b 707 Short_t fld = kInvalidField,
8449e3e0 708 Bool_t mc = false,
709 Bool_t sat = false);
710 TMap fTables;
8449e3e0 711 ClassDef(AliOADBForward,0); // PWGLF/Forward OADB interface
712
713public:
714 // =================================================================
715 /**
716 * @{
717 * @name Tests
718 */
719 static void TestGet(AliOADBForward& t,
720 const TString& table,
721 ULong_t runNo = 0,
722 ERunSelectMode mode = kNear,
723 UShort_t sys = 2,
724 UShort_t sNN = 2760,
725 Short_t fld = -5,
726 Bool_t mc = false,
727 Bool_t sat = false);
728 static void TestInsert(AliOADBForward& t,
729 const TString& table,
730 ULong_t runNo = 0,
731 UShort_t sys = 2,
732 UShort_t sNN = 2760,
733 Short_t fld = -5,
734 Bool_t mc = false,
735 Bool_t sat = false);
736 static void Test();
737 /* @} */
738};
739
740#endif
741/* Local Variables:
742 * mode: C++
743 * End:
744 */