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