PSAFilter.cpp 19.3 KB
Newer Older
dino's avatar
dino committed
1 2 3
/* PSA-actor base class, J. Ljungvall 2008,
based on implementation by Olivier Stezowski*/

dino's avatar
 
dino committed
4
#include "PSAFilter.h"
dino's avatar
dino committed
5 6 7 8 9 10 11 12 13 14 15
#include "AgataKeyFactory.h"
#include "AgataFrameFactory.h"

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>

#include "misc.h"

16 17
std::string PSAFilter::gMotherClass = "PSAFilter";
std::string PSAFilter::gActualClass;
18

dino's avatar
dino committed
19
using namespace std;
dino's avatar
 
dino committed
20
using namespace ADF;
dino's avatar
dino committed
21 22 23 24 25 26

const int preTrigger = 10;

PSAFilter::PSAFilter() :
  fFrameCrystal(NULL),
  fFramePSA(NULL),
dino's avatar
dino committed
27
  fTrigger("data:ccrystal")
dino's avatar
dino committed
28
{	
dino's avatar
 
dino committed
29 30
  fConfPath = GetConfPath();
  fOdirPrefix.clear();
31 32

  fBasisFile.clear();
33 34 35 36 37 38
  fTraceLengthPSA = 80;
  fWriteNumTraces = 0;
  fPsaMinSegMult  = 1;
  fPsaMaxSegMult  = CrystalInterface::kNbSegments;
  fPsaSegCenter   = false;
  fVerbose        = false;
39

dino's avatar
 
dino committed
40
  mCount = 0;
dino's avatar
dino committed
41 42
  fInblock.SetModeIO(ConfAgent::kRead);
  fOublock.SetModeIO(ConfAgent::kWrite);
dino's avatar
 
dino committed
43
  
44 45 46 47 48
  for(int slot = 0; slot < TCOUNT*TMODULO; slot++) {
    DD[slot].numHits = 0;
    for(UInt_t nh = 0; nh < ADF::CrystalInterface::kNbSegments; nh++) {
      DD[slot].theHits[nh].Reset();
    }
dino's avatar
 
dino committed
49
  }
dino's avatar
dino committed
50 51 52 53
}

PSAFilter::~PSAFilter() 
{  
54 55 56 57 58 59 60
  for(int slot = 0; slot < TCOUNT*TMODULO; slot++) {
    for(int nn=0; nn<CrystalInterface::kNbSegments; nn++){
      delete [] DD[slot].fTracesSG[nn];
    }
    for(int  nn = 0; nn < CrystalInterface::kNbCores; nn++) {
      delete [] DD[slot].fTracesCC[nn];
    }
dino's avatar
dino committed
61 62 63 64 65
  }
} 

void PSAFilter::process_config(const Char_t *directory_path, UInt_t *error_code)
{
dino's avatar
 
dino committed
66
  *error_code = 0;
67
  cout << gMotherClass + "::process_config() called with directory_path = " << directory_path << endl;
dino's avatar
 
dino committed
68

dino's avatar
dino committed
69 70 71 72 73
  // first init narval stuff
  NarvalInterface::process_config(directory_path, error_code);
  if( *error_code )
    return;

74 75
  // Get name of daughter class from from configuration directory_path/gMotherClass.conf 
  int rv = getKeyFromFile(GetGlobalConfPath() + gMotherClass + ".conf", "ActualClass", gActualClass);
76
  if(rv == 2)
dino's avatar
 
dino committed
77
    *error_code = 102; // Fatal error because the configuration file MUST be present
78 79 80 81 82
}

