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

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