NCFS-Pack
A generic (astro)particle physics analysis framework
 
Loading...
Searching...
No Matches
RnoMonitor.cxx
Go to the documentation of this file.
1
17
19
56
57#include "RnoMonitor.h"
58#include "Riostream.h"
59
60ClassImp(RnoMonitor); // Class implementation to enable ROOT I/O
61
63RnoMonitor::RnoMonitor(const char* name,const char* title) : TTask(name,title)
64{
70
71 fEvt=0;
72 fDevSample=1;
73 fVarIndex=1;
74 fVarName="";
75 fVarFunc=0;
76 fNbins24=24;
77 fHistos.SetOwner();
78 fFirst=kTRUE;
79
80 SetDevices("RnoGANT");
81 DefineStatistic("RMSdeviation");
84
85 NcAstrolab lab;
86 lab.SetExperiment("RNO-G");
88}
89
91{
97
98 if (fVarFunc)
99 {
100 delete fVarFunc;
101 fVarFunc=0;
102 }
103}
104
105void RnoMonitor::SetDevices(TString devclass,Int_t ista,Int_t ichan)
106{
122
123 fDevClass=devclass;
124 fSta=ista;
125 fChan=ichan;
126}
127
129{
137
138 fDevSample=j;
139}
140
141void RnoMonitor::SetSampleVariable(Int_t i,TString f)
142{
158
159 fVarIndex=i;
160 fVarName="";
161
162 if (fVarFunc)
163 {
164 delete fVarFunc;
165 fVarFunc=0;
166 }
167
168 if (f!="-") fVarFunc=new TF1("VarFunc",f);
169}
170
171void RnoMonitor::SetSampleVariable(TString name,TString f)
172{
186
187 fVarName=name;
188 fVarIndex=0;
189
190 if (fVarFunc)
191 {
192 delete fVarFunc;
193 fVarFunc=0;
194 }
195
196 if (f!="-") fVarFunc=new TF1("VarFunc",f);
197}
198
200{
227
228 if (mode!="Mean" && mode!="Median" && mode!="RMS" && mode!="SpreadMean" && mode!="SpreadMedian" && mode!="RMSdeviation")
229 {
230 cout << " *" << ClassName() << "::DefineStatistic* Unknown mode : " << mode << endl;
231 cout << " Will continue with current mode : " << fAvMode << endl;
232 return;
233 }
234
235 fAvMode=mode;
236
237 fValues.Reset();
238 fValues.SetStoreMode(0);
239 if (fAvMode=="Median" || fAvMode=="SpreadMean" || fAvMode=="SpreadMedian") fValues.SetStoreMode(1);
240}
241
242void RnoMonitor::SetBaselineMode(Int_t mode,Int_t n,Double_t nrms,Double_t fpr)
243{
282
283 if (mode<0 || mode>3 || n<1 || nrms<0 || fpr<0)
284 {
285 cout << " *" << ClassName() << "::SetBaselineMode* Inconsistent input." << endl;
286 cout << " mode:" << mode << " n:" << n << " nrms:" << nrms << " fpr:" << fpr << endl;
287 cout << " Will continue with current settings : mode=" << mode << " n=" << n << " nrms=" << nrms << " fpr=" << fpr << endl;
288 return;
289 }
290
291 if (mode<3)
292 {
293 nrms=-1;
294 fpr=-1;
295 }
296 if (!mode || mode==3) n=-1;
297
298 fBasemode=mode;
299 fBlocksize=n;
300 fNrms=nrms;
301 fFPR=fpr;
302}
303
304void RnoMonitor::SetBandFilters(TArray& freqs,Int_t n)
305{
350
351 Int_t arrsize=freqs.GetSize();
352
353 // Removing all band filter settings
354 if (!arrsize || n<1)
355 {
356 fBands.Set(0);
357 fNkernel=0;
358 return;
359 }
360
361 Int_t oddsize=arrsize%2;
362 Int_t nbands=arrsize/2;
363 if (nbands<1 || n<1 || oddsize)
364 {
365 printf(" *%-s::SetBandFilters* Invalid input array size=%-i nbands=%-i n=%-i \n",ClassName(),arrsize,nbands,n);
366 fBands.Set(0);
367 fNkernel=0;
368 return;
369 }
370
371 fNkernel=n;
372 fBands.Set(arrsize);
373 for (Int_t i=0; i<arrsize; i++)
374 {
375 fBands[i]=freqs.GetAt(i);
376 }
377}
378
380{
388
389 if (n<1)
390 {
391 cout << " *" << ClassName() << "::SetNbins24* Inconsistent input : " << n << endl;
392 cout << " Will continue with current value : " << fNbins24 << endl;
393 return;
394 }
395
396 fNbins24=n;
397}
398
399void RnoMonitor::Exec(Option_t* opt)
400{
410
411 TString name=opt;
412 NcJob* parent=(NcJob*)(gROOT->GetListOfTasks()->FindObject(name.Data()));
413
414 if (!parent) return;
415
416 fEvt=(RnoEvent*)parent->GetObject("RnoEvent");
417 if (!fEvt) return;
418
419 // Do not process rejected events
420 Int_t select=fEvt->GetSelectLevel();
421 if (select<0) return;
422
423 // Get the sampling frequency
424 Double_t fsample=0;
425 NcDevice* daq=fEvt->GetDevice("DAQ");
426 if (daq) fsample=daq->GetSignal("Sampling-rate");
427 fDSP.SetSamplingFrequency(fsample);
428
429 RnoGANT* antx=0; // Pointer for a generic RNO antenna
430 Int_t ista=0; // Station number
431 Int_t ichan=0; // Channel number
432 TObjArray* devs=0; // List of selected devices
433 TString str;
434 TString str2;
435 Int_t ndevs=0;
436 NcSample* sx=0;
437 TString sxname="none";
438 TString varname="none";
439 TString monval="none";
440 Double_t val=0;
441 Double_t time=0; // Time in fractional hours
442 Double_t x=0; // The x value of a point in the graph
443
444 // The daily 24 hour monitoring histograms
445 TH1F* hut24=0;
446 TH1I* hnut24=0;
447 TH1F* hlt24=0;
448 TH1I* hnlt24=0;
449 TH1F* hlmst24=0;
450 TH1I* hnlmst24=0;
451
452 // The bin size in minutes
453 Float_t bsize=24.*60./float(fNbins24);
454
455 devs=fEvt->GetDevices(fDevClass);
456 ndevs=devs->GetEntries();
457
458 for (Int_t idev=0; idev<ndevs; idev++)
459 {
460 antx=(RnoGANT*)devs->At(idev);
461
462 if (!antx) continue;
463
464 ista=antx->GetStation();
465 ichan=antx->GetNumber(-1);
466
467 // Check for the user selected station and channel numbers
468 if (fSta>=0 && ista!=fSta) continue;
469 if (fChan>=0 && ichan!=fChan) continue;
470
471 sx=antx->GetSample(fDevSample);
472
473 if (!sx) continue;
474
475 // Construct the text for the monitored observable
476 sxname=sx->GetName();
478 varname=sx->GetVariableName(fVarIndex);
479 monval=fAvMode;
480 monval+="[";
481 if (!fVarFunc)
482 {
483 monval+=varname;
484 }
485 else
486 {
487 monval+=fVarFunc->GetExpFormula("p");
488 monval.ReplaceAll("x",varname);
489 }
490 monval+="]";
491
492 Int_t ndata=sx->GetN();
493
494 if (!ndata) continue;
495
496 // Fill the data array with the recorded samplings
497 TArrayD data(ndata);
498 for (Int_t j=0; j<ndata; j++)
499 {
500 data[j]=sx->GetEntry(j+1,fVarIndex);
501 }
502
503 // Perform the (Bayesian) Block baseline correction if requested
504 if (fBasemode)
505 {
506 fGin=fEvt->GetSamplingGraph(ista,ichan);
507 if (fBasemode==3)
508 {
509 fBB.GetBlocks(fGin,fNrms,fFPR,&fHblock);
510 }
511 else
512 {
513 fBB.GetBlocks(&fGin,&fHblock,fBlocksize,fBasemode-1);
514 }
515 fBB.Add(&fGin,&fHblock,&fGout,-1);
516
517 ndata=fGout.GetN();
518
519 if (!ndata) continue;
520
521 // Fill the data array with the modified data
522 data.Set(ndata);
523 for (Int_t j=0; j<ndata; j++)
524 {
525 fGout.GetPoint(j,x,data[j]);
526 }
527 }
528
529 // Perform the frequency band filtering if requested
530 Int_t narr=fBands.GetSize();
531 if (narr && fNkernel && fsample>0)
532 {
533 fDSP.Load(&data);
534 // Convert the filter bands from MHz to fractions of the sampling frequency
535 TArrayD bands(narr);
536 for (Int_t j=0; j<narr; j++)
537 {
538 bands[j]=fBands[j]*1.e6/fsample;
539 }
540 TH1F* hkern=(TH1F*)fHistos.FindObject("hFilterKernel");
541 if (!hkern)
542 {
543 hkern=new TH1F();
544 hkern->SetName("hFilterKernel");
545 fDSP.GetMultiBandKernel(bands,fNkernel,hkern);
546 fHistos.Add(hkern);
547 }
548 Int_t i1=0;
549 Int_t i2=0;
550 TArrayD temp=fDSP.FilterMultiBand(bands,fNkernel,0,0,0,&i1,&i2);
551 ndata=i2-i1+1;
552
553 if (ndata<1) continue;
554
555 // Fill the data array with the modified data
556 data.Set(ndata);
557 Int_t index=0;
558 for (Int_t j=i1; j<=i2; j++)
559 {
560 if (index>=ndata) break;
561 data[index]=temp[j];
562 index++;
563 }
564 }
565
566 // Construct the sample with the selected statistic values
567 fValues.Reset();
568 for (Int_t j=0; j<ndata; j++)
569 {
570 val=data[j];
571 if (fVarFunc) val=fVarFunc->Eval(val);
572 fValues.Enter(val);
573 }
574
575 if (fAvMode=="Mean") val=fValues.GetMean(1);
576 if (fAvMode=="Median") val=fValues.GetMedian(1);
577 if (fAvMode=="RMS") val=fValues.GetRMS(1);
578 if (fAvMode=="SpreadMean") val=fValues.GetSpread(1,1);
579 if (fAvMode=="SpreadMedian") val=fValues.GetSpread(1,0);
580 if (fAvMode=="RMSdeviation") val=fValues.GetSigma(1,0);
581
582 str="hUT24-S";
583 str+=ista;
584 str+="Ch";
585 str+=ichan;
586
587 str2=str;
588 str2+="-N";
589
590 hut24=(TH1F*)fHistos.FindObject(str);
591 hnut24=(TH1I*)fHistos.FindObject(str2);
592
593 if (!hut24)
594 {
595 hnut24=new TH1I(str2,"UT24 bin entry counts;Universal Time (hours);Number of entries",fNbins24,0,24);
596 fHistos.Add(hnut24);
597 hut24=new TH1F(str,"UT daily monitoring",fNbins24,0,24);
598 hut24->Sumw2();
599 str.Form("Daily monitoring (%-.3g min. periods) of Station%-i ",bsize,ista);
600 str+=antx->GetName();
601 str2=str;
602 str2+=";Universal Time (hours);";
603 if (fBasemode || fNkernel) str2+="*";
604 str2+="Av. ";
605 str2+=monval;
606 hut24->SetTitle(str2);
607 fHistos.Add(hut24);
608 }
609
610 str="hLT24-S";
611 str+=ista;
612 str+="Ch";
613 str+=ichan;
614
615 str2=str;
616 str2+="-N";
617
618 hlt24=(TH1F*)fHistos.FindObject(str);
619 hnlt24=(TH1I*)fHistos.FindObject(str2);
620
621 if (!hlt24)
622 {
623 hnlt24=new TH1I(str2,"LT24 bin entry counts;Summit Local Time (hours);Number of entries",fNbins24,0,24);
624 fHistos.Add(hnlt24);
625 hlt24=new TH1F(str,"LT daily monitoring",fNbins24,0,24);
626 hlt24->Sumw2();
627 str.Form("Daily monitoring (%-.3g min. periods) of Station%-i ",bsize,ista);
628 str+=antx->GetName();
629 str2=str;
630 str2+=";Summit Local Time (hours);";
631 if (fBasemode || fNkernel) str2+="*";
632 str2+="Av. ";
633 str2+=monval;
634 hlt24->SetTitle(str2);
635 fHistos.Add(hlt24);
636 }
637
638 str="hLMST24-S";
639 str+=ista;
640 str+="Ch";
641 str+=ichan;
642
643 str2=str;
644 str2+="-N";
645
646 hlmst24=(TH1F*)fHistos.FindObject(str);
647 hnlmst24=(TH1I*)fHistos.FindObject(str2);
648
649 if (!hlmst24)
650 {
651 hnlmst24=new TH1I(str2,"LMST24 bin entry counts;Summit Mean Siderial Time (hours);Number of entries",fNbins24,0,24);
652 fHistos.Add(hnlmst24);
653 hlmst24=new TH1F(str,"LMST daily monitoring",fNbins24,0,24);
654 hlmst24->Sumw2();
655 str.Form("Daily monitoring (%-.3g min. periods) of Station%-i ",bsize,ista);
656 str+=antx->GetName();
657 str2=str;
658 str2+=";Summit Mean Siderial Time (hours);";
659 if (fBasemode || fNkernel) str2+="*";
660 str2+="Av. ";
661 str2+=monval;
662 hlmst24->SetTitle(str2);
663 fHistos.Add(hlmst24);
664 }
665
666 time=fEvt->GetUT();
667 hut24->Fill(time,val);
668 hnut24->Fill(time);
669
670 time=fEvt->GetLT(fOffset);
671 hlt24->Fill(time,val);
672 hnlt24->Fill(time);
673
674 time=fEvt->GetLMST(fOffset);
675 hlmst24->Fill(time,val);
676 hnlmst24->Fill(time);
677 } // End of loop over devices
678
679 if (fFirst)
680 {
681 TString sbase="None";
682 if (fBasemode==1) sbase="Mean of consecutive samples per block";
683 if (fBasemode==2) sbase="Median of consecutive samples per block";
684 if (fBasemode==3) sbase="Bayesian Blocks";
685 printf("\n");
686 printf(" *%-s::Exec* Processor parameter settings. \n",ClassName());
687 printf(" Processor name ............ : %-s \n",GetName());
688 printf(" Processor title ........... : %-s \n",GetTitle());
689 printf(" Device class .............. : %-s \n",fDevClass.Data());
690 printf(" Station number ............ : %-i (<0 means all stations) \n",fSta);
691 printf(" Channel number ............ : %-i (<0 means all channels) \n",fChan);
692 printf(" Device sample ............. : Index=%-i Name=%-s \n",fDevSample,sxname.Data());
693 printf(" Sample variable ........... : Index=%-i Name=%-s \n",fVarIndex,varname.Data());
694 printf(" Monitor value ............. : %-s \n",monval.Data());
695 printf(" Baseline correction mode .. : %-i (%-s) \n",fBasemode,sbase.Data());
696 if (fBasemode==1 || fBasemode==2)
697 {
698 printf(" Baseline block size ....... : %-i samples \n",fBlocksize);
699 }
700 if (fBasemode==3)
701 {
702 printf(" Bayesian Block nrms ....... : %-g \n",fNrms);
703 printf(" Bayesian Block FPR ........ : %-g \n",fFPR);
704 }
705 printf(" Band Filter kernel size ... : %-i \n",fNkernel);
706 printf(" Number of bins for 24 hours : %-i \n",fNbins24);
707 fFirst=kFALSE;
708 }
709}
710
712{
718
719 Int_t nh=fHistos.GetEntries();
720 cout << endl;
721 cout << " === The following " << nh << " histograms have been generated for DeviceClass : " << fDevClass << endl;
722 for (Int_t ih=0; ih<nh; ih++)
723 {
724 TObject* hx=fHistos.At(ih);
725 if (!hx) continue;
726 cout << " " << hx->GetName() << " : " << hx->GetTitle() << endl;
727 }
728 cout << " ===============================================================================" << endl;
729}
730
731void RnoMonitor::WriteHistograms(TString filename)
732{
739
740 // The Tree with the baseline parameter settings
741 TTree Parameters("Parameters","Parameter settings");
742 Parameters.Branch("BaseMode",&fBasemode,"BaseMode/I Baseline");
743 Parameters.Branch("Blocksize",&fBlocksize,"Blocksize/I # samples");
744 Parameters.Branch("Nrms",&fNrms,"Nrms/D # RMS deviations");
745 Parameters.Branch("FPR",&fFPR,"FPR/D False Positive Rate");
746 Parameters.Fill();
747
748 // The output file for the produced histograms
749 TFile fout(filename,"RECREATE","RnoMonitor results");
750
751 // Write the parameter settings to the output file
752 Parameters.Write();
753
754 // Write all the histos to the output file
755 Int_t nh=fHistos.GetEntries();
756 TString name;
757 Int_t nen=0;
758 Int_t nbins=0;
759 Int_t nk=0;
760 Double_t sumw2;
761 Double_t mu=0;
762 Double_t msq=0;
763 Double_t rms=0;
764 Double_t cval=0;
765 Double_t var=0;
766 Double_t s=0;
767 for (Int_t ih=0; ih<nh; ih++)
768 {
769 TH1* hx=(TH1*)fHistos.At(ih);
770 if (!hx) continue;
771
772 // Get the corresponding histogram with the individual bin counts
773 name=hx->GetName();
774 name+="-N";
775 TH1* hn=(TH1*)fHistos.FindObject(name);
776
777 // Determine the central value and its error of the individual bin samplings
778 if (hn)
779 {
780 nbins=hx->GetNbinsX();
781 nen=hx->GetEntries();
782
783 TArrayD* arrw2=hx->GetSumw2();
784 for (Int_t ibin=1; ibin<=nbins; ibin++)
785 {
786 nk=hn->GetBinContent(ibin);
787 if (!nk) continue;
788
789 mu=hx->GetBinContent(ibin)/double(nk);
790 sumw2=0;
791 if (arrw2) sumw2=arrw2->At(ibin);
792 msq=sumw2/double(nk);
793 rms=sqrt(msq);
794
795 cval=mu;
796 if (fAvMode=="RMS") cval=rms;
797
798 var=msq-pow(mu,2);
799 s=0;
800 if (var>=0) s=sqrt(var);
801
802 hx->SetBinContent(ibin,cval);
803 // Set the error on the mean according to a Student's t distribution
804 // (i.e. assuming an underlying Gaussian with unknown sigma)
805 hx->SetBinError(ibin,s/sqrt(nk));
806 }
807 hx->SetEntries(nen);
808 }
809
810 hx->Write(); // Write this histogram to the output file
811 }
812
813 // Flush the output file buffers to disk
814 fout.Write();
815
816 cout << endl;
817 cout << " *" << ClassName() << "::WriteHistograms* All generated histograms have been written to file : " << filename << endl;
819}
820
ClassImp(RnoMonitor)
Virtual lab to provide (astro)physical parameters, treat data and relate observations with astrophysi...
Definition NcAstrolab.h:47
Double_t GetLabTimeOffset() const
void SetExperiment(TString name, Int_t id=0)
Signal (Hit) handling of a generic device.
Definition NcDevice.h:14
Base class for top level job in a task based procedure.
Definition NcJob.h:18
TObject * GetObject(const char *classname) const
Definition NcJob.cxx:456
Sampling and statistics tools for various multi-dimensional data samples.
Definition NcSample.h:28
Int_t GetN() const
Int_t GetIndex(TString name) const
TString GetVariableName(Int_t i) const
Double_t GetEntry(Int_t i, Int_t j, Int_t mode=0, Int_t k=0)
NcSample * GetSample(Int_t j=1) const
virtual Float_t GetSignal(Int_t j=1, Int_t mode=0) const
Definition NcSignal.cxx:651
Handling of RNO-G event data.
Definition RnoEvent.h:14
Signal (Hit) handling of an RNO-G Generic Antenna (GANT).
Definition RnoGANT.h:12
Int_t GetNumber(Int_t id=0) const
Definition RnoGANT.cxx:214
Int_t GetStation(Int_t id=0) const
Definition RnoGANT.cxx:150
TTask derived class to monitor RNO-G data over certain time periods.
Definition RnoMonitor.h:17
TString fAvMode
Definition RnoMonitor.h:46
Double_t fFPR
Definition RnoMonitor.h:51
TObjArray fHistos
Definition RnoMonitor.h:44
Int_t fBasemode
Definition RnoMonitor.h:47
void SetBaselineMode(Int_t mode, Int_t n=128, Double_t nrms=1.2, Double_t fpr=0.1)
virtual void Exec(Option_t *opt)
NcDSP fDSP
Definition RnoMonitor.h:55
Int_t fChan
Definition RnoMonitor.h:38
TString fVarName
Definition RnoMonitor.h:41
NcBlocks fBB
Definition RnoMonitor.h:49
void SetDeviceSample(Int_t j)
NcSample fValues
Definition RnoMonitor.h:45
void SetNbins24(Int_t n)
Bool_t fFirst
Definition RnoMonitor.h:58
void SetDevices(TString devclass, Int_t ista=-1, Int_t ichan=-1)
void SetSampleVariable(Int_t i, TString f="-")
virtual ~RnoMonitor()
void SetBandFilters(TArray &freqs, Int_t n)
Int_t fBlocksize
Definition RnoMonitor.h:48
TF1 * fVarFunc
Definition RnoMonitor.h:42
void ListHistograms() const
Int_t fNkernel
Definition RnoMonitor.h:57
TGraph fGin
Definition RnoMonitor.h:52
Int_t fNbins24
Definition RnoMonitor.h:43
Double_t fOffset
Definition RnoMonitor.h:35
TH1F fHblock
Definition RnoMonitor.h:53
Int_t fSta
Definition RnoMonitor.h:37
void WriteHistograms(TString filename)
Double_t fNrms
Definition RnoMonitor.h:50
RnoEvent * fEvt
Definition RnoMonitor.h:34
Int_t fVarIndex
Definition RnoMonitor.h:40
RnoMonitor(const char *name="RnoMonitor", const char *title="RNO-G data monitoring")
Int_t fDevSample
Definition RnoMonitor.h:39
TString fDevClass
Definition RnoMonitor.h:36
TArrayD fBands
Definition RnoMonitor.h:56
void DefineStatistic(TString mode)
TGraph fGout
Definition RnoMonitor.h:54