Diana Software
GlobalDataManagerExample.C
Go to the documentation of this file.
1 
7 {
8  // using namespace Diana;
9  // Avoid to put "Diana::" in front of diana objects.
10  // This turns out to be mandatory in Cint since there are
11  // problems with namespaces in conjunction with templates and
12  // typedefs
13  using namespace Diana;
14  gSystem->Load("libqroot");
15  // In diana there is a common instance of QGlobalDataManager
16  // that is accessed via QBaseModule::GlobalData().
17  // Here instead we have to create it.
19  // set the owner of the new QObjects that we will save
20  // in diana this method is not accessible, and the owner is
21  // set to the current module being executed.
22  dm.SetOwner("DataManagerTest");
23 
24  // Create two QVectors and save to file
25  QVector vec(3),vec2(4);
26  vec[0] = 1; vec[1] = 2; vec[2] = 3;
27  vec2[0] = -1; vec2[1] = 0; vec2[2] = 1; vec2[3] = 2;
28  // Create a GlobalHandle instance to manage each QVector
29  // with the QGlobalDataManager. In the first we set Channel as key.
30  GlobalHandle<QVector> hvec("MyQVector");
31  hvec.SetChannel(3);
32  hvec.Set(vec);
33  dm.Set(&hvec,"dmtest.root");
34  // In the second we set Run as key
35  GlobalHandle<QVector> hvec2("MyQVector2");
36  hvec2.SetRun(753);
37  hvec2.Set(vec2);
38  dm.Set(&hvec2,"dmtest.root");
39 
40  // Get RunData from DB and save it to file, owner will remain "DAQ".
41  // since object is not modified.
42  // The QRunData QObject has a specialized handle, the QRunDataHandle,
43  // that implements the specific DB queries
45  dm.Get("DAQ",&rHandle,"DB");
46  dm.Set(&rHandle,"dmtest.txt");
47 
48  // Modify QRunData and set to file, owner will be "DataManagerTest" since we modified it.
49  // We have to create a new handle, since each object needs its own handle
51  const int channel = newRunData.fBolometerChannels[0]; // modify sampling frequency of this channel;
57  dm.Set(&rHandle2,"dmtest.root");
58 
59  // Write a TObject using a TObjectHandle onto a .root file
60  TH1D histo("histo","My histogram",10,-5,5);
61  histo.Fill(1.1);
62  histo.Fill(3);
63  QTObjectHandle<TH1D> hHandle("MyHisto");
64  hHandle.SetChannel(3);
65  hHandle.SetRun(753);
66  hHandle.Set(histo);
67  dm.Set(&hHandle,"dmtest.root");
68 
69  // print objects in cache
70  dm.Dump(cout);
71 
72  cout<<"***** ATTENTION: Errors below are generated intentionally ******"<<endl;
73  // Here we show how a write error appears
74  QInt testInt = 2;
75  GlobalHandle<QInt> intHandle2("MyFakeInt");
76  intHandle2.Set(testInt);
77  // if we are not root we have no write permissions
78  dm.Set(&intHandle2,"/root/dianaglobal.root");
79 
80  // Here we show how a read error appears
81  GlobalHandle<QInt> intHandle2("MyFakeInt2");
82  dm.Get("MyFakeOwner",&intHandle2,"fakefile.txt");
83  int myInt;
84  // the Get() call in the line below will abort the program
85  // since the object is not valid.
86  myInt = intHandle2.Get();
87  // To not abort, comment the above line and handle the error as follows:
88  if(!intHandle2.IsValid()) {
89  cout<<intHandle2.GetError()<<endl;
90  } else {
91  myInt = intHandle2.Get();
92  }
93 
94 }
dm Set & intHandle2
QRunDataHandle rHandle(753)
const int channel
QVector vec(3)
TH1D histo("histo","My histogram", 10,-5, 5)
GlobalHandle< QVector > hvec2("MyQVector2")
QChannelRunData chanRunData
dm Set & rHandle2
GlobalHandle< QVector > hvec("MyQVector")
QGlobalDataManager dm
QTObjectHandle< TH1D > hHandle("MyHisto")
QRunData newRunData
QVector vec2(4)
base types wrapped into a QObject. Currently implemented types are QInt QDouble and QFloat....
Definition: QBaseType.hh:17
basic channel and run based info. Used in the QRunData object.
double fSamplingFrequency
sampling frequency in Hz
Object to manage I/O (DB, file, or memory) of diana global quantities.
void SetOwner(const std::string &owner)
set the module that is accessing this object
QError Set(GlobalHandle< Q > *gh, const std::string &outSource, bool printError=true) const
Set a QObject using a GlobalHandle.
QError Get(const std::string &owner, GlobalHandle< Q > *gh, const std::string &inSource, bool printError=true) const
Get an object using a global handle.
void Dump(std::ostream &o) const
Dump cached objects to stream.
global handle for QRunData
Basic run based info.
Definition: QRunData.hh:20
int fNumber
Run Number.
Definition: QRunData.hh:41
const QChannelRunData & GetChannelRunData(const int channel) const
get channel based run data quantities
Definition: QRunData.cc:339
std::vector< int > fBolometerChannels
Bolometers channels (thermistors glued to a crystal)
Definition: QRunData.hh:49
void SetChannelRunData(const int channel, const QChannelRunData &chanRunData)
set channel based run data quantities
Definition: QRunData.cc:352
global handle for generic TObject
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...