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
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 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
 513
 514
 515
 516
 517
 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
 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
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 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
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
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
1193
1194
1195
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
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
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
1335
#include <stdio.h>


#include <stdio.h>
#include "L2gammaAlgo.h"
#include "L2gammaResult2006.h"

#include "bemcConstants.h"
#include "eemcConstants.h"

void L2gammaAlgo::setLogFile( const char *fname){ mLogFile=fopen(fname,"w"); }
void L2gammaAlgo::setHistFile( const char *fname  ){ mHistFile=fopen(fname,"w"); }

//#define DEBUG
//#define DEBUG_PATCH_THRESHOLDS
// ----------------------------------------------------------------------------
L2gammaAlgo::L2gammaAlgo(const char* name, L2EmcDb* db, char* outDir, int resOff) 
  :  L2VirtualAlgo( name,  db,  outDir, resOff)
{

  int geom,  thresh; // temporary fix,JB
#if 1
  if(strstr(name,"etow_gamma")) {
    geom= L2gammaAlgo::kEEmcAlgo; thresh = L2gammaAlgo::kThresh1;
  } else if(strstr(name,"etow_ht2")) {
   geom= L2gammaAlgo::kEEmcAlgo; thresh = L2gammaAlgo::kThresh2;
 } else if(strstr(name,"btow_gamma")) {
   geom= L2gammaAlgo::kBEmcAlgo; thresh = L2gammaAlgo::kThresh1;
 } else if(strstr(name,"btow_ht2")) {
   geom= L2gammaAlgo::kBEmcAlgo; thresh = L2gammaAlgo::kThresh2;
 } else {
   printf("undefined config of L2gamma-algo ->%s<-, abort L2main\n",name);
   assert(1==2);
 }
#endif

  
  printf("L2gammaEmCall2006 instantiated geom=%d thresh=%d logpath=%s\n",geom,thresh,mOutDir);
  int I_par[]={
    1, // 0=bemc, 1=eemc
    0, // prescale
    0, // free
    0, // free
    0  // free
  };
  float F_par[]={
    2.50, // high tower threshold
    3.50, // 3x3 patch threshold
    0.,   // free
    0.,   // free
    0.    // free
  };  
  mThresholdLevel=thresh;
  init(geom,I_par,F_par);
  mL2input=0;
  mPrescale=0;

}


// ----------------------------------------------------------------------------
//
// perform initialization at L2 main program startup not
// done in the constructor
//
void L2gammaAlgo::init(int geom, int I_par[5], float F_par[5] )
{

  printf("+ init geom=%d\n",geom);

  mLogFile=0;
  mHistFile=0;
  mUseOfflineGains = I_par[0];
  mUseBbc=false;
  setTowerThreshold( F_par[0] );
  setPatchThreshold( F_par[1] );

  for ( int i=0;i<5;i++ )
    {
      mDefaultI_par[i]=I_par[i];
      mDefaultF_par[i]=F_par[i];
    }
  
  if ( I_par[1] ) mPrescale=I_par[1];

  /* 
   * Select endcap or barrel based on the geom flag.
   */
  mEEmc=geom;mBEmc=!geom;

  mNumEtas = (mEEmc)? kEEmcNumEtas : kBEmcNumEtas; /* number of etabins */
  mNumPhis = (mEEmc)? kEEmcNumPhis : kBEmcNumPhis; /* number of phi bins */
  mNumSubs = (mEEmc)? kEEmcNumSubs : kBEmcNumSubs; /* number of subsectors */
  mNumSecs = (mEEmc)? kEEmcNumSecs : kBEmcNumSecs; /* number of sectors */
  
  mNumTower = (mEEmc)? kEEmcNumTower : kBEmcNumTower;
  mNumClust = mNumTower;
  mNumRdo   = mNumTower;
  mEtaBins  = (mEEmc)? kEEmcEtaBins : kBEmcEtaBins;

  mMaxADC = (mEEmc)? kEEmcMaxADC : kBEmcMaxADC;
  mMaxET  = (mEEmc)? kEEmcMaxET  : kBEmcMaxET;
  mIdealGainT = (mEEmc)? kEEmcIdealGainT : kBEmcIdealGainT;

  const char *names[]={"bemc","eemc"};

  printf("L2gammaAlgo v0.92 (prociutto)                   \n");
  printf("registering new threshold for %4s \n",names[geom]);

#ifdef DEBUGIT
  printf("                                                         \n");

  printf("============================================================================\n");
  //  Jason, I am commenting this for the last time.
  // there is no benefit of rinting this on the L2 console.
  // If this printout of any value write it to a file saved on /data/... disk as all other outputs. Then at least you can retrieve it after the run.
  //Or activate this only in the debug mode - you can have GUI param for debug - if you want

  printf("algorithm residing at %p\n",this);
  printf("l2algo used as input at %p\n",mL2input);
  printf("\n");
  printf("\n");
  printf("mNumEtas  = %-2d\n",mNumEtas);
  printf("mNumPhis  = %-4d\n",mNumPhis);
  printf("mNumSubs  = %-4d\n",mNumSubs);
  printf("mNumSecs  = %-4d\n",mNumSecs);
  printf("mNumTower = %-4d\n",mNumTower);
  printf("mNumClust = %-4d\n",mNumClust);
  printf("mNumRdo   = %-4d\n",mNumRdo);
  printf("mEtaBins  = {");
  for ( int i=0;i<mNumEtas+1;i++ ) { 
    printf("%4.2f, ",mEtaBins[i]);
    if ( !((i+1)%4) ) printf("\n               ");
  }
  printf("X}\n");
  printf("mMaxADC   = %4d\n",mMaxADC);
  printf("mMaxET    = %4.1f\n",mMaxET);
  printf("mIdealGainT = %5.3f\n",mIdealGainT);
  printf("\n");
  printf("============================================================================\n");
  
#endif

  // book "histograms" for QA (clears from previous run)
  jbook();

}



