EconSim.hh 17.4 KB
Newer Older
dauncey's avatar
dauncey committed
1 2 3 4 5 6 7 8
#ifndef EconSim_HH
#define EconSim_HH

#include <iostream>
#include <iomanip>
#include <cassert>

#include "Buffering/HgcrocBoe.hh"
dauncey's avatar
dauncey committed
9 10
#include "Buffering/EconArch.hh"
#include "Buffering/EconFile.hh"
dauncey's avatar
dauncey committed
11 12 13

class EconSim {
  friend class EconAna;
dauncey's avatar
dauncey committed
14

dauncey's avatar
dauncey committed
15
public:
dauncey's avatar
dauncey committed
16 17 18 19 20 21 22 23 24 25 26
  enum {
    NumberOfParallelBuffers=2
  };
  
  enum DataRandomMethod {
    NotRandom,
    Binomial,
    Flat,
    Mixed
  };

dauncey's avatar
dauncey committed
27
  enum DataFormatMethod {
dauncey's avatar
dauncey committed
28
    FixedChannelBitmap,
dauncey's avatar
dauncey committed
29 30 31 32 33 34 35 36 37 38 39
    FixedChannelLabels,
    FixedMixture,
    VariableMixture
  };

  enum DataFlowMethod {
    FixedHgcrocMap,
    VariableHgcrocMap,
    EventBuild
  };

dauncey's avatar
dauncey committed
40 41 42 43 44 45
  //EconSim(unsigned e, unsigned h=18, unsigned m=0) : fEcon(e) {
  EconSim() {
    fSlink=0;

    if(fPrintLevel>0) {	
      std::cout << "EconSim::ctor() "
dauncey's avatar
dauncey committed
46 47 48 49
		<< "Entered" << std::endl;
    }

    setFormatBreakEven();
dauncey's avatar
dauncey committed
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    fActiveInputBuffer=0;    
    fWriteElink=0;

    /*
      if(false) {

      fNumberOfBlocks=1;
    
      for(unsigned h(0);h<18;h++) {
      fHgcrocNumber[0].push_back(h);
      }
      
      for(unsigned e(0);e<7;e++) {
      fElinkNumber[0].push_back(e);
      }
      
      } else {

      fNumberOfBlocks=7;

      for(unsigned h(0);h<18;h++) {
      fHgcrocNumber[h%7].push_back(h);
      }

      for(unsigned e(0);e<7;e++) {
      fElinkNumber[e].push_back(e);
      }

      }

      if(fPrintLevel>1) {
      std::cout << "EconSim::ctor() "
      << "Number of blocks = " << fNumberOfBlocks << std::endl;

      for(unsigned block(0);block<fNumberOfBlocks;block++) {
      std::cout << " Block " << block << ", HGCROCs =";
	
      for(unsigned h(0);h<fHgcrocNumber[block].size();h++) {
      std::cout << " " << fHgcrocNumber[block][h];
      }

      std::cout << ", Elinks =";

      for(unsigned e(0);e<fElinkNumber[block].size();e++) {
      std::cout << " " << fElinkNumber[block][e];
      }

      std::cout << std::endl;
      }
      }
    */


    for(unsigned b(0);b<NumberOfParallelBuffers;b++) {
      for(unsigned block(0);block<20;block++) {
	fReadHgcrocNumber[b][block]=EconFile::MaxNumberOfHgcrocs;
	fReadWord[b][block]=0xffffffff;

	if(fPrintLevel>2) {
	  std::cout << "EconSim::ctor() "
		    << "First buffer " << b << ", block " << block
		    << ", zeroing HGCROC number " << fReadHgcrocNumber[b][block]
		    << ", word number " << fReadWord[b][block] << std::endl;
	}
      }
      for(unsigned h(0);h<EconFile::MaxNumberOfHgcrocs;h++) {
	fFirstBuffer[b][h].resize(channelsToWords(72,false));
      }
    }

    //fHgcrocToElink[h]=(h%7);
  }

  void boj(unsigned s, unsigned e, const EconFile &f, const EconArch &a) {
    fSlink=s;
    fEcon=e;
    fEconFile=&f;
    fEconArch=&a;
  }
  
#ifdef CRAP
  void initialise(unsigned m) {
    //assert(fEconFile->fNumberOfHgcrocs<=18);

    setFormatBreakEven();
    fActiveInputBuffer=0;    
dauncey's avatar
dauncey committed
136 137 138 139
    fWriteElink=0;

    // Random values for now
    /*
dauncey's avatar
dauncey committed
140 141
      for(unsigned h(0);h<fNumberOfHgcrocs;h++) {
      fEconFile->fHgcrocMean[h]=0.5*h+1;
dauncey's avatar
dauncey committed
142 143 144 145 146 147 148 149
      fHgcrocToElink[h]=(h%7);
      }
    */
    
    for(unsigned h(0);h<18;h++) {
      fFirstBuffer[0][h].resize(channelsToWords(72,false));
      fFirstBuffer[1][h].resize(channelsToWords(72,false));

dauncey's avatar
dauncey committed
150
      fEconFile->fHgcrocMean[h]=0.0;      
dauncey's avatar
dauncey committed
151 152
    }
    
dauncey's avatar
dauncey committed
153
    for(unsigned h(0);h<fEconFile->fNumberOfHgcrocs;h++) {
dauncey's avatar
dauncey committed
154
      if(m==0) {
dauncey's avatar
dauncey committed
155 156 157 158
	fEconFile->fHgcrocMean[h]=10;
	if(h<14) fEconFile->fHgcrocMean[h]=20;
	if(h< 7) fEconFile->fHgcrocMean[h]=35;
	if(h==6) fEconFile->fHgcrocMean[h]=40;
dauncey's avatar
dauncey committed
159 160
      }
      if(m==1) {
dauncey's avatar
dauncey committed
161 162 163
	fEconFile->fHgcrocMean[h]=5;
	if(h<14) fEconFile->fHgcrocMean[h]=10;
	if(h< 7) fEconFile->fHgcrocMean[h]=15;
dauncey's avatar
dauncey committed
164 165
      }
      if(m==2) {
dauncey's avatar
dauncey committed
166 167 168
	fEconFile->fHgcrocMean[h]=3;
	if(h<14) fEconFile->fHgcrocMean[h]=7;
	if(h< 7) fEconFile->fHgcrocMean[h]=10;
dauncey's avatar
dauncey committed
169 170
      }
      if(m==3) {
dauncey's avatar
dauncey committed
171 172 173
	fEconFile->fHgcrocMean[h]=2;
	if(h<14) fEconFile->fHgcrocMean[h]=4;
	if(h< 7) fEconFile->fHgcrocMean[h]=5;
dauncey's avatar
dauncey committed
174 175
      }
      if(m>=4) {
dauncey's avatar
dauncey committed
176 177 178
	fEconFile->fHgcrocMean[h]=1;
	if(h<14) fEconFile->fHgcrocMean[h]=2;
	if(h< 7) fEconFile->fHgcrocMean[h]=3;
dauncey's avatar
dauncey committed
179 180 181 182 183 184
      }

      fHgcrocToElink[h]=(h%7);
    }

    std::cout << std::endl << "Econ::ctor() number of HGCROCs = "
dauncey's avatar
dauncey committed
185
	      << fEconFile->fNumberOfHgcrocs << std::endl;
dauncey's avatar
dauncey committed
186 187 188 189
    
    double elinkNum[7]={0,0,0,0,0,0,0};
    double elinkMean[7]={0,0,0,0,0,0,0};
    
dauncey's avatar
dauncey committed
190
    for(unsigned h(0);h<fEconFile->fNumberOfHgcrocs;h++) {
dauncey's avatar
dauncey committed
191
      std::cout << " HGCROC " << std::setw(2) << h
dauncey's avatar
dauncey committed
192
		<< ", average number of channels = " << fEconFile->fHgcrocMean[h]
dauncey's avatar
dauncey committed
193 194 195
		<< ", assigned to Elink = " << fHgcrocToElink[h] << std::endl;
      
      elinkNum[fHgcrocToElink[h]]++;
dauncey's avatar
dauncey committed
196
      elinkMean[fHgcrocToElink[h]]+=fEconFile->fHgcrocMean[h];
dauncey's avatar
dauncey committed
197 198
    }

dauncey's avatar
dauncey committed
199
    /*    
dauncey's avatar
dauncey committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    fTotalNum=0;
    fTotalMean=0;
    
    for(unsigned e(0);e<7;e++) {
      fTotalNum+=elinkNum[e];
      fTotalMean+=elinkMean[e];
      
      std::cout << " Elink " << e << ", number of HGCROCs = " << elinkNum[e]
		<< ", total average number of channels = "
		<< elinkMean[e] << ", average number of 32-bit words = "
		<< 3*elinkNum[e]+0.5*elinkMean[e]
		<< std::endl;
    }    
    
    fTotalWords=3*fTotalNum+0.5*fTotalMean;
    std::cout << " ECON, total average number of channels = "
	      << fTotalMean << ", average number of 32-bit words = "
	      << fTotalWords
	      << std::endl;
dauncey's avatar
dauncey committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    */
  }
#endif

  void slinkNumber(unsigned s) {
    fSlink=s;
  }
  
  void econNumber(unsigned e) {
    fEcon=e;
  }
  
  void econFile(const EconFile *e) {
    fEconFile=e;
  }
  
  const EconFile& econFile() const {
    return *fEconFile;
dauncey's avatar
dauncey committed
237 238 239 240
  }
  
  double mean(unsigned h) {
    assert(h<18);
dauncey's avatar
dauncey committed
241
    return fEconFile->fHgcrocMean[h];
dauncey's avatar
dauncey committed
242
  }
dauncey's avatar
dauncey committed
243 244
  
  void processZs(uint8_t bx, uint8_t l1a) {
dauncey's avatar
dauncey committed
245
    if(fPrintLevel>3) {
dauncey's avatar
dauncey committed
246 247
      std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processZs(" << unsigned(bx)
		<< "," << unsigned(l1a) << ") "
dauncey's avatar
dauncey committed
248 249 250 251
		<< "Entered" << std::endl;
    }
    
    TRandom &random(Random::random());
dauncey's avatar
dauncey committed
252 253 254
    bool binomialOrFlat;
  
    for(unsigned h(0);h<fEconFile->fNumberOfHgcrocs;h++) {
dauncey's avatar
dauncey committed
255

dauncey's avatar
dauncey committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
      // Switch for different random methods
    
      if(fDataRandomMethod==NotRandom) {
	fNumberOfChannels[h]=unsigned(fEconFile->fHgcrocMean[h]+0.5);

      } else if(fDataRandomMethod==Binomial) {
	fNumberOfChannels[h]=random.Binomial(72,fEconFile->fHgcrocMean[h]/72.0);

      } else if(fDataRandomMethod==Flat) {
	int im(fEconFile->fHgcrocMean[h]+0.5);
	if(im>36) {
	  fNumberOfChannels[h]=2*im-72+random.Integer(145-2*im);
	} else {
	  fNumberOfChannels[h]=random.Integer(2*im+1);
	}

      } else if(fDataRandomMethod==Mixed) {
	binomialOrFlat=(random.Integer(2)==0);
	if(binomialOrFlat) {
	  fNumberOfChannels[h]=random.Binomial(72,fEconFile->fHgcrocMean[h]/72.0);
	} else {
	  int im(fEconFile->fHgcrocMean[h]+0.5);
	  if(im>36) {
	    fNumberOfChannels[h]=2*im-72+random.Integer(145-2*im);
	  } else {
	    fNumberOfChannels[h]=random.Integer(2*im+1);
	  }
	}
dauncey's avatar
dauncey committed
284
      
dauncey's avatar
dauncey committed
285 286 287 288 289 290 291
      } else {
	assert(false);
      }

      // Switch for different format methods
      
      if(fDataFormatMethod==FixedChannelBitmap) {
dauncey's avatar
dauncey committed
292 293 294 295 296 297
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],true);

      } else if(fDataFormatMethod==FixedChannelLabels) {
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],false);

      } else if(fDataFormatMethod==FixedMixture) {
dauncey's avatar
dauncey committed
298
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],fEconFile->fHgcrocMean[h]>fFormatBreakEven);
dauncey's avatar
dauncey committed
299 300 301 302 303 304 305 306 307
	
      } else if(fDataFormatMethod==VariableMixture) {
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],fNumberOfChannels[h]>fFormatBreakEven);

      } else {
	assert(false);
      }

      if(fPrintLevel>4) {
dauncey's avatar
dauncey committed
308 309
	std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processZs(" << unsigned(bx)
		  << "," << unsigned(l1a) << ") "
dauncey's avatar
dauncey committed
310 311 312 313 314
		  << "Hgcroc " << h << ", number of channels = "
		  << fNumberOfChannels[h] << ", words = "
		  << fNumberOfWords[h] << std::endl;
      }
      
dauncey's avatar
dauncey committed
315
      // Create HGCROC header
dauncey's avatar
dauncey committed
316

dauncey's avatar
dauncey committed
317
      HgcrocBoe hboe(fEcon,h,bx,l1a,fNumberOfWords[h]);
dauncey's avatar
dauncey committed
318 319

      if(fPrintLevel>4) {
dauncey's avatar
dauncey committed
320 321 322
	std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processZs(" << unsigned(bx)
		  << "," << unsigned(l1a) << ") "
		  << "Hgcroc " << h << " BOE" << std::endl;
dauncey's avatar
dauncey committed
323 324 325
	hboe.print();
      }
    
dauncey's avatar
dauncey committed
326 327 328 329 330 331
      // Write data into selected buffer

      assert((fActiveInputBuffer&0xff)==l1a);
      fFirstBuffer[fActiveInputBuffer%NumberOfParallelBuffers][h].resize(fNumberOfWords[h]);
      fFirstBuffer[fActiveInputBuffer%NumberOfParallelBuffers][h][0]=hboe.word();

dauncey's avatar
dauncey committed
332
      for(unsigned i(1);i<fNumberOfWords[h];i++) {
dauncey's avatar
dauncey committed
333
	fFirstBuffer[fActiveInputBuffer%NumberOfParallelBuffers][h][i]=(l1a<<24)+i;
dauncey's avatar
dauncey committed
334 335
      }
    }
dauncey's avatar
dauncey committed
336 337
    
    // Set counters to zero for this buffer
dauncey's avatar
dauncey committed
338

dauncey's avatar
dauncey committed
339 340 341 342
    for(unsigned block(0);block<fEconArch->fNumberOfBlocks;block++) {
      fReadHgcrocNumber[fActiveInputBuffer%NumberOfParallelBuffers][block]=0;
      fReadWord[fActiveInputBuffer%NumberOfParallelBuffers][block]=0;
    }
dauncey's avatar
dauncey committed
343
    
dauncey's avatar
dauncey committed
344
    fActiveInputBuffer++;
dauncey's avatar
dauncey committed
345 346 347 348
  }

  void processBx(uint32_t *a) {

dauncey's avatar
dauncey committed
349 350
    // Switch for different dataflow methods

dauncey's avatar
dauncey committed
351 352
    if(fDataFlowMethod==FixedHgcrocMap) {

dauncey's avatar
dauncey committed
353
#ifdef NOT_YET
dauncey's avatar
dauncey committed
354
      
dauncey's avatar
dauncey committed
355 356
      //  4 words per BX for 7 eLinks
      unsigned nWords(4);
dauncey's avatar
dauncey committed
357 358
      
      if(fPrintLevel>3) {
dauncey's avatar
dauncey committed
359 360 361
	std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
		  << "Data flow method = fixed HGCROC map, reading "
		  << nWords << " words per BX"	<< std::endl;
dauncey's avatar
dauncey committed
362
      }
dauncey's avatar
dauncey committed
363 364 365
	    
      for(unsigned b(0);b<NumberOfParallelBuffers;b++) {
	if(b!=(fActiveInputBuffer%NumberOfParallelBuffers)) {
dauncey's avatar
dauncey committed
366
	  
dauncey's avatar
dauncey committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	  for(unsigned e(0);e<7;e++) {
	    if(fReadHgcroc[b]<fEconFile->fNumberOfHgcrocs) {
	      
	      for(unsigned w(0);w<nWords;w++) {
		assert(fReadHgcroc[b]<fEconFile->fNumberOfHgcrocs);
		assert(fReadWord[b]<fFirstBuffer[b][fReadHgcroc[b]].size());
		
		if(fPrintLevel>4) {
		  std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
			    << "Copying BX word " << w << " from first buffer " << b
			    << ", HGCROC " << fReadHgcroc[b] << ", word " << fReadWord[b]
			    << " of " << fFirstBuffer[b][fReadHgcroc[b]].size() << " = "
			    << std::hex << fFirstBuffer[b][fReadHgcroc[b]][fReadWord[b]]
			    << std::dec << std::endl;
		}
		assert(fElink[fHgcrocToElink[h]].writeAndIncrementPtr(fFirstBuffer[b][fReadHgcrocNumber[b]][fReadWord[b]]));
	      }
	    }
dauncey's avatar
dauncey committed
385 386 387
	  }
	}
      }
dauncey's avatar
dauncey committed
388 389 390

      // Add zero padding word if odd
      //if((nWords%2)==1) assert(fElink[fHgcrocToElink[h]].writeAndIncrementPtr(0));
dauncey's avatar
dauncey committed
391 392 393
      
      uint64_t d;
      for(unsigned e(0);e<7;e++) {
dauncey's avatar
dauncey committed
394
	if(fElink[e].readAndIncrementPtr(d)) a[e]=(d&0xffffffff);
dauncey's avatar
dauncey committed
395 396 397
	else a[e]=0;

	if(fPrintLevel>4) {
dauncey's avatar
dauncey committed
398 399
	  std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
		    << "Sending eLink " << e << " = "
dauncey's avatar
dauncey committed
400 401 402 403
		    << std::hex << a[e]
		    << std::dec << std::endl;
	}
      }
dauncey's avatar
dauncey committed
404 405

#endif
dauncey's avatar
dauncey committed
406
      
dauncey's avatar
dauncey committed
407 408 409 410 411 412 413 414 415 416
    } else if(fDataFlowMethod==VariableHgcrocMap) {

      unsigned nWords(0);
      
      if(fPrintLevel>3) {
	std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
		  << "Data flow method = variable HGCROC map, reading "
		  << nWords << " words per BX"	<< std::endl;
      }
	    
dauncey's avatar
dauncey committed
417 418
      assert(false);

dauncey's avatar
dauncey committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
      
    } else if(fDataFlowMethod==EventBuild) {
      
      // 24 words per BX for 2 parallel buffers = 960 MHz
      // 12 words per BX for 3 parallel buffers = 480 MHz
      //  8 words per BX for 4 parallel buffers = 320 MHz
      //unsigned nWords(24/(NumberOfParallelBuffers-1));


      // For one block, must be > 55 words x 18 HGCROCs/44 BX = 22.5; also more than output = 7 words/BX
      //  OR
      // For one block, must be > 39 words x 18 HGCROCs/44 BX = 15.95; also more than output = 7 words/BX
      // For seven blocks, must be > 55 words x 3 HGCROCs/44 BX = 3.75; also more than output = 1 word/BX
      //  OR
      // For seven blocks, must be > 39 words x 3 HGCROCs/44 BX = 2.66; also more than output = 1 word/BX

      unsigned nWords(fEconArch->fNumberOfBlocks==1?24:4);
      //unsigned nWords(fEconArch->fNumberOfBlocks==1?16:3);
      
      if(fPrintLevel>3) {
	std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
		  << "Data flow method = event build, reading "
		  << nWords << " words per BX"	<< std::endl;
dauncey's avatar
dauncey committed
442 443
      }
	    
dauncey's avatar
dauncey committed
444
      for(unsigned block(0);block<fEconArch->fNumberOfBlocks;block++) {
dauncey's avatar
dauncey committed
445

dauncey's avatar
dauncey committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	for(unsigned b(0);b<NumberOfParallelBuffers;b++) {
	  if(b!=(fActiveInputBuffer%NumberOfParallelBuffers)) {

	    if(fPrintLevel>4) {
	      std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
			<< "Reading HGCROC number " << fReadHgcrocNumber[b][block]
			<< " < " << fEconArch->fHgcrocNumber[block].size()
			<< ", word number " << fReadWord[b][block] << std::endl;
	    }
	    
	    //if(fReadHgcrocNumber[b]<fEconFile->fNumberOfHgcrocs) {
	    if(fReadHgcrocNumber[b][block]<fEconArch->fHgcrocNumber[block].size()) {

	      for(unsigned w(0);w<nWords;w++) {
		//assert(fReadHgcroc[b]<fEconFile->fNumberOfHgcrocs);
		
		unsigned readHgcroc(fEconArch->fHgcrocNumber[block][fReadHgcrocNumber[b][block]]);
		std::vector<uint32_t> &readBuffer(fFirstBuffer[b][readHgcroc]); // Convenient
		assert(fReadWord[b][block]<readBuffer.size());
	      
		if(fPrintLevel>4) {
		  std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
			    << "Copying BX word " << w << " from block " << block << ", first buffer " << b
			    << ", HGCROC " << readHgcroc << ", word " << fReadWord[b][block]
			    << " of " << readBuffer.size() << " = "
			    << std::hex << readBuffer[fReadWord[b][block]]
			    << std::dec << std::endl;
		}

		// Write word to FIFO
		
		assert(fEventBuffer[block].writeAndIncrementPtr(readBuffer[fReadWord[b][block]]));
		
		// Check if completed all words of this HGCROC
		
		fReadWord[b][block]++;
		if(fReadWord[b][block]==readBuffer.size()) {
		  fReadWord[b][block]=0;
		  fReadHgcrocNumber[b][block]++;
		  
		  // Check if completed all HGCROCs
		  
		  if(fReadHgcrocNumber[b][block]==fEconArch->fHgcrocNumber[block].size()) w=nWords;
		}
	      }
	    }
	  }
	}

	uint64_t d;
	for(unsigned e(0);e<fEconArch->fElinkNumber[block].size();e++) {
	  if(fEventBuffer[block].readAndIncrementPtr(d)) a[fEconArch->fElinkNumber[block][e]]=(d&0xffffffff);
	  else a[fEconArch->fElinkNumber[block][e]]=0;
	}
      }

      if(fPrintLevel>4) {
	for(unsigned e(0);e<7;e++) {
	  std::cout << "EconSim[" << fSlink << "][" << fEcon << "]::processBx() "
		    << "Sending eLink " << e << " = "
		    << std::hex << a[e]
		    << std::dec << std::endl;
	}
      }
        
    } else {
      assert(false);
dauncey's avatar
dauncey committed
513 514 515
    }
  }

