source: tags/Mars-V1.0/mhvstime/MHVsTime.cc

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