// ----------------------------------------------------------------------------
//
// runtime initialization
//
// at this stage, time is not overly critical.  so we will take 
// the time to do most of the hard work.  we will do two things:
//
// 1) establish a high tower threshold for all 720 towers in
//    terms of adc.  This will be the high tower threshold (in
//    adc) plus pedestal.  Thus we will not need to do a ped
//    subtraction in the event loop.
//
// 2) We implement a 3x3 tower patch around each tower.  For 
//    each rdo channel we determine the number of (physically)
//    adjacent towers and 
//
// 3) An optional parameter (use offline gains) will correct
//    the ADC thresholds for gain variations measured offline.
//    When applied to patch sum thresholds, we will also need
//    to apply correction to the towers in the patch sums.
//
int L2gammaAlgo::initRun( int run )
{
  return initRun(run,mDefaultI_par,mDefaultF_par);
}
int L2gammaAlgo::initRun( char *myname, int run, int I_par[5], float F_par[5] )
{
  return initRun( run, I_par, F_par );
}
int L2gammaAlgo::initRun( int run, int I_par[5], float F_par[5] )
{


  printf("%s ::initRun() for run=%d\n",mName,run);
  
  if ( mDb->initRun(run) ) return -7; // this must be in, if your algos are the only one in the game, _you_ must initialize DB for the new run,JB

  mRunNumber = run;

  // recreate histograms (clears)
  //$$$  jbook();
  jclear();

  mUseOfflineGains = I_par[0];
  if ( I_par[1] ) mPrescale = I_par[1];
  mUseBbc=false;
  setTowerThreshold( F_par[0] );
  setPatchThreshold( F_par[1] );


  /***********************************************************************
   * test validity of input parameters
   */
  {
    
    if ( F_par[1] < 0. ) {
      printf("L2gammaAlgo::ERROR -- patch threshold < 0\n");
      return 1;
    }
    if ( F_par[1] > 2.0 * F_par[0] ) {
      printf("L2gammaAlgo::ERROR -- patch threshold > 2x tower threshold\n");
      return 1;
    }
    
    if ( mL2input )
      {
	if ( F_par[0] < mL2input->getPatchThreshold() ) {
	  printf("L2gammaAlgo::WARNING -- l2input to this algorithm has a patch threshold < tower threshold\n");
	  //return 1;
	}
      }

    if ( I_par[0] < 0 || I_par[0] > 1 ) 
      {
	printf("L2gammaAlgo::ERROR -- I_par[0] should be 0 (use ideal gains) or 1 (use online gains)\n");
      }
    
  }/* valid */


  // close histogram and log files and reopen w/ new paths
  if ( mLogFile && mLogFile != stdout ) fclose(mLogFile);
  if ( mHistFile ) fclose(mHistFile);

  //const char *names[]={"bemc","eemc"};

  char clog[128];
  sprintf(clog,"%s/run%d.l2%s.out",mOutDir,run,mName);
  //printf("%s=\n",clog);
  //setHistFile(chis);
  //  setLogFile(clog);
  mLogFile=fopen(clog,"w");
  // assert(mLogFile);
  if(mLogFile==0)  printf("%s ::initRun() for run=%d UNABLE to open log-file=%s=, it is not fatal, continue initialization\n",mName,run,clog);

  if(mLogFile) {
    fprintf(mLogFile,"\n\n====================================================================================================================================\n");
    fprintf(mLogFile,"L2gammaAlgo start of run %d  summary, compiled: %s , %s\n",run,__DATE__,__TIME__);
    fprintf(mLogFile,"calorimeter:              %s\n",mName);
    fprintf(mLogFile,"run:                      %d\n",run);
    fprintf(mLogFile,"use offline gains   I[0]: %d\n",mUseOfflineGains);
    fprintf(mLogFile,"prescaled accept    I[1]: %d\n",mPrescale);
    fprintf(mLogFile,"threshold level:          %d\n",mThresholdLevel);
    fprintf(mLogFile,"tower threshold     F[0]: %-5.2f [GeV]\n",mTowerThreshold);
    fprintf(mLogFile,"patch threshold     F[1]: %-5.2f [GeV]\n",mPatchThreshold);
    fprintf(mLogFile,"patch size:               %s\n","3x3"); /* may change in later revisions */
    fprintf(mLogFile,"logfile:                  %s\n",clog);
    //  fprintf(mLogFile,"histograms:               %s\n",chis);
    fprintf(mLogFile,"database at:              %p\n",mDb);
    fprintf(mLogFile,"l2 input at:              %p\n",mL2input);
    
    fprintf(mLogFile,"\nDetector Geometry\n");
    fprintf(mLogFile,"mEtaBins  = {");
    for ( int i=0;i<mNumEtas+1;i++ ) { 
      fprintf(mLogFile,"%4.2f, ",mEtaBins[i]);
      if ( !((i+1)%4) ) fprintf(mLogFile,"\n               ");
    }
    fprintf(mLogFile,"X}\n");
    fprintf(mLogFile,"mMaxADC   = %4d\n",mMaxADC);
    fprintf(mLogFile,"mMaxET    = %4.1f\n",mMaxET);
    fprintf(mLogFile,"mIdealGainT = %5.3f\n",mIdealGainT);
    fprintf(mLogFile,"\n");
    
    fprintf(mLogFile,"\n");
  }
  if ( !mDb ) return 100;

   

  /// Reset event counters
  mNumberInput    = 0;
  mNumberAcceptHT = 0;
  mNumberAcceptTP = 0;

  mNumberLive=0;

  /// read in database for this run
  if(mLogFile)  fprintf(mLogFile,"initialize EMC ascii database\n\n");
  //$$$ assume it's already initialized by a call in main??? gEmcCDb_init();
  if(mLogFile)  fprintf(mLogFile,"\n");

  ///
  /// determine ideal gains for each of the eta bins
  ///
  //float emc_ideal_gains[mNumEtas];
  float *emc_ideal_gains = new float[mNumEtas];
  if(mLogFile)  fprintf(mLogFile,"configure ideal gains for %d eta bins\n",mNumEtas);
  for ( int i=0;i<mNumEtas;i++ )
    {
      float eta_mean = mEtaBins[ i ] + mEtaBins[ i+1 ];
      eta_mean /= 2.0;
      emc_ideal_gains[i] = mMaxADC / mMaxET / cosh(eta_mean);
     if(mLogFile)   fprintf(mLogFile,"+ etabin=%d eta=%5.2f bemc ideal gain=%5.2f\n", i+1, eta_mean, emc_ideal_gains[i]);
      
    }

  if ( mNumEtas != 12 && mNumEtas != 40 ) {
    if(mLogFile)  fprintf(mLogFile,"L2gammaEmCal::FATAL ERROR -- expect 12 or 40 etabins, got %d\n",mNumEtas);
    if(mLogFile)  fprintf(mLogFile,"  + likely cause -- invalid runtime parameter\n");
    delete emc_ideal_gains;<--- Mismatching allocation and deallocation: emc_ideal_gains
    return 10; /* EMC not properly configured */
  }

  /// clear high tower and patch thresholds
  for ( int index=0;index<mNumTower;index++ )
    {
      mTowerAdcThreshold[index]=0;
      mPatchAdcThreshold[index]=mPatchThreshold;
      mTowerPed[index]=0.;
      mPatchPed[index]=0.;
      mTowerFrequency[index]=0;
      mPatchFrequency[index]=0;
      mTowerGain[index]=-1.0;
    }

  if(mLogFile)  fprintf(mLogFile,"\nInitializing tower ADC threshods\n");

  /// loop over all db entries and calculate ADC thresholds
  for ( int index=0; index<EmcDbIndexMax; index++ ) 
    {

      // get the item from the database
      //$$$struct EmcCDbItem *x = &gEmcCDbByIndex[index];
      const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
      if ( x==0 ) continue;

#if 1
      // make sure db item matches the calorimeter we're using
      if ( mBEmc && !mDb->isBTOW(x) )
	continue;
      if ( mEEmc && !mDb->isETOW(x) )
	continue;
#endif


      /* temporary kludge */
      //$$$  if ( x->gain < 0 ) x->fail |= 0x0080;
     
      // local numbering scheme counts from 0
      int sec = x->sec - 1;
      int sub = 8192;
      if ( mEEmc ) sub = x->sub - 'A';
      if ( mBEmc ) sub = x->sub - 'a';
      int eta = x->eta - 1;
      int phi = phibin(sec,sub); //mNumSubs*sec + sub;
      int tow = tower(phi,eta); //mNumEtas*phi + eta;
      int rdo = x->rdo;

      //fprintf(mLogFile,"sec=%d sub=%d eta=%d tow=%d\n",sec,sub,eta,tow);

      /* temporary kludge */
      //if ( eta < 20 ) x->fail |= 0x0080;


      // equal energy gain
      //float gain = x->gain;    
      float ped  = x->ped;
      float gain = x->gain;
      float ideal = emc_ideal_gains[eta];

      

      // check fail bit and artificially raise HT threshold 
      ushort stat = x->stat;   
      ushort fail = x->fail;   
      if ( gain < 0.0 ) {
	if(mLogFile) 	fprintf(mLogFile,"L2gammaEmCal::WARN %s gain=%5.2f will be masked\n",x->name,x->gain);
	fail = 0xffff;
      }

      mTowerStat[rdo]=stat; mPatchStat[rdo]=stat;
      mTowerFail[rdo]=fail; mPatchFail[rdo]=fail;
      mTowerGain[rdo]=gain;
      mTowerGainIdeal[rdo]=ideal;
      

      if ( !fail ) 
	mTowerAdcCorrection[rdo]=(mUseOfflineGains)?ideal/gain:1.0;        /** I really need to think about this! **/
      else
	mTowerAdcCorrection[rdo]=0.; /* tower is masked out */
            
      // calculate high tower threshold for this
      // rdo channel in ADC
      if ( !fail ) {
	mTowerAdcThreshold[rdo] = (ushort)(ped + (mIdealGainT * mTowerThreshold)/mTowerAdcCorrection[rdo] + 0.5 );
	mTowerPed[rdo]=ped;
      }
      else {
	mTowerAdcThreshold[rdo] = 4095;
	mTowerPed[rdo]=4095;
      }
      
                                                          /* upgrade: verify that tower threshold is above mMinAdc, issue warning and log */

      mRdo2tower[ rdo ] = tow;    // returns tower for given rdo channel
      mTower2rdo[ tow ] = rdo;    // returns rdo channel for given tower

      if ( !fail ) mNumberLive++;
 
    }





  // fprintf(mLogFile,"L2gammaAlgo::initRun(%d) initialize 3x3 patch thresholds for ET=%5.2f\n",mRunNumber,mPatchThreshold);

  if(mLogFile)  fprintf(mLogFile,"Initializing 3x3 tower cluster thresholds\n");

  /// second loop over all db entries, this time we calculate
  /// ADC thresholds on the 3x3 tower patch
  for ( int index=0; index<EmcDbIndexMax; index++ ) 
    {

      // get the item from the database
      //$$$   struct EmcCDbItem *x = &gEmcCDbByIndex[index];
      const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
      if ( x==0 ) continue;

      // make sure db item matches the calorimeter we're using
      if ( mBEmc && !mDb->isBTOW(x) )
	continue;
      if ( mEEmc && !mDb->isETOW(x) )
	continue;
     
      // local numbering scheme counts from 0
      int sec = x->sec - 1;
      int sub = 8192;
      if ( mEEmc ) sub = x->sub - 'A';
      if ( mBEmc ) sub = x->sub - 'a';
      int eta = x->eta - 1;
      int phi = phibin(sec,sub);
      //int tow = tower(phi,eta);
      int rdo = x->rdo;

      mPatchStat[rdo] |= x->stat;
      mPatchFail[rdo] |= x->fail;

      // masked towers will be ignored in the patch thresholds...
      // our butts are covered in the event that a crate is masked
      // out, by the requirement of a high tower in coincidence with
      // a 3x3 tower patch sum.
      if ( x->fail ) continue;
      if ( mTowerFail[rdo] ) continue;

      // equal energy gain
      //float gain = x->gain;    
      float ped  = x->ped; 


      /// loop over neighboring towers, **including the current
      /// tower**, and add ped/gain to the patch threshold.  the
      /// result should be sum_i ped_i/g_i for each rdo channel
      /// (plus the ET threshold already initialized).
      int nn=0;      
      for ( int ieta=eta-1;ieta<=eta+1;ieta++ )
	for ( int iphi=phi-1;iphi<=phi+1;iphi++ )
	  {

	    if ( ieta < 0 || ieta > mNumEtas-1 ) continue; // out of bounds
	    int jeta=ieta;
	    int jphi=(iphi+mNumPhis)%mNumPhis;
	    int jtow=tower(jphi,jeta);
	    int jrdo=mTower2rdo[jtow];

	    mPatchAdcThreshold[jrdo] += ped/mIdealGainT * 
	      mTowerAdcCorrection[rdo];

	    if ( jtow == 12 ) {
	      if(mLogFile)  fprintf(mLogFile,"+ name=%s ped=%5.2f gfact=%5.2f thresh=%5.2f\n",x->name,x->ped,mTowerAdcCorrection[rdo],mPatchAdcThreshold[jrdo] );
	    }

	    // add ped for this channel to all patches 
	    // which contain it
	    mPatchPed[jrdo]+=ped;

	    /// assign this rdo to the physical tower patch
	    mRdoPatch[rdo][nn++]=jrdo;
	    

	  }
      mNumPatch[rdo]=nn;

    }

  //fprintf(mLogFile,"done w/ initialization, writing to log file\n");


  //
  // Write to logfile (stdout for now) the thresholds generated
  //

  for ( int index=0;index<EmcDbIndexMax;index++ )
    {


      // get the item from the database
      //.      struct EmcCDbItem *x = &gEmcCDbByIndex[index];
      const L2EmcDb::EmcCDbItem *x = mDb->getByIndex(index);
      if ( x==0 ) continue;

#if 1
      // make sure db item matches the calorimeter we're using
      if ( mBEmc && !mDb->isBTOW(x) )
	continue;
      if ( mEEmc && !mDb->isETOW(x) )
	continue;
#endif
 
      float gain=x->gain;
      int   rdo=x->rdo;
      if ( gain==0. ) gain=-1.;

      int tow=mRdo2tower[rdo];
      int eta=tow%mNumEtas;


      /*
	Floating point exception occurs in my output???? 
      */

#if 0
      fprintf(mLogFile,"tower=%s tow=%d rdo=%d mask=%2x gain=%5.2f ideal=%5.2f ped=%5.2f thr=%d %5.2f GeV patch=%5.2f + %5.2f GeV\n",
	     x->name,
	     mRdo2tower[rdo],
	     rdo,
	     x->fail,
	     x->gain,
	     emc_ideal_gains[eta],
	     x->ped,
	     mTowerAdcThreshold[rdo],
	     (float)((mTowerAdcThreshold[rdo]-x->ped)*mMaxET/mMaxADC),
	     mPatchAdcThreshold[rdo]-mPatchThreshold,
	     mPatchThreshold);
#endif

#if 1
   if(mLogFile)     fprintf(mLogFile,"tower=%s tow=%d rdo=%d ideal=%5.2f thr=%d patch=%5.2f GeV\n",
	     x->name,
	     mRdo2tower[rdo],
	     rdo,
	     emc_ideal_gains[eta],
	     mTowerAdcThreshold[rdo],
	     mPatchAdcThreshold[rdo]);
#endif



    }

#ifdef DEBUG_PATCH_THRESHOLDS
  for ( int rdo=0;rdo<mNumRdo;rdo++ )
    printPatchConfig(rdo);
#endif

 if(mLogFile) {
   fprintf(mLogFile,"\n");
   fprintf(mLogFile,"total number of towers:        %d\n",mNumTower);
   fprintf(mLogFile,"number of unmasked towers:     %d\n",mNumberLive);
   fprintf(mLogFile,"\n====================================================================================================================================\n\n");
 }
 
  /*
   * cleanup allocated memory
   */
  delete emc_ideal_gains;<--- Mismatching allocation and deallocation: emc_ideal_gains

  return 0;

}/* ::initRun */



