BasicAFC.cpp 7.62 KB
Newer Older
1 2
/***************************************************************************
 *   Copyright (C) 2004 by Olivier Stezowski                               *
3
 *   stezow(AT)ipnl.in2p3.fr                                               *
4 5 6 7 8 9 10 11 12 13 14 15
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
16
 *   along with this program; if not, write to the                         *
17 18 19
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
20

21 22 23 24 25 26
/** \file BasicAFC.cpp compiled in libADF.so */

#ifndef ADF_BasicAFC
#include "BasicAFC.h"
#endif

27 28 29
#include "RunAgent.h"
#include "MetaFrame.h"

30
#include <iostream>
31
#include <iomanip>
32 33 34 35
#include <fstream>

using namespace ADF; 

36 37
std::string BasicAFC::gConfigurationFilename = "BasicAFC.conf";

38 39 40 41 42 43 44 45 46 47 48
BasicAFC::BasicAFC(Int_t id) :
    NarvalConsumer(),
    fEndOfFrames(aMByte,ConfAgent::kWrite),
    fPath("./"),
    fBaseForName("AFC_"),
    fCurrentFile(0x0),
    fCurrentFileNumber(0u),
    fMaxSize(kMaxInt_t),
    fMyID(id),
    fVertex(0x0),
    fInitialized(false)
49
{
50 51 52 53 54 55 56 57 58 59 60 61
    // log system
    Log.GetProcessName() = "BasicAFC";
    Log.SetPID(GetPID());

    // frameio related
    GetFrameIO().SetName("BasicAFC");
    // to be sure unknown frames are written in the ouput
    GetFrameIO().SetModel(ConfAgent::kSafe);
    // to protect readings in the source of frames
    fEndOfFrames.SetModeIO(ConfAgent::kWrite);

    GetFrameIO().Attach(0x0,&fEndOfFrames);
62 63 64
}

BasicAFC::~BasicAFC() 
65
{
66 67 68 69
    // in principle not needed ... just in case reset it has not been called by narval
    UInt_t error = 0u; process_reset(&error) ;
    if ( fVertex )
        delete fVertex;
70 71 72 73
} 

void BasicAFC::process_config (const Char_t *directory_path, UInt_t *error_code)
{
74 75 76 77 78 79
    // first init narval stuff
    NarvalInterface::process_config(directory_path,error_code);

    if ( (*error_code) == 0u ) {
        // now init your stuff if required
    }
80 81 82 83
}

Bool_t  BasicAFC::NewFile()
{
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    Log.SetProcessMethod("NewFile");

    Bool_t ok = false;

    // close if already open
    if ( fCurrentFile ) {
        ::fclose(fCurrentFile);
        fCurrentFile = 0x0;
        std::cout << "  -BasicAFP: the input file has just been closed " << fCurrentFileName << std::endl;
    }

    // open the file
    std::string tmp = fPath;
    if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
        tmp += '/';

    std::ostringstream filename;
    filename	<< tmp << fBaseForName
             << std::setfill('0') << std::setw(4) << fCurrentFileNumber++ << std::setfill(' ')
             << ".adf";
    fCurrentFileName = filename.str();

    fCurrentFile = ::fopen(fCurrentFileName.c_str(),"wb");
    if ( fCurrentFile != 0x0 ) {
        Log << info << "A new output file has just been open "
            << fCurrentFileName << nline;

        fEndOfFrames.SetFile(fCurrentFile,fMaxSize);
        GetFrameIO().SetStatus(BaseFrameIO::kIdle);

        size_t len = fCurrentFileName.length();
        size_t lpt = fCurrentFileName.find_last_of('/');
        fCurrentName = fCurrentFileName.substr(lpt+1, len-lpt-1);

        // add conf frame at the beginning of the file and a vertex frame.
        GetFrameIO().RecordGlobalConfiguration();

        ok = true;
    }
    else {
        Log << warning << "Cannot open file " << fCurrentFileName << nline;
        GetFrameIO().SetStatus(BaseFrameIO::kFinished);
        ok = false;
    }

    Log << dolog;
    return ok;
131 132 133 134
}

