EconSim.hh 9.63 KB
Newer Older
dauncey's avatar
dauncey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 113 114 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 217 218 219 220 221 222 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 248 249 250 251 252 253 254 255 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
#ifndef EconSim_HH
#define EconSim_HH

// DO BINOMIAL, FLAT, CONSTANT RANDOM METHODS


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

#include "Buffering/HgcrocBoe.hh"

class EconSim {
  friend class EconAna;
public:
  enum DataFormatMethod {
    FixedChannelMap,
    FixedChannelLabels,
    FixedMixture,
    VariableMixture
  };

  enum DataFlowMethod {
    FixedHgcrocMap,
    VariableHgcrocMap,
    EventBuild
  };

  EconSim(unsigned e, unsigned h=18, unsigned m=0) : fEcon(e), fHgcroc(h) {
    if(fPrintLevel>0) {
      std::cout << "EconSim::ctor(" << e << "," << h << "," << m << ") "
		<< "Entered" << std::endl;
    }
    assert(fHgcroc<=18);

    setFormatBreakEven();
    fActiveBuffer=0;    
    fReadHgcroc=18;
    fReadWord=0xffffffff;
    fWriteElink=0;

    // Random values for now
    /*
      for(unsigned h(0);h<fHgcroc;h++) {
      fMean[h]=0.5*h+1;
      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));

      fMean[h]=0.0;      
    }
    
    for(unsigned h(0);h<fHgcroc;h++) {
      if(m==0) {
	fMean[h]=10;
	if(h<14) fMean[h]=20;
	if(h< 7) fMean[h]=35;
      }
      if(m==1) {
	fMean[h]=5;
	if(h<14) fMean[h]=10;
	if(h< 7) fMean[h]=15;
      }
      if(m==2) {
	fMean[h]=3;
	if(h<14) fMean[h]=7;
	if(h< 7) fMean[h]=10;
      }
      if(m==3) {
	fMean[h]=2;
	if(h<14) fMean[h]=4;
	if(h< 7) fMean[h]=5;
      }
      if(m>=4) {
	fMean[h]=1;
	if(h<14) fMean[h]=2;
	if(h< 7) fMean[h]=3;
      }

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

    std::cout << std::endl << "Econ::ctor() number of HGCROCs = "
	      << fHgcroc << std::endl;
    
    double elinkNum[7]={0,0,0,0,0,0,0};
    double elinkMean[7]={0,0,0,0,0,0,0};
    
    for(unsigned h(0);h<fHgcroc;h++) {
      std::cout << " HGCROC " << std::setw(2) << h
		<< ", average number of channels = " << fMean[h]
		<< ", assigned to Elink = " << fHgcrocToElink[h] << std::endl;
      
      elinkNum[fHgcrocToElink[h]]++;
      elinkMean[fHgcrocToElink[h]]+=fMean[h];
    }

    
    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;
  }
  
  double mean(unsigned h) {
    assert(h<18);
    return fMean[h];
  }
    
  void processZs(uint8_t bx, uint8_t l1a, bool randomChannels=true) {
    if(fPrintLevel>3) {
      std::cout << "EconSim" << fEcon << "::processZs(" << unsigned(bx)
		<< "," << unsigned(l1a) << ","
		<< (randomChannels?"true":"false") << ") "
		<< "Entered" << std::endl;
    }
    
    TRandom &random(Random::random());

    for(unsigned h(0);h<fHgcroc;h++) {
      if(randomChannels) fNumberOfChannels[h]=random.Binomial(72,fMean[h]/72.0);
      else fNumberOfChannels[h]=unsigned(fMean[h]+0.5);
      
      if(fDataFormatMethod==FixedChannelMap) {
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],true);

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

      } else if(fDataFormatMethod==FixedMixture) {
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],fMean[h]>fFormatBreakEven);
	
      } else if(fDataFormatMethod==VariableMixture) {
	fNumberOfWords[h]=channelsToWords(fNumberOfChannels[h],fNumberOfChannels[h]>fFormatBreakEven);

      } else {
	assert(false);
      }

      if(fPrintLevel>4) {
	std::cout << "EconSim" << fEcon << "::processZs(" << unsigned(bx)
		  << "," << unsigned(l1a) << ","
		  << (randomChannels?"true":"false") << ") "
		  << "Hgcroc " << h << ", number of channels = "
		  << fNumberOfChannels[h] << ", words = "
		  << fNumberOfWords[h] << std::endl;
      }
      
      HgcrocBoe hboe(fEcon,h,bx,l1a,fNumberOfWords[h]);

      assert((fActiveBuffer&0xff)==l1a);

      fFirstBuffer[0][h][0]=hboe.word();

      //assert(fElink[fHgcrocToElink[h]].writeAndIncrementPtr(hboe.word()));

      if(fPrintLevel>4) {
	std::cout << "EconSim" << fEcon << "::processZs(" << unsigned(bx)
		  << "," << unsigned(l1a) << ","
		  << (randomChannels?"true":"false") << ") "
		  << "Hgcroc " << h << " BOE written" << std::endl;
	hboe.print();
      }
    
      for(unsigned i(1);i<fNumberOfWords[h];i++) {
	fFirstBuffer[0][h][i]=(l1a<<24)+i;
	//assert(fElink[fHgcrocToElink[h]].writeAndIncrementPtr(i));
      }

      // Add zero padding word if odd
      //if((nWords%2)==1) assert(fElink[fHgcrocToElink[h]].writeAndIncrementPtr(0));
    }

    fReadHgcroc=0;
    fReadWord=0;
    
    fActiveBuffer++;
  }

  void processBx(uint32_t *a) {

    // SWITCH DEPENDING ON DATAFLOWMETHOD
    if(fDataFlowMethod==FixedHgcrocMap) {
      

    } else if(fDataFlowMethod==VariableHgcrocMap) {
      
      
    } else if(fDataFlowMethod==EventBuild) {
      
      if(fPrintLevel>3) {
	std::cout << "EconSim" << fEcon << "::processBx() "
		  << "Data flow method = event build, reading HGCROC "
		  << fReadHgcroc << ", word " << fReadWord << std::endl;
      }
      
      for(unsigned w(0);w<24 && fReadHgcroc<fHgcroc;) {
	for(;fReadWord<fNumberOfWords[fReadHgcroc] && w<24;fReadWord++) {
	  
	  if(fPrintLevel>4) {
	    std::cout << "EconSim" << fEcon << "::processBx() "
		      << "Writing word " << w << " from HGCROC " << fReadHgcroc
		      << ", word " << fReadWord
		      << " of " << fNumberOfWords[fReadHgcroc] << " = "
		      << std::hex << fFirstBuffer[0][fReadHgcroc][fReadWord]
		      << std::dec << std::endl;
	  }
	  
	  assert(fEventBuffer.writeAndIncrementPtr(fFirstBuffer[0][fReadHgcroc][fReadWord]));
	  w++;
	}
	
	if(fReadWord==fNumberOfWords[fReadHgcroc]) {
	  fReadWord=0;
	  fReadHgcroc++;
	}
      }
      
      uint64_t d;
      for(unsigned e(0);e<7;e++) {
	if(fEventBuffer.readAndIncrementPtr(d)) a[e]=(d&0xffffffff);
	else a[e]=0;

	if(fPrintLevel>4) {
	  std::cout << "EconSim" << fEcon << "::processBx() "
		    << "Reading eLink " << e << " = "
		    << std::hex << a[e]
		    << std::dec << std::endl;
	}
      }
      
    } else {
      assert(false);
    }
      /*
      if(nReadHgcroc<18) {
	//for(unsigned n

	assert(fElink[fWriteElink].writeAndIncrementPtr(fFirstBuffer[fActiveBuffer%2][fReadHgcroc][fReadWord]));
      }
    }
	    
    // Copy data from first buffers to FIFO
    

    //    assert(fElink[fHgcrocToElink[h]].writeAndIncrementPtr(fFirstBuffer[fActiveBuffer%2][h][n]));
    */
    /*
    uint64_t d;
    for(unsigned e(0);e<7;e++) {
      if(fElink[e].readAndIncrementPtr(d)) a[e]=(d&0xffffffff);
      else a[e]=0;
    }
    */
  }

