#ifndef __TMrbAnalyze_h__
#define __TMrbAnalyze_h__
#include "TObject.h"
#include "TH1.h"
#include "TList.h"
#include "TString.h"
#include "TMrbNamedX.h"
#include "TMrbLofNamedX.h"
#include "TMrbLogger.h"
#include "TMrbLofUserVars.h"
#include "TMrbHistoListEntry.h"
#include "TMrbParamListEntry.h"
#include "TMrbModuleListEntry.h"
#include "TMrbCalibrationListEntry.h"
#include "TMrbDCorrListEntry.h"
#include "TMrbIOSpec.h"
#include "TUsrDeadTime.h"
#include "TUsrEvent.h"
#include "TUsrEvtStart.h"
#include "TUsrEvtStop.h"
#include "TUsrHBX.h"
#include "TUsrHitBuffer.h"
#include "TUsrHit.h"
class TMrbAnalyze : public TObject {
public:
enum { kMB = (1024 * 1024) };
enum { kTimeUnitsPerSec = 10000 };
enum { kTimeNsecsPerUnit = 100000 };
enum { kBranchSplitLevel = 99 };
enum { kBranchBufferSize = 32000 };
enum { kUpdateTime = 8 };
enum { kMaxPolyDegree = 5 };
enum EMrbRunStatus { M_ABSENT,
M_STARTING,
M_RUNNING,
M_PAUSING,
M_STOPPING,
M_STOPPED
};
public:
TMrbAnalyze() {};
TMrbAnalyze(TMrbIOSpec * IOSpec);
~TMrbAnalyze() { fLofIOSpecs.Delete(); };
TMrbAnalyze(const TMrbAnalyze &) {};
void SetScaleDown(Int_t ScaleDown = 1);
TH1F * UpdateRateHistory();
TH1F * UpdateDTimeHistory();
Bool_t TestRunStatus();
Bool_t Initialize(TMrbIOSpec * IOSpec);
Bool_t InitializeUserCode(TMrbIOSpec * IOSpec, Bool_t BeforeHB = kFALSE);
Bool_t AdjustWindowPointers();
Bool_t AdjustFunctionPointers();
Bool_t HandleMessage(const Char_t * ArgList);
Bool_t OpenRootFile(const Char_t * FileName);
Bool_t OpenRootFile(TMrbIOSpec * IOSpec);
Bool_t ReplayEvents(TMrbIOSpec * IOSpec);
Bool_t WriteRootTree(const Char_t * FileName);
Bool_t WriteRootTree(TMrbIOSpec * IOSpec);
inline void SetFileSize(Int_t FileSize = 0) {
fFileSize = kMB * FileSize;
};
Bool_t CloseRootTree(TMrbIOSpec * IOSpec = NULL);
inline Int_t GetScaleDown() const { return(fScaleDown); };
inline void SetRunId(Int_t RunId) { fRunId = RunId; };
inline Int_t GetRunId() const { return(fRunId); };
inline void SetBranchSize(Int_t BranchSize = TMrbAnalyze::kBranchBufferSize) { fBranchSize = BranchSize; };
inline void SetSplitLevel(Bool_t SplitLevel = TMrbAnalyze::kBranchSplitLevel) { fSplitLevel = SplitLevel; };
inline Int_t GetBranchSize() const { return(fBranchSize); };
inline Int_t GetSplitLevel() const { return(fSplitLevel); };
inline void SetRunStatus(EMrbRunStatus RunStatus) { fRunStatus = RunStatus; };
inline EMrbRunStatus GetRunStatus() const { return(fRunStatus); };
inline Int_t IsToBeUpdated() {
Bool_t u = fUpdateFlag;
fUpdateFlag = 0;
return(u);
};
inline void SetUpdateFlag() { fUpdateFlag++; };
inline Int_t GetEventsProcessed() const { return(fEventsProcessed); };
inline void SetReplayMode(Bool_t Flag = kTRUE) { fReplayMode = Flag; };
inline Bool_t IsReplayMode() const { return(fReplayMode); };
inline void IncrEventCount() { fEventsProcessed++; };
inline void ResetEventCount() {
fEventsProcessed = 0;
fEventsProcPrev = 0;
};
inline Bool_t IsModScaleDown() const { return((fEventsProcessed % fScaleDown) == 0); };
inline void SetWriteTreeFlag(Bool_t Flag = kTRUE) { fWriteRootTree = Flag; };
inline Bool_t TreeToBeWritten() const { return(fWriteRootTree); };
void MarkHistogramsWithTime();
Int_t OpenFileList(TString &, TMrbIOSpec *);
Int_t ProcessFileList();
Bool_t ReloadParams(const Char_t * ParamFile);
Bool_t ReloadParams(TMrbIOSpec * IOSpec);
Bool_t ReloadVarsAndWdws(const Char_t * ParamFile);
Bool_t ReloadVarsAndWdws(TMrbIOSpec * IOSpec);
Bool_t ReloadMultipleFiles(TString & ParamFileString);
Bool_t Terminate();
Bool_t FinishRun(TMrbIOSpec * IOSpec, Bool_t BeforeSH = kTRUE);
inline void AddIOSpec(TMrbIOSpec * IOSpec) { fLofIOSpecs.Add(IOSpec); };
inline TMrbIOSpec * GetNextIOSpec(TMrbIOSpec * IOSpec) {
if (IOSpec == NULL) return((TMrbIOSpec *) fLofIOSpecs.First());
else return((TMrbIOSpec *) fLofIOSpecs.After(IOSpec));
};
Int_t SaveHistograms(const Char_t * Pattern, const Char_t * HistFile);
Int_t SaveHistograms(const Char_t * Pattern, TMrbIOSpec * IOSpec);
Int_t ClearHistograms(const Char_t * Pattern, TMrbIOSpec * IOSpec = NULL);
inline void SetCurIOSpec(TMrbIOSpec * IOSpec) { fCurIOSpec = IOSpec; };
inline TMrbIOSpec * GetCurIOSpec() { return(fCurIOSpec); };
inline TList * GetLofIOSpecs() { return(&fLofIOSpecs); };
void InitializeLists(Int_t NofModules, Int_t NofParams);
const Char_t * GetModuleName(Int_t ModuleIndex) const;
const Char_t * GetModuleTitle(Int_t ModuleIndex) const;
Int_t GetModuleIndex(const Char_t * ModuleName) const;
Int_t GetModuleIndexByParam(const Char_t * ParamName) const;
const Char_t * GetParamName(Int_t ModuleIndex, Int_t RelParamIndex) const;
const Char_t * GetParamName(Int_t AbsParamIndex) const;
Int_t GetParamIndex(const Char_t * ParamName, Bool_t AbsFlag = kTRUE) const;
Int_t GetParamIndex(Int_t ModuleIndex, Int_t RelParamIndex) const;
TH1 * GetHistoAddr(const Char_t * HistoName) const;
TH1 * GetHistoAddr(Int_t ModuleIndex, Int_t RelParamIndex) const;
TH1 * GetHistoAddr(Int_t AbsParamIndex) const;
TH1 * GetHistoFromList(TObjArray & HistoList, Int_t ModuleNumber, Int_t RelParamIndex) const;
TH1 * GetHistoFromList(TObjArray & HistoList, Int_t AbsParamIndex) const;
TMrbHistoListEntry * GetHistoListEntry(const Char_t * HistoName) const;
TObject * GetParamAddr(const Char_t * ParamName) const;
TObject * GetParamAddr(Int_t ModuleIndex, Int_t RelParamIndex) const;
TObject * GetParamAddr(Int_t AbsParamIndex) const;
Bool_t AddModuleToList(const Char_t * ModuleName, const Char_t * ModuleTitle,
Int_t ModuleIndex, Int_t AbsParamIndex,
Int_t NofParams, Int_t TimeOffset = 0);
Bool_t AddParamToList(const Char_t * ParamName, TObject * ParamAddr, Int_t ModuleIndex, Int_t RelParamIndex);
Bool_t AddHistoToList(TH1 * HistoAddr, Int_t ModuleIndex, Int_t RelParamIndex);
Int_t GetHistoIndex(Int_t ModuleIndex, Int_t RelParamIndex) const;
TMrbModuleListEntry * GetModuleListEntry(Int_t ModuleIndex) const;
TMrbModuleListEntry * GetModuleListEntry(const Char_t * ModuleName) const;
void ResetModuleHits(Int_t StartIndex = 0, Int_t StopIndex = 0);
Int_t ReadCalibrationFromFile(const Char_t * CalibrationFile);
Bool_t AddCalibrationToList(TF1 * CalibrationAddr, Int_t ModuleIndex, Int_t RelParamIndex);
Bool_t AddCalibrationToList(TF1 * CalibrationAddr, Int_t AbsParamIndex);
TF1 * AddCalibrationToList(const Char_t * Name, const Char_t * Formula, Double_t Xmin, Double_t Xmax, Int_t ModuleIndex, Int_t RelParamIndex);
TF1 * GetCalibration(const Char_t * CalibrationName) const;
TF1 * GetCalibration(Int_t ModuleIndex, Int_t RelParamIndex) const;
TF1 * GetCalibration(Int_t AbsParamIndex) const;
TF1 * GetCalibration(Int_t ModuleIndex, Int_t RelParamIndex, Double_t & Gain, Double_t & Offset) const;
TF1 * GetCalibration(Int_t AbsParamIndex, Double_t & Gain, Double_t & Offset) const;
TF1 * GetCalibration(Int_t ModuleIndex, Int_t RelParamIndex, TArrayD & Coeffs) const;
TF1 * GetCalibration(Int_t AbsParamIndex, TArrayD & Coeffs) const;
TMrbCalibrationListEntry * GetCalibrationListEntry(Int_t ModuleIndex, Int_t RelParamIndex) const;
TMrbCalibrationListEntry * GetCalibrationListEntry(Int_t AbsParamIndex) const;
void PrintCalibration(ostream & Out) const;
void PrintCalibration(ostream & Out, Int_t ModuleIndex, Int_t RelParamIndex) const;
void PrintCalibration(ostream & Out, Int_t AbsParamIndex) const;
void PrintCalibration(ostream & Out, const Char_t * CalibrationName) const;
void PrintCalibration(ostream & Out, TMrbNamedX * CalX) const;
inline void PrintCalibration() const { this->PrintCalibration(cout); };
inline void PrintCalibration(Int_t ModuleIndex, Int_t RelParamIndex) const { this->PrintCalibration(cout, ModuleIndex, RelParamIndex); };
inline void PrintCalibration(Int_t AbsParamIndex) const { this->PrintCalibration(cout, AbsParamIndex); };
inline void PrintCalibration(const Char_t * CalibrationName) const { this->PrintCalibration(cout, CalibrationName); };
Double_t Calibrate(Int_t Energy, Int_t ModuleNumber, Int_t Channel, Bool_t Randomize = kFALSE, Bool_t WithinLimits = kFALSE);
Int_t ReadDCorrFromFile(const Char_t * DCorrFile);
Bool_t AddDCorrToList(TF1 * DCorrAddr, Int_t ModuleIndex, Int_t RelParamIndex);
Bool_t AddDCorrToList(TF1 * DCorrAddr, Int_t AbsParamIndex);
TF1 * AddDCorrToList(const Char_t * Name, const Char_t * Formula, Double_t Xmin, Double_t Xmax, Int_t ModuleIndex, Int_t RelParamIndex);
TF1 * GetDCorr(const Char_t * DCorrName) const;
TF1 * GetDCorr(Int_t ModuleIndex, Int_t RelParamIndex) const;
TF1 * GetDCorr(Int_t AbsParamIndex) const;
TF1 * GetDCorr(Int_t ModuleIndex, Int_t RelParamIndex, Double_t & Factor) const;
TF1 * GetDCorr(Int_t ModuleIndex, Int_t RelParamIndex, Double_t & Beta, Double_t & Angle) const;
TF1 * GetDCorr(Int_t AbsParamIndex, Double_t & Factor) const;
TF1 * GetDCorr(Int_t AbsParamIndex, Double_t & Beta, Double_t & Angle) const;
TMrbDCorrListEntry * GetDCorrListEntry(Int_t ModuleIndex, Int_t RelParamIndex) const;
TMrbDCorrListEntry * GetDCorrListEntry(Int_t AbsParamIndex) const;
inline Int_t GetNofModules() const { return(fNofModules); };
inline Int_t GetNofParams() const { return(fNofParams); };
inline Int_t GetNofHistos() const { return(fHistoList.GetEntriesFast()); };
inline TMrbLofNamedX * GetLofHistos() { return(&fHistoList); };
Bool_t SetTimeOffset();
Bool_t SetTimeOffset(Int_t ModuleNumber, Int_t Offset);
Int_t GetTimeOffset(Int_t ModuleNumber) const;
inline void SetDumpCount(Int_t Count) { fDumpCount = Count; };
inline Int_t GetDumpCount() const { return(fDumpCount); };
Bool_t DumpData(const Char_t * Prefix, Int_t Index, const Char_t * CallingClass, const Char_t * CallingMethod,
const Char_t * Msg, const UShort_t * DataPtr, Int_t DataWC);
Bool_t AddResourcesFromFile(const Char_t * ResourceFile);
const Char_t * GetResource(const Char_t * Resource);
inline void SetVerboseMode(Bool_t VerboseFlag = kTRUE) { fVerboseMode = VerboseFlag; };
inline Bool_t IsVerbose() const { return(fVerboseMode); };
inline void SetFakeMode(Bool_t FakeMode = kTRUE) { fFakeMode = FakeMode; };
inline Bool_t IsFakeMode() const { return(fFakeMode); };
inline TMrbNamedX * FindModule(const Char_t * ModuleName) const { return(fModuleList.FindByName(ModuleName)); };
inline TMrbNamedX * FindParam(const Char_t * ParamName) const { return(fParamList.FindByName(ParamName)); };
inline TMrbNamedX * FindHisto(const Char_t * HistoName, Bool_t SingleFlag = kFALSE) const {
return(SingleFlag ? fSingleList.FindByName(HistoName) : fHistoList.FindByName(HistoName));
};
inline TMrbLofNamedX * GetListOfParams() { return(&fParamList); };
inline TMrbLofNamedX * GetListOfModules() { return(&fModuleList); };
inline TMrbLofNamedX * GetListOfHistos() { return(&fHistoList); };
void ResetEventsPerTrigger();
void PrintLists(ostream & out = cout) const;
void PrintLists(const Char_t * FileName) const;
void PrintStartStop(TUsrEvtStart * Start = NULL, TUsrEvtStop * StopEvent = NULL) const;
void PrintStartStop(UInt_t StartTime, UInt_t StopTime) const;
inline TMrbLogger * GetMessageLogger() const { return(fMessageLogger); };
void WaitForLock(const Char_t * LockFile, const Char_t * Msg = NULL);
inline void Help() { gSystem->Exec(Form("mrbHelp %s", this->ClassName())); };
protected:
Bool_t fVerboseMode;
TMrbLogger * fMessageLogger;
Bool_t fWriteRootTree;
TFile * fRootFileOut;
Int_t fFileSize;
Bool_t fReplayMode;
TFile * fRootFileIn;
Bool_t fFakeMode;
Int_t fDumpCount;
Int_t fNofModules;
Int_t fNofParams;
Int_t fScaleDown;
Int_t fRunId;
Int_t fBranchSize;
Int_t fSplitLevel;
EMrbRunStatus fRunStatus;
Int_t fUpdateFlag;
Int_t fEventsProcessed;
Int_t fTimeOfLastUpdate;
Int_t fEventsProcPrev;
Int_t fHistFileVersion;
TMrbIOSpec * fCurIOSpec;
TList fLofIOSpecs;
TString fResourceFile;
TString fResourceName;
TString fResourceString;
TMrbLofNamedX fModuleList;
TMrbLofNamedX fParamList;
TMrbLofNamedX fHistoList;
TMrbLofNamedX fSingleList;
TMrbLofNamedX fCalibrationList;
TMrbLofNamedX fDCorrList;
ClassDef(TMrbAnalyze, 1)
};
#endif