dauncey's avatar
dauncey committed
516 517
  unsigned numberOfHgcrocs() const {
    return fEconFile->fNumberOfHgcrocs;
dauncey's avatar
dauncey committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
  }

  unsigned space(unsigned elink) {
    assert(elink<7);
    return fElink[elink].space();
  }

  unsigned used(unsigned elink) {
    assert(elink<7);
    return fElink[elink].used();
  }

  unsigned numberOfChannels(unsigned h) const {
    assert(h<18);
    return fNumberOfChannels[h];
  }

  unsigned numberOfWords(unsigned h) const {
    assert(h<18);
    return fNumberOfWords[h];
  }

  void reset() {
    for(unsigned e(0);e<7;e++) {
      fElink[e].reset();
    }
  }

  static unsigned channelsToWords(unsigned c, bool map) {
    if(map) return 3+(c+1)/2;
    else    return 1+3*(c+1)/4;
  }
  /*
dauncey's avatar
dauncey committed
551 552 553
    unsigned channelsToWords(unsigned c, double m) const {
    if(fDataFormatMethod==FixedChannelBitmap) {
    return channelsToWords(c,true);
dauncey's avatar
dauncey committed
554 555
      
    } else if(fDataFormatMethod==FixedChannelLabels) {
dauncey's avatar
dauncey committed
556
    return channelsToWords(c,false);
dauncey's avatar
dauncey committed
557 558
      
    } else if(fDataFormatMethod==FixedMixture) {
dauncey's avatar
dauncey committed
559
    return channelsToWords(c,m>fFormatBreakEven);
dauncey's avatar
dauncey committed
560 561
      
    } else if(fDataFormatMethod==VariableMixture) {
dauncey's avatar
dauncey committed
562
    return channelsToWords(c,c>fFormatBreakEven);
dauncey's avatar
dauncey committed
563 564
      
    } else {
dauncey's avatar
dauncey committed
565
    assert(false);
dauncey's avatar
dauncey committed
566 567
    }
    return -1;
dauncey's avatar
dauncey committed
568
    }
dauncey's avatar
dauncey committed
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
  */
  
  static void setFormatBreakEven() {
    if(fFormatBreakEven==0) {    
      if(fPrintLevel>1) {
	std::cout << "EconSim::setFormatBreakEven() " 
		  << "Entered" << std::endl;
      }
      
      unsigned wMap,wLabel;
      for(unsigned c(0);c<72;c++) {
	wMap=channelsToWords(c,true);
	wLabel=channelsToWords(c,false);

	if(fPrintLevel>1) {
	  std::cout << "EconSim::setFormatBreakEven() " 
		    << "Channels = " << c << ", map words = "
		    << wMap << ", label words = " << wLabel
		    << std::endl;
	}

	if(wLabel<=wMap) fFormatBreakEven=c;
	else break;
      }

      if(fPrintLevel>0) {
dauncey's avatar
dauncey committed
595
	std::cout << "EconSim::setFormatBreakEven() "
dauncey's avatar
dauncey committed
596 597 598 599 600 601 602 603 604
		  << "Break-even channel number = " << fFormatBreakEven
		  << std::endl;
      }
    }
  }

  unsigned econNumber() const {
    return fEcon;
  }