void PSAFilter::process_initialise (UInt_t *error_code)
{
  Log.ClearMessage();
83
  Log.GetProcessName()   = gMotherClass; 
84 85 86 87 88
  Log.GetProcessMethod() = "process_initialise";
  Log.SetPID(GetPID());

  fConfPath = GetGlobalConfPath();

dino's avatar
dino committed
89
  GetParameters(error_code);
90 91 92 93 94 95 96 97 98 99
  if(*error_code)
    return;

  // to get the input/output frames
  if( fFrameCrystal ) {
    delete fFrameCrystal;
    fFrameCrystal = NULL;
  }

  Frame * frame;
100
  UInt_t rerr = 0;
101 102 103

  // to get the current version numbers of the Keys and Frames defined on the data flow

104 105
  UInt_t defaultLength = ADF::CrystalInterface::kDefaultLength;
  ADF::CrystalInterface::kDefaultLength = fTraceLengthPSA;  // set "default" lenght of traces to 80
106

107 108 109 110 111
  frame = fTrigger.Add("Agata", "data:ccrystal", GetConfAgent(), true, false);
  if( frame ) {
    // to link some named item with the local variables.
    fFrameCrystal = dynamic_cast< ACrystalFrame * > (frame);
    GObject *glob = fFrameCrystal->Data()->Global();
dino's avatar
 
dino committed
112 113
    glob->LinkItem("CrystalID",     &crystal_id); 
    glob->LinkItem("CrystalStatus", &crystal_status); 
114
  }
115 116 117
  else
    rerr |= 8;

118
  ADF::CrystalInterface::kDefaultLength = defaultLength;  // restore "default" length of traces
119 120 121 122 123 124

  frame = fTrigger.SetOutputFrame("Agata", "data:psa", GetConfAgent());
  if(frame) {
    // to link some named item with the local variables.
    fFramePSA = dynamic_cast< APSAFrame * > (frame);
    GObject *glob = fFramePSA->Data()->Global();
dino's avatar
 
dino committed
125 126 127 128 129 130 131 132
    glob->LinkItem("CrystalID",     &crystal_id); 
    glob->LinkItem("CrystalStatus", &crystal_status); 
    glob->LinkItem("CoreE0",        &CoreE0); 
    glob->LinkItem("CoreE1",        &CoreE1);
    glob->LinkItem("CoreT0",        &CoreT0);
    glob->LinkItem("CoreT1",        &CoreT1);
    //glob->LinkItem("PSAStatus",     &PsaStatus); 
    //glob->LinkItem("Spare",         &PsaStatus); 
dino's avatar
 
dino committed
133
    //glob->Link<Anonymous>("Anonymous", &myAno);
134 135
    //bool bb = glob->IsFullyLinked();
  }
136 137
  else
    rerr |= 4;
138 139

  // the trigger is registered
140 141 142 143 144 145
  if( !fFrameIO.Register(&fTrigger) )
    rerr |= 1;
  
  if(rerr) {
    cout << "Trigger definition error " << rerr << " in PSAFilter::process_initialise()" << endl;
    *error_code = 100 + rerr;
146 147 148 149 150 151 152 153 154 155
    return;
  }

  // OComment
  // by default fFrameIO is in the kUndefined mode ... if the algo has been 
  // properly initialised, it should set the state to kIdle so that the data could be treated 
  fFrameIO.SetStatus(BaseFrameIO::kIdle);

  for(int slot = 0; slot < TCOUNT*TMODULO; slot++) {
    for(int nn = 0; nn < CrystalInterface::kNbSegments; nn++) {
156 157
      DD[slot].fTracesSG[nn] = new Float_t[fTraceLengthPSA];
      memset(DD[slot].fTracesSG[nn], 0, sizeof(Float_t)*fTraceLengthPSA);
158 159 160
      DD[slot].SegE[nn] = 0;
    }
    for(int nn = 0; nn < CrystalInterface::kNbCores; nn++) {
161 162
      DD[slot].fTracesCC[nn] = new Float_t[fTraceLengthPSA];
      memset(DD[slot].fTracesCC[nn], 0, sizeof(Float_t)*fTraceLengthPSA);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
      DD[slot].CoreE[nn] = DD[slot].CoreT[nn] = 0;
    }

    DD[slot].numHits = 0;
    for(UInt_t nh = 0; nh < ADF::CrystalInterface::kNbSegments; nh++) {
      DD[slot].theHits[nh].Reset();
    }
  }

  // version-specific initializations
  *error_code = AlgoSpecificInitialise();

#if TCOUNT > 1
  // launch the threads of the local chains
  cout << "Grid Search called using " << TCOUNT << " THREADS with blocks of " << TMODULO << " EVENTS" << endl; 
  for(int slot = 0; slot < TCOUNT ; slot++) {
    Slot[slot].State  = 0;  // start in the non-running state
    Slot[slot].Thread = new boost::thread(tProcess(this, slot));
dino's avatar
 
dino committed
181
    cout << "   LAUNCHED THREAD tProcess" << endl;
182 183 184 185 186
 }
#endif

}

