Anp::RunAlgSnarl Class Reference

#include <RunAlgSnarl.h>

Inheritance diagram for Anp::RunAlgSnarl:
Anp::AlgSnarl Anp::Base

List of all members.

Public Member Functions

 RunAlgSnarl ()
virtual ~RunAlgSnarl ()
bool Run (Record &record)
void Config (const Registry &reg)
void Set (TDirectory *dir)
bool Init (const Header &header)
void End (const DataBlock &block)
const RecordGetRecord () const

Private Types

typedef std::vector
< Anp::AlgCaddy< Anp::AlgSnarl > > 
SnarlVec

Private Member Functions

bool RunRecord (Record &record)
bool RunRecord ()

Private Attributes

std::string fDirName
bool fInit
bool fDoThread
bool fMakeCopy
bool fPrintEnd
Record fRecord
SnarlVec fAlgVec
AlgCaddy< AlgSnarlfCount
CondVar fCond
Mutex fMutex
pthread_t fThread
int fStatus
int fSignal

Friends

void * Anp::get_snarl_done (void *)

Detailed Description

Definition at line 28 of file RunAlgSnarl.h.


Member Typedef Documentation

typedef std::vector<Anp::AlgCaddy<Anp::AlgSnarl> > Anp::RunAlgSnarl::SnarlVec [private]

Definition at line 54 of file RunAlgSnarl.h.


Constructor & Destructor Documentation

Anp::RunAlgSnarl::RunAlgSnarl (  ) 

Definition at line 24 of file RunAlgSnarl.cxx.

00025    :fDirName(""),
00026     fInit(false),
00027     fDoThread(false),
00028     fMakeCopy(false),
00029     fPrintEnd(true),
00030     fRecord(),
00031     fAlgVec(),
00032     fCount(),
00033     fCond(0),
00034     fMutex(),    
00035     fStatus(1)
00036 {
00037 }

Anp::RunAlgSnarl::~RunAlgSnarl (  )  [virtual]

Definition at line 40 of file RunAlgSnarl.cxx.

00041 {
00042 }


Member Function Documentation

void Anp::RunAlgSnarl::Config ( const Registry reg  )  [virtual]

Reimplemented from Anp::AlgSnarl.

Definition at line 89 of file RunAlgSnarl.cxx.

References fAlgVec, fDirName, fDoThread, fMakeCopy, fPrintEnd, Registry::Get(), Anp::GetMainMutex(), Anp::GetROOTMutex(), it, Registry::KeyExists(), Anp::Mutex::Lock(), Anp::AlgSnarl::Name(), print(), Anp::Read(), UtilString::StringTok(), Anp::Mutex::UnLock(), and Anp::Handle< T >::valid().

