1 |
nikolas |
1.1 |
#include "PamRootManager.h" |
2 |
|
|
#include <TSystem.h> |
3 |
|
|
#include <TParticle.h> |
4 |
|
|
#include <TObjArray.h> |
5 |
|
|
#include <iostream> |
6 |
|
|
|
7 |
|
|
#include <TList.h> |
8 |
|
|
#include <TROOT.h> |
9 |
|
|
ClassImp(PamRootManager) |
10 |
|
|
|
11 |
pam-rm2 |
1.5 |
using namespace std; |
12 |
nikolas |
1.1 |
PamRootManager* PamRootManager::fgInstance = 0; |
13 |
|
|
|
14 |
|
|
PamRootManager::PamRootManager(const char* projectName, FileMode fileMode) |
15 |
|
|
: TObject() |
16 |
|
|
{ |
17 |
|
|
if (fgInstance) { |
18 |
|
|
Fatal("PamRootManager", "Singleton instance already exists."); |
19 |
|
|
return; |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
TString fileName = TString(gSystem->Getenv("PWD"))+"/"+projectName; |
24 |
|
|
fileName += ".root"; |
25 |
|
|
|
26 |
pam-rm2 |
1.5 |
cout<<"OUTPUT ROOTFILE: "<<fileName<<endl; |
27 |
|
|
TString treeTitle("pamtest"); |
28 |
nikolas |
1.1 |
treeTitle += " tree"; |
29 |
|
|
|
30 |
|
|
TTree::SetMaxTreeSize(1000*Long64_t(2000000000)); |
31 |
|
|
|
32 |
|
|
switch (fileMode) { |
33 |
|
|
case kRead: |
34 |
|
|
fFile = new TFile(fileName); |
35 |
|
|
fTree = (TTree*) fFile->Get(projectName); |
36 |
|
|
break; |
37 |
|
|
|
38 |
|
|
case kWrite: |
39 |
|
|
fFile = new TFile(fileName, "recreate"); |
40 |
pam-rm2 |
1.6 |
fTree = new TTree("hits", "PamVMC hits collections"); |
41 |
nikolas |
1.1 |
;; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
fgInstance = this; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
PamRootManager::PamRootManager() |
49 |
|
|
: TObject(), |
50 |
|
|
fFile(0), |
51 |
|
|
fTree(0) |
52 |
|
|
{ |
53 |
|
|
|
54 |
|
|
if (fgInstance) { |
55 |
|
|
Fatal("PamRootManager", "Singleton instance already exists."); |
56 |
|
|
return; |
57 |
|
|
} |
58 |
|
|
|
59 |
pam-rm2 |
1.5 |
fgInstance = this; |
60 |
nikolas |
1.1 |
} |
61 |
|
|
|
62 |
|
|
PamRootManager::~PamRootManager() |
63 |
|
|
{ |
64 |
|
|
fFile->cd(); |
65 |
|
|
delete fFile; |
66 |
pam-rm2 |
1.5 |
//delete fDirectory; |
67 |
nikolas |
1.1 |
fgInstance = 0; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
PamRootManager* PamRootManager::Instance() |
71 |
|
|
{ |
72 |
|
|
// Returns singleton instance. |
73 |
|
|
|
74 |
|
|
return fgInstance; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
void PamRootManager::Register(const char* name, const char* className, |
78 |
|
|
void* objAddress) |
79 |
|
|
{ |
80 |
|
|
// Creates a branch of the given name and associates it with |
81 |
|
|
// the given address. |
82 |
|
|
|
83 |
|
|
if (!fTree->GetBranch(name)) |
84 |
|
|
fTree->Branch(name, className, objAddress, 32000, 99); |
85 |
|
|
else |
86 |
|
|
fTree->GetBranch(name)->SetAddress(objAddress); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
void PamRootManager::Fill() |
90 |
|
|
{ |
91 |
|
|
// Fills the tree. |
92 |
|
|
//TString tmp = gDirectory->GetName(); |
93 |
|
|
fFile->cd(); |
94 |
|
|
fTree->Fill(); |
95 |
|
|
//gDirectory->cd(tmp); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
void PamRootManager:: WriteAll() |
99 |
|
|
{ |
100 |
|
|
// Erites the tree in the file. |
101 |
|
|
|
102 |
|
|
//TString tmp = gDirectory->GetName(); |
103 |
|
|
fFile->cd(); |
104 |
|
|
std::cout<<"TREE NAME: "<<fTree->GetName()<<std::endl; |
105 |
|
|
std::cout<<"DIR NAME: "<<gDirectory->GetName()<<std::endl; |
106 |
|
|
fTree->Write(); |
107 |
|
|
//gDirectory->cd(tmp); |
108 |
|
|
std::cout<<"TREE WRITTEN "<<fTree->GetName()<<std::endl; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
void PamRootManager::ReadEvent(Int_t i) |
112 |
|
|
{ |
113 |
|
|
// Reads the event data for i-th event for all connected branches. |
114 |
|
|
|
115 |
|
|
fTree->GetEntry(i); |
116 |
|
|
} |