dino's avatar
dino committed
187
void PSAFilter::GetParameters(UInt_t *error_code)
188
{
dino's avatar
 
dino committed
189 190
  *error_code = 0;

191
  string configFileName = fConfPath + gMotherClass + ".conf";
dino's avatar
 
dino committed
192
  ifstream configfile( configFileName.c_str() );
dino's avatar
dino committed
193
  if(!configfile.good()) {
dino's avatar
 
dino committed
194
    cout << "Error opening " << configFileName << endl;
dino's avatar
dino committed
195 196 197
    *error_code = 102;
    return;
  }
198
  cout << endl << gMotherClass + "::getParams() reading from --> " << configFileName << endl;
dino's avatar
dino committed
199 200 201 202 203 204 205 206 207

  string line, keyw, data;
  bool ok = true;
  while(getline(configfile, line)) {
    if(!stringSplit(line, " \t", keyw, data))
      continue;       // empty or comment lines

    cout << line;
    ok = false;
dino's avatar
dino committed
208
    if(keyw == "ActualClass") {
dino's avatar
dino committed
209
      ok = data.size() > 0;
210
      gActualClass = data;
dino's avatar
dino committed
211 212 213 214
    }
    else if(keyw == "WriteDataPrefix") {
      ok = data.size() > 0;
      fOdirPrefix = data;
dino's avatar
 
dino committed
215
      forceTailSlash(fOdirPrefix);
dino's avatar
dino committed
216 217 218 219 220
    }
    else if(keyw == "BasisFile") {
      ok = data.size() > 0;
      fBasisFile = data;
    }
dino's avatar
dino committed
221 222 223 224
    else if (keyw == "Verbose" ) {
      fVerbose = true;
      ok = true;
    }
225 226 227
    else if(keyw == "TraceLengthPSA" || keyw == "TraceLength") {
      ok = 1 == sscanf(data.c_str(), "%d", &fTraceLengthPSA);
    }
dino's avatar
dino committed
228
    else if(keyw == "WriteTraces") {
229
      ok = 1 == sscanf(data.c_str(), "%d", &fWriteNumTraces);
dino's avatar
dino committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    }
    else if(keyw == "SegmentFoldGate") {
      ok = 2 == sscanf(data.c_str(), "%d %d", &fPsaMinSegMult, &fPsaMaxSegMult);
    }
    else if(keyw == "PlaceAtSegCenter") {
      fPsaSegCenter = true;
      ok = true;
    }
    else {
      cout << "   --> ignored";
      ok = true;
    }

    if(!ok) {
dino's avatar
 
dino committed
244
      cout << "   --> missing argument(s)" << endl;
dino's avatar
dino committed
245 246 247 248 249 250 251 252 253
      *error_code = 103;
      return;
    }
    else {
      cout << endl;
    }
  }
}

254
Int_t PSAFilter::SetInput(int slot)
dino's avatar
dino committed
255 256 257 258
{
  // loads the values from the frame into the ADFObject attached to the frame
  fFrameCrystal->Read();

259 260 261 262 263
  PsaData *pD = DD + slot;

  pD->crystal_id     = crystal_id;
  pD->crystal_status = crystal_status;

dino's avatar
dino committed
264 265
  for(int isg=0; isg<CrystalInterface::kNbSegments; isg++){
    seg = fFrameCrystal->Data()->GetSegment(isg);
266
    pD->SegE[isg] = (float)seg->GetE(); 
267
    seg->GetSignal()->Get(pD->fTracesSG[isg], fTraceLengthPSA);
dino's avatar
dino committed
268 269 270
  }
  for(int icc=0; icc<CrystalInterface::kNbCores; icc++){
    core = fFrameCrystal->Data()->GetCore(icc);
271 272
    pD->CoreE[icc] = (float)core->GetE(); 
    pD->CoreT[icc] = (float)core->GetT();
273
    core->GetSignal()->Get(pD->fTracesCC[icc], fTraceLengthPSA);
dino's avatar
dino committed
274 275
  }

276 277
  pD->evnumber  = ((AgataKey *)fFrameCrystal->GetKey())->GetEventNumber();
  pD->timestamp = ((AgataKey *)fFrameCrystal->GetKey())->GetTimeStamp();
dino's avatar
dino committed
278

279
  pD->numHits = 0;
dino's avatar
dino committed
280 281 282 283

  return 0;
}