00090 { 
00091    //
00092    // Configure self, create and configure children algorithms,
00093    // create output ROOT file.
00094    //
00095    if(!Anp::Read(reg, "AlgSnarlDoThread", fDoThread)) Anp::Read(reg, "DoThread",  fDoThread);
00096    if(!Anp::Read(reg, "AlgSnarlMakeCopy", fMakeCopy)) Anp::Read(reg, "MakeCopy",  fMakeCopy);
00097    if(!Anp::Read(reg, "AlgSnarlPrintEnd", fPrintEnd)) Anp::Read(reg, "PrintEnd",  fPrintEnd);
00098    
00099    vector<string> nvec;
00100 
00101    const char *value_list = 0;
00102    if(reg.Get("AlgSnarlList", value_list) && value_list)
00103    {
00104       UtilString::StringTok(nvec, string(value_list), ", ");
00105    }
00106    else
00107    {
00108       cerr << "RunAlgSnarl::Config - Registry key AlgSnarlList does not exist" << endl;
00109    }
00110 
00111    const char *value_name = 0;
00112    if(reg.Get("AlgSnarlDirName", value_name) && value_name)
00113    {
00114       fDirName = value_name;
00115    }
00116 
00117    //
00118    // Print self configuration
00119    //
00120    bool print = false;
00121    if(reg.KeyExists("PrintConfig"))
00122    {
00123       print = true;
00124       cout << "RunAlgSnarl::Config" << endl
00125            << "   DirName = " << fDirName << endl
00126            << "   DoThread = " << fDoThread << endl
00127            << "   MakeCopy = " << fMakeCopy << endl
00128            << "   PrintEnd = " << fPrintEnd << endl
00129            << "   Found " << nvec.size() << " AlgSnarl key(s)" << endl;
00130    }
00131 
00132    //
00133    // Create algorithms
00134    //
00135    for(vector<string>::const_iterator it = nvec.begin(); it != nvec.end(); ++it)
00136    {
00137       const string &name = *it;
00138       string algname = name;
00139 
00140       if(name.find("-") != string::npos)
00141       {  
00142          //
00143          // "-" separates Registry configurations for multiple copies of same algorithm
00144          //
00145          algname = name.substr(0, name.find("-"));
00146       } 
00147 
00148       //
00149       // Check if this algorithm has its own registry
00150       //
00151       Anp::Lock<Mutex> lockR(Anp::GetROOTMutex());
00152       Anp::Lock<Mutex> lockM(Anp::GetMainMutex());
00153 
00154       Registry nreg(false);
00155       if(reg.Get(name.c_str(), nreg))
00156       {
00157          const char *value_name = 0;
00158          if(nreg.Get("AlgSnarlName", value_name) && value_name)
00159          {
00160             algname = value_name;
00161          }
00162       }    
00163 
00164       Handle<AlgSnarl> alg = Factory<AlgSnarl>::Instance().Create(algname);
00165 
00166       if(alg.valid())
00167       {
00168          if(print) cout << "   " << name << endl;
00169 
00170          alg -> Name(name);
00171 
00172          fAlgVec.push_back(AlgCaddy<AlgSnarl>(alg, name));       
00173       }
00174       else
00175       {
00176          cerr << "RunAlgSnarl::Config - failed to create algorithm " << algname << endl;
00177       }
00178    }
00179 
00180    //
00181    // Configure algorithms
00182    //
00183    for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00184    {
00185       AlgSnarlPtr alg = it -> alg;
00186 
00187       //
00188       // Cannot use scopped Anp::Lock<Anp::Mutex> here
00189       //
00190       Anp::GetROOTMutex().Lock();
00191 
00192       Registry reg_(false);
00193       if(reg.Get((alg -> Name()).c_str(), reg_))
00194       {
00195          Anp::GetROOTMutex().UnLock();
00196          alg -> Config(reg_); 
00197       }
00198       else
00199       {
00200          Anp::GetROOTMutex().UnLock();
00201          alg -> Config(reg);
00202       }
00203    }
00204 }

void Anp::RunAlgSnarl::End ( const DataBlock block  )  [virtual]

Reimplemented from Anp::AlgSnarl.

Definition at line 249 of file RunAlgSnarl.cxx.

References fAlgVec, fCond, fCount, fDoThread, fMutex, fPrintEnd, fStatus, fThread, it, Anp::AlgSnarl::Name(), Lit::Print(), Anp::AlgCaddy< T >::Print(), Anp::CondVar::SetValue(), and Anp::CondVar::Signal().

