Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

Controller.cpp

Go to the documentation of this file.
00001 
00002 #include "cafe/Controller.hpp"
00003 
00004 #include <iostream>
00005 #include <algorithm>
00006 
00007 #include "cafe/Function.hpp"
00008 #include "cafe/ParseRun.hpp"
00009 #include "cafe/Config.hpp"
00010 #include "cafe/Event.hpp"
00011 
00012 #include "TTree.h"
00013 #include "TFile.h"
00014 
00015 namespace cafe {
00016 
00017     Controller::Controller(const char *name)
00018         : Processor(name),
00019           _outfile(0)
00020     {
00021         Config config(name);
00022 
00023         // Get .Debug entry
00024         Processor::setDebug(config.get("Debug", 0));
00025 
00026         // Get .Run entry
00027         std::string run = config.get("Run","");
00028         if(run != "") {
00029             ParseRun parser;
00030             add(parser.parse(run));
00031         } // else assume Processors are added by other means
00032 
00033         _untag = config.getVString("Untag", " ,");
00034         _tag = config.getVString("Tag", " ,");
00035     }
00036 
00037     Controller::Controller(const char *name, const std::list<Processor*>& procs)
00038         : Processor(name),
00039           _outfile(0)
00040     {
00041         add(procs);
00042     }
00043 
00044     Controller::~Controller()
00045     {
00046         for(std::list<Processor*>::iterator it = _processors.begin();
00047             it != _processors.end();
00048             ++it) {
00049             delete *it;
00050         }
00051     }
00052 
00053 
00054     void Controller::begin()
00055     {
00056         Config config(name());
00057 
00058         // Get .Output entry
00059         std::string outfilename = config.get("Output", "");
00060         if(outfilename.size() != 0) {
00061 
00062           bool useUpdate = config.get("UseUpdate",false);
00063           if (useUpdate) _outfile = TFile::Open(outfilename.c_str(), "UPDATE");
00064           else  _outfile = TFile::Open(outfilename.c_str(), "RECREATE");
00065 
00066             if(_outfile && _outfile->IsOpen()) {
00067                 setDirectory(_outfile);
00068                 out() << "Controller[" << name() << "] Created output file: "
00069                       << outfilename << std::endl;
00070             } else {
00071                 err() << "Controller[" << name() << "] Cannot create output file: "
00072                       << outfilename << std::endl;
00073                 delete _outfile;
00074                 _outfile = 0;
00075             }
00076         }
00077 
00078         // Get .Directory entry
00079         std::string dirname = config.get("Directory", "");
00080         if(dirname.size() != 0) {
00081             if(TDirectory *dir = getDirectory()->mkdir(dirname.c_str())) {
00082                 setDirectory(dir);
00083                 out() << "Controller[" << name() << "] Created directory: " 
00084                       << dirname << std::endl;
00085             } else {
00086                 getDirectory()->cd(dirname.c_str());
00087             }
00088         }
00089 
00090         std::for_each(_processors.begin(), _processors.end(),
00091                      std::mem_fun(&Processor::begin));
00092     }
00093     
00094     void Controller::finish()
00095     {
00096         for(std::list<Processor*>::iterator it = _processors.begin();
00097             it != _processors.end();
00098             ++it) {
00099             std::cout << (*it)->fullName() << " : " << (*it)->eventCount() << std::endl;
00100         }
00101 
00102         std::for_each(_processors.begin(), _processors.end(),
00103                      std::mem_fun(&Processor::finish));
00104 
00105         if(_outfile) {
00106             out() << "Controller[" << name() << "] Closing output file: " 
00107                   << _outfile->GetName() << std::endl;
00108             _outfile->Write();
00109             _outfile->Close();
00110             delete _outfile;
00111             _outfile = 0;
00112         }
00113     }
00114 
00115     void Controller::inputFileOpened(TFile *file) 
00116     {
00117         for(std::list<Processor*>::iterator it = _processors.begin();
00118             it != _processors.end();
00119             ++it) {
00120             (*it)->inputFileOpened(file);
00121         }
00122     }
00123 
00124     void Controller::inputFileClosing(TFile *file)
00125     {
00126         for(std::list<Processor*>::iterator it = _processors.begin();
00127             it != _processors.end();
00128             ++it) {
00129             (*it)->inputFileClosing(file);
00130         }
00131     }
00132 
00133     bool Controller::processEvent(Event& event)
00134     {
00135         event.untag(_untag.begin(), _untag.end());
00136         event.tag(_tag.begin(), _tag.end());
00137         for(std::list<Processor*>::iterator it = _processors.begin();
00138             it != _processors.end();
00139             ++it) {
00140             (*it)->incEventCount();
00141             if(!(*it)->processEvent(event)) return false;
00142         }
00143         return true;
00144     }
00145 
00146     void Controller::setDebug(unsigned int level)
00147     {
00148         Processor::setDebug(level);
00149         for(std::list<Processor*>::iterator it = _processors.begin();
00150             it != _processors.end();
00151             ++it) {
00152             (*it)->setDebug(level);
00153         } 
00154     }
00155 
00156 
00157     bool Controller::add(Processor *proc)
00158     {
00159         if(proc) {
00160             proc->setParent(this);
00161             if(debug() > 0) {
00162                 proc->setDebug(debug());
00163             }
00164             out() << "Controller[" << name() << "]: Adding " << proc->fullName() << std::endl;
00165             _processors.push_back(proc);
00166             return true;
00167         } else {
00168             return false;
00169         }
00170     }
00171 
00172     bool Controller::add(bool (*func)(Event&), const std::string& instance)
00173     {
00174         return add(new Function(instance, func));
00175     }
00176 
00177     bool Controller::add(const std::string& name, const std::string& instance)
00178     {
00179         return add(Processor::Create(name, instance == "" ? name : instance));
00180     }
00181 
00182     bool Controller::add(const std::list<Processor*>& procs)
00183     {
00184         for(std::list<Processor*>::const_iterator it = procs.begin();
00185             it != procs.end();
00186             ++it) {
00187             add(*it);
00188         }
00189         return true;
00190     }
00191 }
00192 
00193 ClassImp(cafe::Controller);

Generated on Tue Mar 28 10:13:03 2006 for CAF by doxygen 1.3.4