REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Public Member Functions | Private Attributes
TRestThread Class Reference

Detailed Description

Threaded worker of a process chain.

Multiple instances of TRestThread is created inside TRestProcessRunner. Each of them can detach a thread containing a process chain, which implements multi thread functionality. Preparation of process chain is also done inside this class.


RESTsoft - Software for Rare Event Searches with TPCs

History of developments:

2014-june: First concept. As part of conceptualization of previous REST code (REST v2) Igor G. Irastorza

2017-Aug: Major change: added for multi-thread capability Kaixiang Ni


Definition at line 24 of file TRestThread.h.

#include <TRestThread.h>

Public Member Functions

void AddProcess (TRestEventProcess *process)
 Add a process. More...
 
void EndProcess ()
 Write and close the output file. More...
 
Bool_t Finished () const
 
TRestAnalysisTreeGetAnalysisTree () const
 
TTree * GetEventTree ()
 
TRestEventGetInputEvent ()
 
TRestEventGetOutputEvent ()
 
TFile * GetOutputFile ()
 
TRestEventProcessGetProcess (int i) const
 
Int_t GetProcessnum () const
 
Int_t GetThreadId () const
 
TRestStringOutput::REST_Verbose_Level GetVerboseLevel () const
 
void Initialize ()
 Set variables by default during initialization.
 
void PrepareToProcess (bool *outputConfig=nullptr)
 Prepare some thing before we can start process. More...
 
void ProcessEvent ()
 Process a single event. More...
 
void SetCompressionLevel (Int_t comp)
 
void SetOutputTree (TRestAnalysisTree *t)
 
void SetProcessRunner (TRestProcessRunner *r)
 
void SetThreadId (Int_t id)
 
void SetVerboseLevel (TRestStringOutput::REST_Verbose_Level verb)
 
void StartProcess ()
 The main function of this class. Thread will run this function until the end. More...
 
void StartThread ()
 Create a thread with the method StartProcess(). More...
 
bool TestRun ()
 Make a test run of our process chain. More...
 
Int_t ValidateChain (TRestEvent *input)
 Check if the input/output of each process in the process chain matches. More...
 

Private Attributes

TRestAnalysisTreefAnalysisTree
 
Int_t fCompressionLevel
 
TTree * fEventTree
 
TRestProcessRunnerfHostRunner
 
TRestEventfInputEvent
 
TRestEventfOutputEvent
 
TFile * fOutputFile
 
std::vector< TRestEventProcess * > fProcessChain
 
Bool_t fProcessNullReturned
 
Int_t fThreadId
 
TRestStringOutput::REST_Verbose_Level fVerboseLevel
 
Bool_t isFinished
 
std::thread t
 

Member Function Documentation

◆ AddProcess()

void TRestThread::AddProcess ( TRestEventProcess process)

Add a process.

The process will be added to the end of the process chain. It will also increase TRestThread's verbose level if the added process's verbose level is higher. Note that we cannot use "debug" verbose level under compatibility output mode, i.e. in condor jobs.

Definition at line 462 of file TRestThread.cxx.

◆ EndProcess()

void TRestThread::EndProcess ( )

Write and close the output file.

This method is called back at the end of TRestProcessRunner::RunProcess() in TRestProcessRunner.

Definition at line 573 of file TRestThread.cxx.

◆ PrepareToProcess()

void TRestThread::PrepareToProcess ( bool *  outputConfig = nullptr)

Prepare some thing before we can start process.

This method will:

  1. Setup the processes in process chain:(set analysis tree, set readonly, call the method InitProcess())
  2. Instantiate the input event and make a test run, calling TestRun(). The address of output event is fixed after the test run.
  3. Open the output file and create output tree inside it. Creating branches according to the output level.
  4. Reset the processes by calling InitProcess() again

Note: this methed runs under single thread node, so there is no conflict when creating files.

Definition at line 223 of file TRestThread.cxx.

◆ ProcessEvent()

void TRestThread::ProcessEvent ( )

Process a single event.

This is the only method that can running purely under multi thread mode here. It gives the input event to the first process in process chain, then it gives the process result to the next process, so on. Finally it gets a result and saves it in the local output event.

Definition at line 483 of file TRestThread.cxx.

◆ StartProcess()

void TRestThread::StartProcess ( )

The main function of this class. Thread will run this function until the end.

It will start a loop, calling GetNextevtFunc(), ProcessEvent(), and FillThreadEventFunc() repeatedly. If the function GetNextEvent() returns false, the loop will break, meaning that we are at the end of the process. Before return it will set "isFinished" to true.

Note: The methods GetNextevtFunc() and FillThreadEventFunc() are all from TRestProcessRunner. The later two will call back the method FillEvent(), EndProcess() in this class. The idea to do so is to make a unified management of these i-o related methods. In TRestRun the three methods are mutex locked. They will be paused until the host run allows it to run. This prevents segmentation violation due to simultaneously read/write.

Definition at line 433 of file TRestThread.cxx.

◆ StartThread()

void TRestThread::StartThread ( )

Create a thread with the method StartProcess().

The thread will be detached afterwards.

Definition at line 449 of file TRestThread.cxx.

◆ TestRun()

bool TRestThread::TestRun ( )

Make a test run of our process chain.

The reason we use test run is that we need to determine the real output event address of a process chain. This is because when we write our code like this:

TRestEvent *inputEvent )
{
fSignalEvent = (TRestRawSignalEvent *)inputEvent;
fOutputEvent = fSignalEvent;
...
}
A base class for any REST event.
Definition: TRestEvent.h:38
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
An event container for time rawdata signals with fixed length.

The output event address can be changed after running.

Test run calls TRestEventProcess::ProcessEvent() first, and uses the output of TRestEventProcess::GetOutputEvent() as the next process's input event. This avoids NULL returned events from ProcessEvent due to cut. In debug mode, we can also observe a process sequence printed by this method

returns false when fOutputEvent is null after 5 times of retry, returns true when fOutputEvent address is determined.

Definition at line 151 of file TRestThread.cxx.

◆ ValidateChain()

Int_t TRestThread::ValidateChain ( TRestEvent input)

Check if the input/output of each process in the process chain matches.

Input event of the whole process chain(i.e., the input event of first process) should match the one read from input file.

Outevent of the previous process should be of the same type as the input event of the later process. Both of them cannot be null.

Definition at line 65 of file TRestThread.cxx.


The documentation for this class was generated from the following files: