DaqDaqBuffer.cpp 37 KB
Newer Older
dauncey's avatar
dauncey committed
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "Hack.hh"

#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>

#include "TH1F.h"

#include "TFileHandler.hh"
#include "Random.hh"

dauncey's avatar
dauncey committed
14 15
typedef std::pair<uint64_t,uint64_t> uint128_t;

dauncey's avatar
dauncey committed
16 17 18 19 20 21 22 23 24 25 26
#include "Buffering/Dimensions.hh"

class SlinkElinkData {
public:
  SlinkElinkData() {
  }

  uint32_t fData[Dimensions::MaxNumberOfEconsPerSlink][Dimensions::MaxNumberOfElinksPerEcon];
};


27 28
#include "Buffering/CircularBuffer.hh"
#include "Buffering/PartitionedBuffer.hh"
dauncey's avatar
dauncey committed
29 30 31 32 33
#include "Buffering/HgcrocSim.hh"
#include "Buffering/HgcrocAna.hh"
#include "Buffering/EconSim.hh"
#include "Buffering/EconAna.hh"
#include "Buffering/LpGbtCheck.hh"
dauncey's avatar
dauncey committed
34 35 36 37
#include "Buffering/BeSlinkSim.hh"
#include "Buffering/BeSlinkAna.hh"
#include "Buffering/SlinkArch.hh"
#include "Buffering/SlinkFile.hh"
dauncey's avatar
dauncey committed
38 39 40
#include "Buffering/SlinkBoe.hh"
#include "Buffering/SlinkEoe.hh"
#define DEBUG_PRINT if(false) std::cout << "Line " << __LINE__ << ": "
dauncey's avatar
dauncey committed
41 42
#include "Buffering/SlinkCheck.hh"

dauncey's avatar
dauncey committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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

#ifndef TriggerRule_PrintLevel
#define TriggerRule_PrintLevel 10
#endif


class TriggerRule {
public:
  enum {
    TriggerLimit=8
  };

  TriggerRule(uint64_t n=43) : fNumberOfBx(n), fTriggers(0) {
    if(TriggerRule_PrintLevel>0) std::cout << "TriggerRule::ctor()  Initialised with trigger limit "
					   << TriggerLimit << " within " << fNumberOfBx << " BX"
					   << std::endl;
  }

  bool allowTrigger(uint64_t bx) {
    if(TriggerRule_PrintLevel>9) std::cout << std::endl
					   << "TriggerRule::allowTriggers()  fTriggers = " << fTriggers
					   << ", called with bx = " << bx << std::endl;

    if(fTriggers<TriggerLimit) {
      fTriggerBx[fTriggers]=bx;
      fTriggers++;

      if(TriggerRule_PrintLevel>9) std::cout << "TriggerRule::allowTriggers()  new fTriggers = " << fTriggers
					     << ", trigger allowed" << std::endl;
      return true;
    }

    if((bx-fTriggerBx[0])<fNumberOfBx) {
      if(TriggerRule_PrintLevel>9) std::cout << "TriggerRule::allowTriggers()  oldest BX = " << fTriggerBx[0]
					     << ", difference = " << bx-fTriggerBx[0]
					     << ", trigger not allowed" << std::endl;
      return false;
    }

    bool reject(true);
    unsigned nReject(0);
    for(;nReject<TriggerLimit && reject;) {
      reject=(bx-fTriggerBx[nReject])>=fNumberOfBx;
      if(reject) {
	if(TriggerRule_PrintLevel>9) std::cout << "TriggerRule::allowTriggers()  Trigger " << nReject << " BX = " << fTriggerBx[nReject]
					       << ", difference = " << bx-fTriggerBx[nReject]
					       << ", rejected" << std::endl;
	nReject++;
      }
    }

    if(TriggerRule_PrintLevel>9) std::cout << "TriggerRule::allowTriggers()  Number of triggers rejected = " << nReject << std::endl;
    
    fTriggers=TriggerLimit-nReject;
    for(unsigned i(0);i<fTriggers;i++) {
      if(TriggerRule_PrintLevel>9) std::cout << "TriggerRule::allowTriggers()  Trigger " << i+nReject << " BX = " << fTriggerBx[i+nReject]
					     << " moved to trigger " << i << std::endl;
      fTriggerBx[i]=fTriggerBx[i+nReject];
    }
    
    if(TriggerRule_PrintLevel>9) std::cout << "TriggerRule::allowTriggers()  new fTriggers = " << fTriggers
					   << ", trigger allowed" << std::endl;
    return true;
  }
  
private:
  const uint64_t fNumberOfBx;
  unsigned fTriggers;
  uint64_t fTriggerBx[TriggerLimit];
};
dauncey's avatar
dauncey committed
113