284
Int_t PSAFilter::SetOutput(int slot)
dino's avatar
dino committed
285 286 287
{
  fFramePSA->Reset();

288 289 290
  PsaData *pD = DD + slot;

  if(pD->numHits < 0)
dino's avatar
 
dino committed
291
    return 0;
dino's avatar
dino committed
292

293
  for(UInt_t nh = 0; nh < pD->numHits; nh++) {
dino's avatar
 
dino committed
294
    ADF::PSAHit *hit = (PSAHit*)fFramePSA->Data()->NewHit();
295
    hit->SetHit(pD->theHits[nh]);
dino's avatar
dino committed
296 297
  }

298 299 300 301 302 303 304 305 306
  ((AgataKey *)fFramePSA->GetKey())->SetEventNumber(pD->evnumber);
  ((AgataKey *)fFramePSA->GetKey())->SetTimeStamp(pD->timestamp);  

  crystal_id     = pD->crystal_id;
  crystal_status = pD->crystal_status;
  CoreE0 = pD->CoreE[0];
  CoreE1 = pD->CoreE[1];
  CoreT0 = pD->CoreT[0];
  CoreT1 = pD->CoreT[1];
dino's avatar
dino committed
307

308 309 310 311 312
  //myAno.Set(crystal_id, 0);
  //myAno.fRealSize = sizeof(crystal_id);
  //myAno.Set(pD->timestamp, myAno.fRealSize);
  //myAno.fRealSize += sizeof(pD->timestamp);

dino's avatar
dino committed
313 314 315 316 317
  UInt_t nwritten = fFramePSA->Write();

  return nwritten ? 0 : 1;
}

318
Int_t PSAFilter::Process(int slot)
dino's avatar
dino committed
319 320 321 322 323 324 325 326 327 328 329 330 331
{
  Int_t result = 0;
  cout << "WARNING!! Empty Process()" << endl << flush;
  return result;
}

void PSAFilter::process_block( void   *input_buffer,  UInt_t size_of_input_buffer,
                               void   *output_buffer, UInt_t size_of_output_buffer,
                               UInt_t *used_size_of_output_buffer,
                               UInt_t *error_code)
{
  fInblock.SetBlock((Char_t *)input_buffer,size_of_input_buffer);
  fOublock.SetBlock((Char_t *)output_buffer,size_of_output_buffer);
dino's avatar
 
dino committed
332

dino's avatar
dino committed
333 334 335 336
  *error_code = PSAFilter::ProcessBlock(fInblock, fOublock);
  *used_size_of_output_buffer = fOublock.GetSize();
}

337 338
#if TCOUNT > 1

dino's avatar
 
dino committed
339 340
// this is the threaded version

