DABC (Data Acquisition Backbone Core)  2.9.9
TreeStore.cxx
Go to the documentation of this file.
1 // $Id: TreeStore.cxx 3862 2018-05-11 10:06:18Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "root/TreeStore.h"
17 
18 #include "dabc/Url.h"
19 
20 #include "TTree.h"
21 #include "TFile.h"
22 
23 root::TreeStore::TreeStore(const std::string &name) :
24  dabc::LocalWorker(name),
25  fTree(0)
26 {
27 }
28 
30 {
31  CloseTree();
32 }
33 
35 {
36  if (fTree) {
37  TFile* f = fTree->GetCurrentFile();
38  f->cd();
39  fTree->Write();
40  delete fTree;
41  fTree = 0;
42  delete f;
43  }
44 }
45 
47 {
48  if (cmd.IsName("Fill")) {
49  if (fTree) {
50  fTree->Fill();
51  cmd.SetStr("StoreInfo", dabc::format("File:%s Tree:%s Size:%s", fTree->GetCurrentFile()->GetName(), fTree->GetName(), dabc::size_to_str(fTree->GetTotBytes()).c_str()));
52  }
53  return dabc::cmd_true;
54  } else
55  if (cmd.IsName("Close")) {
56  CloseTree();
57  return dabc::cmd_true;
58  } else
59  if (cmd.IsName("CreateBranch")) {
60  if (fTree==0) return dabc::cmd_false;
61 
62  if (cmd.GetPtr("member") != 0)
63  fTree->Branch(cmd.GetStr("name").c_str(), cmd.GetPtr("member"), cmd.GetStr("kind").c_str());
64  else
65  fTree->Branch(cmd.GetStr("name").c_str(), cmd.GetStr("class_name").c_str(), (void**) cmd.GetPtr("obj"));
66  return dabc::cmd_true;
67  } else
68  if (cmd.IsName("Create")) {
69  if (fTree) CloseTree();
70 
71  dabc::Url url(cmd.GetStr("fname","f.root"));
72 
73  int64_t maxsize = url.GetOptionInt("maxsize", cmd.GetInt("maxsize",0));
74 
75  TFile *f = TFile::Open(url.GetFullName().c_str(), "RECREATE", cmd.GetStr("ftitle","ROOT file store").c_str());
76  if (f==0) return dabc::cmd_false;
77  if (maxsize>0) TTree::SetMaxTreeSize(maxsize*1024*1024);
78  fTree = new TTree(cmd.GetStr("tname","T").c_str(), cmd.GetStr("ttitle","ROOT Tree").c_str());
79  cmd.SetPtr("tree_ptr", fTree);
80  return dabc::cmd_true;
81  }
82 
83  return dabc::cmd_false;
84 }
Represents command with its arguments.
Definition: Command.h:99
void SetPtr(const std::string &name, void *p)
Set pointer argument for the command.
Definition: Command.cxx:151
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
std::string GetStr(const std::string &name, const std::string &dflt="") const
Definition: Command.h:136
int GetInt(const std::string &name, int dflt=0) const
Definition: Command.h:139
void * GetPtr(const std::string &name, void *deflt=0) const
Get pointer argument from the command.
Definition: Command.cxx:158
bool IsName(const char *name) const
Returns true if object name is the same as specified one.
Definition: Reference.cxx:177
Uniform Resource Locator interpreter.
Definition: Url.h:33
std::string GetFullName() const
Definition: Url.cxx:124
int GetOptionInt(const std::string &optname, int dflt=0) const
Definition: Url.cxx:290
virtual ~TreeStore()
Definition: TreeStore.cxx:29
TreeStore(const std::string &name)
Definition: TreeStore.cxx:23
virtual int ExecuteCommand(dabc::Command)
Main method where commands are executed.
Definition: TreeStore.cxx:46
void CloseTree()
Definition: TreeStore.cxx:34
Event manipulation API.
Definition: api.h:23
std::string format(const char *fmt,...)
Definition: string.cxx:49
std::string size_to_str(unsigned long sz, int prec=1, int select=0)
Convert size to string of form like 4.2 GB or 3.7 MB.
Definition: string.cxx:75
@ cmd_false
Definition: Command.h:37
@ cmd_true
Definition: Command.h:38