]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGLF/FORWARD/analysis2/AliOADBForward.h
Merge branch 'workdir'
[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="");
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
2a50d35b 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 null
8449e3e0 316 * - Otherwise, give up and return null
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
c8b1a7db 322 * @param mode Run selection mode
8449e3e0 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
c8b1a7db 327 * @param sat For satellite interactions
8449e3e0 328 *
329 * @return Found entry, or null
330 */
2a50d35b 331 Entry* Get(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
345 * - Otherwise, give up and return null
346 *
347 * This allow us to specify default objects for a period, and for
348 * collision system, energy, and field setting.
349 *
350 * @param run Run number
c8b1a7db 351 * @param mode Run selection mode
8449e3e0 352 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
353 * @param sNN Center of mass energy (GeV)
354 * @param fld L3 magnetic field (kG)
355 * @param mc For MC only
c8b1a7db 356 * @param sat For satellite interactions
8449e3e0 357 *
358 * @return Found data, or null
359 */
2a50d35b 360 TObject* GetData(ULong_t run = 0,
361 ERunSelectMode mode = kNear,
362 UShort_t sys = 0,
363 UShort_t sNN = 0,
364 Short_t fld = 0,
365 Bool_t mc = false,
366 Bool_t sat = false) const;
8449e3e0 367 /* @} */
368 /**
369 * Print the contents of the tree
370 *
371 * @param option Passed to TTree::Scan
372 */
373 void Print(Option_t* option="") const; //*MENU*
374 /**
375 * Browse this table
376 */
377 void Browse(TBrowser* b);
378 /**
379 * Check if the tree was opened.
380 *
381 * @param rw If true, also check if the file is read/write
382 *
383 * @return true if everything is dandy
384 */
385 Bool_t IsOpen(Bool_t rw=false) const;
386
387 TTree* fTree; // Our tree
388 Entry* fEntry; // Entry cache
389 Bool_t fVerbose; // To be verbose or not
2a50d35b 390 ERunSelectMode fMode; // Run query mode
391 Bool_t fFallBack; // Enable fall-back
8449e3e0 392
393 ClassDef(Table,1);
394 };
395 // === Interface ===================================================
396 /**
397 * Constructor
8449e3e0 398 */
399 AliOADBForward();
400 /**
401 * Destructor
402 *
403 */
404 ~AliOADBForward();
405 // --- Open/close/update -------------------------------------------
406 /**
407 * @{
408 * @name Input/output
409 */
410 /**
411 * Open a file containing tables. Note, this member function can be
412 * called multiple times to open tables in different files.
413 *
414 * If a table is already associated with this handler, it will not
415 * be re-associated.
416 *
417 * @param fileName Path to file to get/write tables from/in
418 * @param rw if true, open read+write, otherwise read-only
419 * @param tables Tables to open
420 * @param verb Verbosity flag
c8b1a7db 421 * @param fallback If true allow for fall-backs
8449e3e0 422 *
423 * @return true on success
424 */
425 Bool_t Open(const TString& fileName,
426 const TString& tables = "*",
427 Bool_t rw = false,
2a50d35b 428 Bool_t verb = false,
429 Bool_t fallback= false);
8449e3e0 430 /**
431 * Open a file containing tables. Note, this member function can be
432 * called multiple times to open tables in different files.
433 *
434 * If a table is already associated with this handler, it will not
435 * be re-associated.
436 *
437 * @param file File to get/write tables from/in
438 * @param rw if true, open read+write, otherwise read-only
439 * @param tables Tables to open
440 * @param verb Verbosity flag
c8b1a7db 441 * @param fallback If true allow for fall-backs
8449e3e0 442 *
443 * @return true on success
444 */
445 Bool_t Open(TFile* file,
446 const TString& tables,
447 Bool_t rw = false,
2a50d35b 448 Bool_t verb = false,
449 Bool_t fallback= false);
8449e3e0 450 /**
451 * Close this database
452 *
453 *
454 * @return true on success
455 */
456 Bool_t Close();
457 /**
458 * Flush to disk
459 *
460 * @return true on success
461 */
462 Bool_t Update();
463 /* @} */
464 // --- Queries -----------------------------------------------------
465 /**
466 * @{
467 * @name Queries
468 */
469 /**
470 * Query the table for an object. The strategy is as follows.
471 *
472 * - First query with all fields
473 * - If this returns a single entry, return that.
474 * - If not, then ignore the run number (if given)
475 * - If this returns a single entry, return that
476 * - Otherwise, give up and return null
477 *
478 * This allow us to specify default objects for a period, and for
479 * collision system, energy, and field setting.
480 *
c8b1a7db 481 * @param table Table name
8449e3e0 482 * @param run Run number
c8b1a7db 483 * @param mode Run selection mode
8449e3e0 484 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
485 * @param sNN Center of mass energy (GeV)
486 * @param fld L3 magnetic field (kG)
c8b1a7db 487 * @param mc For MC only
488 * @param sat For satellite interactions
8449e3e0 489 *
490 * @return Found entry, or null
491 */
492 Entry* Get(const TString& table,
493 ULong_t run = 0,
494 ERunSelectMode mode = kNear,
495 UShort_t sys = 0,
496 UShort_t sNN = 0,
497 Short_t fld = 0,
498 Bool_t mc = false,
499 Bool_t sat = false) const;
500 /**
501 * Query the table for an object. The strategy is as follows.
502 *
503 * - First query with all fields
504 * - If this returns a single entry, return that.
505 * - If not, then ignore the run number (if given)
506 * - If this returns a single entry, return that
507 * - Otherwise, give up and return null
508 *
509 * This allow us to specify default objects for a period, and for
510 * collision system, energy, and field setting.
511 *
c8b1a7db 512 * @param table Table name
8449e3e0 513 * @param run Run number
c8b1a7db 514 * @param mode Run selection mode
8449e3e0 515 * @param sys Collision system (1: pp, 2: PbPb, 3: pPb)
516 * @param sNN Center of mass energy (GeV)
517 * @param fld L3 magnetic field (kG)
518 * @param mc For MC only
c8b1a7db 519 * @param sat For satellite interactions
8449e3e0 520 *
521 * @return Found data, or null
522 */
523 TObject* GetData(const TString& table,
2a50d35b 524 ULong_t run = 0,
525 ERunSelectMode mode = kNear,
526 UShort_t sys = 0,
527 UShort_t sNN = 0,
528 Short_t fld = 0,
529 Bool_t mc = false,
530 Bool_t sat = false) const;
8449e3e0 531 // --- Insert ------------------------------------------------------
532 /**
533 * Insert a new entry into the table
534 *
c8b1a7db 535 * @param table Table name
8449e3e0 536 * @param o Object to write
537 * @param runNo Run number
538 * @param sys Collision system (1: pp, 2:PbPb, 3:pPb)
539 * @param sNN Center of mass energy (GeV)
540 * @param field L3 magnetic field (kG)
541 * @param mc If true, only for MC
c8b1a7db 542 * @param sat For satellite interactions
8449e3e0 543 * @param aliRev AliROOT revision
c8b1a7db 544 * @param author Creater of this correction
8449e3e0 545 *
546 * @return true on success
547 */
548 Bool_t Insert(const TString& table,
549 TObject* o,
550 ULong_t runNo,
551 UShort_t sys,
552 UShort_t sNN,
553 Short_t field,
554 Bool_t mc=false,
555 Bool_t sat=false,
556 ULong_t aliRev=0,
557 const TString& author="");
77f97e3f
CHC
558 /**
559 * Copy one entry to another entry
560 *
561 * @param table Table name
562 * @param oldRunNo Old run number
563 * @param oldSys Old collision system
564 * @param oldSNN Old center of mass energy
565 * @param oldField Old L3 magnetic field strength
566 * @param newRunNo New run number
567 * @param newSys New collision system
568 * @param newSNN New center of mass energy
569 * @param newField New L3 magnetic field strength
570 * @param mc True for MC only queries
571 * @param sat True for including satellite queries
572 *
573 * @return true on success
574 */
575 Bool_t CopyEntry(const TString& table,
576 ULong_t oldRunNo,
577 UShort_t oldSys,
578 UShort_t oldSNN,
579 Short_t oldField,
580 ULong_t newRunNo,
581 UShort_t newSys,
582 UShort_t newSNN,
583 Short_t newField,
584 Bool_t mc,
585 Bool_t sat);
8449e3e0 586 /* @} */
587 /**
588 * Print the content of all tables
589 *
590 * @param option Passed on to tables
591 */
592 void Print(const Option_t* option="") const; //*MENU*
593 /**
594 * Browse this database
595 *
596 * @param b Browser
597 */
598 void Browse(TBrowser* b);
599 /**
600 * Find a table by name
601 *
602 * @param name Name of table
603 * @param quite Do not print warning if not found
604 * @return Table or null
605 */
606 Table* FindTable(const TString& name, Bool_t quite=false) const;
607 /**
608 * Get all tables
609 *
610 *
611 * @return Map of all tables
612 */
613 const TMap& GetTables() const { return fTables; }
614protected:
615 /**
616 * Get a list of associated files
617 *
618 * @param files On return, contains list of files
619 *
620 * @return Number of associated files found
621 */
622 Int_t GetFiles(TList& files) const;
623 /**
624 * Get a table (TTree) from a file
625 *
626 * @param file File to look in
627 * @param rw If true, open read/write, false read-only
628 * @param name Name of the table
629 * @param mode Default mode for new table, or override mode
630 * for existing tables if not default
631 *
632 * @return Table or (if rw=true) possibly newly created table
633 */
634 Table* GetTableFromFile(TFile* file, Bool_t rw,
635 const TString& name,
636 const TString& mode) const;
637 /**
638 * Helper function to append to query string
639 *
640 * @param q String to attach to
641 * @param s What to attach
642 * @param andNotOr If true, assume @b and, otherwise @b or
643 */
644 static void AppendToQuery(TString& q, const TString& s, Bool_t andNotOr=true);
645 /**
646 * Helper function to build a query string
647 *
648 * @param sys Collision system (1:pp, 2: PbPb, 3:pPb)
649 * @param sNN Collision energy in GeV
650 * @param fld L3-Field strength and polarity
651 * @param mc For Monte-carlo
652 * @param sat For satelitte collisions
653 *
654 * @return Query string
655 */
656 static TString Conditions(UShort_t sys = 0,
657 UShort_t sNN = 0,
2a50d35b 658 Short_t fld = kInvalidField,
8449e3e0 659 Bool_t mc = false,
660 Bool_t sat = false);
661 TMap fTables;
8449e3e0 662 ClassDef(AliOADBForward,0); // PWGLF/Forward OADB interface
663
664public:
665 // =================================================================
666 /**
667 * @{
668 * @name Tests
669 */
670 static void TestGet(AliOADBForward& t,
671 const TString& table,
672 ULong_t runNo = 0,
673 ERunSelectMode mode = kNear,
674 UShort_t sys = 2,
675 UShort_t sNN = 2760,
676 Short_t fld = -5,
677 Bool_t mc = false,
678 Bool_t sat = false);
679 static void TestInsert(AliOADBForward& t,
680 const TString& table,
681 ULong_t runNo = 0,
682 UShort_t sys = 2,
683 UShort_t sNN = 2760,
684 Short_t fld = -5,
685 Bool_t mc = false,
686 Bool_t sat = false);
687 static void Test();
688 /* @} */
689};
690
691#endif
692/* Local Variables:
693 * mode: C++
694 * End:
695 */