341 342 343 344 345 346 347
UInt_t PSAFilter::ProcessBlock (ADF::FrameBlock &in, ADF::FrameBlock &out)
{
  // attach the input/output buffer to the FrameIO system
  fFrameIO.Attach(&in,&out);

  // start the processing
  UInt_t error_code = 0;
dino's avatar
 
dino committed
348
  UInt_t nevs = mCount;
349

dino's avatar
 
dino committed
350 351
  if(mCount == 0)
    mRate.reset();
dino's avatar
 
dino committed
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  bool eoi   = false;
  int  slot  = -1;
  int  evcI  =  0;
  int  evcO  =  0;
  while (true) {

    slot = (slot + 1) % TCOUNT;
    CSlot *pslot = Slot + slot;

    if(pslot->State) {
      // if this slot has been activated, wait until it has finished its job
      if(pslot->State != 2) {  // not yet done
        boost::mutex::scoped_lock lk(pslot->Mutex);
        while(pslot->State != 2) {
          pslot->Condition.wait(lk);
        }
      }
      // pass content of the old slot to the output
      for(int snn = 0; snn < pslot->Count; snn++) {
        int sslot = slot*TMODULO+snn;
        if(DD[sslot].retValue) {
          //chState[slot] = 0; ???
          error_code = 2;
          LOCK_COUT;
dino's avatar
 
dino committed
377
          Log.GetProcessMethod() = "ProcessBlock";
378 379 380
          Log << error << " During : Process()" << dolog;
          break;
        }
dino's avatar
 
dino committed
381
        if(DD[sslot].numHits >0) {
382 383 384 385 386 387 388
          // spectra and traces
          PostProcess(sslot);
          // fill the output buffer
          if( SetOutput(sslot) ) {
            //chState[slot] = 0; ???
            error_code = 3;
            LOCK_COUT;
dino's avatar
 
dino committed
389
            Log.GetProcessMethod() = "ProcessBlock";
390 391 392 393 394 395 396 397
            Log << error << " During : SetOutput()" << dolog;
            break;
          }
          // ok, so send the produced frame to the ouput
          if( !fFrameIO.Record() ) {
            //chState[slot] = 0; ???
            error_code = 4;
            LOCK_COUT;
dino's avatar
 
dino committed
398
            Log.GetProcessMethod() = "ProcessBlock";
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
            Log << error << " During : Record()" << dolog;
            break;
          }
          evcO++;
        }
        DD[sslot].numHits = 0;
      }
    }
    if(error_code) {
      //chState[slot] = 0; // ??
      break;
    }

    // fill this slot with new data from the input
    pslot->Count = 0;
    int snn = 0;
    for( ; snn < TMODULO; snn++) {
      if(!fFrameIO.Notify() ) {
        eoi = true;
        break;
      }
      int sslot = slot*TMODULO+snn;
      if( SetInput(sslot) ) {
        error_code = 1;
        LOCK_COUT;
dino's avatar
 
dino committed
424
        Log.GetProcessMethod() = "ProcessBlock";
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        Log << error << " During : SetInput()" << dolog;
        break;
      }
      DD[sslot].retValue = 0;
      evcI++;
    }
    pslot->Count = snn;
    if(snn == 0)
      break;

    // tell the thread to proceed
    {
      boost::mutex::scoped_lock lk(pslot->Mutex);
      pslot->State = 1;
    }
    pslot->Condition.notify_one();
    if(eoi)
      break;
  }

  // Wait for the launched threads to finish

dino's avatar
 
dino committed
447 448 449 450
  // At this point the status of fFrameIO is BaseFrameIO::kIdle and we should no more write on it.
  // However, the output from the still running threads is saved by forcing it to BaseFrameIO::kRunning 
  // It would be more efficient to synchronize the unfinished threads at the next call of ProcessBlock
  // but we do it here so as to keep the correspondence between input and output buffers
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

  for(int nrs = 0; nrs < TCOUNT; nrs++) {

    slot = (slot + 1) % TCOUNT;
    CSlot *pslot = Slot + slot;

    if(pslot->State) {
      // if this slot has been activated, wait that it has finished its job
      if(pslot->State != 2) {  // not yet done
        boost::mutex::scoped_lock lk(pslot->Mutex);
        while(pslot->State != 2) {
          pslot->Condition.wait(lk);
        }
      }
      // pass content of the old slot to the output
      for(int snn = 0; snn < pslot->Count; snn++) {
        int sslot = slot*TMODULO+snn;
        if(DD[sslot].retValue ) {
          //chState[slot] = 0; ???
          error_code = 2;
          LOCK_COUT;
dino's avatar
 
dino committed
472
          Log.GetProcessMethod() = "ProcessBlock";
473 474 475 476
          Log << error << " During : Process()" << dolog;
          break;
        }
        if(DD[sslot].numHits >0 ) {
dino's avatar
 
dino committed
477 478
          // spectra and traces
          PostProcess(sslot);
479 480 481 482 483
         // fill the output buffer
          if( SetOutput(sslot) ) {
            //chState[slot] = 0; ???
            error_code = 3;
            LOCK_COUT;
dino's avatar
 
dino committed
484
            Log.GetProcessMethod() = "ProcessBlock";
485 486 487
            Log << error << " During : SetOutput()" << dolog;
            break;
          }
dino's avatar
 
dino committed
488
          // Reopen fFrameIO by forcing it to BaseFrameIO::kRunning
489 490 491 492 493 494
          if (fFrameIO.GetStatus() == BaseFrameIO::kIdle )	
            fFrameIO.SetStatus(BaseFrameIO::kRunning);
          if( !fFrameIO.Record() ) {
            //chState[slot] = 0; ???
            error_code = 4;
            LOCK_COUT;
dino's avatar
 
dino committed
495
            Log.GetProcessMethod() = "ProcessBlock";
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
            Log << error << " During : Record()" << dolog;
            break;
          }
          evcO++;
        }
        DD[sslot].numHits = 0;
      }
      {
        boost::mutex::scoped_lock lk(pslot->Mutex);
        pslot->State = 0;
      }
    }
    if(error_code) {
      //chState[slot] = 0; // ??
      break;
    }
  }

  fFrameIO.Detach(&in, &out);

dino's avatar
 
dino committed
516
  nevs = mCount - nevs;
dino's avatar
 
dino committed
517

518 519 520 521
  LOCK_COUT;
  cout << setw(2) << crystal_id
       << setw(24) << left << "-PSAFilter:" << right
       << " " << setw(5) << nevs << " evts (" << setw(8) << fOublock.GetSize() << ")"
dino's avatar
 
dino committed
522
       << " Tot = " << setw(7) << mCount
dino's avatar
 
dino committed
523
       << "  " << setw(4) << mRate.rate(mCount) << "/s"
dino's avatar
 
dino committed
524
       << endl;
525 526 527 528 529 530 531 532

  return error_code;
}	      