// -------------------------------------------------------------------------------------------- eval --
//
// evaluate trigger conditions
//
// most of the work has already been done for us.  at the start of
// the run, we took the user-supplied ET thresholds on the high 
// tower and associated cluster (patch) and determined two thresholds:
//
// o mTowerAdcThreshold[rdo] -- the ADC value for which the tower will
//   exceed the specified ET threshold (i.e. ped*4096/60+E_T).
//
// o mPatchAdcThreshold[rdo] -- actually a floating point ET threshold
//   to be calculated ONLY when we have a tower above the specified
//   ET threshold.
//
// therefore, the only thing we need to do w/in eval is to loop over
// all rdo's and find a high tower, then test the 3x3 tower cluster
// against its threshold.
//

bool  L2gammaAlgo::doEvent( int L0trigger, int inputEventID, TrgDataType* trgData,
			    int bemcIn, unsigned short *bemcData,
			    int eemcIn, unsigned short *eemcData )
{
  rdtscl_macro(mEveTimeStart);
  mAccept=false;
  if ( mEEmc ) {
    mAccept=doEvent( inputEventID, trgData, eemcIn, eemcData );
  }
  else {
    mAccept=doEvent( inputEventID, trgData, bemcIn, bemcData );
  } 

  rdtscl_macro(mEveTimeStop);
  mEveTimeDiff=mEveTimeStop-mEveTimeStart;
  int  kTick=mEveTimeDiff/1000;
  //   printf("gg=%f t1=%d t2=%d \n",mEveTimeDiff/1000.,mEveTimeStart,mEveTimeStop);
  mhT->fill(kTick);
  
  return mAccept;
}


