source: trunk/MagicSoft/Mars/mfileio/MReadMarsFile.cc@ 6503

Last change on this file since 6503 was 6503, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MReadMarsFile
28//
29// This task works more or less like MReadTree, but in addition PreProcess
30// reads all the information from the 'RunHeader' tree.
31//
32// Warning: Until now this works only for 'one run header per file'
33//
34/////////////////////////////////////////////////////////////////////////////
35#include "MReadMarsFile.h"
36
37#include <fstream>
38
39#include <TFile.h>
40#include <TTree.h>
41
42#include "MLog.h"
43#include "MLogManip.h"
44
45#include "MParList.h"
46#include "MTaskList.h"
47
48#include "MRawRunHeader.h"
49
50#include "MMcRunHeader.hxx"
51
52ClassImp(MReadMarsFile);
53
54using namespace std;
55
56// --------------------------------------------------------------------------
57//
58// Default constructor. Don't use it.
59//
60MReadMarsFile::MReadMarsFile() : fRun(NULL)
61{
62 fName = "MRead";
63 fTitle = "Read tree and run headers from Mars file.";
64}
65
66// --------------------------------------------------------------------------
67//
68// Default constructor. It creates a MReadTree object to read the
69// RunHeaders and disables Auto Scheme for this tree.
70//
71MReadMarsFile::MReadMarsFile(const char *tname, const char *fname,
72 const char *name, const char *title)
73 : MReadTree(tname, fname)
74{
75 fName = name ? name : "MRead";
76 fTitle = title ? title : "Read tree and run headers from Mars file.";
77
78 //
79 // open the input stream
80 //
81 fRun = new MReadTree("RunHeaders", fname, "ReadRunHeaders");
82
83 //
84 // This disables the auto scheme. because reading new runheader is done
85 // at a low frequency we don't loose time if we always read all
86 // runheaders
87 //
88 fRun->DisableAutoScheme();
89}
90
91// --------------------------------------------------------------------------
92//
93// Destructor. Deleted the MReadTree object for the RunHeaders
94//
95MReadMarsFile::~MReadMarsFile()
96{
97 delete fRun;
98}
99
100Byte_t MReadMarsFile::IsFileValid(const char *name)
101{
102 ifstream fin(name);
103 if (!fin)
104 return 0;
105
106 Char_t c[4];
107 fin.read(c, 4);
108 if (!fin)
109 return 0;
110
111 if (!(c[0]=='r'&& c[1]=='o' && c[2]=='o' && c[3]=='t'))
112 return 0;
113
114 TFile f(name, "READ");
115
116 TTree *t = (TTree*)f.Get("Events");
117 if (!t)
118 return 0;
119
120 // FIXME: Replace numbers by enum! Maybe use bits?
121 if (t->FindBranch("MRawEvtData."))
122 return t->FindBranch("MMcEvt.") ? 2 : 1;
123
124 if (t->FindBranch("MCerPhotEvt."))
125 return t->FindBranch("MMcEvt.") ? 4 : 3;
126
127 return 0;
128}
129
130// --------------------------------------------------------------------------
131//
132// see MReadTree::AddFile, too. The file is also added to the chain for
133// the run headers. If adding file gives a different result for both
134// chains -1 is returned, otherwise the number of files which were added.
135//
136Int_t MReadMarsFile::AddFile(const char *fname, Int_t entries)
137{
138 //
139 // FIXME! A check is missing whether the file already exists or not.
140 //
141 //
142 // returns the number of file which were added
143 //
144 Int_t n1 = fRun->AddFile(fname);
145 Int_t n2 = MReadTree::AddFile(fname, entries);
146
147 return n1 != n2 ? -1 : n1;
148}
149
150// --------------------------------------------------------------------------
151//
152// Sort the files by their file-names
153//
154void MReadMarsFile::SortFiles()
155{
156 fRun->SortFiles();
157 MReadTree::SortFiles();
158}
159
160// --------------------------------------------------------------------------
161//
162// This overload MReadTree::Notify. Before the MReadTree Notify
163// TObjects are called the RunHeaders of the next files are read.
164//
165// WARNING: This doesn't work correctly yet, if the files are not read in
166// increasing order.
167//
168Bool_t MReadMarsFile::Notify()
169{
170 UInt_t runtype = 0xffff;
171
172 const MRawRunHeader *rawheader = (MRawRunHeader*)fParList->FindObject("MRawRunHeader");
173 if (rawheader)
174 runtype = rawheader->GetRunType();
175
176 //
177 // Try to read the new run headers. If reading the new run header
178 // was successfull call the ReInits
179 //
180 const Int_t idx = GetFileIndex();
181 fRun->SetEventNum(idx<0?0:idx); // Assumption: One Entry per File!
182 if (!fRun->Process())
183 {
184 *fLog << err << "ERROR - Cannot read new runheaders #" << idx;
185 *fLog << " after reading event #" << GetNumEntry() << endl;
186 return kFALSE;
187 }
188
189 if (!MReadTree::Notify())
190 return kFALSE;
191
192 if (rawheader && runtype!=0xffff && runtype != rawheader->GetRunType())
193 {
194 *fLog << warn << "Warning - You are mixing files with different run types (";
195 *fLog << runtype << ", " << rawheader->GetRunType() << ")" << endl;
196 }
197
198 const MMcRunHeader *mcheader = (MMcRunHeader*)fParList->FindObject("MMcRunHeader");
199 if (mcheader)
200 {
201 if (mcheader->GetCamVersion()==50)
202 {
203 *fLog << warn << "Warning - You are using a file created with Camera 0.5." << endl;
204 *fLog << "In this camera version some events have undefined Impact-Values" << endl;
205 *fLog << "(MMcEvt::fImpact) Please don't use it for MC studies using the" << endl;
206 *fLog << "impact parameter." << endl;
207 }
208 }
209
210 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
211 if (!tlist)
212 {
213 *fLog << err << dbginf << "ERROR - MTaskList not found in Parameter List." << endl;
214 return kFALSE;
215 }
216
217 if (tlist->ReInit())
218 return kTRUE;
219
220 //MReadTree::Notify();
221
222 *fLog << err << "ERROR - ReInit of '" << tlist->GetDescriptor() << "' failed." << endl;
223 return kFALSE;
224}
225
226// --------------------------------------------------------------------------
227//
228// PreProcessed the MReadTree to read the run headers and its base class.
229// see MReadTree::PreProcess for more information
230//
231Int_t MReadMarsFile::PreProcess(MParList *pList)
232{
233 fParList = pList;
234
235 if (!fRun->PreProcess(pList))
236 {
237 *fLog << err << "Error - PreProcessing MReadMarsFile::fRun... aborting." << endl;
238 return kFALSE;
239 }
240
241 /*
242 const Int_t idx = GetFileIndex();
243 fRun->SetEventNum(idx<0?0:idx); // Assumption: One Entry per File!
244 if (!fRun->Process())
245 {
246 *fLog << err << "Error - Processing MReadMarsFile::fRun... aborting." << endl;
247 return kFALSE;
248 }
249 */
250 return MReadTree::PreProcess(pList);
251}
Note: See TracBrowser for help on using the repository browser.