| 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 |  | 
|---|
| 72 | ClassImp(MHVsTime); | 
|---|
| 73 |  | 
|---|
| 74 | using namespace std; | 
|---|
| 75 |  | 
|---|
| 76 | const TString MHVsTime::gsDefName  = "MHVsTime"; | 
|---|
| 77 | const 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 | // | 
|---|
| 84 | MHVsTime::MHVsTime(const char *rule, const char *error) | 
|---|
| 85 | : fGraph(NULL), fData(NULL), fError(NULL), 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 | // | 
|---|
| 109 | MHVsTime::~MHVsTime() | 
|---|
| 110 | { | 
|---|
| 111 | if (fGraph) | 
|---|
| 112 | delete fGraph; | 
|---|
| 113 |  | 
|---|
| 114 | if (fData) | 
|---|
| 115 | delete fData; | 
|---|
| 116 |  | 
|---|
| 117 | if (fError) | 
|---|
| 118 | delete fError; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // -------------------------------------------------------------------------- | 
|---|
| 122 | // | 
|---|
| 123 | // Return the data members used by the data chain to be used in | 
|---|
| 124 | // MTask::AddBranchToList | 
|---|
| 125 | // | 
|---|
| 126 | TString MHVsTime::GetDataMember() const | 
|---|
| 127 | { | 
|---|
| 128 | return fData ? fData->GetDataMember() : (TString)""; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | // -------------------------------------------------------------------------- | 
|---|
| 132 | // | 
|---|
| 133 | // PreProcess the MDataChain. Create a new TGraph. Delete an old one if | 
|---|
| 134 | // already allocated. | 
|---|
| 135 | // | 
|---|
| 136 | Bool_t MHVsTime::SetupFill(const MParList *plist) | 
|---|
| 137 | { | 
|---|
| 138 | if (!fGraph || !fData) | 
|---|
| 139 | { | 
|---|
| 140 | *fLog << err << "ERROR - MHVsTime cannot be used with its default constructor!" << endl; | 
|---|
| 141 | return kFALSE; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | if (!fData->PreProcess(plist)) | 
|---|
| 145 | return kFALSE; | 
|---|
| 146 |  | 
|---|
| 147 | if (fError && !fError->PreProcess(plist)) | 
|---|
| 148 | return kFALSE; | 
|---|
| 149 |  | 
|---|
| 150 | fGraph->Set(1); | 
|---|
| 151 | fGraph->SetPoint(0, 0, 0); // Dummy point! | 
|---|
| 152 | fGraph->SetEditable();     // Used as flag: First point? yes/no | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | TString title(fData ? GetRule() : (TString)"Graph"); | 
|---|
| 156 | title += " vs "; | 
|---|
| 157 | title += fUseEventNumber ? "Event Number" : "Time"; | 
|---|
| 158 |  | 
|---|
| 159 | fGraph->SetNameTitle(fName, fTitle==gsDefTitle?title:fTitle); | 
|---|
| 160 |  | 
|---|
| 161 | fMean = 0; | 
|---|
| 162 | fN    = 0; | 
|---|
| 163 |  | 
|---|
| 164 | return kTRUE; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | // -------------------------------------------------------------------------- | 
|---|
| 168 | // | 
|---|
| 169 | // Set the name of the TGraph and the MHVsTime container | 
|---|
| 170 | // | 
|---|
| 171 | void MHVsTime::SetName(const char *name) | 
|---|
| 172 | { | 
|---|
| 173 | fGraph->SetName(name); | 
|---|
| 174 | MH::SetName(name); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | // -------------------------------------------------------------------------- | 
|---|
| 178 | // | 
|---|
| 179 | // Set the title of the TGraph and the MHVsTime container | 
|---|
| 180 | // | 
|---|
| 181 | void MHVsTime::SetTitle(const char *title) | 
|---|
| 182 | { | 
|---|
| 183 | fGraph->SetTitle(title); | 
|---|
| 184 | MH::SetTitle(title); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | // -------------------------------------------------------------------------- | 
|---|
| 188 | // | 
|---|
| 189 | // Set the next data point. If the graph exceeds fMaxPts remove the first | 
|---|
| 190 | // | 
|---|
| 191 | Bool_t MHVsTime::Fill(const MParContainer *par, const Stat_t w) | 
|---|
| 192 | { | 
|---|
| 193 | Double_t t = 0; | 
|---|
| 194 | if (fUseEventNumber) | 
|---|
| 195 | { | 
|---|
| 196 | const MRawEvtHeader *h = dynamic_cast<const MRawEvtHeader*>(par); | 
|---|
| 197 | t = h ? h->GetDAQEvtNumber() : fGraph->GetN(); | 
|---|
| 198 | } | 
|---|
| 199 | else | 
|---|
| 200 | { | 
|---|
| 201 | const MTime *tm = dynamic_cast<const MTime*>(par); | 
|---|
| 202 | if (!tm) | 
|---|
| 203 | { | 
|---|
| 204 | *fLog << err << dbginf << "No MTime found..." << endl; | 
|---|
| 205 | return kFALSE; | 
|---|
| 206 | } | 
|---|
| 207 | // If the time is not valid skip this entry | 
|---|
| 208 | if (!*tm) | 
|---|
| 209 | return kTRUE; | 
|---|
| 210 |  | 
|---|
| 211 | // Do not fill events with equal time | 
|---|
| 212 | if (*tm==fLast || *tm==MTime()) | 
|---|
| 213 | return kTRUE; | 
|---|
| 214 |  | 
|---|
| 215 | fLast = *tm; | 
|---|
| 216 |  | 
|---|
| 217 | t = tm->GetAxisTime(); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | const Double_t v = fData->GetValue(); | 
|---|
| 221 | const Double_t e = fError ? fError->GetValue() : 0; | 
|---|
| 222 |  | 
|---|
| 223 | //*fLog << all << "ADD " << v << " " << e << endl; | 
|---|
| 224 |  | 
|---|
| 225 | fMean    += v; | 
|---|
| 226 | fMeanErr += e; | 
|---|
| 227 | fN++; | 
|---|
| 228 |  | 
|---|
| 229 | if (fN==fNumEvents) | 
|---|
| 230 | { | 
|---|
| 231 | if (fMaxPts>0 && fGraph->GetN()>fMaxPts || fGraph->IsEditable()) | 
|---|
| 232 | { | 
|---|
| 233 | fGraph->RemovePoint(0); | 
|---|
| 234 | fGraph->SetEditable(kFALSE); | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | fGraph->SetPoint(fGraph->GetN(), t, fMean/fN*fScale); | 
|---|
| 238 |  | 
|---|
| 239 | if (fError) | 
|---|
| 240 | static_cast<TGraphErrors*>(fGraph)->SetPointError(fGraph->GetN()-1, 0, fMeanErr/fN*fScale); | 
|---|
| 241 |  | 
|---|
| 242 | fMean = 0; | 
|---|
| 243 | fMeanErr = 0; | 
|---|
| 244 | fN = 0; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | return kTRUE; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | void MHVsTime::Paint(Option_t *opt) | 
|---|
| 251 | { | 
|---|
| 252 | /* | 
|---|
| 253 | *fLog << all << fGraph << " " << gPad->GetName() << " ----> Paint " << opt << endl; | 
|---|
| 254 | TListIter Next(gPad->GetListOfPrimitives()); TObject *obj; | 
|---|
| 255 | while ((obj=Next())) *fLog << obj << " " << obj->GetName() << " " << obj->ClassName() << " " << Next.GetOption() << endl; | 
|---|
| 256 | */ | 
|---|
| 257 |  | 
|---|
| 258 | if (!fGraph) | 
|---|
| 259 | return; | 
|---|
| 260 |  | 
|---|
| 261 | //    *fLog << all << fGraph->GetN() << " " << opt << endl; | 
|---|
| 262 |  | 
|---|
| 263 | if (fGraph->GetN()==0) | 
|---|
| 264 | return; | 
|---|
| 265 |  | 
|---|
| 266 | TString str(opt); | 
|---|
| 267 | if (!str.Contains("A")) | 
|---|
| 268 | str += "A"; | 
|---|
| 269 | if (!str.Contains("P")) | 
|---|
| 270 | str += "P"; | 
|---|
| 271 | if (str.Contains("same", TString::kIgnoreCase)) | 
|---|
| 272 | { | 
|---|
| 273 | str.ReplaceAll("same", ""); | 
|---|
| 274 | str.ReplaceAll("A", ""); | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | // SetPoint deletes the histogram! | 
|---|
| 278 | if (fUseEventNumber) | 
|---|
| 279 | fGraph->GetHistogram()->SetXTitle("Event Number"); | 
|---|
| 280 | else | 
|---|
| 281 | { | 
|---|
| 282 | fGraph->GetHistogram()->SetXTitle("Time"); | 
|---|
| 283 | fGraph->GetHistogram()->GetXaxis()->SetLabelSize(0.033); | 
|---|
| 284 | fGraph->GetHistogram()->GetXaxis()->SetTimeFormat("%H:%M:%S %F1995-01-01 00:00:00 GMT"); | 
|---|
| 285 | fGraph->GetHistogram()->GetXaxis()->SetTimeDisplay(1); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | if (TestBit(kIsLogy)) | 
|---|
| 289 | gPad->SetLogy(); | 
|---|
| 290 |  | 
|---|
| 291 | // This is a workaround if the TGraph has only one point. | 
|---|
| 292 | // Otherwise MStatusDisplay::Update hangs. | 
|---|
| 293 | gPad->GetListOfPrimitives()->Remove(fGraph); | 
|---|
| 294 | fGraph->Draw(fGraph->GetN()<2 ? "A" : str); | 
|---|
| 295 | //gPad->GetListOfPrimitives()->Add(fGraph, fGraph->GetN()<2 ? "A" : opt); | 
|---|
| 296 | //    AppendPad(str); | 
|---|
| 297 | //    fGraph->Draw(str); | 
|---|
| 298 |  | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | // -------------------------------------------------------------------------- | 
|---|
| 302 | // | 
|---|
| 303 | // This displays the TGraph like you expect it to be (eg. time on the axis) | 
|---|
| 304 | // It should also make sure, that the displayed time always is UTC and | 
|---|
| 305 | // not local time... | 
|---|
| 306 | // | 
|---|
| 307 | void MHVsTime::Draw(Option_t *opt) | 
|---|
| 308 | { | 
|---|
| 309 | TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fGraph); | 
|---|
| 310 | pad->SetBorderMode(0); | 
|---|
| 311 | AppendPad(opt); | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | // -------------------------------------------------------------------------- | 
|---|
| 315 | // | 
|---|
| 316 | // Used to rebuild a MHVsTime object of the same type (data members, | 
|---|
| 317 | // dimension, ...) | 
|---|
| 318 | // | 
|---|
| 319 | MParContainer *MHVsTime::New() const | 
|---|
| 320 | { | 
|---|
| 321 | MHVsTime *h=new MHVsTime(fData ? (const char*)GetRule() : NULL); | 
|---|
| 322 | h->SetScale(fScale); | 
|---|
| 323 | if (fUseEventNumber) | 
|---|
| 324 | h->SetUseEventNumber(); | 
|---|
| 325 | h->SetMaxPts(fMaxPts); | 
|---|
| 326 | return h; | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | TString MHVsTime::GetRule() const | 
|---|
| 330 | { | 
|---|
| 331 | return fData ? fData->GetRule() : (TString)""; | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | // -------------------------------------------------------------------------- | 
|---|
| 335 | // | 
|---|
| 336 | // Returns the total number of bins in a histogram (excluding under- and | 
|---|
| 337 | // overflow bins) | 
|---|
| 338 | // | 
|---|
| 339 | Int_t MHVsTime::GetNbins() const | 
|---|
| 340 | { | 
|---|
| 341 | return fGraph->GetN(); | 
|---|
| 342 | } | 
|---|
| 343 | /* | 
|---|
| 344 | TH1 *MHVsTime::GetHist() | 
|---|
| 345 | { | 
|---|
| 346 | return fGraph ? fGraph->GetHistogram() : 0; | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | const TH1 *MHVsTime::GetHist() const | 
|---|
| 350 | { | 
|---|
| 351 | return fGraph ? fGraph->GetHistogram() : 0; | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | TH1 *MHVsTime::GetHistByName(const TString name) | 
|---|
| 355 | { | 
|---|
| 356 | return GetHist(); | 
|---|
| 357 | } | 
|---|
| 358 | */ | 
|---|