bool L2gammaAlgo::doEvent( int inpEveId, TrgDataType* trgData, 
			   int  emcIn, unsigned short *emcData )
{
  //I think you will need to use inpEveId for second copy, if not - ignore it,JB
  clear();
  // we got nothin'
  if ( !emcIn ) return false;

  mNumberInput++;

  mHistos[0].fill(0); 

  //
  // first section of the code evaluates the trigger decision
  //

  // track execution time of the algorithm (in cpu ticks)
  unsigned long eval_time_start;
  unsigned long eval_time_stop;
  rdtscl_macro(eval_time_start);

  bool eht  = false;
  bool trig = false;

  float  sum=0.;                                   /* tower patch sum, 3x3 cluster around high tower */
  ushort htrdo=8192;                               /* rdo of high tower */
  ushort tprdo=8192;                               /* rdo of high cluster */

  nRdosHT=0;
  nRdosTP=0;

  // ------------------------------------------------------------------------------------ preprocess --
#ifdef OPT_PREPROCESS

  // If another "lower threshold" is being run as input to this algorithm,
  // then we process the results of that algorithm and jump to the QA
  // section.

  if ( mL2input ) {
    for ( ushort i=0;i<mL2input->getNumberOfHighTowers();i++ )
      {
	ushort rdo=mL2input->mListOfRdosHT[i];
#ifdef DEBUG
	mHistos[3].fill( emcData[rdo] );
#endif
	if ( emcData[rdo] < mTowerAdcThreshold[rdo]) continue;	
	mADCofHT[ nRdosHT ]      = emcData[rdo];
	mListOfRdosHT[ nRdosHT++ ] = rdo; /* store rdo of high tower */	
	mHistos[4].fill( emcData[rdo] );

	eht=true; 
	htrdo=rdo;
	sum = 0.;
	for ( int i=0;i<mNumPatch[rdo];i++ ) {
	  int prdo=mRdoPatch[rdo][i];
	  if ( mTowerFail[prdo] ) continue; /* skip masked towers */
	  sum += emcData[prdo] / mIdealGainT * mTowerAdcCorrection[rdo];
	}
	ushort sumx2=(ushort)(2.0*sum); 
#ifdef DEBUG
	mHistos[7].fill( sumx2 );
#endif

	if ( sum > mPatchAdcThreshold[rdo] ) {
	  trig=true;  
	  tprdo=rdo;
	  mETofTP[ nRdosTP ] = sum-mPatchAdcThreshold[rdo]+mPatchThreshold;
	  mListOfRdosTP[ nRdosTP++ ] = rdo; /* store rdo of cluster */
	  mHistos[5].fill( emcData[rdo] );	  
	  mHistos[8].fill( sumx2 );
	}
      }
    goto QA;
  }
#endif

  // ------------------------------------------------------------------------- standalone processing --

  // Here we loop over all RDO channels and determine whether thresholds have
  // been met.  We store results for higher thresholds/other algorithms to
  // process at a later point.

  for ( ushort rdo=0; rdo<mNumRdo; rdo++ ) 
    {

      // check if tower is above ADC=ET*gain+ped threshold
      // these two lines consume about 1/2 of the time of the 
      // algorithm for any given event.
#ifdef DEBUG
      mHistos[3].fill( emcData[rdo] );
#endif
      if ( emcData[rdo] < mTowerAdcThreshold[rdo]) continue;

      // past here, we have a high tower trigger.  when we
      // have such a trigger, the following code takes about
      // the same time as the preceding two lines.
      
      mADCofHT[ nRdosHT ]      = emcData[rdo];
      mListOfRdosHT[ nRdosHT++ ] = rdo;                   /* store rdo of high tower for 2nd stage */
      mHistos[4].fill( emcData[rdo] );
       
      eht=true; 
      htrdo=rdo;

      
      // Sum the 3x3 patch of towers around the high tower
      sum = 0.;
      for ( int i=0;i<mNumPatch[rdo];i++ ) {
	int prdo=mRdoPatch[rdo][i];
	if ( mTowerFail[prdo] ) continue; /* skip masked towers */
	sum += emcData[prdo] / mIdealGainT * mTowerAdcCorrection[rdo];
      }
      ushort sumx2=(ushort)(2.0*sum);
#ifdef DEBUG
      mHistos[7].fill( sumx2 );
#endif

      // we have found a high tower in coincidence 
      // with a 3x3 patch of towers above specified
      // thresholds
      if ( sum > mPatchAdcThreshold[rdo] ) {
	trig=true;  
	tprdo=rdo;
	mETofTP[ nRdosTP ] = sum-mPatchAdcThreshold[rdo]+mPatchThreshold;
	mListOfRdosTP[ nRdosTP++ ] = rdo; /* store rdo of cluster */
	mHistos[5].fill( emcData[rdo] );
	mHistos[8].fill( sumx2 );
      }

    }
  
  // ------------------------------------------------------------------------ process QA histograms --

  // Fill QA histograms, set bits in the L2 result, and return the result

#ifdef OPT_PREPROCESS
 QA:
#endif 

  bool prescaleAccept = false;
  if ( mPrescale>0 ) 
    prescaleAccept = !((mNumberInput) % mPrescale);

  mHistos[1].fill(nRdosHT); // Number of high towers > threshold
  mHistos[2].fill(nRdosTP); // Number of clusters > threshold


  if ( eht ) { 
    mNumberAcceptHT++;
    mTowerFrequency[mRdo2tower[htrdo]]++;
    mHistos[0].fill(1);
  }

  if ( trig ) {
    mNumberAcceptTP++;
    mPatchFrequency[mRdo2tower[tprdo]]++;
    mHistos[0].fill(2);
  }
  rdtscl_macro(eval_time_stop);

  /* algorithm selects fraction of events to do QA on */
  unsigned long qa_time_start;
  unsigned long qa_time_stop;
  rdtscl_macro(qa_time_start);
     
  // ------------------------------------------------------------------------- save trigger decision --
  

 L2gammaResult mResult;
  // initialize "result"
  mResult.result_version    = LEVEL2_GAMMA_RESULT_VERSION;
  mResult.threshold         = (mThresholdLevel==kThresh1)? (0x1|0x2) : (0x4|0x8);
  // reset monitor portion of L2gammaResult
  mResult.elapsed=0x00;
  // clear L2 trigger result
  mResult.trigger = 0x00;

  float max=0.;
  ushort maxrdo=8192;

  // set high tower bit
  if ( eht ) 
    mResult.trigger |= 0x1; 


  // set cluster bit and 
  for ( ushort i=0;i<nRdosTP;i++ )
    {
      if ( mETofTP[i] > max ) {
	max=mETofTP[i];
	maxrdo=mListOfRdosTP[i];
      }
    }
  if ( maxrdo < mNumRdo ) 
    {
      mResult.trigger |= 0x2;
      ushort tow=mRdo2tower[maxrdo];
      mResult.phibin=tow/mNumEtas;
      mResult.etabin=tow%mNumEtas;
      max-=mPatchAdcThreshold[maxrdo];
      max+=mPatchThreshold;
      if ( max < 127.5 )
	mResult.ptclusterx2 = ((ushort)(2.0*max));
      else
	mResult.ptclusterx2 = 255;
      float pttow=emcData[maxrdo]-mTowerPed[maxrdo];
      pttow*=mTowerAdcCorrection[maxrdo]/mIdealGainT;
      if ( pttow < 127.5 )
	mResult.pttowerx2 = (ushort)(2.0*pttow);
      else
	mResult.pttowerx2 = 255;
      if ( mEEmc ) mResult.phibin |= 0x8;
   }


  // set prescale bit
  if ( prescaleAccept )
    mResult.trigger |= 0x4;

  // set trigger bit
  if ( trig || prescaleAccept )
    mResult.trigger |= 0x8;
   
  rdtscl_macro(qa_time_stop);
  int dt=(int)(qa_time_stop-eval_time_start)/1000;

  mEvalTime += dt; 
  //mResult.mon.elapsed = 0;

  /**
   ** Get pointer to block of bytes where L2gammaResult will reside
   **/
  unsigned int *pResult 
//  = &trgData->TrgSum.L2Result[L2RESULTS_OFFSET_PIG +2*mEEmc+mThresholdLevel];
    = &trgData->TrgSum.L2Result[mResultOffset+2*mEEmc+mThresholdLevel];

  /**
   ** And write to L2result
   **/
  memcpy(pResult,&mResult,sizeof(L2gammaResult));
  //#define __L2GAMMA_PRINT_RESULT__
#ifdef __L2GAMMA_PRINT_RESULT__
  print_L2gammaResult(mResult);
#endif

  /**
   ** Test trig and prescale and fill QA histograms when they are satisfied
   **/
 

  if ( trig || prescaleAccept )
    {
      mHistos[0].fill(3);
      for ( ushort i=0;i<nRdosHT;i++ ) {
	int tower=(int)mRdo2tower[mListOfRdosHT[i]];
	mHistos[6].fill( (mADCofHT[i] - (ushort)mTowerPed[ mListOfRdosHT[i] ])/5,tower );
	mHistos[13].fill(tower);
      }

      for ( ushort i=0;i<nRdosTP;i++ ) {
	int tower=(int)mRdo2tower[mListOfRdosTP[i]];
	mHistos[9].fill( (int)(mETofTP[i]*2),tower );
	mHistos[14].fill( tower );
      }

      mHistos[18].fill( dt );

    }

  mHistos[15].fill(dt);
  if ( eht ) mHistos[16].fill(dt);
  if ( trig ) mHistos[17].fill(dt);

  return ( trig || prescaleAccept );
  
}

// ----------------------------------------------------------------------------
//

void L2gammaAlgo::clear()
{

  nRdosHT=0;
  nRdosTP=0;

}

// ----------------------------------------------------------------------------

void L2gammaAlgo::finishRun()
{

  int run=mRunNumber;

 
  char chis[128];
  printf("%s/run%d.l2%s.hist.bin\n",mOutDir,run,mName);
  sprintf(chis,"%s/run%d.l2%s.hist.bin",mOutDir,run,mName);
  printf("L2gamma:saving '%s'\n",chis);
  //  mLogFile=fopen(clog,"a");
  //  mLogFile=stdout;

  mHistFile=fopen(chis,"w");
  if(mHistFile==0)  printf("%s ::finishRun() for run=%d UNABLE to open histo-file=%s=, continue initialization\n",mName,run,chis); 
  //assert( mHistFile);

  //
  // trap some potential divide by zeros but still do output
  //
  if ( !mNumberInput ) mNumberInput=-1; /* short/nonexistent run */
  if ( !mNumberLive  ) mNumberLive=-1;  /* db is ft^ */


  //  if ( !mLogFile ) 
  //    mLogFile = stdout;
 if(mLogFile) {
   fprintf(mLogFile,"\n\n===================================================================================================================================\n");
   fprintf(mLogFile,"L2gammaAlgo end of run %d summary\n",run);
   fprintf(mLogFile,"run:            %d\n",run);
   fprintf(mLogFile,"tower threshold: %5.2f [GeV]\n",mTowerThreshold);
   fprintf(mLogFile,"patch threshold: %5.2f [GeV]\n",mPatchThreshold);
   fprintf(mLogFile,"patch size:      3x3\n"); /* may change in later revisions */
   fprintf(mLogFile,"eval time:      %d [kTicks]\n",mEvalTime);
   fprintf(mLogFile,"avg time/event: %d [kTicks]\n",mEvalTime/mNumberInput);
   fprintf(mLogFile,"# input:        %d\n",mNumberInput);
   fprintf(mLogFile,"# ht accept:    %d\n",mNumberAcceptHT);
   fprintf(mLogFile,"# ht+tp accept: %d\n",mNumberAcceptTP);
   fprintf(mLogFile,"\n");
   
   //
   // first section, loop over the tower and patch frequencies
   // to look for hot towers
   //
   float avgt=0,avgp=0;
   int maxtf=0,maxpf=0;
   
   for ( int tow=0;tow<mNumTower;tow++ )
     {       
       avgt+=mTowerFrequency[tow];
       avgp+=mPatchFrequency[tow];      
       if ( mTowerFrequency[tow]>maxtf ) maxtf=mTowerFrequency[tow];
       if ( mPatchFrequency[tow]>maxpf ) maxpf=mTowerFrequency[tow];
     }
   avgt=( ((float)avgt)/(float)mNumberLive );
   avgp=( ((float)avgp)/(float)mNumberLive );
   
   //  fprintf(mLogFile,"sumt=%5.2f sump=%5.2f nlive=%d\n",avgt,avgp,mNumberLive);
   fprintf(mLogFile,"max # ht accept in one tower:          %d\n",maxtf);
   fprintf(mLogFile,"max # ht+tp accept in one tower:       %d\n",maxpf);
   fprintf(mLogFile,"avg # ht accept:                       %6f\n",avgt);
   fprintf(mLogFile,"avg # ht+tp accept:                    %6f\n",avgp);
   fprintf(mLogFile,"expected # ht accept in one tower:     %6f\n",((float)mNumberAcceptHT)/mNumberLive);
   fprintf(mLogFile,"expected # ht+tp accept in one tower:  %6f\n",((float)mNumberAcceptTP)/mNumberLive);
   fprintf(mLogFile,"\n");
   
   
  //#define DEBUG_PATCH_THRESHOLDS

#ifndef DEBUG_PATCH_THRESHOLDS
   fprintf(mLogFile,"Summary of hot towers (# ht accept > 3*average or # ht+tp accept > 3*average):\n\n");

   for ( int tow=0;tow<mNumTower;tow++ )
     {
       int phi=tow/mNumEtas;
       int eta=tow%mNumEtas;
       int sec=phi/mNumSubs;
       int sub=phi%mNumSubs;
       int myrdo=mTower2rdo[tow];
       
       
       bool hott = (mTowerFrequency[tow] > (3.0*avgt));
       bool hotp = 0 && (mPatchFrequency[tow] > (3.0*avgp));
       
       /*           ^^^^^^^^^^^^^ need a statistically robust sample or we will get noise! */
       
       if ( hott||hotp ) fprintf(mLogFile,"\n");
       
       if ( mBEmc ) 
	 {
	   if ( hott )
	     fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot tower %02dt%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'a',eta+1,mTowerFrequency[tow],avgt);
	   if ( hotp )
	     fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot patch %02dt%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'a',eta+1,mPatchFrequency[tow],avgp);
	 }
       else 
	 {
	   if ( hott )
	     fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot tower %02dT%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'A',eta+1,mTowerFrequency[tow],avgt);
	   if ( hotp )
	     fprintf(mLogFile,"L2gammaAlgo::WARNING -- possible hot patch %02dT%c%02d freq=%d avg=%5.2f\n",sec+1,sub+'A',eta+1,mPatchFrequency[tow],avgp);
	 }
       
       if ( hott || hotp )
	 { 
	   
	   printPatchConfig( myrdo );
	   
	 } 
       
     }
 }