#else // TCOUNT > 1

// this is the non-threaded version

dino's avatar
dino committed
533 534 535 536 537 538 539
UInt_t PSAFilter::ProcessBlock (ADF::FrameBlock &in, ADF::FrameBlock &out)
{
  // attach the input/output buffer to the FrameIO system
  fFrameIO.Attach(&in,&out);

  // start the processing
  UInt_t error_code = 0;
dino's avatar
 
dino committed
540 541 542 543
  UInt_t nevs = mCount;

  if(mCount == 0)
    mRate.reset();
dino's avatar
dino committed
544 545 546 547 548 549

  while ( fFrameIO.Notify() ) {

    // fill local variables with data from the input
    if( SetInput() ) {
      error_code = 1;
dino's avatar
dino committed
550
      LOCK_COUT;
dino's avatar
 
dino committed
551
      Log.GetProcessMethod() = "ProcessBlock";
dino's avatar
dino committed
552 553 554 555 556 557 558
      Log << error << " During : SetInput()" << dolog;
      break;
    }

    // process the input buffer
    if( Process() ) {
      error_code = 2;
dino's avatar
dino committed
559
      LOCK_COUT;
dino's avatar
 
dino committed
560
      Log.GetProcessMethod() = "ProcessBlock";
dino's avatar
dino committed
561 562 563 564
      Log << error << " During : Process()" << dolog;
      break;
    }

565
    if(DD[0].numHits < 1)
dino's avatar
 
dino committed
566 567
      continue;

568 569 570
    // spectra and traces
    PostProcess();

dino's avatar
dino committed
571 572 573
    // fill the output buffer
    if( SetOutput() ) {
      error_code = 3;
dino's avatar
dino committed
574
      LOCK_COUT;
dino's avatar
 
dino committed
575
      Log.GetProcessMethod() = "ProcessBlock";
dino's avatar
dino committed
576 577 578 579 580 581 582
      Log << error << " During : SetOutput()" << dolog;
      break;
    }

    // ok, so send the produced frame to the ouput
    if( !fFrameIO.Record() ) {
      error_code = 4;
dino's avatar
dino committed
583
      LOCK_COUT;
dino's avatar
 
dino committed
584
      Log.GetProcessMethod() = "ProcessBlock";
dino's avatar
dino committed
585 586 587 588 589 590 591 592
      Log << error << " During : Record()" << dolog;
      break;
    }

  }

  fFrameIO.Detach(&in, &out);

dino's avatar
 
dino committed
593
  nevs = mCount - nevs;
dino's avatar
 
dino committed
594

dino's avatar
dino committed
595 596
  LOCK_COUT;
  cout << setw(2) << crystal_id
dino's avatar
 
dino committed
597
       << setw(24) << left << "-PSAFilter:" << right
dino's avatar
dino committed
598
       << " " << setw(5) << nevs << " evts (" << setw(8) << fOublock.GetSize() << ")"
dino's avatar
 
dino committed
599
       << " Tot = " << setw(7) << mCount
dino's avatar
 
dino committed
600
       << "  " << setw(4) << mRate.rate(mCount) << "/s"
dino's avatar
 
dino committed
601
       << endl;
dino's avatar
dino committed
602 603 604 605

  return error_code;
}	      