dauncey's avatar
dauncey committed
114
/*
dauncey's avatar
dauncey committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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
class SlinkBoe {
public:
  SlinkBoe() : fBoe(0) {
  }
  
  SlinkBoe(uint32_t si, uint32_t bi, uint32_t ei, uint16_t et)
    : fSourceId(si), fBxId(bi), fEventId(ei), fEventType(et), fBoe(0x5555) {
  }

  uint32_t sourceId() const {
    return fSourceId;
  }

  uint32_t bxId() const {
    return fBxId;
  }

  uint32_t eventId() const {
    return fEventId;
  }

  uint16_t eventType() const {
    return fEventType;
  }

  void setWords(const uint64_t *d) {
    fSourceId=(d[0]&0xffffffff);
    fBxId=(d[0]>>32);

    fEventId=(d[1]&0xffffffff);
    fEventType=(d[1]>>32)&0xffff;
    fBoe=(d[1]>>48);
  }

  void words(uint64_t *d) const {
    d[0]=fBxId;
    d[0]=(d[0]<<32)+fSourceId;

    d[1]=fBoe;
    d[1]=(d[1]<<16)+fEventType;
    d[1]=(d[1]<<32)+fEventId;
  }

  bool valid() const {
    return fBoe==0x5555;
  }
  
  void print() const {
    uint64_t d[2];
    words(d);
    std::cout << "SlinkBoe::print() Value = 0x" << std::hex << std::setw(16) << std::setfill('0') << d[1]
	      << " " << std::setw(16) << std::setfill('0') << d[0] << std::dec << (valid()?", valid":", not valid") << std::endl;
    std::cout << " Source ID = " << fSourceId << " = 0x" << std::hex << fSourceId << std::dec << std::endl;
    std::cout << " BX ID = " << fBxId << " = 0x" << std::hex << fBxId << std::dec << std::endl;
    std::cout << " Event ID = " << fEventId << " = 0x" << std::hex << fEventId << std::dec << std::endl;
    std::cout << " Event type = " << fEventType << " = 0x" << std::hex << fEventType << std::dec << std::endl;
    std::cout << " BOE = " << fBoe << " = 0x" << std::hex << fBoe << std::dec << std::endl;
  }
  
private:
  uint32_t fSourceId;
  uint32_t fBxId;
  uint32_t fEventId;
  uint16_t fEventType;
  uint16_t fBoe;
};

class SlinkEoe {
public:
  SlinkEoe() : fEoe(0) {
  }
  
  SlinkEoe(uint32_t fi, uint16_t s, uint16_t c, uint16_t el)
    : fFedUserId(fi), fStatus(s), fCrc(c), fEventLength(el), fEoe(0xaaaaaaaa) {
  }

  uint32_t fedUserId() const {
    return fFedUserId;
  }

  uint16_t status() const {
    return fStatus;
  }

  uint16_t crc() const {
    return fCrc;
  }
  
  uint32_t eventLength() const {
    return fEventLength;
  }

  void setWords(const uint64_t *d) {
    fFedUserId=(d[0]&0xffffffff);
    fStatus=(d[0]>>32)&0xffff;
    fCrc=(d[0]>>48)&0xffff;

    fEventLength=(d[1]&0xffffffff);
    fEoe=(d[1]>>32);
  }

  void words(uint64_t *d) const {
dauncey's avatar
dauncey committed
217 218 219
    d[0]=fCrc;
    d[0]=(d[0]<<16)+fStatus;
    d[0]=(d[0]<<32)+fFedUserId;
dauncey's avatar
dauncey committed
220

dauncey's avatar
dauncey committed
221 222
    d[1]=fEoe;
    d[1]=(d[1]<<32)+fEventLength;
dauncey's avatar
dauncey committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  }

  bool valid() const {
    return fEoe==0xaaaaaaaa;
  }
  
  void print() const {
    uint64_t d[2];
    words(d);
    std::cout << "SlinkEoe::print() Value = 0x" << std::hex << std::setw(16) << std::setfill('0') << d[1]
	      << " " << std::setw(16) << std::setfill('0') << d[0] << std::dec << (valid()?", valid":", not valid") << std::endl;
    std::cout << " FED user ID = " << fFedUserId << " = 0x" << std::hex << fFedUserId << std::dec << std::endl;
    std::cout << " Status = " << fStatus << " = 0x" << std::hex << fStatus << std::dec << std::endl;
    std::cout << " CRC = " << fCrc << " = 0x" << std::hex << fCrc << std::dec << std::endl;
    std::cout << " Event length = " << fEventLength << " = 0x" << std::hex << fEventLength << std::dec << std::endl;
    std::cout << " EOE = " << fEoe << " = 0x" << std::hex << fEoe << std::dec << std::endl;
  }
  
private:
  uint32_t fFedUserId;
  uint16_t fStatus;
  uint16_t fCrc;
  uint32_t fEventLength;
  uint32_t fEoe;
};
dauncey's avatar
dauncey committed
248 249
*/
/*
dauncey's avatar
dauncey committed
250 251 252 253 254 255
class HgcrocBoe {
public:
  HgcrocBoe() {
    fWord=0x00ff0000; // Non-existant HGCROC value
  }

dauncey's avatar
dauncey committed
256 257 258 259 260 261
  HgcrocBoe(uint32_t w) {
    fWord=w;
  }

  HgcrocBoe(uint8_t e, uint8_t h, uint8_t b, uint8_t l, uint8_t n) {
    fWord=(b<<24)+((18*e+h)<<16)+(l<<8)+n;
dauncey's avatar
dauncey committed
262 263 264 265 266 267
  }

  uint8_t bx() const {
    return fWord>>24;
  }
  
dauncey's avatar
dauncey committed
268 269 270 271
  uint8_t econ() const {
    return ((fWord>>16)&0xff)/18;
  }
  
dauncey's avatar
dauncey committed
272
  uint8_t hgcroc() const {
dauncey's avatar
dauncey committed
273 274 275 276 277
    return ((fWord>>16)&0xff)%18;
  }
  
  uint8_t l1a() const {
    return (fWord>>8)&0xff;
dauncey's avatar
dauncey committed
278 279 280
  }
  
  uint16_t numberOfWords() const {
dauncey's avatar
dauncey committed
281
    return fWord&0xff;
dauncey's avatar
dauncey committed
282 283 284 285 286 287 288 289 290
  }
  
  uint32_t word() const {
    return fWord;
  }

  void word(uint32_t w) {
    fWord=w;
  }
dauncey's avatar
dauncey committed
291 292 293 294 295 296 297 298

  void print() const {
    std::cout << "HgcrocBoe::print() Value = 0x" << std::hex << std::setw(8) << std::setfill('0') << fWord << std::dec << std::endl;
    std::cout << " BX = " << unsigned(bx()) << std::endl;
    std::cout << " ECON, HGCROC = " << unsigned(econ()) << ", " << unsigned(hgcroc()) << std::endl;
    std::cout << " L1Accept = " << unsigned(l1a()) << std::endl;
    std::cout << " Number of words = " << unsigned(numberOfWords()) << std::endl;
  }
dauncey's avatar
dauncey committed
299 300 301 302
  
private:
  uint32_t fWord;
};
dauncey's avatar
dauncey committed
303
*/
dauncey's avatar
dauncey committed
304
/*
dauncey's avatar
dauncey committed
305 306 307
class SlinkCheck {
public:
  SlinkCheck() : fEventActive(false) {
dauncey's avatar
dauncey committed
308 309
    nCalls=0;
    fL1Accept=-1;
dauncey's avatar
dauncey committed
310 311 312
    nHgcrocWords=0;
  }

dauncey's avatar
dauncey committed
313 314 315 316 317 318
  void processWords(uint64_t *d, bool be=false) {
    std::cout << "SlinkCheck::processWords() call " << nCalls << " words = 0x" << std::hex << d[1] << " " << d[0] << std::dec << std::endl;
    nCalls++;

    assert(be);
    
dauncey's avatar
dauncey committed
319 320 321 322 323
    if(!fEventActive) {
      fBoe.setWords(d);
      fEventActive=fBoe.valid();

      if(fEventActive) {
dauncey's avatar
dauncey committed
324 325
	std::cout << "SlinkCheck::processWords()  L1Accept " << fL1Accept << ", new BOE found" << std::endl;
	fBoe.print();
dauncey's avatar
dauncey committed
326 327

	fL1Accept=(fL1Accept+1)&0xffffffff;
dauncey's avatar
dauncey committed
328 329 330 331 332 333 334 335 336 337
	assert(fBoe.eventId()==fL1Accept);

	for(unsigned e(0);e<5;e++) {
	  for(unsigned h(0);h<18;h++) {
	    if(e==0) completedHgcroc[e][h]=false;
	    if(e==1) completedHgcroc[e][h]=(h>=12);
	    if(e>=2) completedHgcroc[e][h]=(h>=9);
	  }
	}

dauncey's avatar
dauncey committed
338
      } else {
dauncey's avatar
dauncey committed
339 340
	std::cout << "SlinkCheck::processWords()  No BOE found" << std::endl;
	fBoe.print();
dauncey's avatar
dauncey committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
      }
      
      return;
    }
    
    fEoe.setWords(d);
    if(fEoe.valid()) {
      assert(fEoe.fedUserId()==fBoe.sourceId());
      assert(fEoe.status()==0x77);
      assert(fEoe.crc()==0xff);
      assert(fEoe.eventLength()==nWords);

      if(nWords==0) {
	std::cout << "SlinkCheck::processWords()  Empty event found" << std::endl;
	fBoe.print();
	fEoe.print();
      }
      
dauncey's avatar
dauncey committed
359 360
      std::cout << "SlinkCheck::processWords()  New EOE found" << std::endl;
      fEoe.print();
dauncey's avatar
dauncey committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
      
      fEventActive=false;
      nWords=0;
      return;
    } else {
      //std::cout << "SlinkCheck::processWords()  No EOE found" << std::endl;
      //fEoe.print();
    }
    
    nWords++;

    fData[0]=d[0]&0xffffffff;
    fData[1]=d[0]>>32;
    fData[2]=d[1]&0xffffffff;
    fData[3]=d[1]>>32;

    for(unsigned i(0);i<4;i++) processHgcrocWord(fData[i]);
  }

dauncey's avatar
dauncey committed
380 381 382 383 384 385 386 387 388
  bool allComplete() const {
    for(unsigned e(0);e<5;e++) {
      for(unsigned h(0);h<18;h++) {
	if(!completedHgcroc[e][h]) return false;
      }
    }
    return true;
  }
  
dauncey's avatar
dauncey committed
389 390
  void processHgcrocWord(uint32_t w) {
    if(nHgcrocWords==0) {
dauncey's avatar
dauncey committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404
      std::cout << "All completed =";
      for(unsigned e(0);e<5;e++) {
	std::cout << " ";
	for(unsigned h(0);h<18;h++) {
	  std::cout << (completedHgcroc[e][h]?1:0);
	}
      }
      std::cout << std::endl;
      
      if(allComplete()) {
	assert(w==0);
	return;
      }
      
dauncey's avatar
dauncey committed
405
      fHgcrocBoe.word(w);
dauncey's avatar
dauncey committed
406 407 408 409 410
      
      DEBUG_PRINT << " processHgcrocWord, fL1Accept = " << fL1Accept << " &0xff = " << (fL1Accept&0xff) << std::endl;
      fHgcrocBoe.print();
      
      assert(fHgcrocBoe.l1a()==(fL1Accept&0xff));
dauncey's avatar
dauncey committed
411 412
      
    } else {
dauncey's avatar
dauncey committed
413 414
      DEBUG_PRINT << " processHgcrocWord, nHgcrocWords = " << nHgcrocWords << ", word = " << w << std::endl;

dauncey's avatar
dauncey committed
415 416 417 418
      assert(w==nHgcrocWords);
    }
      
    nHgcrocWords++;
dauncey's avatar
dauncey committed
419 420 421 422
    if(nHgcrocWords==fHgcrocBoe.numberOfWords()) {
      completedHgcroc[fHgcrocBoe.econ()][fHgcrocBoe.hgcroc()]=true;
      nHgcrocWords=0;
    }
dauncey's avatar
dauncey committed
423 424
  }

dauncey's avatar
dauncey committed
425 426 427
  uint64_t numberOfCalls() const {
    return nCalls;
  }
dauncey's avatar
dauncey committed
428 429
  
private:
dauncey's avatar
dauncey committed
430 431
  uint64_t nCalls;

dauncey's avatar
dauncey committed
432 433 434 435 436 437 438 439 440 441 442 443
  bool fEventActive;
  uint32_t fL1Accept;

  SlinkBoe fBoe;
  SlinkEoe fEoe;
  uint16_t nWords;

  uint32_t fData[4];

  HgcrocBoe fHgcrocBoe;
  unsigned nHgcrocWords;

dauncey's avatar
dauncey committed
444 445 446
  bool completedHgcroc[5][18];
};
*/
dauncey's avatar
dauncey committed
447