#endif
   
 if ( mLogFile ) {
   fprintf(mLogFile,"\n====================================================================================================================================\n\n");
   finishCommonHistos();

 }
 
  finish(); /* output histogram */

}

void L2gammaAlgo::finish()
{
  // Write histograms at end of run
  if ( mHistFile ) {
    for ( unsigned int i=0;i<sizeof(mHistos)/sizeof(L2Histo);i++)
      {
	mHistos[i].write(mHistFile);
      }
    fflush(mHistFile);
  }
}

// ----------------------------------------------------------------------------

void L2gammaAlgo::jclear()
{
  for ( int i=0;i<19;i++ ) mHistos[i].reset();
}
void L2gammaAlgo::jbook()
{
  // Book L2 histograms for QA

  // on first call book histograms
  if ( 1 ) {

    /* Event counters */
    
    mHistos[0]=L2Histo(100, "Counter.  0=N_{input} 1=N_{ht} 2=N_{clust} 3=N_{accept}", 10);
    mHistos[1]=L2Histo(101, "N high towers > threshold", 10);
    mHistos[2]=L2Histo(102, "N clusters > threshold", 10);
    
    /* ADC spectra */
    
    mHistos[3]=L2Histo(103, "Raw ADC of ht (if DEBUG)", 1024);
    mHistos[4]=L2Histo(104, "Raw ADC of ht, ht > threshold", 1024);
    mHistos[5]=L2Histo(105, "Raw ADC of ht, cluster > threshold", 1024);
    mHistos[6]=L2Histo(106, "HT event accepted; X: (ADC-ped)/5;  Y: tower index", 128,mNumTower);

    /* Cluster spectra */
    
    mHistos[7]=L2Histo(107, "2*ET sum of cluster, ht > threshold", 32);
    mHistos[8]=L2Histo(108, "2*ET sum of cluster, cluster > threshold", 32);
    mHistos[9]=L2Histo(109, "2*ET sum of cluster - ped/gain, event accepted", 32,mNumTower);
    
    /* BBC difference (not implemented) */
    
    mHistos[10]=L2Histo(110, "Raw BBC TAC difference+256 (not implemented)", 1);
    mHistos[11]=L2Histo(111, "Raw BBC TAC difference+256, ht > threshold (not implemented)", 1);
    mHistos[12]=L2Histo(112, "Raw BBC TAC difference+256, cluster > threshold (not implemented)" , 1);

    /* Tower and cluster trigger frequencies */
    
    mHistos[13]=L2Histo(113, "High tower > threshold frequency, X: ieta+mNumEta*iphi", mNumTower);
    mHistos[14]=L2Histo(114, "Cluster > threshold frequency, X: ieta+mNumEta*iphi", mNumTower);
    
    /* Event timing */
    
    mHistos[15]=L2Histo(115, "kTicks / input event", 128);
    mHistos[16]=L2Histo(116, "kTicks / ht event", 128);
    mHistos[17]=L2Histo(117, "kTicks / cluster event", 128);
    mHistos[18]=L2Histo(118, "kTicks / trigger", 128);

  }

  
}