606 607
#endif  // TCOUNT == 1

dino's avatar
dino committed
608 609
void PSAFilter::process_reset (UInt_t *error_code)
{
dino's avatar
 
dino committed
610 611
 *error_code = 0;
 Log.ClearMessage();
dino's avatar
dino committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625
  Log.GetProcessMethod() = "process_reset"; 
  fFrameIO.Print(Log()); 
  // connect to the frameIO
  //   if ( fNarvalIO ) 
  //     { delete fNarvalIO; fNarvalIO = NULL; }
  //   // to get the input/output frameIO
  //   if ( fFrameCrystal ) 
  //     { delete fFrameCrystal; fFrameCrystal = NULL; }
  //   if ( fFramePSA ) 
  //     { delete fFramePSA; fFramePSA = NULL; }
  //   if ( fTrigger ) 
  //     { delete fTrigger; fTrigger = NULL; }	
  Log << dolog;

dino's avatar
 
dino committed
626 627
  hGroup.write(mCount, true);
  mCount = 0;
dino's avatar
dino committed
628
}
dino's avatar
 
dino committed
629

dino's avatar
dino committed
630 631
void PSAFilter::process_start (UInt_t *error_code)
{
dino's avatar
 
dino committed
632 633
  Log.GetProcessName()   = "PSAFilter";
  Log.GetProcessMethod() = "process_start"; 
dino's avatar
dino committed
634 635 636 637 638 639
  Log << info << " Start the inner loop " << dolog; 
  *error_code = 0;
}

void PSAFilter::process_stop (UInt_t *error_code)
{
dino's avatar
dino committed
640
  cout << "\nPSAFilter::process_stop called with GetPID() " << GetPID() << endl;
dino's avatar
dino committed
641
  *error_code = 0;
dino's avatar
 
dino committed
642 643 644

  hGroup.write(mCount, true);
  mCount = 0;
dino's avatar
dino committed
645
}
dino's avatar
 
dino committed
646

dino's avatar
dino committed
647 648
void PSAFilter::process_pause (UInt_t *error_code)
{
dino's avatar
dino committed
649
  cout << "\nPSAFilter::process_pause called with GetPID() " << GetPID() << endl;
dino's avatar
dino committed
650 651
  *error_code = 0;
}
dino's avatar
 
dino committed
652

dino's avatar
dino committed
653 654
void PSAFilter::process_resume (UInt_t *error_code)
{
dino's avatar
dino committed
655
  cout << "\nPSAFilter::process_resume called with GetPID() " << GetPID() << endl;
dino's avatar
dino committed
656 657
  *error_code = 0;
}
dino's avatar
 
dino committed
658

dino's avatar
dino committed
659 660 661
/*
void PSAFilter::process_unload (UInt_terror_code)
{
dino's avatar
 
dino committed
662
cout << "PSAFilter::process_unload called with GetPID()" << GetPID() << endl;
dino's avatar
dino committed
663 664
*error_code = 0;
} */
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

#if TCOUNT > 1

void tProcess::operator()()
{
  while(true) {
    // verify if can proceed
    if(pslot->State != 1) {
      boost::mutex::scoped_lock lk(pslot->Mutex);
      while(pslot->State != 1 ) {
        pslot->Condition.wait(lk);
      }
    }

    // do the job
    for(int nnn = 0; nnn < pslot->Count; nnn++) {
      int sslot  = slot*TMODULO + nnn;
      int retval = ppsa->Process(sslot);
      ppsa->DD[sslot].retValue = retval;
    }

    // notify job done
    {
      boost::mutex::scoped_lock lk(pslot->Mutex);
      pslot->State = 2;
    }
    pslot->Condition.notify_one();
  }
}

#endif  // TCOUNT > 1