source: trunk/MagicSoft/Mars/mhvstime/MHVsTime.cc@ 5212

Last change on this file since 5212 was 4891, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 8.9 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, 11/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MHVsTime
28//
29// Use this class if you want to display any rule vs time (or event number)
30//
31// Axis titles
32// ===========
33//
34// 1) If no other title is given the rule for the y-axis is used.
35// 2) If the MH3 has a non-default title (MH3::SetTitle called)
36// this title is set as the histogram title. It can be used to overwrite
37// the axis titles. For more information see TH1::SetTitle, eg.
38// SetTitle("MyGraph;;Counts");
39// The title for the x-axis is ignored and set automatically (MAKE SURE
40// YOU HAVE TWO SEMICOLON!)
41//
42// eg.
43// MHVsTime hist("MHillas.fAlpha");
44// MHVsTime hist("MPointintPos.GetAbsErr");
45// MHVsTime hist("MPointintPos.GetAbsErr*kRad2Deg");
46//
47// To set a maximum number of data-points (eg. to display the last 20min
48// only) call SetMaxPts(200)
49//
50// SetMaxPts(-1) disables this feature.
51//
52/////////////////////////////////////////////////////////////////////////////
53#include "MHVsTime.h"
54
55#include <ctype.h> // tolower
56#include <fstream>
57
58#include <TPad.h>
59#include <TStyle.h>
60#include <TCanvas.h>
61
62#include <TGraphErrors.h>
63
64#include "MLog.h"
65#include "MLogManip.h"
66
67#include "MTime.h"
68#include "MParList.h"
69#include "MDataChain.h"
70#include "MRawEvtHeader.h"
71
72ClassImp(MHVsTime);
73
74using namespace std;
75
76const TString MHVsTime::gsDefName = "MHVsTime";
77const TString MHVsTime::gsDefTitle = "Container for a graph vs time/evtnumber";
78
79// --------------------------------------------------------------------------
80//
81// Default constructor. For more informations about a valid rule
82// see MDataChain.
83//
84MHVsTime::MHVsTime(const char *rule, const char *error)
85 : fGraph(NULL), fData(NULL), fError(0), fScale(1), fMaxPts(-1),
86 fNumEvents(1), fUseEventNumber(0)
87{
88 fName = gsDefName;
89 fTitle = gsDefTitle;
90
91 if (!rule)
92 return;
93
94 fData = new MDataChain(rule);
95
96 if (error)
97 fError = new MDataChain(error);
98
99 fGraph = error ? new TGraphErrors : new TGraph;
100 fGraph->SetPoint(0, 0, 0); // Dummy point!
101 fGraph->SetEditable(); // Used as flag: First point? yes/no
102 fGraph->SetMarkerStyle(kFullDotMedium);
103}
104
105// --------------------------------------------------------------------------
106//
107// Deletes the histogram
108//
109MHVsTime::~MHVsTime()
110{
111 if (fGraph)
112 delete fGraph;
113
114 if (fData)
115 delete fData;
116}
117
118// --------------------------------------------------------------------------
119//
120// Return the data members used by the data chain to be used in
121// MTask::AddBranchToList
122//
123TString MHVsTime::GetDataMember() const
124{
125 return fData ? fData->GetDataMember() : (TString)"";
126}
127
128// --------------------------------------------------------------------------
129//
130// PreProcess the MDataChain. Create a new TGraph. Delete an old one if
131// already allocated.
132//
133Bool_t MHVsTime::SetupFill(const MParList *plist)
134{
135 if (!fGraph || !fData)
136 {
137 *fLog << err << "ERROR - MHVsTime cannot be used with its default constructor!" << endl;
138 return kFALSE;
139 }
140
141 if (!fData->PreProcess(plist))
142 return kFALSE;
143
144 if (fError && !fError->PreProcess(plist))
145 return kFALSE;
146
147 fGraph->Set(1);
148 fGraph->SetPoint(0, 0, 0); // Dummy point!
149 fGraph->SetEditable(); // Used as flag: First point? yes/no
150
151
152 TString title(fData ? GetRule() : (TString)"Graph");
153 title += " vs ";
154 title += fUseEventNumber ? "Event Number" : "Time";
155
156 fGraph->SetNameTitle(fName, fTitle==gsDefTitle?title:fTitle);
157
158 fMean = 0;
159 fN = 0;
160
161 return kTRUE;
162}
163
164// --------------------------------------------------------------------------
165//
166// Set the name of the TGraph and the MHVsTime container
167//
168void MHVsTime::SetName(const char *name)
169{
170 fGraph->SetName(name);
171 MParContainer::SetName(name);
172}
173
174// --------------------------------------------------------------------------
175//
176// Set the title of the TGraph and the MHVsTime container
177//
178void MHVsTime::SetTitle(const char *title)
179{
180 fGraph->SetTitle(title);
181 MParContainer::SetTitle(title);
182}
183
184// --------------------------------------------------------------------------
185//
186// Set the next data point. If the graph exceeds fMaxPts remove the first
187//
188Bool_t MHVsTime::Fill(const MParContainer *par, const Stat_t w)
189{
190 Double_t t = 0;
191 if (fUseEventNumber)
192 {
193 const MRawEvtHeader *h = dynamic_cast<const MRawEvtHeader*>(par);
194 t = h ? h->GetDAQEvtNumber() : fGraph->GetN();
195 }
196 else
197 {
198 const MTime *tm = dynamic_cast<const MTime*>(par);
199 if (!tm)
200 {
201 *fLog << err << dbginf << "No MTime found..." << endl;
202 return kFALSE;
203 }
204 // If the time is not valid skip this entry
205 if (!*tm)
206 return kTRUE;
207
208 // Do not fill events with equal time
209 if (*tm==fLast || *tm==MTime())
210 return kTRUE;
211
212 fLast = *tm;
213
214 t = tm->GetAxisTime();
215 }
216
217 const Double_t v = fData->GetValue();
218 const Double_t e = fError ? fError->GetValue() : 0;
219
220 //*fLog << all << "ADD " << v << " " << e << endl;
221
222 fMean += v;
223 fMeanErr += e;
224 fN++;
225
226 if (fN==fNumEvents)
227 {
228 if (fMaxPts>0 && fGraph->GetN()>fMaxPts || fGraph->IsEditable())
229 {
230 fGraph->RemovePoint(0);
231 fGraph->SetEditable(kFALSE);
232 }
233
234 fGraph->SetPoint(fGraph->GetN(), t, fMean/fN*fScale);
235
236 if (fError)
237 static_cast<TGraphErrors*>(fGraph)->SetPointError(fGraph->GetN()-1, 0, fMeanErr/fN*fScale);
238
239 fMean = 0;
240 fMeanErr = 0;
241 fN = 0;
242 }
243
244 return kTRUE;
245}
246
247void MHVsTime::Paint(Option_t *opt)
248{
249 // SetPoint deletes the histogram!
250 if (fUseEventNumber)
251 fGraph->GetHistogram()->SetXTitle("Event Number");
252 else
253 {
254 fGraph->GetHistogram()->SetXTitle("Time");
255 fGraph->GetHistogram()->GetXaxis()->SetLabelSize(0.033);
256 fGraph->GetHistogram()->GetXaxis()->SetTimeFormat("%H:%M:%S %F1995-01-01 00:00:00 GMT");
257 fGraph->GetHistogram()->GetXaxis()->SetTimeDisplay(1);
258 }
259
260 if (TestBit(kIsLogy))
261 gPad->SetLogy();
262
263 // This is a workaround if the TGraph has only one point.
264 // Otherwise MStatusDisplay::Update hangs.
265 gPad->GetListOfPrimitives()->Remove(fGraph);
266 gPad->GetListOfPrimitives()->Add(fGraph, fGraph->GetN()<2 ? "A" : opt);
267}
268
269// --------------------------------------------------------------------------
270//
271// This displays the TGraph like you expect it to be (eg. time on the axis)
272// It should also make sure, that the displayed time always is UTC and
273// not local time...
274//
275void MHVsTime::Draw(Option_t *opt)
276{
277 if (!fGraph)
278 return;
279
280 if (fGraph->GetN()==0)
281 return;
282
283 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fGraph);
284 pad->SetBorderMode(0);
285
286 TString str(opt);
287
288 if (!str.Contains("A"))
289 str += "A";
290 if (!str.Contains("P"))
291 str += "P";
292
293 if (str.Contains("same", TString::kIgnoreCase))
294 {
295 str.ReplaceAll("same", "");
296 str.ReplaceAll("A", "");
297 }
298
299 AppendPad(str);
300 fGraph->Draw(str);
301}
302
303// --------------------------------------------------------------------------
304//
305// Used to rebuild a MHVsTime object of the same type (data members,
306// dimension, ...)
307//
308MParContainer *MHVsTime::New() const
309{
310 MHVsTime *h=new MHVsTime(fData ? (const char*)GetRule() : NULL);
311 h->SetScale(fScale);
312 if (fUseEventNumber)
313 h->SetUseEventNumber();
314 h->SetMaxPts(fMaxPts);
315 return h;
316}
317
318TString MHVsTime::GetRule() const
319{
320 return fData ? fData->GetRule() : (TString)"";
321}
322
323// --------------------------------------------------------------------------
324//
325// Returns the total number of bins in a histogram (excluding under- and
326// overflow bins)
327//
328Int_t MHVsTime::GetNbins() const
329{
330 return fGraph->GetN();
331}
332/*
333TH1 *MHVsTime::GetHist()
334{
335 return fGraph ? fGraph->GetHistogram() : 0;
336}
337
338const TH1 *MHVsTime::GetHist() const
339{
340 return fGraph ? fGraph->GetHistogram() : 0;
341}
342
343TH1 *MHVsTime::GetHistByName(const TString name)
344{
345 return GetHist();
346}
347*/
Note: See TracBrowser for help on using the repository browser.