00250 {
00251    if(fDoThread)
00252    {
00253       if(fStatus == 0)
00254       {
00255          Anp::Lock<Anp::Mutex> lock(fMutex);
00256          fCond.SetValue(-1);
00257          fCond.Signal();      
00258       }
00259       else
00260       {
00261          cerr << "RunAlgSnarl::End - invalid thread status: " << fStatus << endl;
00262       }
00263 
00264       const int status_join = pthread_join(fThread, NULL);
00265       assert(status_join == 0 && "pthread_join() has failed");
00266    }   
00267    
00268    if(fPrintEnd)
00269    {
00270       cout << "--------------------------------------------------------------------" << endl;
00271       cout << "                    Printing RunAlgSnarl summary " << endl;
00272       cout << "--------------------------------------------------------------------" << endl;
00273       
00274       fCount.Print(0, 0, "snarl event");
00275       cout << endl;
00276       
00277       const unsigned int name_width = Anp::NameWidth<AlgSnarl>(fAlgVec);
00278       const unsigned int numb_width = Anp::NumbWidth<AlgSnarl>(fAlgVec);
00279       
00280       for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00281       {
00282          it -> Print(name_width, numb_width, "snarl");
00283       }
00284       if(!fAlgVec.empty()) cout << endl;
00285       
00286       for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00287       {
00288          it -> Print(name_width, numb_width, "event");
00289       }
00290       if(!fAlgVec.empty()) cout << endl;
00291       
00292       for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00293       {
00294          it -> Print(name_width, numb_width, "weight");
00295       }
00296       if(!fAlgVec.empty()) cout << endl;
00297       
00298       cout << "RunAlgSnarl: erasing all stored algorithms..." << endl;
00299    }
00300 
00301    SnarlVec::iterator it = fAlgVec.begin();
00302    while(it != fAlgVec.end())
00303    {
00304       AlgSnarlPtr alg = it -> alg;
00305       
00306       if(fPrintEnd)
00307       {
00308          cout << "--------------------------------------------------------------------" << endl;
00309          cout << alg -> Name() << endl;
00310       }
00311 
00312       it -> alg -> End(block); // inform algorithms that data processing is completed
00313       
00314       it = fAlgVec.erase(it); // remove local reference to algorithm
00315       
00316       if(it == fAlgVec.end() && fPrintEnd) cout << endl;
00317    }
00318 
00319    if(fPrintEnd)
00320    {
00321       cout << "--------------------------------------------------------------------" << endl;
00322       cout << "                    End of RunAlgSnarl summary " << endl;
00323       cout << "--------------------------------------------------------------------" << endl;
00324    }
00325 }

const Record & Anp::RunAlgSnarl::GetRecord (  )  const [inline]

Definition at line 91 of file RunAlgSnarl.h.

References fRecord.

00092    {
00093       return fRecord;
00094    }

bool Anp::RunAlgSnarl::Init ( const Header header  )  [virtual]

Reimplemented from Anp::AlgSnarl.

Definition at line 220 of file RunAlgSnarl.cxx.

References fAlgVec, fDoThread, fInit, fStatus, fThread, Anp::get_snarl_done(), it, and Anp::AlgSnarl::Name().

Referenced by Run().

00221 {
00222    //
00223    // Initiliaze self and children algorithms
00224    //
00225    assert(!fInit && "RunAlgSnarl::Init() is called second time - logic errror");
00226 
00227    fInit = true;
00228 
00229    for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00230    {
00231       Handle<AlgSnarl> alg = it -> alg;
00232 
00233       if(!(alg -> Init(header)))
00234       {
00235          cerr << "RunAlgSnarl::Init - failed to initialize: " << alg -> Name() << endl;
00236       }
00237    }
00238    
00239    if(fDoThread)
00240    {
00241       fStatus = pthread_create(&fThread, NULL, Anp::get_snarl_done, this);
00242       assert(fStatus == 0 && "invalid pthread_create status");
00243    }
00244 
00245    return true;
00246 }

bool Anp::RunAlgSnarl::Run ( Record record  )  [virtual]

Implements Anp::AlgSnarl.

Definition at line 45 of file RunAlgSnarl.cxx.

References fCond, fDoThread, fInit, fMakeCopy, fMutex, fRecord, Anp::Record::GetHeader(), Anp::CondVar::GetValue(), Init(), RunRecord(), Anp::CondVar::SetValue(), Anp::CondVar::Signal(), and Anp::CondVar::Wait().

Referenced by RunRecord().

00046 {
00047    //
00048    // Call "Run(record)" for children algorithms, count pot and save record to TTree
00049    //
00050    
00051    if(!fInit)
00052    {
00053       if(!RunAlgSnarl::Init(record.GetHeader()))
00054       {
00055          return false;
00056       }
00057    }
00058 
00059    if(fMakeCopy)
00060    {      
00061       if(fDoThread)
00062       {
00063          Anp::Lock<Anp::Mutex> lock(fMutex);
00064          
00065          while(fCond.GetValue() == 1)
00066          {
00067             fCond.Wait(fMutex);
00068          }
00069          
00070          assert(fCond.GetValue() == 0 && "invalid signal value");
00071 
00072          fRecord = record;
00073          fCond.SetValue(1);
00074          fCond.Signal();
00075 
00076          return true;
00077       }
00078       else
00079       {
00080          fRecord = record;
00081          return RunAlgSnarl::RunRecord(fRecord);
00082       }
00083    }
00084 
00085    return RunAlgSnarl::RunRecord(record);
00086 }