// ----------------------------------------------------------------------------
#ifndef DEBUG_PATCH_THRESHOLDS
void L2gammaAlgo::printPatchConfig( int rdo )
{

  // get the tower id
  int tow = mRdo2tower[rdo];
  int phi = tow/mNumEtas;
  int eta = tow%mNumEtas;

  int sec = phi/mNumSubs;
  int sub = phi%mNumSubs;

  /*
   
  The goal here is to output several quantities in an ascii 
  format which matches the physical layout of the towers in
  order to help us debug problems online

  as least as far as the endcap is concerned (I can switch the
  order for the barrel if need be.  Specifically, we want the
  following output:

  tower: 06TC05
  gain:  22.64
  ped:   20.90
  thr:   212 [2.8 GeV]

  pedestals           gains, stat, fail, high tower frequency, etc...

  18.7 | -3.0 | 22.5
  -----+-----+-----
  22.9 | 20.9 | 15.9
  -----+------+-----
  11.1 | 16.7 | 32.0

  */

  if ( mEEmc ) 
    fprintf(mLogFile,"etow:  %02dT%c%02d\n",sec+1,sub+'A',eta+1);
  if ( mBEmc )
    fprintf(mLogFile,"btow:  %02dt%c%02d\n",sec+1,sub+'a',eta+1);    

  fprintf(mLogFile,"index: %4d\n",tow);
  fprintf(mLogFile,"gain:  %6.3f [ideal=%6.3f]\n",mTowerGain[rdo],mTowerGainIdeal[rdo]);
  fprintf(mLogFile,"ped:   %6.3f\n",mTowerPed[rdo]);
  fprintf(mLogFile,"thr:   %3d [%4.2f GeV]\n",mTowerAdcThreshold[rdo],mTowerAdcThreshold[rdo]/mIdealGainT);
  fprintf(mLogFile,"\n");

  //      14.3 | 14.5 |  8.8      22.6 | 22.9 | 21.6      80   | 80   | 80         0   |  0   |  0           1 |    0 |    0
  fprintf(mLogFile,"loaded peds             gain factors            status bits             mask bits                frequency > thr\n");

  for ( int i_eta=eta+1;i_eta>=eta-1;i_eta-- ) {

    int phi_l = (phi+mNumPhis-1)%mNumPhis;
    int phi_r = (phi+mNumPhis+1)%mNumPhis;
    int phi_c = phi;

    //int phis[]={phi_l,phi,phi_r};
    float peds[]={0,0,0};
    float gains[]={0,0,0};
    ushort stats[]={0xff,0xff,0xff};
    ushort fails[]={0xff,0xff,0xff};
    int freqs[]={0,0,0};

    // get numbers from db
    if ( i_eta < mNumEtas && i_eta >= 0 ) {

      int tow_l = tower(phi_l,i_eta);
      int tow_r = tower(phi_r,i_eta);
      int tow_c = tower(phi_c,i_eta);

      int rdo_l = mTower2rdo[tow_l];
      int rdo_r = mTower2rdo[tow_r];
      int rdo_c = mTower2rdo[tow_c];

      peds[0]=mTowerPed[rdo_l];peds[1]=mTowerPed[rdo_c];peds[2]=mTowerPed[rdo_r];
      gains[0]=mTowerAdcCorrection[rdo_l];gains[1]=mTowerAdcCorrection[rdo_c];gains[2]=mTowerAdcCorrection[rdo_r];
      stats[0]=mTowerStat[rdo_l];stats[1]=mTowerStat[rdo_c];stats[2]=mTowerStat[rdo_r];
      fails[0]=mTowerFail[rdo_l];fails[1]=mTowerFail[rdo_c];fails[2]=mTowerFail[rdo_r];
      freqs[0]=mTowerFrequency[tow_l];freqs[1]=mTowerFrequency[tow_c];freqs[2]=mTowerFrequency[tow_r];


    }

    fprintf(mLogFile, "%4.1f | %4.1f | %4.1f\t", peds[0],peds[1],peds[2] );
    fprintf(mLogFile, "%4.2f | %4.2f | %4.2f\t", gains[0],gains[1],gains[2] );
    fprintf(mLogFile, "%2x   | %2x   | %2x  \t", stats[0],stats[1],stats[2] );
    fprintf(mLogFile, "%2x   | %2x   | %2x  \t", fails[0],fails[1],fails[2] );
    fprintf(mLogFile, "%4.0f | %4.0f | %4.0f\t", (float)freqs[0],(float)freqs[1],(float)freqs[2] );

    fprintf(mLogFile,"\n");

    if ( i_eta > eta-1 ) {
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "\n" );
    }

  }

}
#endif
#ifdef DEBUG_PATCH_THRESHOLDS
void L2gammaAlgo::printPatchConfig( int rdo )
{

  // get the tower id
  int tow = mRdo2tower[rdo];
  int phi = tow/mNumEtas;
  int eta = tow%mNumEtas;

  int sec = phi/mNumSubs;
  int sub = phi%mNumSubs;

  /*
   
  The goal here is to output several quantities in an ascii 
  format which matches the physical layout of the towers in
  order to help us debug problems online

  as least as far as the endcap is concerned (I can switch the
  order for the barrel if need be.  Specifically, we want the
  following output:

  tower: 06TC05
  gain:  22.64
  ped:   20.90
  thr:   212 [2.8 GeV]

  pedestals           gains, stat, fail, high tower frequency, etc...

  18.7 | -3.0 | 22.5
  -----+------+-----
  22.9 | 20.9 | 15.9
  -----+------+-----
  11.1 | 16.7 | 32.0

  */

  if ( mEEmc ) 
    fprintf(mLogFile,"etow:  %02dT%c%02d\n",sec+1,sub+'A',eta+1);
  if ( mBEmc )
    fprintf(mLogFile,"btow:  %02dt%c%02d\n",sec+1,sub+'a',eta+1);    

  fprintf(mLogFile,"index: %4d\n",tow);
  fprintf(mLogFile,"gain:  %6.3f [ideal=%6.3f]\n",mTowerGain[rdo],mTowerGainIdeal[rdo]);
  fprintf(mLogFile,"ped:   %6.3f\n",mTowerPed[rdo]);
  fprintf(mLogFile,"thr:   %3d [%4.2f GeV]\n",mTowerAdcThreshold[rdo],mTowerAdcThreshold[rdo]/mIdealGainT);
  fprintf(mLogFile,"patch: %6.3f\n",mPatchAdcThreshold[rdo]);
  fprintf(mLogFile,"\n");

  //      14.3 | 14.5 |  8.8      22.6 | 22.9 | 21.6      80   | 80   | 80         0   |  0   |  0           1 |    0 |    0
  fprintf(mLogFile,"loaded peds             gain factors            status bits             mask bits                frequency > thr\n");

  for ( int i_eta=eta+1;i_eta>=eta-1;i_eta-- ) {

    int phi_l = (phi+mNumPhis-1)%mNumPhis;
    int phi_r = (phi+mNumPhis+1)%mNumPhis;
    int phi_c = phi;

    int phis[]={phi_l,phi,phi_r};
    float peds[]={0,0,0};
    float gains[]={0,0,0};
    ushort stats[]={0xff,0xff,0xff};
    ushort fails[]={0xff,0xff,0xff};
    int freqs[]={0,0,0};

    float corr[]={0.,0.,0.};

    // get numbers from db
    if ( i_eta < mNumEtas && i_eta >= 0 ) {

      int tow_l = tower(phi_l,i_eta);
      int tow_r = tower(phi_r,i_eta);
      int tow_c = tower(phi_c,i_eta);

      int rdo_l = mTower2rdo[tow_l];
      int rdo_r = mTower2rdo[tow_r];
      int rdo_c = mTower2rdo[tow_c];

      peds[0]=mTowerPed[rdo_l];peds[1]=mTowerPed[rdo_c];peds[2]=mTowerPed[rdo_r];
      //gains[0]=mTowerAdcCorrection[rdo_l];gains[1]=mTowerAdcCorrection[rdo_c];gains[2]=mTowerAdcCorrection[rdo_r];
      gains[0]=(float)mIdealGainT;
      gains[1]=(float)mIdealGainT;
      gains[2]=(float)mIdealGainT;
      //$$$      gains[0]=mTowerGainIdeal[rdo_l]/m
      stats[0]=mTowerStat[rdo_l];stats[1]=mTowerStat[rdo_c];stats[2]=mTowerStat[rdo_r];
      fails[0]=mTowerFail[rdo_l];fails[1]=mTowerFail[rdo_c];fails[2]=mTowerFail[rdo_r];
      freqs[0]=mTowerFrequency[tow_l];freqs[1]=mTowerFrequency[tow_c];freqs[2]=mTowerFrequency[tow_r];
      
      corr[0]=mTowerAdcCorrection[rdo_l];
      corr[1]=mTowerAdcCorrection[rdo_c];
      corr[2]=mTowerAdcCorrection[rdo_r];


    }

    fprintf(mLogFile, "%4.1f | %4.1f | %4.1f\t", peds[0],peds[1],peds[2] );
    fprintf(mLogFile, "%4.0f | %4.0f | %4.0f\t", gains[0],gains[1],gains[2] );
    fprintf(mLogFile, "%2x   | %2x   | %2x  \t", stats[0],stats[1],stats[2] );
    fprintf(mLogFile, "%2x   | %2x   | %2x  \t", fails[0],fails[1],fails[2] );
    fprintf(mLogFile, "%4.2f | %4.2f | %4.2f\t", 
	    corr[0]*peds[0]/gains[0],
	    corr[1]*peds[1]/gains[1],
	    corr[2]*peds[2]/gains[2]);

    fprintf(mLogFile,"\n");

    if ( i_eta > eta-1 ) {
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "-----+------+-----\t");
      fprintf(mLogFile, "\n" );
    }
    else
      fprintf(mLogFile, "\n\n" );

  }

}

#endif