UInt_t BasicAFC::ProcessBlock (ADF::FrameBlock &in)
{	
135
	Log.ClearMessage(); Log.SetProcessMethod("ProcessBlock"); 
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    // attach the FrameBlocks
    GetFrameIO().Attach(&in,0x0);

    UInt_t nevtsIn = GetFrameIO().GetFramesWritten();
    while ( 1 ) {
        // fill the output block with the frames from the input block
        while ( GetFrameIO().Notify() ) {
        }
        if ( GetFrameIO().GetCurrentBlockOUT()->IsEoB() ) { // the output file is full
            NewFile();
            continue;
        }
        // interruption: means end of file or out is full
        if ( GetFrameIO().GetCurrentBlockIN()->IsEoB() )  // the input block is treated
            break;
    }
    UInt_t nevtsOut = GetFrameIO().GetFramesWritten();
    //
    GetFrameIO().Detach(&in,0x0);

    if(fMyID >= 0) {
        std::cout << std::setw(2) << fMyID;
        std::cout << std::left << std::setw(24) << "-" << gConfigurationFilename << ":" << std::right
                  << " " << std::setw(5) << nevtsOut-nevtsIn
                  << " evts (" << std::setw(8) << in.GetSize() << ")"
                  << " Tot = " << std::setw(8) << nevtsOut
                  << "           " << fCurrentName << std::endl;
    }
    return 0u;
166
}
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
Bool_t BasicAFC::SetParameters(std::string _pathforfiles, std::string _basename, int starting_number)
{
    Log.ClearMessage(); Log.SetProcessMethod("SetParameters");

    if(fInitialized) {
        Log << warning << "actor already initialized, SetParameters method ignored " << nline;
        Log << dolog;
        return false;
    }

    fPath = _pathforfiles;
    fBaseForName = _basename;
    fCurrentFileNumber = starting_number;

    fInitialized = true;

    Log << dolog;

    return true;
}
188 189 190

void BasicAFC::process_initialise (UInt_t *error_code)
{
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    *error_code = 0;
    Log.ClearMessage(); Log.SetProcessMethod("process_initialise");

    // creates a trigger to register at the beginning of the file a global vertex frame
    // that is initiated from ADF.conf
    fVertex = AgataFrameTrigger::Build("GVertex","","meta:vertex");
    if ( fVertex )
        GetFrameIO().Register(fVertex);

    if(!fInitialized) {
        // read an input file to get the base filename and the path
        std::string conffile = GetConfPath() + gConfigurationFilename;

        std::ifstream filein(conffile.data());
        if ( filein.is_open() == true ) {

            std::string pathforfiles, basename;
            UInt_t starting_number;
            filein >> pathforfiles >> basename >> starting_number;

            if ( filein.good() ) {
                fPath = pathforfiles;
                fBaseForName = basename;
                fCurrentFileNumber = starting_number;
                //	GetFrameIO().GetConfAgent()->GetRunAgent()->SetSubRun(starting_number);
            }
            filein.close();
        }
        fInitialized = true;
    }

    // open the first file
    if ( !NewFile() )
        *error_code = 1;

    Log << dolog;
227 228 229 230
}

void BasicAFC::process_reset (UInt_t *error_code)
{
231 232 233 234 235 236 237 238 239
    *error_code = 0;
    Log.ClearMessage(); Log.SetProcessMethod("process_reset");

    if ( fCurrentFile )
        ::fclose(fCurrentFile);

    GetFrameIO().Print( Log() );

    Log << dolog;
240 241 242
}

/*
243 244 245 246 247
 void BasicAFC::process_unload (UInt_terror_code)
 {
 std::cout << "process_unload called with GetPID()" << GetPID() << std::endl;
 *error_code = 0;
 } */
248 249 250 251 252