bool Anp::RunAlgSnarl::RunRecord (  )  [private]

Definition at line 363 of file RunAlgSnarl.cxx.

References fCond, fDoThread, fRecord, and Anp::CondVar::GetValue().

Referenced by Run().

00364 {
00365    assert(fCond.GetValue() == 1 && "invalid signal value");
00366 
00367    if(fDoThread)
00368    {   
00369       RunAlgSnarl::RunRecord(fRecord);
00370    }
00371    else
00372    {
00373       cerr << "RunAlgSnarl::RunRecord(void) - is called in single threaded mode" << endl;
00374    }
00375 
00376    return true;
00377 }

bool Anp::RunAlgSnarl::RunRecord ( Record record  )  [private]

Definition at line 329 of file RunAlgSnarl.cxx.

References Anp::AlgCaddy< T >::AllSnarl(), fAlgVec, fCount, Anp::Record::GetHeader(), Anp::Header::IsValid(), it, Anp::AlgCaddy< T >::OutSnarl(), and Run().

00330 {
00331    //
00332    // Call "Run(record)" for children algorithms, count pot and save record to TTree
00333    //
00334    if(!record.GetHeader().IsValid())
00335    {
00336       cerr << "RunAlgSnarl::RunREcord - header is invalid" << endl;
00337       return false;
00338    }
00339 
00340    //
00341    // Count input snarls and events
00342    //
00343    fCount.AllSnarl(record);
00344 
00345    for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00346    {
00347       it -> AllSnarl(record);
00348 
00349       if(!(it -> alg -> Run(record))) return false;
00350 
00351       it -> OutSnarl(record);
00352    }
00353 
00354    //
00355    // Count output snarls and events
00356    //
00357    fCount.OutSnarl(record);
00358 
00359    return true;
00360 }

void Anp::RunAlgSnarl::Set ( TDirectory *  dir  )  [virtual]

Reimplemented from Anp::AlgSnarl.

Definition at line 207 of file RunAlgSnarl.cxx.

References fAlgVec, fDirName, Anp::GetDir(), and it.

00208 {
00209    //
00210    // Set output TDirectory for children algorithms
00211    //
00212 
00213    for(SnarlVec::iterator it = fAlgVec.begin(); it != fAlgVec.end(); ++it)
00214    {      
00215       it -> alg -> Set(Anp::GetDir(fDirName, dir));
00216    }
00217 }


Friends And Related Function Documentation

void* Anp::get_snarl_done ( void *   )  [friend]

Member Data Documentation

Definition at line 68 of file RunAlgSnarl.h.

Referenced by Config(), End(), Init(), RunRecord(), and Set().

Definition at line 75 of file RunAlgSnarl.h.

Referenced by End(), Run(), and RunRecord().

Reimplemented from Anp::Base.

Definition at line 70 of file RunAlgSnarl.h.

Referenced by End(), and RunRecord().

std::string Anp::RunAlgSnarl::fDirName [private]

Definition at line 60 of file RunAlgSnarl.h.

Referenced by Config(), and Set().

Definition at line 63 of file RunAlgSnarl.h.

Referenced by Config(), End(), Init(), Run(), and RunRecord().

bool Anp::RunAlgSnarl::fInit [private]

Definition at line 62 of file RunAlgSnarl.h.

Referenced by Init(), and Run().

Definition at line 64 of file RunAlgSnarl.h.

Referenced by Config(), and Run().

Definition at line 76 of file RunAlgSnarl.h.

Referenced by End(), and Run().

Definition at line 65 of file RunAlgSnarl.h.

Referenced by Config(), and End().

Definition at line 67 of file RunAlgSnarl.h.

Referenced by GetRecord(), Run(), and RunRecord().

Definition at line 80 of file RunAlgSnarl.h.

Definition at line 79 of file RunAlgSnarl.h.

Referenced by End(), and Init().

pthread_t Anp::RunAlgSnarl::fThread [private]

Definition at line 77 of file RunAlgSnarl.h.

Referenced by End(), and Init().


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

Generated on 24 Apr 2018 for loon by  doxygen 1.6.1