dauncey's avatar
dauncey committed
605 606 607 608

  unsigned econSlink() const {
    return fSlink;
  }
dauncey's avatar
dauncey committed
609
  
dauncey's avatar
dauncey committed
610 611 612 613
  const EconArch& econArch() const {
    return *fEconArch;
  }

dauncey's avatar
dauncey committed
614 615
public:
  static unsigned fPrintLevel;
dauncey's avatar
dauncey committed
616 617

  static DataRandomMethod fDataRandomMethod;
dauncey's avatar
dauncey committed
618 619 620
  static DataFormatMethod fDataFormatMethod;
  static DataFlowMethod fDataFlowMethod;
  static unsigned fFormatBreakEven;
dauncey's avatar
dauncey committed
621
  /*
dauncey's avatar
dauncey committed
622 623 624
  double fTotalNum;
  double fTotalMean;
  double fTotalWords;
dauncey's avatar
dauncey committed
625 626
  */
  
dauncey's avatar
dauncey committed
627 628
protected:
private:
dauncey's avatar
dauncey committed
629 630 631 632 633 634 635 636
  unsigned fSlink;
  unsigned fEcon;
  const EconFile *fEconFile;
  const EconArch *fEconArch;

  //unsigned fNumberOfHgcrocs;
  //double fHgcrocMean[18];
  