dauncey's avatar
dauncey committed
448
/*
dauncey's avatar
dauncey committed
449 450
  class Noce {
  public:
dauncey's avatar
dauncey committed
451 452 453 454
  Noce(unsigned h=18, unsigned m=0) : fHgcroc(h) {
  }

  void processBx(uint32_t *a) {
dauncey's avatar
dauncey committed
455
  uint64_t d;
dauncey's avatar
dauncey committed
456
  
dauncey's avatar
dauncey committed
457 458 459 460 461 462 463 464
  for(unsigned e(0);e<7;e++) {
  if(nArray[e]==0xffffffff) {
  if(array[bx%2][e]!=0) {
  hArray[e]=array[bx%2][e];
  unsigned hgcroc((hArray[e]&0xff0000)>>16);
  assert(hgcroc<18);
  assert((hgcroc%7)==e);
  hActive[hgcroc]=e;
dauncey's avatar
dauncey committed
465
	    
dauncey's avatar
dauncey committed
466 467 468
  //ur[e].write(0,array[bx%2][e]);
  nArray[e]=1;
  }
dauncey's avatar
dauncey committed
469
	  
dauncey's avatar
dauncey committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  } else {
  if(nArray[e]<(hArray[e]&0xffff)) {
  //ur[e].write(nArray[e],array[bx%2][e]);
  nArray[e]++;
  if((nArray[e]%2)==0) {
  unsigned hgcroc((hArray[e]&0xff0000)>>16);
  //uint64_t d(array[hActive[hgcroc]][bx%2]);
  uint64_t d(array[bx%2][e]);
  d=d<<32;
  //d+=array[hActive[hgcroc]][(bx+1)%2];
  d+=array[(bx+1)%2][e];
  //uint64_t d((((unsigned long)array[hActive[hgcroc]][bx%2])<<32)+array[hActive[hgcroc]][(l+1)%2]);
  //std::cout << "Writing to HGCROC " << hgcroc << " = " << std::hex << d << std::dec << std::endl;


  //assert(pb.writeAndIncrementPtr(hgcroc,d));
  pb.writeAndIncrementPtr(hgcroc,d);
  }
dauncey's avatar
dauncey committed
488
	    
dauncey's avatar
dauncey committed
489 490 491 492 493 494 495 496
  if(nArray[e]==(hArray[e]&0xffff)) {
  //std::cout << std::endl << "Elink " << e << ", print event for BX "
  //	<< ((hArray[e]&0xff000000)>>24) << ", HGCROC "
  //	<< ((hArray[e]&0xff0000)>>16) << ", length " << (hArray[e]&0xffff) << std::endl;
  //for(unsigned i(0);i<(hArray[e]&0xffff);i++) {
  //std::cout << "Word " << i << " = " << ur[e].read(i)
  //	  << " = 0x" << std::hex << ur[e].read(i) << std::dec
  //	  << std::endl;
dauncey's avatar
dauncey committed
497
		
dauncey's avatar
dauncey committed
498 499 500
  //if(i==0) assert(hArray[e]==ur[e].read(i));
  //else     assert(i        ==ur[e].read(i));
  //}
dauncey's avatar
dauncey committed
501

dauncey's avatar
dauncey committed
502 503 504 505
  if((nArray[e]%2)==1) {
  unsigned hgcroc((hArray[e]&0xff0000)>>16);
  uint64_t d(array[bx%2][e]);
  //std::cout << "Writing to HGCROC " << hgcroc << " = " << std::hex << d << std::dec << std::endl;
dauncey's avatar
dauncey committed
506

dauncey's avatar
dauncey committed
507 508 509
  //assert(pb.writeAndIncrementPtr(hgcroc,d));
  pb.writeAndIncrementPtr(hgcroc,d);
  }
dauncey's avatar
dauncey committed
510

dauncey's avatar
dauncey committed
511 512
  unsigned hgcroc((hArray[e]&0xff0000)>>16);
  completeEvent[hgcroc][(hArray[e]&0xff000000)>>24]=true;
dauncey's avatar
dauncey committed
513

dauncey's avatar
dauncey committed
514 515 516 517 518 519
  nArray[e]=0xffffffff;
  hActive[hgcroc]=0xffffffff;
  }
  }
  }
  }
dauncey's avatar
dauncey committed
520 521
  }

dauncey's avatar
dauncey committed
522
  private:
dauncey's avatar
dauncey committed
523 524 525 526
  uint32_t array[2][7];
  unsigned nArray[7];
  uint32_t hArray[7];
  unsigned hActive[18];
dauncey's avatar
dauncey committed
527
  };
dauncey's avatar
dauncey committed
528 529
*/

dauncey's avatar
dauncey committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
class BeReadState {
  enum {
    maxEcon=5,
    maxHgcroc=18
  };

public:
  BeReadState() {
    reset();
  }

  void reset() {
    fEcon=maxEcon;
    fHgcroc=0;
    fWord=0;
    fSentEoe=true;
    
    fNHgcroc[0]=18;
    fNHgcroc[1]=12;
    fNHgcroc[2]=9;
    fNHgcroc[3]=9;
    fNHgcroc[4]=9;
  }

  bool isActive() const {
    return fEcon<maxEcon;
  }
  
  void setActive() {
    fEcon=0;
    fHgcroc=0;
    fWord=0;
    fSentEoe=false;
  }
  
  unsigned econ() const {
    return fEcon;
  }
  
  unsigned hgcroc() const {
    return fHgcroc;
  }
  
  bool sentEoe() const {
    return fSentEoe;
  }
  
  void stepEoe() {
    fSentEoe=true;
  }
  
  void step(uint32_t w) {
    assert(isActive());

    if(fWord==0) {
      HgcrocBoe hboe(w);
      fNWord=hboe.numberOfWords();
    }

    fWord++;
    if(fWord==fNWord) {
      fWord=0;
      fHgcroc++;
      if(fHgcroc==fNHgcroc[fEcon]) {
	fHgcroc=0;
	fEcon++;
      }
    }
  }
  
  
private:
  unsigned fNHgcroc[maxEcon];
  unsigned fNWord;

