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 |
using namespace std; |
12 |
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 = projectName; |
24 |
// TString fileName = TString(gSystem->Getenv("PWD"))+"/"+projectName; |
25 |
fileName += ".root"; |
26 |
|
27 |
cout<<"OUTPUT ROOTFILE: "<<fileName<<endl; |
28 |
TString treeTitle("pamtest"); |
29 |
treeTitle += " tree"; |
30 |
|
31 |
TTree::SetMaxTreeSize(1000*Long64_t(2000000000)); |
32 |
|
33 |
|
34 |
switch (fileMode) { |
35 |
case kRead: |
36 |
fFile = new TFile(fileName); |
37 |
fTree = (TTree*) fFile->Get(projectName); |
38 |
break; |
39 |
|
40 |
case kWrite: |
41 |
fFile = new TFile(fileName, "recreate"); |
42 |
fTree = new TTree("hits", "PamVMC hits collections"); |
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 |
} |