dauncey's avatar
dauncey committed
637
  //unsigned fHgcrocToElink[18];
dauncey's avatar
dauncey committed
638
  CircularBuffer<1> fElink[7];
dauncey's avatar
dauncey committed
639
  CircularBuffer<16> fEventBuffer[7];
dauncey's avatar
dauncey committed
640 641 642 643

  unsigned fNumberOfChannels[18];
  unsigned fNumberOfWords[18];

dauncey's avatar
dauncey committed
644 645 646 647
  //unsigned fReadHgcroc[NumberOfParallelBuffers];
  //unsigned fReadWord[NumberOfParallelBuffers];
  unsigned fReadHgcrocNumber[NumberOfParallelBuffers][20];
  unsigned fReadWord[NumberOfParallelBuffers][20];
dauncey's avatar
dauncey committed
648
  unsigned fWriteElink;
dauncey's avatar
dauncey committed
649 650 651 652

  //unsigned fNumberOfBlocks;
  std::vector<unsigned> fHgcrocNumber[7];
  std::vector<unsigned> fElinkNumber[7];
dauncey's avatar
dauncey committed
653
  
dauncey's avatar
dauncey committed
654 655
  unsigned fActiveInputBuffer;
  std::vector<uint32_t> fFirstBuffer[NumberOfParallelBuffers][18];
dauncey's avatar
dauncey committed
656 657 658 659
};

unsigned EconSim::fPrintLevel(1);

dauncey's avatar
dauncey committed
660 661 662
EconSim::DataRandomMethod EconSim::fDataRandomMethod(EconSim::Binomial);
EconSim::DataFormatMethod EconSim::fDataFormatMethod(EconSim::FixedChannelBitmap);
EconSim::DataFlowMethod EconSim::fDataFlowMethod(EconSim::FixedHgcrocMap);unsigned EconSim::fFormatBreakEven(0);
dauncey's avatar
dauncey committed
663 664

#endif