  unsigned fEcon;
  unsigned fHgcroc;
  unsigned fWord;
  bool fSentEoe;
};



dauncey's avatar
dauncey committed
613 614
int main(int argc, char* argv[]) {

dauncey's avatar
dauncey committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  unsigned l1AcceptAverageRate(750);
  unsigned printLevel(1);
  unsigned seconds(1);
  unsigned hgcrocLimit(4);
  unsigned slinkNumber[Dimensions::MaxNumberOfSlinks];
  unsigned block(1);
  unsigned long rSeed(0);
  unsigned econRandom(EconSim::Mixed);
  unsigned econFormat(EconSim::FixedChannelBitmap);
  unsigned econFlow(EconSim::FixedHgcrocMap);

  for(unsigned n(0);n<Dimensions::MaxNumberOfSlinks;n++) slinkNumber[n]=n;
  
  if((argc%2)!=1) {
    std::cerr << "Usage: " << argv[0] << " --<parameter> <value>" << std::endl;
    return 1;
  }
dauncey's avatar
dauncey committed
632

dauncey's avatar
dauncey committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
  for(int i(1);i<argc;i+=2) {
    std::string sarg1(argv[i]);
    std::istringstream sarg2(argv[i+1]);
    
    if(sarg1=="--seed") sarg2 >> rSeed;
    if(sarg1=="--seconds") sarg2 >> seconds;
    if(sarg1=="--l1AcceptAverageRate") sarg2 >> l1AcceptAverageRate;
    if(sarg1=="--hgcrocLimit") sarg2 >> hgcrocLimit;
    if(sarg1=="--printLevel") sarg2 >> printLevel;

    if(sarg1=="--slinkFile") sarg2 >> slinkNumber[0];
    if(sarg1=="--slinkNumber") sarg2 >> slinkNumber[0];
    if(sarg1=="--block") sarg2 >> block;
    if(sarg1=="--econRandom") sarg2 >> econRandom;
    if(sarg1=="--econFormat") sarg2 >> econFormat;
    //if(sarg1=="--econFlow") sarg2 >> econFlow;
  }
  
dauncey's avatar
dauncey committed
651
  bool doingEcon(true);
dauncey's avatar
dauncey committed
652
  bool doingBe(true);
dauncey's avatar
dauncey committed
653

dauncey's avatar
dauncey committed
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  std::ostringstream sout;
  sout << "_L1AcceptAverageRate" << l1AcceptAverageRate;
  sout << "_HgcrocLimit" << hgcrocLimit;

  if(doingEcon) {
    sout << "_SlinkNumber" << std::setw(2) << std::setfill('0') << slinkNumber[0];
    sout << "_EconRandom" << econRandom;
    sout << "_EconFormat" << econFormat;
    sout << "_Block" << block;
    assert(block==1 || block==7);
  }

  if(doingBe) {
  }

  sout << "_Seconds" << seconds;
dauncey's avatar
dauncey committed
670

dauncey's avatar
dauncey committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  std::string sName("DaqDaqBuffer/DaqDaqBuffer");

  if(!doingEcon) sName+="_HgcrocOnly";
  else if(!doingBe) sName+="_HgcrocAndEcon";
  else sName+="_FullChain";

  sName+=sout.str();

  bool randomL1Accept(true);

  //EconSim::fDataRandomMethod=EconSim::Flat;
  EconSim::fDataRandomMethod=EconSim::DataRandomMethod(econRandom);
  
  //EconSim::fDataFormatMethod=EconSim::FixedChannelBitmap;
  //EconSim::fDataFormatMethod=EconSim::FixedChannelLabels;
  //EconSim::fDataFormatMethod=EconSim::FixedMixture;
  //EconSim::fDataFormatMethod=EconSim::VariableMixture;
  EconSim::fDataFormatMethod=EconSim::DataFormatMethod(econFormat);
dauncey's avatar
dauncey committed
689
  
dauncey's avatar
dauncey committed
690 691 692 693 694 695 696 697
  EconSim::fDataFlowMethod=EconSim::EventBuild;

  HgcrocSim::fPrintLevel=printLevel;
  EconSim::fPrintLevel=printLevel;
  BlockCheck::fPrintLevel=printLevel;
  LpGbtCheck::fPrintLevel=printLevel;
  BeSlinkSim::fPrintLevel=printLevel;
  SlinkCheck::fPrintLevel=printLevel;
dauncey's avatar
dauncey committed
698

dauncey's avatar
dauncey committed
699 700 701
  const unsigned NumberOfSlinks(1);
  assert(NumberOfSlinks<=Dimensions::MaxNumberOfSlinks);
  
dauncey's avatar
dauncey committed
702 703
  assert(sizeof(SlinkBoe)==16);
  assert(sizeof(SlinkEoe)==16);
dauncey's avatar
dauncey committed
704
  assert(sizeof(uint128_t)==16);
dauncey's avatar
dauncey committed
705 706

  Random random;
dauncey's avatar
dauncey committed
707 708
  random.random().SetSeed(rSeed);

dauncey's avatar
dauncey committed
709 710 711 712 713
  CircularBuffer<1> cbBoe;
  CircularBuffer<1> cb;
  CircularBuffer<1> cbFinal;
  PartitionedBuffer<18,4> pb[5];

dauncey's avatar
dauncey committed
714
  BeReadState brs;
dauncey's avatar
dauncey committed
715 716 717 718
  
  uint64_t d;


dauncey's avatar
dauncey committed
719 720 721
  unsigned nHWords(0);


dauncey's avatar
dauncey committed
722 723
  uint32_t array[5][2][7];
  unsigned nArray[5][7];
dauncey's avatar
dauncey committed
724 725
  //uint32_t hArray[5][7];
  HgcrocBoe hArray[5][7];
dauncey's avatar
dauncey committed
726 727 728
  UltraRam ur[7];
  unsigned hActive[5][18];
  bool completeEvent[5][18][256];
dauncey's avatar
dauncey committed
729 730
  bool doneEvent[5][18][256];
  unsigned nHgcrocWords=-1;
dauncey's avatar
dauncey committed
731 732 733 734 735
  unsigned nHgcrocWord(0);
  
  unsigned readL1A=0;
  unsigned readEcon=0;
  unsigned readHgcroc=0;
dauncey's avatar
dauncey committed
736
  unsigned readState=5*18;
dauncey's avatar
dauncey committed
737 738 739 740 741 742 743 744 745 746 747
  
  for(unsigned ec(0);ec<5;ec++) {
    for(unsigned e(0);e<7;e++) {
      array[ec][0][e]=-1;
      array[ec][1][e]=-1;
      nArray[ec][e]=-1;
    }
    for(unsigned h(0);h<18;h++) {
      hActive[ec][h]=-1;
      for(unsigned i(0);i<256;i++) {
	completeEvent[ec][h][i]=false;
dauncey's avatar
dauncey committed
748
	doneEvent[ec][h][i]=true;
dauncey's avatar
dauncey committed
749 750 751 752
      }
    }
  }
  /*  
dauncey's avatar
dauncey committed
753 754 755 756 757 758 759 760 761 762
      for(unsigned l(0);l<100;l++) {
      econTest.readBx(array[l%2]);
      std::cout << "Array "
      << array[l%2][0] << ", "
      << array[l%2][1] << ", "
      << array[l%2][2] << ", "
      << array[l%2][3] << ", "
      << array[l%2][4] << ", "
      << array[l%2][5] << ", "
      << array[l%2][6] << std::endl;
dauncey's avatar
dauncey committed
763
    
dauncey's avatar
dauncey committed
764
      for(unsigned e(0);e<7;e++) {
dauncey's avatar
dauncey committed
765
      if(nArray[e]==0xffffffff) {
dauncey's avatar
dauncey committed
766 767 768 769 770 771
      if(array[l%2][e]!=0) {
      hArray[e]=array[l%2][e];
      unsigned hgcroc((hArray[e]&0xff0000)>>16);
      assert(hgcroc<18);
      assert((hgcroc%7)==e);
      hActive[hgcroc]=e;
dauncey's avatar
dauncey committed
772
	  
dauncey's avatar
dauncey committed
773 774 775
      ur[e].write(0,array[l%2][e]);
      nArray[e]=1;
      }
dauncey's avatar
dauncey committed
776 777

      } else {
dauncey's avatar
dauncey committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791
      if(nArray[e]<(hArray[e]&0xffff)) {
      ur[e].write(nArray[e],array[l%2][e]);
      nArray[e]++;
      if((nArray[e]%2)==0) {
      unsigned hgcroc((hArray[e]&0xff0000)>>16);
      //uint64_t d(array[hActive[hgcroc]][l%2]);
      uint64_t d(array[l%2][e]);
      d=d<<32;
      //d+=array[hActive[hgcroc]][(l+1)%2];
      d+=array[(l+1)%2][e];
      //uint64_t d((((unsigned long)array[hActive[hgcroc]][l%2])<<32)+array[hActive[hgcroc]][(l+1)%2]);
      std::cout << "Writing to HGCROC " << hgcroc << " = " << std::hex << d << std::dec << std::endl;
      pb.writeAndIncrementPtr(hgcroc,d);
      }
dauncey's avatar
dauncey committed
792
	      
dauncey's avatar
dauncey committed
793 794 795 796 797 798 799 800 801 802 803 804
      if(nArray[e]==(hArray[e]&0xffff)) {
      std::cout << std::endl << "Elink " << e << ", print event for BX "
      << ((hArray[e]&0xff000000)>>24) << ", HGCROC "
      << ((hArray[e]&0xff0000)>>16) << ", length " << (hArray[e]&0xffff) << std::endl;
      for(unsigned i(0);i<(hArray[e]&0xffff);i++) {
      std::cout << "Word " << i << " = " << ur[e].read(i)
      << " = 0x" << std::hex << ur[e].read(i) << std::dec
      << std::endl;

      if(i==0) assert(hArray[e]==ur[e].read(i));
      else     assert(i        ==ur[e].read(i));
      }
dauncey's avatar
dauncey committed
805

dauncey's avatar
dauncey committed
806 807 808 809 810 811 812
      nArray[e]=0xffffffff;
      unsigned hgcroc((hArray[e]&0xff0000)>>16);
      hActive[hgcroc]=0xffffffff;
      }
      }
      }
      }
dauncey's avatar
dauncey committed
813 814
      }

dauncey's avatar
dauncey committed
815 816 817 818
      for(unsigned h(0);h<18;h++) {
      std::cout << std::endl << "HGCROC " << h << std::endl;
      uint64_t d;
      for(unsigned i(0);pb.readAndIncrementPtr(h,d);i++) {
dauncey's avatar
dauncey committed
819 820 821 822 823
      uint32_t dHi(d>>32);
      uint32_t dLo(d&0xffffffff);
      
      std::cout << " Word " << 2*i << " = 0x" << std::hex << dLo << std::dec << std::endl;
      std::cout << " Word " << 2*i+1 << " = 0x" << std::hex << dHi << std::dec << std::endl;
dauncey's avatar
dauncey committed
824 825
      }
      }
dauncey's avatar
dauncey committed
826
  
dauncey's avatar
dauncey committed
827 828 829 830
      econTest.reset();
      //return 0;
      */

dauncey's avatar
dauncey committed
831 832 833
  TFileHandler tfh(sName);

  
dauncey's avatar
dauncey committed
834
  unsigned nHistory(10000);
dauncey's avatar
dauncey committed
835
  
dauncey's avatar
dauncey committed
836 837 838 839 840
  HgcrocSim hgcrocSim;
  HgcrocAna hgcrocAna;
  
  SlinkFile slinkFile[NumberOfSlinks];
  SlinkArch slinkArch[NumberOfSlinks];
dauncey's avatar
dauncey committed
841

dauncey's avatar
dauncey committed
842
  SlinkElinkData slinkElinkData[NumberOfSlinks];
dauncey's avatar
dauncey committed
843
  
dauncey's avatar
dauncey committed
844 845 846
  EconSim econSim[NumberOfSlinks][Dimensions::MaxNumberOfEconsPerSlink];
  EconAna econAna[NumberOfSlinks][Dimensions::MaxNumberOfEconsPerSlink];
  LpGbtCheck lpGbtCheck[NumberOfSlinks][Dimensions::MaxNumberOfEconsPerSlink];
dauncey's avatar
dauncey committed
847
  
dauncey's avatar
dauncey committed
848 849 850 851 852 853 854 855 856 857
  BeSlinkSim beSlinkSim[NumberOfSlinks];
  BeSlinkAna beSlinkAna[NumberOfSlinks];
  SlinkCheck slinkCheck[NumberOfSlinks];







dauncey's avatar
dauncey committed
858 859 860 861 862 863
  double totalLinkWords(0.0);

  unsigned nEventWords(0);

  std::string sNum[7]={"0","1","2","3","4","5","6"};

dauncey's avatar
dauncey committed
864

dauncey's avatar
dauncey committed
865 866 867 868 869 870 871


  // Initialisation
  
  hgcrocSim.boj(hgcrocLimit);
  hgcrocAna.boj(hgcrocSim,nHistory);
  
dauncey's avatar
dauncey committed
872 873
  if(doingEcon) {
    for(unsigned sl(0);sl<NumberOfSlinks;sl++) {
dauncey's avatar
dauncey committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
      slinkFile[sl].slinkNumber(sl);
      
      std::ostringstream soutFile;
      soutFile << "Slink" << std::setw(2) << std::setfill('0') << slinkNumber[sl] << ".txt";
      assert(slinkFile[sl].read(soutFile.str()));
      
      slinkArch[sl].initialise(&(slinkFile[sl]),block==1);
      slinkArch[sl].print();
      
      for(unsigned ec(0);ec<slinkArch[sl].numberOfEcons();ec++) {
	//econSim[sl][ec].slinkNumber(sl);
	//econSim[sl][ec].econNumber(ec);
	//econSim[sl][ec].econFile(&(slinkFile[sl].econFile(ec)));
	econSim[sl][ec].boj(sl,ec,slinkFile[sl].econFile(ec),slinkArch[sl].econArch(ec));
	econAna[sl][ec].boj(econSim[sl][ec],nHistory);
dauncey's avatar
dauncey committed
889
	
dauncey's avatar
dauncey committed
890 891 892 893
	lpGbtCheck[sl][ec].initialise(sl,ec,econSim[sl][ec].econArch());
      }
  
      if(doingBe) {
dauncey's avatar
dauncey committed
894
	beSlinkSim[sl].boj(slinkArch[sl]);
dauncey's avatar
dauncey committed
895
	beSlinkAna[sl].boj(beSlinkSim[sl],nHistory);
dauncey's avatar
dauncey committed
896
	
dauncey's avatar
dauncey committed
897
	slinkCheck[sl].boj(slinkArch[sl]);
dauncey's avatar
dauncey committed
898 899
      }
    }
dauncey's avatar
dauncey committed
900
  }
dauncey's avatar
dauncey committed
901

dauncey's avatar
dauncey committed
902 903 904 905 906
  std::cout << std::endl << "lpGBT total average 32-bit words per event = "
	    << totalLinkWords << std::endl;
  
  TH1F *hWorst=new TH1F("Worst",";Log10(secs);Worst",35,-5.5,29.5);

dauncey's avatar
dauncey committed
907 908 909 910 911 912 913 914
  TH1D *hCounts=new TH1D("MainCounts",";Counts",
			 10,0,10);




  //TH1D *hEconSpace[5][7];
  //TH1F *hEconSpaceHistory[5][7];
dauncey's avatar
dauncey committed
915 916 917
  TH1D *hSboeSpace;
  TH1F *hSboeSpaceHistory;
  TH1D *hSpace[5][18];
dauncey's avatar
dauncey committed
918 919 920 921 922 923
  TH1F *hSpaceHistory[5][18];
  std::string sHgcroc[18]={"00","01","02","03","04","05","06","07","08","09",
			   "10","11","12","13","14","15","16","17"};

  for(unsigned ec(0);ec<5;ec++) {
    for(unsigned e(0);e<7;e++) {
dauncey's avatar
dauncey committed
924 925 926 927
      //hEconSpace[ec][e]=new TH1D((std::string("Econ")+sNum[ec]+"Elink"+sNum[e]+"Space").c_str(),
      //				 ";Circular buffer space",4096,0,4096);
    //hEconSpaceHistory[ec][e]=new TH1F((std::string("Econ")+sNum[ec]+"Elink"+sNum[e]+"SpaceHistory").c_str(),
      //				";BX;Circular buffer space",nHistory,0,nHistory);
dauncey's avatar
dauncey committed
928 929
    }
    for(unsigned h(0);h<18;h++) {
dauncey's avatar
dauncey committed
930 931
      hSpace[ec][h]=new TH1D((std::string("BeEcon")+sNum[ec]+"Hgcroc"+sHgcroc[h]+"Space").c_str(),
			     ";Partitioned buffer space",1000,0,1000);
dauncey's avatar
dauncey committed
932
      hSpaceHistory[ec][h]=new TH1F((std::string("BeEcon")+sNum[ec]+"Hgcroc"+sHgcroc[h]+"SpaceHistory").c_str(),
dauncey's avatar
dauncey committed
933
				    ";BX;Partitioned buffer space",nHistory,0,nHistory);
dauncey's avatar
dauncey committed
934 935 936
    }
  }

dauncey's avatar
dauncey committed
937 938 939 940 941
  hSboeSpace=new TH1D((std::string("Sboe")+"Space").c_str(),
		      ";BOE buffer space",4096,0,4096);
  hSboeSpaceHistory=new TH1F((std::string("Sboe")+"SpaceHistory").c_str(),
			     ";BX;BOE buffer space",nHistory,0,nHistory);
  
dauncey's avatar
dauncey committed
942
  int64_t runSec(seconds);
dauncey's avatar
dauncey committed
943
  //int64_t runSec(-10000); // for 0.0001 run secs
dauncey's avatar
dauncey committed
944
  //runSec=-1000; // for 0.001 run secs
dauncey's avatar
dauncey committed
945
  //int64_t runSec(-100); // for 0.01 run secs
dauncey's avatar
dauncey committed
946
  //runSec=-10; // for 0.1 run secs
dauncey's avatar
dauncey committed
947 948
  //int64_t runSec(1); // for 1 run secs
  //int64_t runSec(10); // for 10 run secs
dauncey's avatar
dauncey committed
949
  //int64_t runSec(100); // ~4 mins for 100 run secs
dauncey's avatar
dauncey committed
950 951 952 953
  //int64_t runSec(1000); // ~40 mins for 1000 run secs
  //int64_t runSec(10000); // ~6 hours for 10000 run secs

  uint64_t bxPerSec(40000000);
dauncey's avatar
dauncey committed
954
  double runSecs(runSec>=0?runSec:-1.0/runSec);
dauncey's avatar
dauncey committed
955 956 957 958 959 960 961 962
  uint64_t maxBx(runSec>=0?bxPerSec*runSec:bxPerSec/(-runSec));

  std::cout << "Max BX = " << maxBx << ", run time = " << (runSec>=0?runSec:-1.0/runSec) << " sec" << std::endl;
  
  unsigned nL1A(0);
  unsigned nOut(0);
  unsigned worstL1A(0);
  unsigned worstPower(1);
dauncey's avatar
dauncey committed
963
  uint64_t numberL1AIn(0);
dauncey's avatar
dauncey committed
964
  uint64_t numberL1AOut(0);
dauncey's avatar
dauncey committed
965 966 967 968

  uint64_t nHgcrocDeadtime(0);

  //TriggerRule tr(50);
dauncey's avatar
dauncey committed
969 970 971 972
  
  for(uint64_t bx(0);bx<maxBx;bx++) {
    if(((100*bx)%maxBx)==0) std::cout << "BX " << bx << " = " << 100*bx/maxBx << " %" << std::endl;

dauncey's avatar
dauncey committed
973 974 975 976 977
    //if(bx>100000) {
    //BeSlinkSim::fPrintLevel=6;
    //SlinkCheck::fPrintLevel=6;
    //}
    
dauncey's avatar
dauncey committed
978 979 980 981 982 983 984
    ////////////////////////////////////////////////////////////
    //
    // L1A and HGCROC
    //
    ////////////////////////////////////////////////////////////

    bool newL1AIn(false);
dauncey's avatar
dauncey committed
985 986
    bool newL1AOut(false);

dauncey's avatar
dauncey committed
987 988 989
    if(!hgcrocSim.allowL1Accept()) {
      nHgcrocDeadtime++;
    } else {
dauncey's avatar
dauncey committed
990
      if(randomL1Accept) newL1AIn=(random.random().Uniform()<(l1AcceptAverageRate/40000.0));
dauncey's avatar
dauncey committed
991
      else newL1AIn=((bx%53)==0);
dauncey's avatar
dauncey committed
992
    }
dauncey's avatar
dauncey committed
993 994 995
    
    /*
      if(worstL1A<nL1A) {
dauncey's avatar
dauncey committed
996 997 998 999
      worstL1A=nL1A;
      std::cout << "New worst case = " << worstL1A << std::endl;
      }
    */
dauncey's avatar
dauncey committed
1000 1001
    /*
      if(bx==worstPower*(bxPerSec/100000)-1) {
dauncey's avatar
dauncey committed
1002 1003 1004
      std::cout << "Worst plotting for BX = " << bx << std::endl;
      hWorst->Fill(log10(worstPower)-5.0,worstL1A);
      worstPower*=10;
dauncey's avatar
dauncey committed
1005
      }
dauncey's avatar
dauncey committed
1006
    */
dauncey's avatar
dauncey committed
1007 1008 1009 1010 1011 1012 1013 1014 1015

    newL1AOut=hgcrocSim.processBx(newL1AIn);
    hgcrocAna.analyseBx(bx);

    ////////////////////////////////////////////////////////////
    //
    // ECON
    //
    ////////////////////////////////////////////////////////////
dauncey's avatar
dauncey committed
1016
    
dauncey's avatar
dauncey committed
1017 1018 1019 1020
    if(doingEcon) {
      for(unsigned sl(0);sl<NumberOfSlinks;sl++) {
	for(unsigned ec(0);ec<5;ec++) {
	  if(newL1AOut) {
dauncey's avatar
dauncey committed
1021 1022
	    econSim[sl][ec].processZs(bx&0xff,numberL1AOut&0xff);
	    econAna[sl][ec].analyseZs(bx);
dauncey's avatar
dauncey committed
1023
	  }
dauncey's avatar
dauncey committed
1024 1025 1026
	  //econSim[sl][ec].processBx(array[ec][bx%2]);
	  econSim[sl][ec].processBx(slinkElinkData[sl].fData[ec]);
	  econAna[sl][ec].analyseBx(bx);
dauncey's avatar
dauncey committed
1027 1028

	  if(!doingBe) {
dauncey's avatar
dauncey committed
1029 1030
	    //lpGbtCheck[sl][ec].processBx(array[ec][bx%2]);
	    lpGbtCheck[sl][ec].processBx(slinkElinkData[sl].fData[ec]);
dauncey's avatar
dauncey committed
1031 1032
	  }
	}
dauncey's avatar
dauncey committed
1033
      }
dauncey's avatar
dauncey committed
1034 1035 1036 1037 1038 1039 1040 1041 1042
      
      ////////////////////////////////////////////////////////////
      //
      // BE
      //
      ////////////////////////////////////////////////////////////

      if(doingBe) {

dauncey's avatar
dauncey committed
1043 1044 1045 1046 1047 1048
	for(unsigned sl(0);sl<NumberOfSlinks;sl++) {
	  if(newL1AIn) {
	    beSlinkSim[sl].processL1Accept(bx,numberL1AIn);
	    beSlinkAna[sl].analyseL1Accept(bx);
	  }
	    
dauncey's avatar
dauncey committed
1049
	  beSlinkSim[sl].processBx(bx,slinkElinkData[sl]);
dauncey's avatar
dauncey committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	  beSlinkAna[sl].analyseBx(bx);

	  for(unsigned w(0);w<beSlinkSim[sl].numberOfSlinkWords();w++) {
	    slinkCheck[sl].processWord(beSlinkSim[sl].slinkWord(w).first,
				       beSlinkSim[sl].slinkWord(w).second);
	  }
	}
      
#ifdef NOT_YET
      
dauncey's avatar
dauncey committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	if(newL1AIn) {
	  uint64_t d[2];
	  SlinkBoe sboe(0xce00,bx&0xffffffff,numberL1AIn&0xffffffff,0xab);
	  sboe.words(d);
	  assert(cbBoe.writeAndIncrementPtr(d[0]));
	  assert(cbBoe.writeAndIncrementPtr(d[1]));

	  hSboeSpace->Fill(cbBoe.space());
	  hSboeSpaceHistory->Fill(bx,cbBoe.space());

	  DEBUG_PRINT << " L1A = " << numberL1AIn << ", write Slink BOE 0x" << std::hex << d[1] << " " << d[0] << std::dec << std::endl;
	  //sboe.print();
	}
dauncey's avatar
dauncey committed
1073

dauncey's avatar
dauncey committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095


	for(unsigned ec(0);ec<5;ec++) {
	  /*
	    std::cout << "Array "
	    << array[ec][bx%2][0] << ", "
	    << array[ec][bx%2][1] << ", "
	    << array[ec][bx%2][2] << ", "
	    << array[ec][bx%2][3] << ", "
	    << array[ec][bx%2][4] << ", "
	    << array[ec][bx%2][5] << ", "
	    << array[ec][bx%2][6] << std::endl;
	  */
	  for(unsigned e(0);e<7;e++) {
	    if(nArray[ec][e]==0xffffffff) {
	      if(array[ec][bx%2][e]!=0) {
		hArray[ec][e].word(array[ec][bx%2][e]);
		//unsigned hgcroc((hArray[ec][e]&0xff0000)>>16);
		unsigned hgcroc(hArray[ec][e].hgcroc());
		assert(hgcroc<18);
		assert((hgcroc%7)==e);
		hActive[ec][hgcroc]=e;
dauncey's avatar
dauncey committed
1096
	    
dauncey's avatar
dauncey committed
1097 1098 1099
		//ur[e].write(0,array[ec][bx%2][e]);
		nArray[ec][e]=1;
	      }
dauncey's avatar
dauncey committed
1100
	  
dauncey's avatar
dauncey committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	    } else {
	      //if(nArray[ec][e]<(hArray[ec][e]&0xff)) {
	      if(nArray[ec][e]<(hArray[ec][e].numberOfWords())) {
		//ur[e].write(nArray[ec][e],array[ec][bx%2][e]);
		nArray[ec][e]++;
		if((nArray[ec][e]%2)==0) {
		  //unsigned hgcroc((hArray[ec][e]&0xff0000)>>16);
		  unsigned hgcroc(hArray[ec][e].hgcroc());
		  //uint64_t d(array[ec][hActive[hgcroc]][bx%2]);
		  uint64_t d(array[ec][bx%2][e]);
		  d=d<<32;
		  //d+=array[ec][hActive[hgcroc]][(bx+1)%2];
		  d+=array[ec][(bx+1)%2][e];
		  //uint64_t d((((unsigned long)array[ec][hActive[hgcroc]][bx%2])<<32)+array[ec][hActive[hgcroc]][(l+1)%2]);
		  //std::cout << "Writing to HGCROC " << hgcroc << " = " << std::hex << d << std::dec << std::endl;

		  //HgcrocBoe hBoe(readL1A&0xff,hgcroc&0xff,(numberL1AOut-1)&0xff,0);

		  assert(pb[ec].writeAndIncrementPtr(hgcroc,d));
		  //pb[ec].writeAndIncrementPtr(hgcroc,d);
		}
dauncey's avatar
dauncey committed
1122
	    
dauncey's avatar
dauncey committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
		//if(nArray[ec][e]==(hArray[ec][e]&0xff)) {
		if(nArray[ec][e]==(hArray[ec][e].numberOfWords())) {
		  /*
		    std::cout << std::endl << "Elink " << e << ", print event for BX "
		    << ((hArray[ec][e]&0xff000000)>>24) << ", HGCROC "
		    << ((hArray[ec][e]&0xff0000)>>16) << ", length " << (hArray[ec][e]&0xffff) << std::endl;
		    for(unsigned i(0);i<(hArray[ec][e]&0xffff);i++) {
		    std::cout << "Word " << i << " = " << ur[e].read(i)
		    << " = 0x" << std::hex << ur[e].read(i) << std::dec
		    << std::endl;
dauncey's avatar
dauncey committed
1133
		
dauncey's avatar
dauncey committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
		    if(i==0) assert(hArray[ec][e]==ur[e].read(i));
		    else     assert(i        ==ur[e].read(i));
		    }
		  */

		  if((nArray[ec][e]%2)==1) {
		    //unsigned hgcroc((hArray[ec][e]&0xff0000)>>16);
		    unsigned hgcroc(hArray[ec][e].hgcroc());
		    uint64_t d(array[ec][bx%2][e]);
		    //std::cout << "Writing to HGCROC " << hgcroc << " = " << std::hex << d << std::dec << std::endl;

		    assert(pb[ec].writeAndIncrementPtr(hgcroc,d));
		    //pb[ec].writeAndIncrementPtr(hgcroc,d);
		  }

		  //unsigned hgcroc((hArray[ec][e]&0xff0000)>>16);
		  unsigned hgcroc(hArray[ec][e].hgcroc());
		  //completeEvent[ec][hgcroc][(hArray[ec][e]&0xff00)>>8]=true;
		  completeEvent[ec][hgcroc][hArray[ec][e].l1a()]=true;
		  //doneEvent[ec][hgcroc][(hArray[ec][e]&0xff00)>>8]=false;
		  doneEvent[ec][hgcroc][hArray[ec][e].l1a()]=false;

		  nArray[ec][e]=0xffffffff;
		  hActive[ec][hgcroc]=0xffffffff;
		}
	      }
	    }
	  }
	}

	unsigned ecMin,hMin,spaceMin(0xffffffff);
    
	for(unsigned ec(0);ec<5;ec++) {
	  for(unsigned h(0);h<18;h++) {
	    hSpace[ec][h]->Fill(pb[ec].space(h));
	    hSpaceHistory[ec][h]->Fill(bx,pb[ec].space(h));

	    if(spaceMin>pb[ec].space(h)) {
	      spaceMin=pb[ec].space(h);
	      ecMin=ec;
	      hMin=h;
	    }
	  }
	}
    
	// Readout 
	uint64_t d[2];
	uint32_t dh[10];
	/*
	  std::cout << "Complete event list";
	  for(unsigned ec(0);ec<5;ec++) {
	  std::cout << " ";
	  for(unsigned h(0);h<18;h++) {
	  std::cout << (completeEvent[ec][h][readL1A&0xff]?1:0);
	  }
	  }
	  std::cout << std::endl;
	*/

dauncey's avatar
dauncey committed
1193 1194 1195
#endif
	
#ifdef NOT_YET
dauncey's avatar
dauncey committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232





	unsigned nBxWords(0);
	bool completeAllEvent(true);

	//std::cout << "Line " << __LINE__ << ", BX " << bx << ": starting BE readout, readEcon = "
	DEBUG_PRINT << " BX " << bx << ": starting BE readout, readEcon = "
		    << brs.econ() << " readHgcroc = " << brs.hgcroc() << " nHgcrocWords = "
		    << nHgcrocWords << " completeEvent = "
		    << (completeEvent[brs.econ()][brs.hgcroc()][readL1A&0xff]?"true":"false") << std::endl;

	while(nBxWords<6) {
    
	  // Check if inactive
	  if(!brs.isActive()) {

	    // Check for sent EOE
	    DEBUG_PRINT << " inactive, checking for EOE" << std::endl;

	    if(!brs.sentEoe()) {
	      if(nHWords>0) {
		d[0]=dh[1];
		d[0]=(d[0]<<32)+dh[0];
		d[1]=dh[3];
		d[1]=(d[1]<<32)+dh[2];
		slinkCheck.processWords(d);
		nEventWords++;
		nBxWords+=2;
	      
		nHWords=0;
		dh[0]=0;
		dh[1]=0;
		dh[2]=0;
		dh[3]=0;
dauncey's avatar
dauncey committed
1233
	      }
dauncey's avatar
dauncey committed
1234 1235 1236 1237 1238 1239 1240 1241 1242
	    
	      SlinkEoe seoe(0xce00,0x77,0xff,nEventWords);
	    
	      seoe.words(d);
	    
	      DEBUG_PRINT << " make Slink EOE 0x" << std::hex << d[1] << " " << d[0] << std::dec << std::endl;
	    
	      slinkCheck.processWords(d);
	      nBxWords+=2;
dauncey's avatar
dauncey committed
1243

dauncey's avatar
dauncey committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
	      brs.stepEoe();
	  
	    } else {
	  
	      // Check if L1A to be processed
	      DEBUG_PRINT << " inactive, checking for L1A" << std::endl;
	  
	      if(cbBoe.readAndIncrementPtr(d[0])) {
		assert(cbBoe.readAndIncrementPtr(d[1]));
	    
		DEBUG_PRINT << " read Slink BOE 0x" << std::hex << d[1] << " " << d[0] << std::dec << std::endl;
dauncey's avatar
dauncey committed
1255

dauncey's avatar
dauncey committed
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
		readL1A++;
		nEventWords=0;
	    
		slinkCheck.processWords(d);
		nBxWords+=2;
		brs.setActive();
	    
		DEBUG_PRINT << " nBxWords = " << nBxWords << std::endl;
	  
	      } else {
		// No L1A so skip rest of BX
		nBxWords=999;
	    
		DEBUG_PRINT << " no Slink BOE read, nBxWords = " << nBxWords << std::endl;
	      }
	    }
	  }
      
	  // Check if active
	  if(brs.isActive() && nBxWords<6) {
	    unsigned e(brs.econ());
	    unsigned h(brs.hgcroc());
	    DEBUG_PRINT << " active, readState e, h = " << e << "," << h << std::endl;

	    // Check if waiting for HGCROC data
	    if(!completeEvent[e][h][readL1A&0xff]) {
	      DEBUG_PRINT << " HGCROC event data not complete" << std::endl;
	      nBxWords=999;

	    } else {
	      // Data to read
	      uint64_t dw;
	      assert(pb[e].readAndIncrementPtr(h,dw));

	      DEBUG_PRINT << " read buffer, word = 0x" << std::hex << dw << " = " << std::dec << dw << std::endl;

	      dh[nHWords]=dw&0xffffffff;
	      brs.step(dh[nHWords]);
	      DEBUG_PRINT << " nHWords = " << nHWords << ", saved HGCROC word = 0x" << std::hex << dh[nHWords] << " = " << std::dec << dh[nHWords] << std::endl;

	      nHWords++;
	      if(nHWords==4) {
		d[0]=dh[1];
		d[0]=(d[0]<<32)+dh[0];
		d[1]=dh[3];
		d[1]=(d[1]<<32)+dh[2];
		slinkCheck.processWords(d);
		nEventWords++;
		nBxWords+=2;

		nHWords=0;
		dh[0]=0;
		dh[1]=0;
		dh[2]=0;
		dh[3]=0;
	      }
	  
	      // If target unchanged 
	      if(brs.econ()==e && brs.hgcroc()==h) {
		dh[nHWords]=dw>>32;
		brs.step(dh[nHWords]);
		DEBUG_PRINT << " nHWords = " << nHWords << ", saved HGCROC word = 0x" << std::hex << dh[nHWords] << " = " << std::dec << dh[nHWords] << std::endl;

		nHWords++;
		if(nHWords==4) {
		  d[0]=dh[1];
		  d[0]=(d[0]<<32)+dh[0];
		  d[1]=dh[3];
		  d[1]=(d[1]<<32)+dh[2];
		  slinkCheck.processWords(d);
		  nEventWords++;
		  nBxWords+=2;

		  nHWords=0;
		  dh[0]=0;
		  dh[1]=0;
		  dh[2]=0;
		  dh[3]=0;
		}
dauncey's avatar
dauncey committed
1335 1336
	      }

dauncey's avatar
dauncey committed
1337
	      DEBUG_PRINT << " checking for event finished, isActive = " << (brs.isActive()?"true":"false") << ", nBxWords = " << nBxWords << std::endl;
dauncey's avatar
dauncey committed
1338

dauncey's avatar
dauncey committed
1339 1340
	      if(!brs.isActive() && nBxWords<6) {
	      }
dauncey's avatar
dauncey committed
1341 1342 1343 1344 1345
	    }
	  }
	}


dauncey's avatar
dauncey committed
1346 1347 1348 1349 1350 1351 1352 1353 1354









dauncey's avatar
dauncey committed
1355 1356

    
dauncey's avatar
dauncey committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	for(;readEcon<5 && nBxWords<6;) {
	  for(;readHgcroc<18 && nBxWords<6;) {
	    if(completeEvent[readEcon][readHgcroc][readL1A&0xff]) {
	      if(nHgcrocWords==-1) {
		if(cbBoe.readAndIncrementPtr(d[0])) {
		  assert(cbBoe.readAndIncrementPtr(d[1]));
		  slinkCheck.processWords(d);
		  nBxWords+=2;
		} else nBxWords=6;
	      }

	      if(nBxWords<6) {
		pb[readEcon].readAndIncrementPtr(readHgcroc,d[nBxWords%2]);
		if(nHgcrocWords==-1) {
		  nHgcrocWords=(d[nBxWords%2]&0xffff);
		}
		nBxWords++;
		nHgcrocWord++;
dauncey's avatar
dauncey committed
1375
	    
dauncey's avatar
dauncey committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
		if(nHgcrocWord==nHgcrocWords) {
		  completeEvent[readEcon][readHgcroc][readL1A&0xff]=false;
		  doneEvent[readEcon][readHgcroc][readL1A&0xff]=true;
		  nHgcrocWords=-1;

		  readHgcroc++;
		  if(readHgcroc>=18) {
		    readHgcroc=0;
		    readEcon++;
		  }
		  if(readEcon>=5) {
		    readL1A++;
		    readEcon=0;
		  }

		  if((nBxWords%2)==1) {
		    d[1]=0;
		    nBxWords++;
		  }
		}
dauncey's avatar
dauncey committed
1396
	    
dauncey's avatar
dauncey committed
1397 1398 1399 1400 1401
		if((nBxWords%2)==0) 	      slinkCheck.processWords(d);
	    
	      }
	    } else {
	      nBxWords=6;
dauncey's avatar
dauncey committed
1402 1403 1404 1405
	    }
	  }
	}

dauncey's avatar
dauncey committed
1406 1407 1408 1409 1410
	/*
	  if(completeAllEvent) {
	  unsigned nWord(0);
	  for(readEcon<5;readEcon++) {
	  for(readHgcroc<18;readHgcroc++) {
dauncey's avatar
dauncey committed
1411
	  
dauncey's avatar
dauncey committed
1412 1413 1414
	  }
	  }
	  }
dauncey's avatar
dauncey committed
1415 1416


dauncey's avatar