source: trunk/MagicSoft/Mars/mbase/MReadTree.cc@ 457

Last change on this file since 457 was 454, checked in by harald, 24 years ago
Import the first sources of the MAGIC Analysis and Reconstruction Software. T. Bretz and H. Kornmayer 20.December 2000
File size: 5.0 KB
Line 
1////////////////////////////////////////////////////////////////////////
2//
3// MRawFile
4//
5// This tasks reads the raw binary file like specified in the TDAS???
6// and writes the data in the corresponding containers which are
7// either retrieved from the parameter list or created and added.
8//
9////////////////////////////////////////////////////////////////////////
10
11#include "MReadTree.h"
12
13#include <iostream.h>
14#include <fstream.h>
15
16#include <TFile.h>
17#include <TTree.h>
18#include <TObjArray.h>
19
20#include "MTime.h"
21#include "MParList.h"
22
23ClassImp(MReadTree)
24
25MReadTree::MReadTree(const char *fname, const char *tname,
26 const char *name, const char *title)
27{
28 *fName = name ? name : "MReadTree";
29 *fTitle = title ? title : "Task to loop over all events in one single tree";
30
31 //
32 // open the input stream
33 //
34 fFileName = fname;
35 fTreeName = tname;
36}
37
38Bool_t MReadTree::PreProcess (MParList *pList)
39{
40 //
41 // open file
42 //
43 fFile = new TFile(fFileName, "READ");
44
45 if (!fFile->IsOpen())
46 {
47 cout << "MReadTree::PreProcess: ERROR: Cannot open file '";
48 cout << fFileName << "'" << endl;
49 return kFALSE;
50 }
51
52 fTree = (TTree*)fFile->Get(fTreeName);
53
54 if (!fTree)
55 {
56 cout << "MReadTree::PreProcess: ERROR: Cannot open tree '";
57 cout << fTreeName << "'" << endl;
58 return kFALSE;
59 }
60
61 fNumEntries = (UInt_t)fTree->GetEntries();
62 fNumEntry = 0;
63
64 cout << "File: '" << fFileName << "' Tree: '" << fTreeName;
65 cout << "' with " << fNumEntries << " Entries opened." << endl;
66
67 //
68 // Get all branches of this tree and
69 // create the Iterator to loop over all branches
70 //
71 TIter Next(fTree->GetListOfBranches());
72 TBranch *branch=NULL;
73
74 //
75 // loop over all tasks for processing
76 //
77 while ( (branch=(TBranch*)Next()) )
78 {
79 //
80 // Get Name of Branch
81 //
82 const char *name = branch->GetName();
83
84 //
85 // check if object is existing in the list
86 //
87 MParContainer *pcont = (MParContainer*)pList->FindObject(name);
88
89 if (!pcont)
90 {
91 //
92 // if object is not existing in the list try to create one
93 //
94 cout << "MReadTree::PreProcess - WARNING: '" << name << "' not found... creating." << endl;
95
96 //
97 // try to get class from root environment
98 //
99 TClass *cls = gROOT->GetClass(name);
100
101 if (!cls)
102 {
103 //
104 // if class is not existing in the root environment
105 // we cannot proceed reading this branch
106 //
107 cout << "MReadTree::PreProcess - Warning: Class '" << name << "' not existing in dictionary. Branch skipped." << endl;
108 continue;
109 }
110
111 //
112 // create the container and add it to the list
113 //
114 pcont = (MParContainer*)cls->New();
115 cout << pcont << endl;
116 pList->AddToList(pcont);
117 }
118
119 //
120 // here pcont is a pointer the to container in which the data from
121 // the actual branch should be stored - enable branch.
122 //
123 branch->SetAddress(&pcont);
124 }
125
126 return kTRUE;
127}
128
129Bool_t MReadTree::Process()
130{
131 //
132 // check for end of file
133 //
134 if (fNumEntry==fNumEntries)
135 return kFALSE;
136
137 //
138 // get entry
139 //
140 fTree->GetEntry(fNumEntry/*++*/);
141 return kTRUE;
142}
143
144Bool_t MReadTree::PostProcess()
145{
146 //
147 // Close File
148 //
149 fFile->Close();
150
151 return kTRUE;
152}
153
154Bool_t MReadTree::DecEventNum(UInt_t dec)
155{
156 //
157 // Decrease the number of the event which is read by Process() next
158 // by one or more
159 //
160
161 //!
162 //! this function makes Process() read the event one (or more) before
163 //! the actual position (event) in the tree
164 //!
165 if (fNumEntry < dec/*+1*/)
166 {
167 cout << "MReadTree::SetPrevEvent: WARNING: " << fNumEntry/*-1*/ << "-" << dec << " out of Range." << endl;
168 return kFALSE;
169 }
170
171 fNumEntry -= dec/*+1*/;
172 return kTRUE;
173}
174
175Bool_t MReadTree::IncEventNum(UInt_t inc)
176{
177 //
178 // Increase the number of the event which is read by Process() next
179 // by one or more
180 //
181
182 //!
183 //! this function makes Process() read the next (or more) after
184 //! the actual position (event) in the tree
185 //! (Be careful: IncEventNum() or IncEventNum(1) does not chenge anything
186 //! in the standard behaviour of the task)
187 //!
188 if (fNumEntry+inc/*-1*/ >= fNumEntries)
189 {
190 cout << "MReadTree::SkipEvents: WARNING: " << fNumEntry/*-1*/ << "+" << inc << " out of Range." << endl;
191 return kFALSE;
192 }
193
194 fNumEntry += inc/*-1*/;
195 return kTRUE;
196}
197
198Bool_t MReadTree::SetEventNum(UInt_t nr)
199{
200 //
201 // this function makes Process() read event number nr next
202 //
203 if (nr>=fNumEntries)
204 {
205 cout << "MReadTree::SetEventNum: WARNING: " << nr << " out of Range." << endl;
206 return kFALSE;
207 }
208
209 fNumEntry = nr;
210 return kTRUE;
211}
212
Note: See TracBrowser for help on using the repository browser.