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

Last change on this file since 8526 was 8478, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 10.3 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-2007
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// Class Version 2:
54// ----------------
55// + MData *fData; // Object from which the data is filled
56// - MDataChain *fData; // Object from which the data is filled
57// + MData *fError; // Object from which the error is filled
58// - MDataChain *fError; // Object from which the error is filled
59//
60// Class Version 3:
61// ----------------
62// + Double_t fMinimum; // User defined minimum
63// + Double_t fMaximum; // User defined maximum
64//
65/////////////////////////////////////////////////////////////////////////////
66#include "MHVsTime.h"
67
68#include <ctype.h> // tolower
69#include <fstream>
70
71#include <TPad.h>
72#include <TStyle.h>
73#include <TCanvas.h>
74
75#include <TGraphErrors.h>
76
77#include "MLog.h"
78#include "MLogManip.h"
79
80#include "MTime.h"
81#include "MParList.h"
82#include "MDataPhrase.h"
83#include "MRawEvtHeader.h"
84
85ClassImp(MHVsTime);
86
87using namespace std;
88
89const TString MHVsTime::gsDefName = "MHVsTime";
90const TString MHVsTime::gsDefTitle = "Container for a graph vs time/evtnumber";
91
92// --------------------------------------------------------------------------
93//
94// Default constructor. For more informations about a valid rule
95// see MDataPhrase.
96//
97MHVsTime::MHVsTime(const char *rule, const char *error)
98 : fGraph(NULL), fData(NULL), fError(NULL), fScale(1), fMaxPts(-1),
99 fNumEvents(1), fMinimum(-1111), fMaximum(-1111), fUseEventNumber(0)
100{
101 fName = gsDefName;
102 fTitle = gsDefTitle;
103
104 if (!rule)
105 return;
106
107 fData = new MDataPhrase(rule);
108
109 if (error)
110 fError = new MDataPhrase(error);
111
112 fGraph = error ? new TGraphErrors : new TGraph;
113 fGraph->SetPoint(0, 0, 0); // Dummy point!
114 fGraph->SetEditable(); // Used as flag: First point? yes/no
115 fGraph->SetMarkerStyle(kFullDotMedium);
116}
117
118// --------------------------------------------------------------------------
119//
120// Deletes the histogram
121//
122MHVsTime::~MHVsTime()
123{
124 if (fGraph)
125 delete fGraph;
126
127 if (fData)
128 delete fData;
129
130 if (fError)
131 delete fError;
132}
133
134// --------------------------------------------------------------------------
135//
136// Return the data members used by the data chain to be used in
137// MTask::AddBranchToList
138//
139TString MHVsTime::GetDataMember() const
140{
141 return fData ? fData->GetDataMember() : (TString)"";
142}
143
144// --------------------------------------------------------------------------
145//
146// PreProcess the MDataPhrase. Create a new TGraph. Delete an old one if
147// already allocated.
148//
149Bool_t MHVsTime::SetupFill(const MParList *plist)
150{
151 if (!fGraph || !fData)
152 {
153 *fLog << err << "ERROR - MHVsTime cannot be used with its default constructor!" << endl;
154 return kFALSE;
155 }
156
157 if (!fData->PreProcess(plist))
158 return kFALSE;
159
160 if (fError && !fError->PreProcess(plist))
161 return kFALSE;
162
163 fGraph->Set(1);
164 fGraph->SetPoint(0, 0, 0); // Dummy point!
165 fGraph->SetEditable(); // Used as flag: First point? yes/no
166
167 TString title(fData ? GetRule() : (TString)"Graph");
168 title += " vs ";
169 title += fUseEventNumber ? "Event Number" : "Time";
170
171 fGraph->SetNameTitle(fName, fTitle==gsDefTitle?title:fTitle);
172
173 fMean = 0;
174 fN = 0;
175
176 fMin = FLT_MAX;
177 fMax = -FLT_MAX;
178
179 return kTRUE;
180}
181
182// --------------------------------------------------------------------------
183//
184// Set the name of the TGraph and the MHVsTime container
185//
186void MHVsTime::SetName(const char *name)
187{
188 fGraph->SetName(name);
189 MH::SetName(name);
190}
191
192// --------------------------------------------------------------------------
193//
194// Set the title of the TGraph and the MHVsTime container
195//
196void MHVsTime::SetTitle(const char *title)
197{
198 fGraph->SetTitle(title);
199 MH::SetTitle(title);
200}
201
202// --------------------------------------------------------------------------
203//
204// Set the next data point. If the graph exceeds fMaxPts remove the first
205//
206Bool_t MHVsTime::Fill(const MParContainer *par, const Stat_t w)
207{
208 Double_t t = 0;
209 if (fUseEventNumber)
210 {
211 const MRawEvtHeader *h = dynamic_cast<const MRawEvtHeader*>(par);
212 t = h ? h->GetDAQEvtNumber() : fGraph->GetN();
213 }
214 else
215 {
216 const MTime *tm = dynamic_cast<const MTime*>(par);
217 if (!tm)
218 {
219 *fLog << err << dbginf << "No MTime found..." << endl;
220 return kFALSE;
221 }
222 // If the time is not valid skip this entry
223 if (!*tm)
224 return kTRUE;
225
226 // Do not fill events with equal time
227 if (*tm==fLast || *tm==MTime())
228 return kTRUE;
229
230 fLast = *tm;
231
232 t = tm->GetAxisTime();
233 }
234
235 const Double_t v = fData->GetValue();
236 const Double_t e = fError ? fError->GetValue() : 0;
237
238 //*fLog << all << "ADD " << v << " " << e << endl;
239
240 fMean += v;
241 fMeanErr += e;
242 fN++;
243
244 if (fN==fNumEvents)
245 {
246 if (fMaxPts>0 && fGraph->GetN()>fMaxPts || fGraph->IsEditable())
247 {
248 fGraph->RemovePoint(0);
249 fGraph->SetEditable(kFALSE);
250 }
251
252 const Double_t val = fMean/fN*fScale;
253
254 fGraph->SetPoint(fGraph->GetN(), t, val);
255
256 if (fError)
257 static_cast<TGraphErrors*>(fGraph)->SetPointError(fGraph->GetN()-1, 0, fMeanErr/fN*fScale);
258
259 fMin = TMath::Min(fMin, val);
260 fMax = TMath::Max(fMax, val);
261
262 fMean = 0;
263 fMeanErr = 0;
264 fN = 0;
265 }
266
267 return kTRUE;
268}
269
270// --------------------------------------------------------------------------
271//
272// Set Minimum and Maximum;
273Bool_t MHVsTime::Finalize()
274{
275 const Double_t add = (fMax-fMin)*0.15;
276
277 if (fMinimum==-1111)
278 fGraph->SetMinimum(fMin-add);
279 if (fMaximum==-1111)
280 fGraph->SetMaximum(fMax+add);
281
282 return kTRUE;
283}
284
285void MHVsTime::Paint(Option_t *opt)
286{
287 if (!fGraph)
288 return;
289
290 if (fGraph->GetN()==0)
291 return;
292
293 TString str(opt);
294 if (!str.Contains("A"))
295 str += "A";
296 if (!str.Contains("P"))
297 str += "P";
298 if (str.Contains("same", TString::kIgnoreCase))
299 {
300 str.ReplaceAll("same", "");
301 str.ReplaceAll("A", "");
302 }
303
304 // This is not done automatically anymore since root 5.12/00
305 // and it is necessary to force a proper update of the axis.
306 TH1 *h = fGraph->GetHistogram();
307 if (h)
308 {
309 delete h;
310 fGraph->SetHistogram(0);
311 h = fGraph->GetHistogram();
312 }
313 if (h)
314 {
315 TAxis *axe = h->GetXaxis();
316 // SetPoint deletes the histogram!
317 if (fUseEventNumber)
318 axe->SetTitle("Event Number");
319 else
320 {
321 axe->SetTitle("Time");
322 axe->SetLabelSize(0.033);
323 axe->SetTimeFormat("%H:%M %F1995-01-01 00:00:00 GMT");
324 axe->SetTimeDisplay(1);
325 }
326 }
327
328 if (TestBit(kIsLogy))
329 gPad->SetLogy();
330
331 // If this is set to early the plot remains empty in root 5.12/00
332 if (fMinimum!=-1111)
333 fGraph->SetMinimum(fMinimum);
334 if (fMaximum!=-1111)
335 fGraph->SetMaximum(fMaximum);
336
337
338 // This is a workaround if the TGraph has only one point.
339 // Otherwise MStatusDisplay::Update hangs.
340 gPad->GetListOfPrimitives()->Remove(fGraph);
341 fGraph->Draw(fGraph->GetN()<2 ? "A" : str.Data());
342}
343
344// --------------------------------------------------------------------------
345//
346// This displays the TGraph like you expect it to be (eg. time on the axis)
347// It should also make sure, that the displayed time always is UTC and
348// not local time...
349//
350void MHVsTime::Draw(Option_t *opt)
351{
352 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fGraph);
353 pad->SetBorderMode(0);
354 AppendPad(opt);
355}
356
357// --------------------------------------------------------------------------
358//
359// Used to rebuild a MHVsTime object of the same type (data members,
360// dimension, ...)
361//
362MParContainer *MHVsTime::New() const
363{
364 MHVsTime *h=new MHVsTime(fData ? (const char*)GetRule() : NULL);
365 h->SetScale(fScale);
366 if (fUseEventNumber)
367 h->SetUseEventNumber();
368 h->SetMaxPts(fMaxPts);
369 return h;
370}
371
372TString MHVsTime::GetRule() const
373{
374 return fData ? fData->GetRule() : (TString)"";
375}
376
377// --------------------------------------------------------------------------
378//
379// Returns the total number of bins in a histogram (excluding under- and
380// overflow bins)
381//
382Int_t MHVsTime::GetNbins() const
383{
384 return fGraph->GetN();
385}
386/*
387TH1 *MHVsTime::GetHist()
388{
389 return fGraph ? fGraph->GetHistogram() : 0;
390}
391
392const TH1 *MHVsTime::GetHist() const
393{
394 return fGraph ? fGraph->GetHistogram() : 0;
395}
396
397TH1 *MHVsTime::GetHistByName(const TString name)
398{
399 return GetHist();
400}
401*/
Note: See TracBrowser for help on using the repository browser.