  unsigned nHgcroc() const {
    return fHgcroc;
  }

  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;
  }
  /*
  unsigned channelsToWords(unsigned c, double m) const {
    if(fDataFormatMethod==FixedChannelMap) {
      return channelsToWords(c,true);
      
    } else if(fDataFormatMethod==FixedChannelLabels) {
      return channelsToWords(c,false);
      
    } else if(fDataFormatMethod==FixedMixture) {
      return channelsToWords(c,m>fFormatBreakEven);
      
    } else if(fDataFormatMethod==VariableMixture) {
      return channelsToWords(c,c>fFormatBreakEven);
      
    } else {
      assert(false);
    }
    return -1;
  }
  */
  
  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) {
	std::cout << "EconSim::setFormatBreakEvent() "
		  << "Break-even channel number = " << fFormatBreakEven
		  << std::endl;
      }
    }
  }

  unsigned econNumber() const {
    return fEcon;
  }
  
public:
  static unsigned fPrintLevel;
  static DataFormatMethod fDataFormatMethod;
  static DataFlowMethod fDataFlowMethod;
  static unsigned fFormatBreakEven;

  double fTotalNum;
  double fTotalMean;
  double fTotalWords;

protected:
private:
  const unsigned fEcon;
  const unsigned fHgcroc;
  double fMean[18];
  unsigned fHgcrocToElink[18];
  CircularBuffer<1> fElink[7];
  CircularBuffer<16> fEventBuffer;

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

  unsigned fReadHgcroc;
  unsigned fReadWord;
  unsigned fWriteElink;
  
  unsigned fActiveBuffer;
  std::vector<uint32_t> fFirstBuffer[2][18];
};

unsigned EconSim::fPrintLevel(1);

EconSim::DataFormatMethod EconSim::fDataFormatMethod(EconSim::FixedChannelMap);
EconSim::DataFlowMethod EconSim::fDataFlowMethod(EconSim::FixedHgcrocMap);
unsigned EconSim::fFormatBreakEven(0);

#endif