#include "TMSetupDisplays.h"
#include "TMControlMutex.h"
#include "TMConfig.h"

#include "TMHistogrammingList.h"
#include "TMHistogrammingHitMapEq.h"
#include "TMHistogrammingHitMapChip.h"
#include "TMHistogrammingHitsPerEventEq.h"
#include "TMHistogrammingHitsPerEventAll.h"
#include "TMHistogrammingEventsWithHitsEq.h"
#include "TMHistogrammingEventsWithHitsAll.h"
#include "TMHistogrammingGlobalClusterXY.h"
#include "TMHistogrammingGlobalClusterZY.h"
#include "TMHistogrammingGlobalClusterXYZ.h"
#include "TMHistogrammingGlobalPixelXY.h"
#include "TMHistogrammingGlobalPixelZY.h"
#include "TMHistogrammingGlobalPixelPhiDist.h"
#include "TMHistogrammingGlobalPixelZDist.h"
#include "TMHistogrammingCheckButton.h"
#include "TMHistogrammingPixelMultiplicity.h"
#include "TMHistogrammingPixelMultiplicityHS.h"
#include "TMHistogrammingPixelMultiplicityChip.h"
#include "TMHistogrammingClusterWidthZ.h"
#include "TMHistogrammingClusterWidthPhi.h"
#include "TMHistogrammingGlobalPixelXYZ.h"
#include "TMHistogrammingGlobalPixelPhiDistRef.h"
#include "TMHistogrammingGlobalPixelZDistRef.h"
#include "TMHistogrammingClusterWidthZvsPhiInner.h"
#include "TMHistogrammingClusterWidthZvsPhiOuter.h"
#include "TMHistogrammingClustersPerEqInner.h"
#include "TMHistogrammingClustersPerEqOuter.h"
#include "TMHistogrammingClustersPerHSA.h"
#include "TMHistogrammingClustersPerHSC.h"
#include "TMHistogrammingClustersPerStave.h"
#include "TMHistogrammingClusterSizeInner.h"
#include "TMHistogrammingClusterSizeOuter.h"
#include "TMHistogrammingClusterTypeInner.h"
#include "TMHistogrammingClusterTypeOuter.h"
#include "TMHistogrammingFastorAll.h"
#include "TMHistogrammingFastorEq.h"
#include "TMHistogrammingFastorMissingAll.h"
#include "TMHistogrammingFastorMissingEq.h"
#include "TMHistogrammingFastorProblemAll.h"
#include "TMHistogrammingFastorProblemEq.h"
#include "TMHistogrammingFastorInnerVsOuter.h"
#include "TMHistogrammingBunchCrossingMod4.h"
#include "TMHistogrammingClusterMultiplicity.h"
#include "TMHistogrammingClusterInnerVsOuter.h"
#include "TMHistogrammingEventDump.h"
#include "TMHistogrammingEventDumpPayload.h"
#include "TMHistogrammingEventDumpPayloadErrorOnly.h"
#include "TMHistogrammingEventDumpHeaderShort.h"
#include "TMHistogrammingEventDumpHeaderShortErrorOnly.h"
#include "TMHistogrammingEventDumpHeaderLong.h"
#include "TMHistogrammingEventDumpHeaderLongErrorOnly.h"
#include "TMHistogrammingEventDumpCalibHeader.h"
#include "TMHistogrammingCErrorVsEq.h"
#include "TMHistogrammingCErrorVsEC.h"
#include "TMHistogrammingCErrorFractionVsEq.h"
#include "TMHistogrammingCErrorFractionVsEC.h"
#include "TMHistogrammingCErrorPos.h"
#include "TMHistogrammingCErrorVsEvent.h"
#include "TMHistogrammingCErrorVsECperEq.h"
#include "TMHistogrammingCErrorFractionVsECperEq.h"
#include "TMHistogrammingCErrorMessages.h"


// ADD CODE HERE

#include "TMDisplayingList.h"
#include "TMDisplayingTextView.h"
#include "TMDisplayingShowDet.h"
#include "TMDisplayingShowClusterTypes.h"
#include "TMDisplayingHitMapEq.h"
#include "TMDisplayingHitsPerEventAll.h"
#include "TMDisplayingEventsWithHitsAll.h"
#include "TMDisplayingFastorAll.h"
#include "TMDisplayingFastorMissingAll.h"
#include "TMDisplayingFastorProblemAll.h"
#include "TMDisplayingCErrorShowEC.h"
#include "TMDisplayingCErrorEqChoice.h"
#include "TMDisplayingCheckButton.h"
#include "TMControlSPD.h"
#include "TMAbout.h"
#include "SPDMood.h"
#include <TSystem.h>
#include <TLine.h>
#include <TPolyLine3D.h>
#include <TGFileDialog.h>
#include <TGMenu.h>
#include <pthread.h>

ClassImp(TMSetupDisplays)

extern pthread_mutex_t gMutex;

//___________________________________________________________________________________
void* TMSetupDisplays::THROpenDisplay1(void *arg) {
  TMSetupDisplays* tp = (TMSetupDisplays*)arg;
  tp->ProcessOpenDisplay(tp->GetThreadInt1());
  return 0;
}
//___________________________________________________________________________________
void* TMSetupDisplays::THROpenDisplay4(void *arg) {
  TMSetupDisplays* tp = (TMSetupDisplays*)arg;
  tp->ProcessOpenDisplay(tp->GetThreadInt1(),tp->GetThreadInt2(),tp->GetThreadInt3(),tp->GetThreadHist1());
  return 0;
}
//___________________________________________________________________________________
void* TMSetupDisplays::THRCloseDisplay2(void *arg) {
  TMSetupDisplays* tp = (TMSetupDisplays*)arg;
  tp->ProcessCloseDisplay(tp->GetThreadInt1(),tp->GetThreadInt2());
  return 0;
}
//___________________________________________________________________________________
void* TMSetupDisplays::THRCloseDisplay3(void *arg) {
  TMSetupDisplays* tp = (TMSetupDisplays*)arg;
  tp->ProcessCloseDisplay(tp->GetThreadInt1(),tp->GetThreadInt2(),tp->GetThreadInt3());
  return 0;
}
//___________________________________________________________________________________
void* TMSetupDisplays::THRCloneDisplay3(void *arg) {
  TMSetupDisplays* tp = (TMSetupDisplays*)arg;
  tp->ProcessCloneDisplay(tp->GetThreadInt1(),tp->GetThreadInt2(),tp->GetThreadHist1());
  return 0;
}

//___________________________________________________________________________________
TMSetupDisplays::TMSetupDisplays(const TGWindow *p, UInt_t wid, UInt_t hei) : 
  TGMainFrame(p, wid, hei), 
  fCloneCounter(0), 
  fThreadInt1(0), fThreadInt2(0), fThreadInt3(0), fThreadBool1(kFALSE), 
  fPleaseWait(kFALSE)
{
  fLayout  = new TGLayoutHints(kLHintsLeft | kLHintsExpandX,1,1,1,1);
  fLayout2 = new TGLayoutHints(kLHintsExpandY | kLHintsExpandX,0,0,0,0);
  fLayout3 = new TGLayoutHints(kLHintsRight | kLHintsExpandY,0,0,0,0);
  fLayout4 = new TGLayoutHints(kLHintsCenterX | kLHintsExpandY,0,0,0,0);
  fLayout5 = new TGLayoutHints(kLHintsNormal | kLHintsExpandY,10,10,10,10);
  fLayout6 = new TGLayoutHints(kLHintsExpandY | kLHintsExpandX,0,5,0,0);
  fLayout7 = new TGLayoutHints(kLHintsExpandY,0,0,0,0);
//  fLayout4 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,10,10,10,10);
//  fLayout5 = new TGLayoutHints(kLHintsBottom | kLHintsExpandX,1,1,1,1);

  gClient->GetColorByName("white", white);
  gClient->GetColorByName("red", red);
  gClient->GetColorByName("black", black);

  if (DUMMY_DUMMY != gNrHistogramming) {
    printf("WARNING: TMSetupDisplays::TMSetupDisplays: gNrHistogramming != numberOfIds (%d!=%d)\n",gNrHistogramming,DUMMY_DUMMY);
  }

  for (UInt_t i=0; i<gNrHistogramming; i++) {
    fNrHistogramming[i] = 0;
    fNrPlotsEntry[i] = NULL;
  }

  TGCompositeFrame *tf; // temp frame
  TGCompositeFrame *tf2; // temp frame


  // add menuBar
  TGMenuBar *menuBar = new TGMenuBar(this);
  TGPopupMenu *fileMenu = new TGPopupMenu(fClient->GetRoot());
  fileMenu->AddEntry("Load Default Setup", FILE_LOAD_DEFAULT_SETUP);
  fileMenu->AddEntry("Save Default Setup", FILE_SAVE_DEFAULT_SETUP);
  fileMenu->AddEntry("Load Setup", FILE_LOAD_SETUP);
  fileMenu->AddEntry("Save Setup As", FILE_SAVE_SETUP);
  fileMenu->AddEntry("Clear All", FILE_CLEAR_SETUP);
  fileMenu->Associate(this);
  menuBar->AddPopup("&File", fileMenu, new TGLayoutHints(kLHintsTop | kLHintsLeft));
  AddFrame(menuBar,new TGLayoutHints(kLHintsTop));



  fMainTab = new TGTab(this, wid, hei);
  AddFrame(fMainTab, fLayout2);


  UInt_t id;
  TString label;

  //________________________________________________________________________________________________
  //  GLOBAL DISPLAYS TAB

  fGlobalTab = fMainTab->AddTab("Global Displays");
  fGlobalFrame = new TGCompositeFrame(fGlobalTab,wid,hei,kHorizontalFrame);
  fGlobalTab->AddFrame(fGlobalFrame, fLayout2);

  // GLOBAL CLU __________________________________________________________________
  fGlobalClu = new TGCompositeFrame(fGlobalFrame,280,20,kVerticalFrame | kFixedSize);
  fGlobalFrame->AddFrame(fGlobalClu, fLayout5);

  tf = new TGCompositeFrame(fGlobalClu, 10, 10, kHorizontalFrame);
  fGlobalClu->AddFrame(tf,fLayout);
  fGlobalCluTitle = new TGLabel(tf, "Clusters");
  tf->AddFrame(fGlobalCluTitle, fLayout2);
  tf = new TGCompositeFrame(fGlobalClu, 10, 10, kHorizontalFrame);
  fGlobalClu->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fGlobalCluButtonAllPlus = new TGTextButton(tf2, "+ ALL", GLOBAL_CLU_ALLPLUS);
  tf2->AddFrame(fGlobalCluButtonAllPlus, fLayout4);
  fGlobalCluButtonAllPlus->Associate(this);
  fGlobalCluButtonAllMinus = new TGTextButton(tf2, "- ALL", GLOBAL_CLU_ALLMINUS);
  tf2->AddFrame(fGlobalCluButtonAllMinus, fLayout4);
  fGlobalCluButtonAllMinus->Associate(this);
  fGlobalCluButtonAllShow = new TGTextButton(tf2, "SHOW ALL", GLOBAL_CLU_ALLSHOW);
  tf2->AddFrame(fGlobalCluButtonAllShow, fLayout4);
  fGlobalCluButtonAllShow->Associate(this);

  // CLUSTER XY______
  AddEntry(GLOBAL_CLUSTER_XY, fGlobalClu, "Clu XY");

  // CLUSTER ZY______
  AddEntry(GLOBAL_CLUSTER_ZY, fGlobalClu, "Clu ZY");

  // CLUSTER XYZ______
  AddEntry(GLOBAL_CLUSTER_XYZ, fGlobalClu, "Clu XYZ");

  // CLUSTER MULTIPLICITY______
  AddEntry(CLUSTER_MULTIPLICITY, fGlobalClu, "Clu Mult");

  // CLUSTER INNER VS OUTER______
  AddEntry(CLUSTER_INNER_VS_OUTER, fGlobalClu, "Clu Inner vs Outer");

  // CLUSTERS PER EQ INNER______
  AddEntry(CLUSTERS_PER_EQ_INNER, fGlobalClu, "Clu Per Eq Inner");

  // CLUSTERS PER EQ OUTER______
  AddEntry(CLUSTERS_PER_EQ_OUTER, fGlobalClu, "Clu Per Eq Outer");

  // CLUSTERS PER HS A______
  AddEntry(CLUSTERS_PER_HS_A, fGlobalClu, "Clu Per HS Side A");

  // CLUSTERS PER HS C______
  AddEntry(CLUSTERS_PER_HS_C, fGlobalClu, "Clu Per HS Side C");

  // CLUSTERS PER STAVE______
  AddEntry(CLUSTERS_PER_STAVE, fGlobalClu, "Clu Per Stave");

  // CLUSTER SIZE INNER______
  AddEntry(CLUSTER_SIZE_INNER, fGlobalClu, "Clu Size Inner");

  // CLUSTER SIZE OUTER______
  AddEntry(CLUSTER_SIZE_OUTER, fGlobalClu, "Clu Size Outer");

  // CLUSTER TYPE INNER______
  AddEntry(CLUSTER_TYPE_INNER, fGlobalClu, "Clu Type Inner");

  // CLUSTER TYPE OUTER______
  AddEntry(CLUSTER_TYPE_OUTER, fGlobalClu, "Clu Type Outer");

  // CLUSTER_WIDTH_Z______
  AddEntry(CLUSTER_WIDTH_Z, fGlobalClu, "Clu Wid Z");

  // CLUSTER_WIDTH_PHI______
  AddEntry(CLUSTER_WIDTH_PHI, fGlobalClu, "Clu Wid Phi");

  // CLUSTER_WIDTH_ZvsPHI_INNER______
  AddEntry(CLUSTER_WIDTH_ZVSPHI_INNER, fGlobalClu, "Clu Wid Z vs Phi Inner");

  // CLUSTER_WIDTH_ZvsPHI_OUTER______
  AddEntry(CLUSTER_WIDTH_ZVSPHI_OUTER, fGlobalClu, "Clu Wid Z vs Phi Outer");


  // GLOBAL PIX __________________________________________________________________
  fGlobalPix = new TGCompositeFrame(fGlobalFrame,220,20,kVerticalFrame | kFixedSize);
  fGlobalFrame->AddFrame(fGlobalPix, fLayout5);

  tf = new TGCompositeFrame(fGlobalPix, 10, 10, kHorizontalFrame);
  fGlobalPix->AddFrame(tf,fLayout);
  fGlobalPixTitle = new TGLabel(tf, "Pixels");
  tf->AddFrame(fGlobalPixTitle, fLayout2);
  tf = new TGCompositeFrame(fGlobalPix, 10, 10, kHorizontalFrame);
  fGlobalPix->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fGlobalPixButtonAllPlus = new TGTextButton(tf2, "+ ALL", GLOBAL_PIX_ALLPLUS);
  tf2->AddFrame(fGlobalPixButtonAllPlus, fLayout4);
  fGlobalPixButtonAllPlus->Associate(this);
  fGlobalPixButtonAllMinus = new TGTextButton(tf2, "- ALL", GLOBAL_PIX_ALLMINUS);
  tf2->AddFrame(fGlobalPixButtonAllMinus, fLayout4);
  fGlobalPixButtonAllMinus->Associate(this);
  fGlobalPixButtonAllShow = new TGTextButton(tf2, "SHOW ALL", GLOBAL_PIX_ALLSHOW);
  tf2->AddFrame(fGlobalPixButtonAllShow, fLayout4);
  fGlobalPixButtonAllShow->Associate(this);

  // GLOBAL PIXEL XY______
  AddEntry(GLOBAL_PIXEL_XY, fGlobalPix, "Pix XY");

  // GLOBAL PIXEL ZY______
  AddEntry(GLOBAL_PIXEL_ZY, fGlobalPix, "Pix ZY");

  // GLOBAL_PIXEL_XYZ______
  AddEntry(GLOBAL_PIXEL_XYZ, fGlobalPix, "Pix XYZ");

  // PIXEL MULTIPLICITY______
  AddEntry(PIXEL_MULTIPLICITY, fGlobalPix, "Pix Mult");

  // PIXEL MULTIPLICITY_HS______
  AddEntry(PIXEL_MULTIPLICITYHS, fGlobalPix, "Pix Mult HS");

  // PIXEL MULTIPLICITY_CHIP______
  AddEntry(PIXEL_MULTIPLICITYCHIP, fGlobalPix, "Pix Mult Chip");

  // GLOBAL PIXEL PHI DIST______
  AddEntry(GLOBAL_PIXEL_PHIDIST, fGlobalPix, "Pix Phi");

  // GLOBAL PIXEL Z DIST______
  AddEntry(GLOBAL_PIXEL_ZDIST, fGlobalPix, "Pix Z");

  // GLOBAL PIXEL PHI DISTREF______
  AddEntry(GLOBAL_PIXEL_PHIDISTREF, fGlobalPix, "Pix Phi Ref");

  // GLOBAL PIXEL Z DISTREF______
  AddEntry(GLOBAL_PIXEL_ZDISTREF, fGlobalPix, "Pix Z Ref");



  // GLOBAL OTHER __________________________________________________________________
  fGlobalOther = new TGCompositeFrame(fGlobalFrame,220,20,kVerticalFrame | kFixedSize);
  fGlobalFrame->AddFrame(fGlobalOther, fLayout5);

  tf = new TGCompositeFrame(fGlobalOther, 10, 10, kHorizontalFrame);
  fGlobalOther->AddFrame(tf,fLayout);
  fGlobalOtherTitle = new TGLabel(tf, "Other");
  tf->AddFrame(fGlobalOtherTitle, fLayout2);
  tf = new TGCompositeFrame(fGlobalOther, 10, 10, kHorizontalFrame);
  fGlobalOther->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fGlobalOtherButtonAllPlus = new TGTextButton(tf2, "+ ALL", GLOBAL_OTHER_ALLPLUS);
  tf2->AddFrame(fGlobalOtherButtonAllPlus, fLayout4);
  fGlobalOtherButtonAllPlus->Associate(this);
  fGlobalOtherButtonAllMinus = new TGTextButton(tf2, "- ALL", GLOBAL_OTHER_ALLMINUS);
  tf2->AddFrame(fGlobalOtherButtonAllMinus, fLayout4);
  fGlobalOtherButtonAllMinus->Associate(this);
  fGlobalOtherButtonAllShow = new TGTextButton(tf2, "SHOW ALL", GLOBAL_OTHER_ALLSHOW);
  tf2->AddFrame(fGlobalOtherButtonAllShow, fLayout4);
  fGlobalOtherButtonAllShow->Associate(this);

  // BC mod 4
  AddEntry(BC_MOD_4, fGlobalOther, "BC mod 4");





  //________________________________________________________________________________________________
  //  PIXEL DISPLAYS TAB

  fPixelTab = fMainTab->AddTab("Pixel Displays");
  fPixelFrame = new TGCompositeFrame(fPixelTab,wid,hei,kHorizontalFrame);
  fPixelTab->AddFrame(fPixelFrame, fLayout2);

  // HITMAPS__________________________________________________________________
  fHitMap = new TGCompositeFrame(fPixelFrame,180,20,kVerticalFrame | kFixedSize);
  fPixelFrame->AddFrame(fHitMap, fLayout5);

  tf = new TGCompositeFrame(fHitMap, 10, 10, kHorizontalFrame);
  fHitMap->AddFrame(tf,fLayout);
  fHitMapTitle = new TGLabel(tf, "HIT-MAPS");
  tf->AddFrame(fHitMapTitle, fLayout2);
  tf = new TGCompositeFrame(fHitMap, 10, 10, kHorizontalFrame);
  fHitMap->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fHitMapButtonAllPlus = new TGTextButton(tf2, "+ ALL", HITMAP_ALLPLUS);
  tf2->AddFrame(fHitMapButtonAllPlus, fLayout4);
  fHitMapButtonAllPlus->Associate(this);
  fHitMapButtonAllMinus = new TGTextButton(tf2, "- ALL", HITMAP_ALLMINUS);
  tf2->AddFrame(fHitMapButtonAllMinus, fLayout4);
  fHitMapButtonAllMinus->Associate(this);
  fHitMapButtonAllShow = new TGTextButton(tf2, "SHOW ALL", HITMAP_ALLSHOW);
  tf2->AddFrame(fHitMapButtonAllShow, fLayout4);
  fHitMapButtonAllShow->Associate(this);

  for (UInt_t eq=0; eq<20; eq++) {
    id = HITMAPEQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fHitMap, label.Data());
  }


  // HITSPEREVENT__________________________________________________________________
  fHitsPerEvent = new TGCompositeFrame(fPixelFrame,180,20,kVerticalFrame | kFixedSize);
  fPixelFrame->AddFrame(fHitsPerEvent, fLayout5);

  tf = new TGCompositeFrame(fHitsPerEvent, 10, 10, kHorizontalFrame);
  fHitsPerEvent->AddFrame(tf,fLayout);
  fHitsPerEventTitle = new TGLabel(tf, "HITS-PER-EVENT");
  tf->AddFrame(fHitsPerEventTitle, fLayout2);
  tf = new TGCompositeFrame(fHitsPerEvent, 10, 10, kHorizontalFrame);
  fHitsPerEvent->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fHitsPerEventButtonAllPlus = new TGTextButton(tf2, "+ ALL", HITSPEREVENT_ALLPLUS);
  tf2->AddFrame(fHitsPerEventButtonAllPlus, fLayout4);
  fHitsPerEventButtonAllPlus->Associate(this);
  fHitsPerEventButtonAllMinus = new TGTextButton(tf2, "- ALL", HITSPEREVENT_ALLMINUS);
  tf2->AddFrame(fHitsPerEventButtonAllMinus, fLayout4);
  fHitsPerEventButtonAllMinus->Associate(this);
  fHitsPerEventButtonAllShow = new TGTextButton(tf2, "SHOW ALL", HITSPEREVENT_ALLSHOW);
  tf2->AddFrame(fHitsPerEventButtonAllShow, fLayout4);
  fHitsPerEventButtonAllShow->Associate(this);

  AddEntry(HITSPEREVENTALL, fHitsPerEvent, "All Eq");

  for (UInt_t eq=0; eq<20; eq++) {
    id = HITSPEREVENTEQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fHitsPerEvent, label.Data());
  }

  // EVENTSWITHHITS__________________________________________________________________
  fEventsWithHits = new TGCompositeFrame(fPixelFrame,180,20,kVerticalFrame | kFixedSize);
  fPixelFrame->AddFrame(fEventsWithHits, fLayout5);

  tf = new TGCompositeFrame(fEventsWithHits, 10, 10, kHorizontalFrame);
  fEventsWithHits->AddFrame(tf,fLayout);
  fEventsWithHitsTitle = new TGLabel(tf, "EVENTS-WITH-HITS");
  tf->AddFrame(fEventsWithHitsTitle, fLayout2);
  tf = new TGCompositeFrame(fEventsWithHits, 10, 10, kHorizontalFrame);
  fEventsWithHits->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fEventsWithHitsButtonAllPlus = new TGTextButton(tf2, "+ ALL", EVENTSWITHHITS_ALLPLUS);
  tf2->AddFrame(fEventsWithHitsButtonAllPlus, fLayout4);
  fEventsWithHitsButtonAllPlus->Associate(this);
  fEventsWithHitsButtonAllMinus = new TGTextButton(tf2, "- ALL", EVENTSWITHHITS_ALLMINUS);
  tf2->AddFrame(fEventsWithHitsButtonAllMinus, fLayout4);
  fEventsWithHitsButtonAllMinus->Associate(this);
  fEventsWithHitsButtonAllShow = new TGTextButton(tf2, "SHOW ALL", EVENTSWITHHITS_ALLSHOW);
  tf2->AddFrame(fEventsWithHitsButtonAllShow, fLayout4);
  fEventsWithHitsButtonAllShow->Associate(this);

  AddEntry(EVENTSWITHHITSALL, fEventsWithHits, "All Eq");

  for (UInt_t eq=0; eq<20; eq++) {
    id = EVENTSWITHHITSEQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fEventsWithHits, label.Data());
  }




  //________________________________________________________________________________________________
  //  FASTOR DISPLAYS TAB

  fFastorTab = fMainTab->AddTab("FastOR Displays");
  fFastorFrame = new TGCompositeFrame(fFastorTab,wid,hei,kHorizontalFrame);
  fFastorTab->AddFrame(fFastorFrame, fLayout2);

  //  FASTOR SIGNALS __________________________________________________________________
  fFastorEq = new TGCompositeFrame(fFastorFrame,180,20,kVerticalFrame | kFixedSize);
  fFastorFrame->AddFrame(fFastorEq, fLayout5);

  tf = new TGCompositeFrame(fFastorEq, 10, 10, kHorizontalFrame);
  fFastorEq->AddFrame(tf,fLayout);
  fFastorEqTitle = new TGLabel(tf, "FastOR Signals");
  tf->AddFrame(fFastorEqTitle, fLayout2);

  tf = new TGCompositeFrame(fFastorEq, 10, 10, kHorizontalFrame);
  fFastorEq->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fFastorEqButtonAllPlus = new TGTextButton(tf2, "+ ALL", FASTOR_EQ_ALLPLUS);
  tf2->AddFrame(fFastorEqButtonAllPlus, fLayout4);
  fFastorEqButtonAllPlus->Associate(this);
  fFastorEqButtonAllMinus = new TGTextButton(tf2, "- ALL", FASTOR_EQ_ALLMINUS);
  tf2->AddFrame(fFastorEqButtonAllMinus, fLayout4);
  fFastorEqButtonAllMinus->Associate(this);
  fFastorEqButtonAllShow = new TGTextButton(tf2, "SHOW ALL", FASTOR_EQ_ALLSHOW);
  tf2->AddFrame(fFastorEqButtonAllShow, fLayout4);
  fFastorEqButtonAllShow->Associate(this);

  AddEntry(FASTOR_ALL, fFastorEq, "All Eq");

  for (UInt_t eq=0; eq<20; eq++) {
    id = FASTOR_EQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fFastorEq, label.Data());
  }

  //  FASTOR MISSING __________________________________________________________________
  fFastorMissing = new TGCompositeFrame(fFastorFrame,180,20,kVerticalFrame | kFixedSize);
  fFastorFrame->AddFrame(fFastorMissing, fLayout5);

  tf = new TGCompositeFrame(fFastorMissing, 10, 10, kHorizontalFrame);
  fFastorMissing->AddFrame(tf,fLayout);
  fFastorMissingTitle = new TGLabel(tf, "Ratio of Missing FastOR Signals");
  tf->AddFrame(fFastorMissingTitle, fLayout2);

  tf = new TGCompositeFrame(fFastorMissing, 10, 10, kHorizontalFrame);
  fFastorMissing->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fFastorMissingButtonAllPlus = new TGTextButton(tf2, "+ ALL", FASTOR_MISSING_EQ_ALLPLUS);
  tf2->AddFrame(fFastorMissingButtonAllPlus, fLayout4);
  fFastorMissingButtonAllPlus->Associate(this);
  fFastorMissingButtonAllMinus = new TGTextButton(tf2, "- ALL", FASTOR_MISSING_EQ_ALLMINUS);
  tf2->AddFrame(fFastorMissingButtonAllMinus, fLayout4);
  fFastorMissingButtonAllMinus->Associate(this);
  fFastorMissingButtonAllShow = new TGTextButton(tf2, "SHOW ALL", FASTOR_MISSING_EQ_ALLSHOW);
  tf2->AddFrame(fFastorMissingButtonAllShow, fLayout4);
  fFastorMissingButtonAllShow->Associate(this);

  AddEntry(FASTOR_MISSING_ALL, fFastorMissing, "All Eq");

  for (UInt_t eq=0; eq<20; eq++) {
    id = FASTOR_MISSING_EQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fFastorMissing, label.Data());
  }

  //  FASTOR PROBLEMS __________________________________________________________________
  fFastorProblem = new TGCompositeFrame(fFastorFrame,180,20,kVerticalFrame | kFixedSize);
  fFastorFrame->AddFrame(fFastorProblem, fLayout5);

  tf = new TGCompositeFrame(fFastorProblem, 10, 10, kHorizontalFrame);
  fFastorProblem->AddFrame(tf,fLayout);
  fFastorProblemTitle = new TGLabel(tf, "Events with hits w/o FastOR");
  tf->AddFrame(fFastorProblemTitle, fLayout2);

  tf = new TGCompositeFrame(fFastorProblem, 10, 10, kHorizontalFrame);
  fFastorProblem->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fFastorProblemButtonAllPlus = new TGTextButton(tf2, "+ ALL", FASTOR_PROBLEM_EQ_ALLPLUS);
  tf2->AddFrame(fFastorProblemButtonAllPlus, fLayout4);
  fFastorProblemButtonAllPlus->Associate(this);
  fFastorProblemButtonAllMinus = new TGTextButton(tf2, "- ALL", FASTOR_PROBLEM_EQ_ALLMINUS);
  tf2->AddFrame(fFastorProblemButtonAllMinus, fLayout4);
  fFastorProblemButtonAllMinus->Associate(this);
  fFastorProblemButtonAllShow = new TGTextButton(tf2, "SHOW ALL", FASTOR_PROBLEM_EQ_ALLSHOW);
  tf2->AddFrame(fFastorProblemButtonAllShow, fLayout4);
  fFastorProblemButtonAllShow->Associate(this);

  AddEntry(FASTOR_PROBLEM_ALL, fFastorProblem, "All Eq");

  for (UInt_t eq=0; eq<20; eq++) {
    id = FASTOR_PROBLEM_EQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fFastorProblem, label.Data());
  }

  //  FASTOR ADDITIONAL __________________________________________________________________
  fFastorAdditional = new TGCompositeFrame(fFastorFrame,230,20,kVerticalFrame | kFixedSize);
  fFastorFrame->AddFrame(fFastorAdditional, fLayout5);

  tf = new TGCompositeFrame(fFastorAdditional, 10, 10, kHorizontalFrame);
  fFastorAdditional->AddFrame(tf,fLayout);
  fFastorAdditionalTitle = new TGLabel(tf, "Additional Plots");
  tf->AddFrame(fFastorAdditionalTitle, fLayout2);

  tf = new TGCompositeFrame(fFastorAdditional, 10, 10, kHorizontalFrame);
  fFastorAdditional->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fFastorAdditionalButtonAllPlus = new TGTextButton(tf2, "+ ALL", FASTOR_ADDITIONAL_ALLPLUS);
  tf2->AddFrame(fFastorAdditionalButtonAllPlus, fLayout4);
  fFastorAdditionalButtonAllPlus->Associate(this);
  fFastorAdditionalButtonAllMinus = new TGTextButton(tf2, "- ALL", FASTOR_ADDITIONAL_ALLMINUS);
  tf2->AddFrame(fFastorAdditionalButtonAllMinus, fLayout4);
  fFastorAdditionalButtonAllMinus->Associate(this);
  fFastorAdditionalButtonAllShow = new TGTextButton(tf2, "SHOW ALL", FASTOR_ADDITIONAL_ALLSHOW);
  tf2->AddFrame(fFastorAdditionalButtonAllShow, fLayout4);
  fFastorAdditionalButtonAllShow->Associate(this);

  // Inner vs Outer
  AddEntry(FASTOR_INNER_VS_OUTER, fFastorAdditional, "Inner vs Outer");




  //________________________________________________________________________________________________
  //  CONS ERROR DISPLAYS TAB

  fCErrorTab = fMainTab->AddTab("Cons Error Displays");
  fCErrorFrame = new TGCompositeFrame(fCErrorTab,wid,hei,kHorizontalFrame);
  fCErrorTab->AddFrame(fCErrorFrame, fLayout2);

  //  NUMBER ONE __________________________________________________________________
  fCErrorOne = new TGCompositeFrame(fCErrorFrame,285,20,kVerticalFrame | kFixedSize);
  fCErrorFrame->AddFrame(fCErrorOne, fLayout5);

  tf = new TGCompositeFrame(fCErrorOne, 10, 10, kHorizontalFrame);
  fCErrorOne->AddFrame(tf,fLayout);
  fCErrorOneTitle = new TGLabel(tf, "Main Selections");
  tf->AddFrame(fCErrorOneTitle, fLayout2);

  tf = new TGCompositeFrame(fCErrorOne, 10, 10, kHorizontalFrame);
  fCErrorOne->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fCErrorOneButtonAllPlus = new TGTextButton(tf2, "+ ALL", CERROR_ONE_ALLPLUS);
  tf2->AddFrame(fCErrorOneButtonAllPlus, fLayout4);
  fCErrorOneButtonAllPlus->Associate(this);
  fCErrorOneButtonAllMinus = new TGTextButton(tf2, "- ALL", CERROR_ONE_ALLMINUS);
  tf2->AddFrame(fCErrorOneButtonAllMinus, fLayout4);
  fCErrorOneButtonAllMinus->Associate(this);
  fCErrorOneButtonAllShow = new TGTextButton(tf2, "SHOW ALL", CERROR_ONE_ALLSHOW);
  tf2->AddFrame(fCErrorOneButtonAllShow, fLayout4);
  fCErrorOneButtonAllShow->Associate(this);

  AddEntry(CERROR_VS_EQ, fCErrorOne, "Errors Vs Equipment");
  AddEntry(CERROR_VS_EC, fCErrorOne, "Errors Vs Error Code");
  AddEntry(CERROR_FRACTION_VS_EQ, fCErrorOne, "Events with Errors Vs Eq");
  AddEntry(CERROR_FRACTION_VS_EC, fCErrorOne, "Events with Errors Vs EC");
  AddEntry(CERROR_VS_EVENT, fCErrorOne, "Errors Vs Event");
  AddEntry(CERROR_POS, fCErrorOne, "Bytes Read Vs Event");
  AddEntry(CERROR_MESSAGES, fCErrorOne, "Error Messages");

  //  NUMBER TWO __________________________________________________________________
  fCErrorTwo = new TGCompositeFrame(fCErrorFrame,180,20,kVerticalFrame | kFixedSize);
  fCErrorFrame->AddFrame(fCErrorTwo, fLayout5);

  tf = new TGCompositeFrame(fCErrorTwo, 10, 10, kHorizontalFrame);
  fCErrorTwo->AddFrame(tf,fLayout);
  fCErrorTwoTitle = new TGLabel(tf, "Errors Vs Error Code");
  tf->AddFrame(fCErrorTwoTitle, fLayout2);

  tf = new TGCompositeFrame(fCErrorTwo, 10, 10, kHorizontalFrame);
  fCErrorTwo->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fCErrorTwoButtonAllPlus = new TGTextButton(tf2, "+ ALL", CERROR_TWO_ALLPLUS);
  tf2->AddFrame(fCErrorTwoButtonAllPlus, fLayout4);
  fCErrorTwoButtonAllPlus->Associate(this);
  fCErrorTwoButtonAllMinus = new TGTextButton(tf2, "- ALL", CERROR_TWO_ALLMINUS);
  tf2->AddFrame(fCErrorTwoButtonAllMinus, fLayout4);
  fCErrorTwoButtonAllMinus->Associate(this);
  fCErrorTwoButtonAllShow = new TGTextButton(tf2, "SHOW ALL", CERROR_TWO_ALLSHOW);
  tf2->AddFrame(fCErrorTwoButtonAllShow, fLayout4);
  fCErrorTwoButtonAllShow->Associate(this);

  for (UInt_t eq=0; eq<20; eq++) {
    id = CERROR_VS_EC_EQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fCErrorTwo, label.Data());
  }



  //  NUMBER THREE __________________________________________________________________
  fCErrorThree = new TGCompositeFrame(fCErrorFrame,180,20,kVerticalFrame | kFixedSize);
  fCErrorFrame->AddFrame(fCErrorThree, fLayout5);

  tf = new TGCompositeFrame(fCErrorThree, 10, 10, kHorizontalFrame);
  fCErrorThree->AddFrame(tf,fLayout);
  fCErrorThreeTitle = new TGLabel(tf, "Events with Errors Vs EC");
  tf->AddFrame(fCErrorThreeTitle, fLayout2);

  tf = new TGCompositeFrame(fCErrorThree, 10, 10, kHorizontalFrame);
  fCErrorThree->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fCErrorThreeButtonAllPlus = new TGTextButton(tf2, "+ ALL", CERROR_THREE_ALLPLUS);
  tf2->AddFrame(fCErrorThreeButtonAllPlus, fLayout4);
  fCErrorThreeButtonAllPlus->Associate(this);
  fCErrorThreeButtonAllMinus = new TGTextButton(tf2, "- ALL", CERROR_THREE_ALLMINUS);
  tf2->AddFrame(fCErrorThreeButtonAllMinus, fLayout4);
  fCErrorThreeButtonAllMinus->Associate(this);
  fCErrorThreeButtonAllShow = new TGTextButton(tf2, "SHOW ALL", CERROR_THREE_ALLSHOW);
  tf2->AddFrame(fCErrorThreeButtonAllShow, fLayout4);
  fCErrorThreeButtonAllShow->Associate(this);

  for (UInt_t eq=0; eq<20; eq++) {
    id = CERROR_FRACTION_VS_EC_EQ_0 + eq;
    label = Form("Eq %2d", eq);
    AddEntry(id, fCErrorThree, label.Data());
  }


  //________________________________________________________________________________________________
  //  DUMP DISPLAYS TAB

  fDumpTab = fMainTab->AddTab("Dump Displays");
  fDumpFrame = new TGCompositeFrame(fDumpTab,wid,hei,kHorizontalFrame);
  fDumpTab->AddFrame(fDumpFrame, fLayout2);

  //  DUMP ONE __________________________________________________________________
  fDumpMain = new TGCompositeFrame(fDumpFrame,220,20,kVerticalFrame | kFixedSize);
  fDumpFrame->AddFrame(fDumpMain, fLayout5);

  tf = new TGCompositeFrame(fDumpMain, 10, 10, kHorizontalFrame);
  fDumpMain->AddFrame(tf,fLayout);
  fDumpMainTitle = new TGLabel(tf, "All Events");
  tf->AddFrame(fDumpMainTitle, fLayout2);

  tf = new TGCompositeFrame(fDumpMain, 10, 10, kHorizontalFrame);
  fDumpMain->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fDumpMainButtonAllPlus = new TGTextButton(tf2, "+ ALL", DUMP_MAIN_ALLPLUS);
  tf2->AddFrame(fDumpMainButtonAllPlus, fLayout4);
  fDumpMainButtonAllPlus->Associate(this);
  fDumpMainButtonAllMinus = new TGTextButton(tf2, "- ALL", DUMP_MAIN_ALLMINUS);
  tf2->AddFrame(fDumpMainButtonAllMinus, fLayout4);
  fDumpMainButtonAllMinus->Associate(this);
  fDumpMainButtonAllShow = new TGTextButton(tf2, "SHOW ALL", DUMP_MAIN_ALLSHOW);
  tf2->AddFrame(fDumpMainButtonAllShow, fLayout4);
  fDumpMainButtonAllShow->Associate(this);

  AddEntry(DUMP_PAYLOAD, fDumpMain, "Payload");
  AddEntry(DUMP_HEADER_SHORT, fDumpMain, "Header Short");
  AddEntry(DUMP_HEADER_LONG, fDumpMain, "Header Long");
  AddEntry(DUMP_CALIB_HEADER, fDumpMain, "Calib Header");

  //  DUMP TWO __________________________________________________________________
  fDumpErrorOnly = new TGCompositeFrame(fDumpFrame,220,20,kVerticalFrame | kFixedSize);
  fDumpFrame->AddFrame(fDumpErrorOnly, fLayout5);

  tf = new TGCompositeFrame(fDumpErrorOnly, 10, 10, kHorizontalFrame);
  fDumpErrorOnly->AddFrame(tf,fLayout);
  fDumpErrorOnlyTitle = new TGLabel(tf, "Events With Errors Only");
  tf->AddFrame(fDumpErrorOnlyTitle, fLayout2);

  tf = new TGCompositeFrame(fDumpErrorOnly, 10, 10, kHorizontalFrame);
  fDumpErrorOnly->AddFrame(tf,fLayout);
  tf2 = new TGCompositeFrame(tf, 10, 10, kHorizontalFrame);
  tf->AddFrame(tf2,fLayout4);
  fDumpErrorOnlyButtonAllPlus = new TGTextButton(tf2, "+ ALL", DUMP_ERROR_ONLY_ALLPLUS);
  tf2->AddFrame(fDumpErrorOnlyButtonAllPlus, fLayout4);
  fDumpErrorOnlyButtonAllPlus->Associate(this);
  fDumpErrorOnlyButtonAllMinus = new TGTextButton(tf2, "- ALL", DUMP_ERROR_ONLY_ALLMINUS);
  tf2->AddFrame(fDumpErrorOnlyButtonAllMinus, fLayout4);
  fDumpErrorOnlyButtonAllMinus->Associate(this);
  fDumpErrorOnlyButtonAllShow = new TGTextButton(tf2, "SHOW ALL", DUMP_ERROR_ONLY_ALLSHOW);
  tf2->AddFrame(fDumpErrorOnlyButtonAllShow, fLayout4);
  fDumpErrorOnlyButtonAllShow->Associate(this);

  AddEntry(DUMP_ERROR_ONLY_PAYLOAD, fDumpErrorOnly, "Payload");
  AddEntry(DUMP_ERROR_ONLY_HEADER_SHORT, fDumpErrorOnly, "Header Short");
  AddEntry(DUMP_ERROR_ONLY_HEADER_LONG, fDumpErrorOnly, "Header Long");





//!!!  //________________________________________________________________________________________________
//!!!  //  NEW DISPLAYS TAB
//!!!
//!!!  fNewTab = fMainTab->AddTab("New Displays");
//!!!  fNewFrame = new TGCompositeFrame(fNewTab,wid,hei,kHorizontalFrame);
//!!!  fNewTab->AddFrame(fNewFrame, fLayout2);
//!!!
//!!!  //  NEW STUFF __________________________________________________________________
//!!!  fNewStuff = new TGCompositeFrame(fNewFrame,220,20,kVerticalFrame | kFixedSize);
//!!!  fNewFrame->AddFrame(fNewStuff, fLayout5);
//!!!
//!!!  tf = new TGCompositeFrame(fNewStuff, 10, 10, kHorizontalFrame);
//!!!  fNewStuff->AddFrame(tf,fLayout);
//!!!//  fNewStuffTitle = new TGLabel(tf, "New Stuff");
//!!!//  tf->AddFrame(fNewStuffTitle, fLayout2);
//!!!
//!!!

  // ADD CODE HERE





  SetWindowName("SPD-MOOD Setup");

  SetWMSizeHints(150, 150, 2000, 1000, 0, 0);
  SetWMSize(wid, hei);
  MapSubwindows();
  Resize();
  MapWindow();

  DontCallClose();
  
}
//___________________________________________________________________________________
TMSetupDisplays::~TMSetupDisplays() {
//!!!  delete[] fF;
//!!!  delete fTop;
//!!!  delete fLayout;
//!!!  delete fLayout2;
//!!!  delete fLayout3;
//!!!  delete fLayout4;
//!!!  delete fLayout5;
//!!!  delete[] fCheck;
//!!!  delete[] fTextB;
//!!!  delete[] fTextBall;
}
//___________________________________________________________________________________
void TMSetupDisplays::ClearSetup() {
  TMDisplayingList::Instance()->Clear();
  TMHistogrammingList::Instance()->Clear();
  // set correct label numbering etc:
  for (UInt_t i=0; i<gNrHistogramming; i++) {
    fNrHistogramming[i] = 0;
    UpdateNrLabel(i);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::SaveDefaultSetup() {
  TString defaultFileName = Form("%s/config/default.setupconf",getenv("SPDMOOD_SITE"));
  SaveSetup(defaultFileName.Data());
}
//___________________________________________________________________________________
void TMSetupDisplays::SaveSetupChoice() {
  TGFileInfo file_info;
  new TGFileDialog(gClient->GetRoot(), this, kFDSave, &file_info);
  TString fileName = Form("%s", file_info.fFilename);
  if (fileName.CompareTo("(null)")!=0) {
    SaveSetup(file_info.fFilename);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::SaveSetup(const Char_t *fileName) {
  ofstream sFile;
  sFile.open(fileName);
  if (!sFile) {
    printf("ERROR: TMSetupDisplays::SaveSetup: Could not save setup to file '%s'.\n",fileName);
    return;
  }

  sFile << "HISTOGRAMMING" << "\n";
  TMHistogrammingList::Instance()->SaveEntries(sFile);


  sFile << "DISPLAYING" << "\n";
  TMDisplayingList::Instance()->SaveEntries(sFile);

  sFile.close();
}
//___________________________________________________________________________________
void TMSetupDisplays::LoadDefaultSetup() {
  TString defaultFileName = Form("%s/config/default.setupconf",getenv("SPDMOOD_SITE"));
  LoadSetup(defaultFileName.Data());
}
//___________________________________________________________________________________
void TMSetupDisplays::LoadSetupChoice() {
  TGFileInfo file_info;
  new TGFileDialog(gClient->GetRoot(), this, kFDOpen, &file_info);
  TString fileName = Form("%s", file_info.fFilename);
  if (fileName.CompareTo("(null)")!=0) {
    LoadSetup(file_info.fFilename);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::LoadSetup(const Char_t *fileName) {
  ifstream sFile;
  sFile.open(fileName);
  if (!sFile) {
    printf("ERROR: TMSetupDisplays::LoadSetup: Could not load setup from file '%s'.\n",fileName);
    return;
  }

  ClearSetup(); // clean up whatever is present...

  Int_t mode = -1;
  while (1) {

    Char_t tmpVal[200];
    sFile >> tmpVal;
    if (sFile.eof()) break;

    TString text = Form("%s",tmpVal);
    if (text.CompareTo("HISTOGRAMMING") == 0) mode=0;
    else if (text.CompareTo("DISPLAYING") == 0) mode=1;
    else if (mode!=-1) {

      if (mode==0) {
	UInt_t d_id = atoi(tmpVal);
	UInt_t id = d_id - ZERO_TO_DISP_ID_SHIFT;
	sFile >> tmpVal; UInt_t plotNr = atoi(tmpVal);
	sFile >> tmpVal; Bool_t paused = (atoi(tmpVal) != 0);
	sFile >> tmpVal; Bool_t autoreset = (atoi(tmpVal) != 0);
	sFile >> tmpVal; ULong_t trigmask0 = strtoul(tmpVal,NULL,0);
	sFile >> tmpVal; ULong_t trigmask1 = strtoul(tmpVal,NULL,0);
	
	if (id<gNrHistogramming) {
	  if (fNrHistogramming[id]<=plotNr) {
	    for (UInt_t pn=fNrHistogramming[id]; pn<=plotNr; pn++) {
	      ProcessPlusHistogramming(id);
	    }
	  }
	  TMHistogrammingList::Instance()->SetPaused(d_id, plotNr, paused);
	  TMHistogrammingList::Instance()->SetAutoReset(d_id, plotNr, autoreset);
	  TMHistogrammingList::Instance()->SetTriggerMask(d_id, plotNr, trigmask0, trigmask1);
	}
 
      }
      
      else if (mode==1) {
	UInt_t d_id = atoi(tmpVal);
	sFile >> tmpVal; UInt_t plotNr = atoi(tmpVal);
	sFile >> tmpVal; UInt_t cloneId = atoi(tmpVal);
	sFile >> tmpVal; Bool_t autoupdate = (atoi(tmpVal)!=0);
	sFile >> tmpVal; UInt_t eventMod = atoi(tmpVal);
	sFile >> tmpVal; UInt_t eventModEq = atoi(tmpVal);
	sFile >> tmpVal; Int_t posX = atoi(tmpVal);
	sFile >> tmpVal; Int_t posY = atoi(tmpVal);
	sFile >> tmpVal; UInt_t posW = atoi(tmpVal);
	sFile >> tmpVal; UInt_t posH = atoi(tmpVal);

	ProcessOpenDisplay(d_id, plotNr, cloneId);
	TMDisplayingList::Instance()->SetAutoUpdate(d_id, plotNr, cloneId, autoupdate);
	TMDisplayingList::Instance()->SetEventMod(d_id, plotNr, cloneId, eventMod);
	TMDisplayingList::Instance()->SetEventModEq(d_id, plotNr, cloneId, eventModEq);
	TMDisplayingList::Instance()->ResizeDisplay(d_id, plotNr, cloneId, posX, posY, posW, posH);

      }

    }

  }
  
  sFile.close();
}
//___________________________________________________________________________________
void TMSetupDisplays::AddEntry(UInt_t id, TGCompositeFrame *parent, const Char_t *label) {
  if (id>=gNrHistogramming) {
    printf("ERROR TMSetupDisplays::AddEntry: id (%d) out of bounds\n",id);
  }
  UInt_t d_id = id+ZERO_TO_DISP_ID_SHIFT;
  UInt_t p_id = id+ZERO_TO_PLUS_ID_SHIFT;
  UInt_t m_id = id+ZERO_TO_MINUS_ID_SHIFT;

  TGCompositeFrame *tf = new TGCompositeFrame(parent, 10, 10, kHorizontalFrame);
  parent->AddFrame(tf,fLayout);

  fLabelEntry[id] = new TGLabel(tf,label);
  fLabelEntry[id]->SetTextJustify(kTextLeft);
  tf->AddFrame(fLabelEntry[id],fLayout6);

  TGCompositeFrame *tf2 = new TGCompositeFrame(tf, 135, 10, kHorizontalFrame | kFixedSize);
  tf->AddFrame(tf2,fLayout3);

  fPlusEntry[id] = new TGTextButton(tf2, "+", p_id);
  tf2->AddFrame(fPlusEntry[id],fLayout2);
  fPlusEntry[id]->Associate(this);

  fMinusEntry[id] = new TGTextButton(tf2, "-", m_id);
  tf2->AddFrame(fMinusEntry[id],fLayout2);
  fMinusEntry[id]->Associate(this);

  fNrPlotsEntry[id] = new TGLabel(tf2,"0");
  tf2->AddFrame(fNrPlotsEntry[id],fLayout2);

  TGCompositeFrame *tf3 = new TGCompositeFrame(tf2, 45, 10, kHorizontalFrame | kFixedSize);
  tf2->AddFrame(tf3,fLayout7);

  fShowEntry[id] = new TGTextButton(tf3, "SHOW", d_id);
  tf3->AddFrame(fShowEntry[id],fLayout2);
  fShowEntry[id]->Associate(this);

}
//___________________________________________________________________________________
UInt_t TMSetupDisplays::GetNewCloneId() {
  fCloneCounter++;
  return fCloneCounter;
}
//___________________________________________________________________________________
UInt_t TMSetupDisplays::GetChipID(UInt_t eq, UInt_t hs, UInt_t chip) {
  return ZERO_TO_DISP_ID_SHIFT + HITMAPCHIP_0_0_0 + 60*eq + 10*hs + chip;
}




//___________________________________________________________________________________
void TMSetupDisplays::ThreadProcessCloseDisplay(UInt_t d_id, UInt_t plotNr) {
  TMControlSPD::Instance()->SlowDownRunning(1);
  fThreadInt1 = d_id;
  fThreadInt2 = plotNr;
  pthread_t newTh;
  pthread_create(&newTh, NULL, THRCloseDisplay2, (void *)this);
  if (!(TMControlMutex::Instance()->DoIHaveLock())) pthread_join(newTh, NULL);
  TMControlSPD::Instance()->SetTimeSafeUpdate();
}
//___________________________________________________________________________________
void TMSetupDisplays::ThreadProcessCloseDisplay(UInt_t d_id, UInt_t plotNr, UInt_t cloneId) {
  TMControlSPD::Instance()->SlowDownRunning(1);
  fThreadInt1 = d_id;
  fThreadInt2 = plotNr;
  fThreadInt3 = cloneId;
  pthread_t newTh;
  pthread_create(&newTh, NULL, THRCloseDisplay3, (void *)this);
  if (!(TMControlMutex::Instance()->DoIHaveLock())) pthread_join(newTh, NULL);
  TMControlSPD::Instance()->SetTimeSafeUpdate();
}
//___________________________________________________________________________________
void TMSetupDisplays::ThreadProcessOpenDisplay(UInt_t d_id) {
  TMControlSPD::Instance()->SlowDownRunning(1);
  fThreadInt1 = d_id;
  pthread_t newTh;
  pthread_create(&newTh, NULL, THROpenDisplay1, (void *)this);
  if (!(TMControlMutex::Instance()->DoIHaveLock())) pthread_join(newTh, NULL);
  TMControlSPD::Instance()->SetTimeSafeUpdate();
}
//___________________________________________________________________________________
void TMSetupDisplays::ThreadProcessOpenDisplay(UInt_t d_id, UInt_t plotNr, UInt_t cloneId, TMHistogramming *hist) {
  TMControlSPD::Instance()->SlowDownRunning(1);
  TMControlSPD::Instance()->HoldRunning();
  fThreadInt1 = d_id;
  fThreadInt2 = plotNr;
  fThreadInt3 = cloneId;
  fThreadHist1 = hist;
  pthread_t newTh;
  pthread_create(&newTh, NULL, THROpenDisplay4, (void *)this);
  if (!(TMControlMutex::Instance()->DoIHaveLock())) pthread_join(newTh, NULL);
  TMControlSPD::Instance()->SetTimeSafeUpdate();
  TMControlSPD::Instance()->ContinueRunning();
}
//___________________________________________________________________________________
void TMSetupDisplays::ThreadProcessOpenDisplayID(UInt_t id, UInt_t span) {
  for (UInt_t d_id=id+ZERO_TO_DISP_ID_SHIFT; d_id<id+ZERO_TO_DISP_ID_SHIFT+span; d_id++) {
    ThreadProcessOpenDisplay(d_id);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ThreadProcessCloneDisplay(UInt_t d_id, UInt_t plotNr, TMHistogramming *hist) {
  TMControlSPD::Instance()->SlowDownRunning(1);
  fThreadInt1 = d_id;
  fThreadInt2 = plotNr;
  fThreadHist1 = hist;
  pthread_t newTh;
  pthread_create(&newTh, NULL, THRCloneDisplay3, (void *)this);
  if (!(TMControlMutex::Instance()->DoIHaveLock())) pthread_join(newTh, NULL);
  TMControlSPD::Instance()->SetTimeSafeUpdate();
}





//___________________________________________________________________________________
void TMSetupDisplays::ProcessCloneDisplay(UInt_t d_id, UInt_t plotNr, TMHistogramming *hist) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProCloneDi");
    TMControlMutex::Instance()->ITakeLock();
  }
  ProcessOpenDisplay(d_id, plotNr, GetNewCloneId(), hist);
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessAutoResetDisplay(UInt_t d_id, UInt_t plotNr, Bool_t autoreset) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProAutoResDisp");
    TMControlMutex::Instance()->ITakeLock();
  }
  TMDisplayingList::Instance()->SetAutoReset(d_id,plotNr,autoreset);
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessPauseDisplay(UInt_t d_id, UInt_t plotNr, Bool_t pause) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProPausDisp");
    TMControlMutex::Instance()->ITakeLock();
  }
  TMHistogrammingList::Instance()->SetPaused(d_id,plotNr,pause);
  TMDisplayingList::Instance()->SetPaused(d_id,plotNr,pause);
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessKillDisplay(UInt_t d_id, UInt_t plotNr) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProKillDisp");
    TMControlMutex::Instance()->ITakeLock();
  }
  TMDisplayingList::Instance()->RemoveDisplayingObj(d_id,plotNr);
  TMDisplayingList::Instance()->ReducePlotNr(d_id,plotNr);
  TMHistogrammingList::Instance()->RemoveHistogrammingObj(d_id,plotNr);
  UInt_t id = d_id - ZERO_TO_DISP_ID_SHIFT;
  fNrHistogramming[id]--;
  UpdateNrLabel(id);
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessCloseDisplay(UInt_t d_id, UInt_t plotNr) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProCloseDisp");
    TMControlMutex::Instance()->ITakeLock();
  }
  TMDisplayingList::Instance()->RemoveDisplayingObj(d_id,plotNr);
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessCloseDisplay(UInt_t d_id, UInt_t plotNr, UInt_t cloneId) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProCloseDisp4");
    TMControlMutex::Instance()->ITakeLock();
  }
  TMDisplayingList::Instance()->RemoveDisplayingObj(d_id,plotNr,cloneId);
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessCloseAllDisplays(Bool_t hold) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProCloseAllDisp");
    TMControlMutex::Instance()->ITakeLock();
  }
  TMDisplayingList::Instance()->Clear();
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessOpenDisplayID(UInt_t id, UInt_t span) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProOpenDispId");
    TMControlMutex::Instance()->ITakeLock();
  }
  for (UInt_t d_id=id+ZERO_TO_DISP_ID_SHIFT; d_id<id+ZERO_TO_DISP_ID_SHIFT+span; d_id++) {
    ProcessOpenDisplay(d_id);
  }
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessOpenDisplay(UInt_t d_id) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProOpenDisp1");
    TMControlMutex::Instance()->ITakeLock();
  }
  UInt_t id = d_id - ZERO_TO_DISP_ID_SHIFT;
  if (id>=gNrHistogramming) {
    printf("ERROR TMSetupDisplays::ProcessOpenDisplay: id (%d) out of bounds\n",id);
  }
  UInt_t nrHistos = fNrHistogramming[id];
  if (nrHistos == 0) {
    printf("TMSetupDisplays::ProcessOpenDisplay: No histogramming object for ID %d\n",id);
    if (!ihavelock) {
      TMControlMutex::Instance()->IReleaseLock();
      pthread_mutex_unlock(&gMutex);
    }
    return;
  }
  for (UInt_t plotNr=0; plotNr<nrHistos; plotNr++) {
    ProcessOpenDisplay(d_id, plotNr);
  }
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessOpenDisplay(UInt_t d_id, UInt_t plotNr, UInt_t cloneId) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProOpenDisp3");
    TMControlMutex::Instance()->ITakeLock();
  }
  UInt_t id = d_id - ZERO_TO_DISP_ID_SHIFT;
  if (id>=gNrHistogramming) {
    printf("ERROR TMSetupDisplays::ProcessOpenDisplay: id (%d) out of bounds\n",id);
  }
  UInt_t nrHistos = fNrHistogramming[id];
  if (plotNr >= nrHistos) {
    printf("TMSetupDisplays::ProcessOpenDisplay: No histogramming object for ID %d, plot %d\n",id,plotNr);
    if (!ihavelock) {
      TMControlMutex::Instance()->IReleaseLock();
      pthread_mutex_unlock(&gMutex);
    }
    return;
  }
  if (!TMDisplayingList::Instance()->Find(d_id,plotNr,cloneId)) {
    TMHistogramming *hist = TMHistogrammingList::Instance()->GetHistogrammingObj(d_id,plotNr);
    ProcessOpenDisplay(d_id,plotNr,cloneId,hist);
  }
  else {
    TMDisplayingList::Instance()->HighLight(d_id,plotNr);
  }
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessOpenDisplay(UInt_t d_id, UInt_t plotNr, UInt_t cloneId, TMHistogramming *hist) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProOpenDisp4");
    TMControlMutex::Instance()->ITakeLock();
  }
  UInt_t id = d_id - ZERO_TO_DISP_ID_SHIFT;
  if (id>=gNrHistogramming) {
    printf("ERROR TMSetupDisplays::ProcessOpenDisplay: id (%d) out of bounds\n",id);
    if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
    return;
  }

  TMDisplaying * dispObj = NULL;

  if (id <= HITMAPEQ_19 && id>=HITMAPEQ_0) {
    UInt_t eq = id-HITMAPEQ_0;
    TString frameName = Form("Hit Map  Eq %d",eq);
    dispObj = new TMDisplayingHitMapEq(frameName.Data(),gClient->GetRoot(),1000,700,hist,d_id,plotNr,cloneId);
  }

  else if (id <= HITMAPCHIP_19_5_9 && id>=HITMAPCHIP_0_0_0) {
    UInt_t tmpVal = id-HITMAPCHIP_0_0_0;
    UInt_t chip = tmpVal%10;
    UInt_t hs   = (tmpVal/10)%6;
    UInt_t eq   = tmpVal/60;
    TString frameName = Form("Hit Map  Eq %d  HS %d  Chip %d",eq,hs,chip);
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),600,500,hist,d_id,plotNr,cloneId);
  }

  else if (id<=HITSPEREVENTEQ_19 && id>=HITSPEREVENTEQ_0) {
    UInt_t eq = id-HITSPEREVENTEQ_0;
    TString frameName = Form("Hits Per Event And Chip  Eq %d",eq);
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==HITSPEREVENTALL) {
    TString frameName = Form("Hits Per Event And Eq");
    dispObj = new TMDisplayingHitsPerEventAll(frameName.Data(),gClient->GetRoot(),600,600,hist,d_id,plotNr,cloneId);
  }

  else if (id<=EVENTSWITHHITSEQ_19 && id>=EVENTSWITHHITSEQ_0) {
    UInt_t eq = id-EVENTSWITHHITSEQ_0;
    TString frameName = Form("Events With Hits Per Event And Chip  Eq %d",eq);
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==EVENTSWITHHITSALL) {
    TString frameName = Form("Events With Hits Per Event And Eq");
    dispObj = new TMDisplayingEventsWithHitsAll(frameName.Data(),gClient->GetRoot(),600,600,hist,d_id,plotNr,cloneId);
  }

  else if (id==GLOBAL_CLUSTER_XY) {
    TString frameName = Form("Global Cluster XY");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_CLUSTER_ZY) {
    TString frameName = Form("Global Cluster ZY");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_CLUSTER_XYZ) {
    TString frameName = Form("Global Cluster XYZ");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_XY) {
    TString frameName = Form("Global Pixel XY");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_ZY) {
    TString frameName = Form("Global Pixel ZY");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_PHIDIST) {
    TString frameName = Form("Global Pixel Phi Dist");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_ZDIST) {
    TString frameName = Form("Global Pixel Z Dist");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==PIXEL_MULTIPLICITY) {
    TString frameName = Form("Pixel Multiplicity Per Event");
    dispObj = new TMDisplayingCheckButton(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId,"Dump Pixel Multiplicity To Terminal For Each Event");
  }
  else if (id==PIXEL_MULTIPLICITYHS) {
    TString frameName = Form("Pixel Multiplicity Per HS");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==PIXEL_MULTIPLICITYCHIP) {
    TString frameName = Form("Pixel Multiplicity Per Chip");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_WIDTH_Z) {
    TString frameName = Form("Cluster Width Z");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_WIDTH_PHI) {
    TString frameName = Form("Cluster Width Phi");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_XYZ) {
    TString frameName = Form("Global Pixel XYZ");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_PHIDISTREF) {
    TString frameName = Form("Global Pixel Phi Dist Ref");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==GLOBAL_PIXEL_ZDISTREF) {
    TString frameName = Form("Global Pixel Z Dist Ref");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_WIDTH_ZVSPHI_INNER) {
    TString frameName = Form("Cluster Width Z vs Phi (inner layer)");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_WIDTH_ZVSPHI_OUTER) {
    TString frameName = Form("Cluster Width Z vs Phi (outer layer)");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTERS_PER_EQ_INNER) {
    TString frameName = Form("Clusters Per Eq Inner Layer");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTERS_PER_EQ_OUTER) {
    TString frameName = Form("Clusters Per Eq Outer Layer");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTERS_PER_HS_A) {
    TString frameName = Form("Clusters Per HS Side A");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTERS_PER_HS_C) {
    TString frameName = Form("Clusters Per HS Side C");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTERS_PER_STAVE) {
    TString frameName = Form("Clusters Per Stave");
    dispObj = new TMDisplayingShowDet(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_SIZE_INNER) {
    TString frameName = Form("Cluster Size Inner Layer");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_SIZE_OUTER) {
    TString frameName = Form("Cluster Size Outer Layer");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_TYPE_INNER) {
    TString frameName = Form("Cluster Type Inner Layer");
    dispObj = new TMDisplayingShowClusterTypes(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_TYPE_OUTER) {
    TString frameName = Form("Cluster Type Outer Layer");
    dispObj = new TMDisplayingShowClusterTypes(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_MULTIPLICITY) {
    TString frameName = Form("Cluster Multiplicity");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CLUSTER_INNER_VS_OUTER) {
    TString frameName = Form("Cluster Multiplicity Inner vs Outer Layer");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==FASTOR_ALL) {
    TString frameName = Form("FastOR Signals Per Eq");
    dispObj = new TMDisplayingFastorAll(frameName.Data(),gClient->GetRoot(),600,600,hist,d_id,plotNr,cloneId);
  }
  else if (id<=FASTOR_EQ_19 && id>=FASTOR_EQ_0) {
    UInt_t eq = id-FASTOR_EQ_0;
    TString frameName = Form("FastOR Signals Eq %d (Per Chip)",eq);
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==FASTOR_MISSING_ALL) {
    TString frameName = Form("Ratio of Missing FastOR Signals(Per Eq)");
    dispObj = new TMDisplayingFastorMissingAll(frameName.Data(),gClient->GetRoot(),600,600,hist,d_id,plotNr,cloneId);
  }
  else if (id<=FASTOR_MISSING_EQ_19 && id>=FASTOR_MISSING_EQ_0) {
    UInt_t eq = id-FASTOR_MISSING_EQ_0;
    TString frameName = Form("Ratio of Missing FastOR Signals Eq %d (Per Chip)",eq);
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==FASTOR_PROBLEM_ALL) {
    TString frameName = Form("Events with Hits w/o FastOR (Per Eq)");
    dispObj = new TMDisplayingFastorProblemAll(frameName.Data(),gClient->GetRoot(),600,600,hist,d_id,plotNr,cloneId);
  }
  else if (id<=FASTOR_PROBLEM_EQ_19 && id>=FASTOR_PROBLEM_EQ_0) {
    UInt_t eq = id-FASTOR_PROBLEM_EQ_0;
    TString frameName = Form("Events with Hits w/o FastOR Eq %d (Per Chip)",eq);
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==FASTOR_INNER_VS_OUTER) {
    TString frameName = Form("Number FastORs Inner vs Outer Layer");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_VS_EQ) {
    TString frameName = Form("Number Consistency Errors vs Equipment");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_VS_EC) {
    TString frameName = Form("Number Consistency Errors vs Error Code");
    dispObj = new TMDisplayingCErrorShowEC(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_FRACTION_VS_EQ) {
    TString frameName = Form("Fraction of Events with Consistency Errors vs Equipment");
    dispObj = new TMDisplaying(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_FRACTION_VS_EC) {
    TString frameName = Form("Fraction of Events with Consistency Errors vs Error Code");
    dispObj = new TMDisplayingCErrorShowEC(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_POS) {
    TString frameName = Form("Bytes Read (for Consistency Errors) vs Event Number");
    dispObj = new TMDisplayingCErrorEqChoice(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_MESSAGES) {
    TString frameName = Form("Consistency Error Messages");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==CERROR_VS_EVENT) {
    TString frameName = Form("Number of Errors vs Event Number");
    dispObj = new TMDisplayingCErrorEqChoice(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id<=CERROR_VS_EC_EQ_19 && id>=CERROR_VS_EC_EQ_0) {
    UInt_t eq = id-CERROR_VS_EC_EQ_0;
    TString frameName = Form("Errors vs Error Code  Eq %d",eq);
    dispObj = new TMDisplayingCErrorShowEC(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id<=CERROR_FRACTION_VS_EC_EQ_19 && id>=CERROR_FRACTION_VS_EC_EQ_0) {
    UInt_t eq = id-CERROR_FRACTION_VS_EC_EQ_0;
    TString frameName = Form("Fraction of Events with Errors vs Error Code  Eq %d",eq);
    dispObj = new TMDisplayingCErrorShowEC(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_PAYLOAD) {
    TString frameName = Form("Payload Dump");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_HEADER_SHORT) {
    TString frameName = Form("Header Short Dump");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_HEADER_LONG) {
    TString frameName = Form("Header Long Dump");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_CALIB_HEADER) {
    TString frameName = Form("Calib Header Dump");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_ERROR_ONLY_PAYLOAD) {
    TString frameName = Form("Payload Dump (Events with errors)");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_ERROR_ONLY_HEADER_SHORT) {
    TString frameName = Form("Header Short Dump (Events with errors)");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==DUMP_ERROR_ONLY_HEADER_LONG) {
    TString frameName = Form("Header Long Dump (Events with errors)");
    dispObj = new TMDisplayingTextView(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId);
  }
  else if (id==BC_MOD_4) {
    TString frameName = Form("BC mod 4");
    dispObj = new TMDisplayingCheckButton(frameName.Data(),gClient->GetRoot(),800,600,hist,d_id,plotNr,cloneId,"Dump BC To Terminal For Each Event");
  }


  // ADD CODE HERE


  else {
    printf("TMSetupDisplays::ProcessOpenDisplay: Unknown Display ID (%d)!\n",id);
    if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
    return;
  }

  TMDisplayingList::Instance()->AddDisplayingObj(dispObj,d_id,plotNr,cloneId);

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessPlusHistogramming(UInt_t id, UInt_t span) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProPlusHi");
    TMControlMutex::Instance()->ITakeLock();
  }
  for (UInt_t i_id=id; i_id<id+span; i_id++) {
    ProcessPlusHistogramming(i_id);
  }
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessPlusHistogramming(UInt_t id) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProPlusHi1");
    TMControlMutex::Instance()->ITakeLock();
  }

  if (id>=gNrHistogramming) {
    printf("Error: TMSetupDisplays::ProcessPlusHistogramming: id (%d) out of bounds\n",id);
    if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
    return;
  }


  UInt_t plotNr = fNrHistogramming[id];
  UInt_t d_id = id + ZERO_TO_DISP_ID_SHIFT;

  if (id <= HITMAPEQ_19 && id>=HITMAPEQ_0) {
    UInt_t eq = id-HITMAPEQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingHitMapEq(eq),d_id,plotNr);
    // create histogramming objs for all 60 chips:
    for (UInt_t hs=0; hs<6; hs++) {
      for (UInt_t chip=0; chip<10; chip++) {
	UInt_t chip_d_id = GetChipID(eq,hs,chip);
	UInt_t chip_id = chip_d_id - ZERO_TO_DISP_ID_SHIFT;
	UInt_t chip_plotNr = fNrHistogramming[chip_id];
	TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingHitMapChip(eq,hs,chip),chip_d_id,chip_plotNr);
	fNrHistogramming[chip_id]++;
      }
    }
  }
  //  else if (id <= HITMAPCHIP_19_5_9 && id>=HITMAPCHIP_0_0_0) {}
  
  else if (id<=HITSPEREVENTEQ_19 && id>=HITSPEREVENTEQ_0) {
    UInt_t eq = id-HITSPEREVENTEQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingHitsPerEventEq(eq),d_id,plotNr);
  }
  else if (id==HITSPEREVENTALL) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingHitsPerEventAll(),d_id,plotNr);
  }

  else if (id<=EVENTSWITHHITSEQ_19 && id>=EVENTSWITHHITSEQ_0) {
    UInt_t eq = id-EVENTSWITHHITSEQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventsWithHitsEq(eq),d_id,plotNr);
  }
  else if (id==EVENTSWITHHITSALL) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventsWithHitsAll(),d_id,plotNr);
  }

  else if (id==GLOBAL_CLUSTER_XY) {
    TLine *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      tla[mod] = new TLine(x1,y1,x2,y2);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalClusterXY(tla),d_id,plotNr);
  }
  else if (id==GLOBAL_CLUSTER_ZY) {
    TLine *tla[960];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t y1,y2,z1,z2;
      Float_t y3,y4,z3,z4;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      z1=TMControlSPD::Instance()->GetGlobalZ();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,255);
      z2=TMControlSPD::Instance()->GetGlobalZ();
      y2=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      z3=TMControlSPD::Instance()->GetGlobalZ();
      y3=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,0);
      z4=TMControlSPD::Instance()->GetGlobalZ();
      y4=TMControlSPD::Instance()->GetGlobalY();
      tla[mod+240*0] = new TLine(z1,y1,z2,y2);
      tla[mod+240*1] = new TLine(z2,y2,z3,y3);
      tla[mod+240*2] = new TLine(z3,y3,z4,y4);
      tla[mod+240*3] = new TLine(z4,y4,z1,y1);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalClusterZY(tla),d_id,plotNr);
  }
  else if (id==GLOBAL_CLUSTER_XYZ) {
    TPolyLine3D *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2,z1,z2;
      Float_t x3,x4,y3,y4,z3,z4;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      z1=TMControlSPD::Instance()->GetGlobalZ();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      z2=TMControlSPD::Instance()->GetGlobalZ();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x3=TMControlSPD::Instance()->GetGlobalX();
      y3=TMControlSPD::Instance()->GetGlobalY();
      z3=TMControlSPD::Instance()->GetGlobalZ();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,0);
      x4=TMControlSPD::Instance()->GetGlobalX();
      y4=TMControlSPD::Instance()->GetGlobalY();
      z4=TMControlSPD::Instance()->GetGlobalZ();
      tla[mod] = new TPolyLine3D();
      tla[mod]->SetPoint(0,z1,x1,y1);
      tla[mod]->SetPoint(1,z2,x2,y2);
      tla[mod]->SetPoint(2,z3,x3,y3);
      tla[mod]->SetPoint(3,z4,x4,y4);
      tla[mod]->SetPoint(4,z1,x1,y1);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalClusterXYZ(tla),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_XY) {
    TLine *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      tla[mod] = new TLine(x1,y1,x2,y2);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelXY(tla),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_ZY) {
    TLine *tla[960];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t y1,y2,z1,z2;
      Float_t y3,y4,z3,z4;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      z1=TMControlSPD::Instance()->GetGlobalZ();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,255);
      z2=TMControlSPD::Instance()->GetGlobalZ();
      y2=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      z3=TMControlSPD::Instance()->GetGlobalZ();
      y3=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,0);
      z4=TMControlSPD::Instance()->GetGlobalZ();
      y4=TMControlSPD::Instance()->GetGlobalY();
      tla[mod+240*0] = new TLine(z1,y1,z2,y2);
      tla[mod+240*1] = new TLine(z2,y2,z3,y3);
      tla[mod+240*2] = new TLine(z3,y3,z4,y4);
      tla[mod+240*3] = new TLine(z4,y4,z1,y1);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelZY(tla),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_XYZ) {
    TPolyLine3D *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2,z1,z2;
      Float_t x3,x4,y3,y4,z3,z4;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      z1=TMControlSPD::Instance()->GetGlobalZ();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      z2=TMControlSPD::Instance()->GetGlobalZ();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x3=TMControlSPD::Instance()->GetGlobalX();
      y3=TMControlSPD::Instance()->GetGlobalY();
      z3=TMControlSPD::Instance()->GetGlobalZ();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,0);
      x4=TMControlSPD::Instance()->GetGlobalX();
      y4=TMControlSPD::Instance()->GetGlobalY();
      z4=TMControlSPD::Instance()->GetGlobalZ();
      tla[mod] = new TPolyLine3D();
      tla[mod]->SetPoint(0,z1,x1,y1);
      tla[mod]->SetPoint(1,z2,x2,y2);
      tla[mod]->SetPoint(2,z3,x3,y3);
      tla[mod]->SetPoint(3,z4,x4,y4);
      tla[mod]->SetPoint(4,z1,x1,y1);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelXYZ(tla),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_PHIDIST) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelPhiDist(),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_ZDIST) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelZDist(),d_id,plotNr);
  }
  else if (id==PIXEL_MULTIPLICITY) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingPixelMultiplicity(),d_id,plotNr);
  }
  else if (id==PIXEL_MULTIPLICITYHS) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingPixelMultiplicityHS(),d_id,plotNr);
  }
  else if (id==PIXEL_MULTIPLICITYCHIP) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingPixelMultiplicityChip(),d_id,plotNr);
  }
  else if (id==CLUSTER_WIDTH_Z) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterWidthZ(),d_id,plotNr);
  }
  else if (id==CLUSTER_WIDTH_PHI) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterWidthPhi(),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_PHIDISTREF) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelPhiDistRef(),d_id,plotNr);
  }
  else if (id==GLOBAL_PIXEL_ZDISTREF) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingGlobalPixelZDistRef(),d_id,plotNr);
  }
  else if (id==CLUSTER_WIDTH_ZVSPHI_INNER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterWidthZvsPhiInner(),d_id,plotNr);
  }
  else if (id==CLUSTER_WIDTH_ZVSPHI_OUTER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterWidthZvsPhiOuter(),d_id,plotNr);
  }
  else if (id==CLUSTERS_PER_EQ_INNER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClustersPerEqInner(),d_id,plotNr);
  }
  else if (id==CLUSTERS_PER_EQ_OUTER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClustersPerEqOuter(),d_id,plotNr);
  }
  else if (id==CLUSTERS_PER_HS_A) {
    TLine *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      tla[mod] = new TLine(x1,y1,x2,y2);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClustersPerHSA(tla),d_id,plotNr);
  }
  else if (id==CLUSTERS_PER_HS_C) {
    TLine *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      tla[mod] = new TLine(x1,y1,x2,y2);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClustersPerHSC(tla),d_id,plotNr);
  }
  else if (id==CLUSTERS_PER_STAVE) {
    TLine *tla[240];
    for (UInt_t mod=0; mod<240; mod++) {
      Float_t x1,x2,y1,y2;
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,0,0);
      x1=TMControlSPD::Instance()->GetGlobalX();
      y1=TMControlSPD::Instance()->GetGlobalY();
      TMControlSPD::Instance()->TransformClusterToGlobal(mod,159,255);
      x2=TMControlSPD::Instance()->GetGlobalX();
      y2=TMControlSPD::Instance()->GetGlobalY();
      tla[mod] = new TLine(x1,y1,x2,y2);
    }
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClustersPerStave(tla),d_id,plotNr);
  }
  else if (id==CLUSTER_SIZE_INNER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterSizeInner(),d_id,plotNr);
  }
  else if (id==CLUSTER_SIZE_OUTER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterSizeOuter(),d_id,plotNr);
  }
  else if (id==CLUSTER_TYPE_INNER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterTypeInner(),d_id,plotNr);
  }
  else if (id==CLUSTER_TYPE_OUTER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterTypeOuter(),d_id,plotNr);
  }
  else if (id==CLUSTER_MULTIPLICITY) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterMultiplicity(),d_id,plotNr);
  }
  else if (id==CLUSTER_INNER_VS_OUTER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingClusterInnerVsOuter(),d_id,plotNr);
  }
  else if (id==FASTOR_ALL) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorAll(),d_id,plotNr);
  }
  else if (id<=FASTOR_EQ_19 && id>=FASTOR_EQ_0) {
    UInt_t eq = id-FASTOR_EQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorEq(eq),d_id,plotNr);
  }
  else if (id==FASTOR_MISSING_ALL) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorMissingAll(),d_id,plotNr);
  }
  else if (id<=FASTOR_MISSING_EQ_19 && id>=FASTOR_MISSING_EQ_0) {
    UInt_t eq = id-FASTOR_MISSING_EQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorMissingEq(eq),d_id,plotNr);
  }
  else if (id==FASTOR_PROBLEM_ALL) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorProblemAll(),d_id,plotNr);
  }
  else if (id<=FASTOR_PROBLEM_EQ_19 && id>=FASTOR_PROBLEM_EQ_0) {
    UInt_t eq = id-FASTOR_PROBLEM_EQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorProblemEq(eq),d_id,plotNr);
  }
  else if (id==FASTOR_INNER_VS_OUTER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingFastorInnerVsOuter(),d_id,plotNr);
  }
  else if (id==CERROR_VS_EQ) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorVsEq(),d_id,plotNr);
  }
  else if (id==CERROR_VS_EC) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorVsEC(),d_id,plotNr);
  }
  else if (id==CERROR_FRACTION_VS_EQ) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorFractionVsEq(),d_id,plotNr);
  }
  else if (id==CERROR_FRACTION_VS_EC) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorFractionVsEC(),d_id,plotNr);
  }
  else if (id==CERROR_POS) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorPos(),d_id,plotNr);
  }
  else if (id==CERROR_MESSAGES) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorMessages(),d_id,plotNr);
  }
  else if (id<=CERROR_VS_EC_EQ_19 && id>=CERROR_VS_EC_EQ_0) {
    UInt_t eq = id-CERROR_VS_EC_EQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorVsECperEq(eq),d_id,plotNr);
  }
  else if (id<=CERROR_FRACTION_VS_EC_EQ_19 && id>=CERROR_FRACTION_VS_EC_EQ_0) {
    UInt_t eq = id-CERROR_FRACTION_VS_EC_EQ_0;
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorFractionVsECperEq(eq),d_id,plotNr);
  }
  else if (id==CERROR_VS_EVENT) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingCErrorVsEvent(),d_id,plotNr);
  }
  else if (id==DUMP_PAYLOAD) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpPayload(),d_id,plotNr);
  }
  else if (id==DUMP_HEADER_SHORT) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpHeaderShort(),d_id,plotNr);
  }
  else if (id==DUMP_HEADER_LONG) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpHeaderLong(),d_id,plotNr);
  }
  else if (id==DUMP_CALIB_HEADER) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpCalibHeader(),d_id,plotNr);
  }
  else if (id==DUMP_ERROR_ONLY_PAYLOAD) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpPayloadErrorOnly(),d_id,plotNr);
  }
  else if (id==DUMP_ERROR_ONLY_HEADER_SHORT) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpHeaderShortErrorOnly(),d_id,plotNr);
  }
  else if (id==DUMP_ERROR_ONLY_HEADER_LONG) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingEventDumpHeaderLongErrorOnly(),d_id,plotNr);
  }
  else if (id==BC_MOD_4) {
    TMHistogrammingList::Instance()->AddHistogrammingObj(new TMHistogrammingBunchCrossingMod4(),d_id,plotNr);
  }


  // ADD CODE HERE



  else {
    if (!ihavelock) {
      TMControlMutex::Instance()->IReleaseLock();
      pthread_mutex_unlock(&gMutex);
    }
    return;
  }

  fNrHistogramming[id]++;
  UpdateNrLabel(id);

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessMinusHistogramming(UInt_t id, UInt_t span) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProMinusHi2");
    TMControlMutex::Instance()->ITakeLock();
  }
  for (UInt_t i_id=id; i_id<id+span; i_id++) {
    ProcessMinusHistogramming(i_id);
  }
  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessMinusHistogramming(UInt_t id) {
  Bool_t ihavelock = TMControlMutex::Instance()->DoIHaveLock();
  if (!ihavelock) {
    pthread_mutex_lock(&gMutex);
    //    printf("TMSetupDisp::ProMinusHi1");
    TMControlMutex::Instance()->ITakeLock();
  }

  if (id>=gNrHistogramming) {
    printf("Error: TMSetupDisplays::ProcessMinusHistogramming: id (%d) out of bounds\n",id);
    if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
    return;
  }

  if (fNrHistogramming[id]==0) {
    UpdateNrLabel(id);
    if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
    return;
  }
  UInt_t d_id = id + ZERO_TO_DISP_ID_SHIFT;

  if (id < gNrHistogramming) {
    fNrHistogramming[id]--;
    TMDisplayingList::Instance()->RemoveDisplayingObj(d_id,fNrHistogramming[id]);
    TMHistogrammingList::Instance()->RemoveHistogrammingObj(d_id,fNrHistogramming[id]);
  }

  // in case of eq hitmap; do also the chips:
  if (id <= HITMAPEQ_19 && id>=HITMAPEQ_0) {
    UInt_t eq = id-HITMAPEQ_0;
    for (UInt_t hs=0; hs<6; hs++) {
      for (UInt_t chip=0; chip<10; chip++) {
	UInt_t chip_d_id = GetChipID(eq,hs,chip);
	UInt_t chip_id = chip_d_id - ZERO_TO_DISP_ID_SHIFT;
	fNrHistogramming[chip_id]--;
	TMDisplayingList::Instance()->RemoveDisplayingObj(chip_d_id,fNrHistogramming[chip_id]);
	TMHistogrammingList::Instance()->RemoveHistogrammingObj(chip_d_id,fNrHistogramming[chip_id]);
      }
    }
  }



  // ADD CODE HERE maybe



  UpdateNrLabel(id);

  if (!ihavelock) {
    TMControlMutex::Instance()->IReleaseLock();
    pthread_mutex_unlock(&gMutex);
  }
}
//___________________________________________________________________________________
void TMSetupDisplays::UpdateNrLabel(UInt_t id) {
  if (id>=gNrHistogramming) {
    printf("Error: TMSetupDisplays::UpdateNrLabel: id (%d) out of bounds\n",id);
    return;
  }

  if (fNrPlotsEntry[id] == NULL) return;

  TString newText = Form("%d",fNrHistogramming[id]);
  Pixel_t newColor;
  if      (fNrHistogramming[id] > 1) newColor = red;
  else if (fNrHistogramming[id] > 0) newColor = white;
  else                               newColor = black;


  fNrPlotsEntry[id]->SetText(newText.Data());
  fNrPlotsEntry[id]->SetTextColor(newColor);
  gSystem->ProcessEvents();
}
//___________________________________________________________________________________
void TMSetupDisplays::UpdateNrLabelLater(UInt_t id, UInt_t span) {
  if (id>=gNrHistogramming) {
    printf("Error: TMSetupDisplays::UpdateNrLabelLater: id (%d) out of bounds\n",id);
    return;
  }

  for (UInt_t i_id=id; i_id<id+span; i_id++) {

    if (fNrPlotsEntry[i_id] == NULL) continue;

    fNrPlotsEntry[i_id]->SetText("");
  }
  gSystem->ProcessEvents();
}
//___________________________________________________________________________________
void TMSetupDisplays::ProcessDeadNoisy() {
  for (UInt_t eq=0; eq<20; eq++) {
    if (TMControlSPD::Instance()->IsEqActive(eq)) {
      UInt_t d_id = HITMAPEQ_0 + eq + ZERO_TO_DISP_ID_SHIFT;
      // assuming always histogramming obj 0 to be valid for this search:
      TMHistogrammingHitMapEq *hist = (TMHistogrammingHitMapEq*) TMHistogrammingList::Instance()->GetHistogrammingObj(d_id,0); 
      if (hist!=NULL) {
	TH2F *hitmap = hist->GetHitMap();

	// get nr of noisy *****************************************************************
	printf("*** Eq %d   Nr noisy: ***\n",eq);
	printf("|HS|chip 0|chip 1|chip 2|chip 3|chip 4|chip 5|chip 6|chip 7|chip 8|chip 9|\n");
	
	for (UInt_t hs=0; hs<6; hs++) {
	  printf("|%d|",hs);
	  for (UInt_t chip=0; chip<10; chip++) {
	    Int_t nrNoisy=0;
	    for (UInt_t col=0; col<32; col++) {
	      for (UInt_t row=0; row<256; row++) {
		
		UInt_t binx = chip*32+col+1;
		UInt_t biny = (5-hs)*256+row+1;
		
		if (hitmap->GetBinContent(binx,biny) > TMConfig::Instance()->GETnoisyLimit()) {
		  nrNoisy++;
		  // mask it as well!
		  TMControlSPD::Instance()->SetMask2(eq,hs,chip,col,row,kTRUE);
		}
	      }
	    }
	    printf("%d|",nrNoisy);
	  }
	  printf("\n");
	}
	
	// get nr of dead *****************************************************************
	printf("*** Eq %d   Nr dead: ***\n",eq);
	printf("|HS|chip 0|chip 1|chip 2|chip 3|chip 4|chip 5|chip 6|chip 7|chip 8|chip 9|\n");
	for (UInt_t hs=0; hs<6; hs++) {
	  printf("|%d|",hs);
	  for (UInt_t chip=0; chip<10; chip++) {
	    Int_t nrDead=0;
	    for (UInt_t col=0; col<32; col++) {
	      for (UInt_t row=0; row<256; row++) {
		
		UInt_t binx = chip*32+col+1;
		UInt_t biny = (5-hs)*256+row+1;
		
		if (hitmap->GetBinContent(binx,biny) <= TMConfig::Instance()->GETdeadLimit()) {
		  nrDead++;
		}
	      }
	    }
	    printf("%d|",nrDead);
	  }
	  printf("\n");
	}
	
      }
    }  
  }
  TMControlSPD::Instance()->UpdateMask();  
}
//___________________________________________________________________________________
Bool_t TMSetupDisplays::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2) {
  if (fPleaseWait) {
    time_t stamp = time(NULL);
    TString stampMsg = Form("%s",ctime(&stamp));
    stampMsg.ReplaceAll("\n","");
    printf("Busy - Please try again... (%s)\n",stampMsg.Data());
    return kTRUE;
  }
  fPleaseWait = kTRUE;

  switch(GET_MSG(msg)) {
  case kC_COMMAND :
    switch (GET_SUBMSG(msg)) {

    case kCM_BUTTON :
      {
	// a "+" button was pressed:
	if (parm1>=ZERO_TO_PLUS_ID_SHIFT && parm1<ZERO_TO_PLUS_ID_SHIFT+(Int_t)gNrHistogramming) {
	  UpdateNrLabelLater(parm1-ZERO_TO_PLUS_ID_SHIFT);
	  ProcessPlusHistogramming(parm1-ZERO_TO_PLUS_ID_SHIFT);
	}
	// a "-" button was pressed:
	else if (parm1>=ZERO_TO_MINUS_ID_SHIFT && parm1<ZERO_TO_MINUS_ID_SHIFT+(Int_t)gNrHistogramming) {
	  UpdateNrLabelLater(parm1-ZERO_TO_MINUS_ID_SHIFT);
	  ProcessMinusHistogramming(parm1-ZERO_TO_MINUS_ID_SHIFT);
	}
	// a "show" button was pressed:
	else if (parm1>=ZERO_TO_DISP_ID_SHIFT && parm1<ZERO_TO_DISP_ID_SHIFT+(Int_t)gNrHistogramming) {
	  ThreadProcessOpenDisplay(parm1);
	}




	// Hitmaps ALL
	else if (parm1==HITMAP_ALLPLUS) {
	  UpdateNrLabelLater(HITMAPEQ_0,20);
	  ProcessPlusHistogramming(HITMAPEQ_0,20);
	}
	else if (parm1==HITMAP_ALLMINUS) {
	  UpdateNrLabelLater(HITMAPEQ_0,20);
	  ProcessMinusHistogramming(HITMAPEQ_0,20);
	}
	else if (parm1==HITMAP_ALLSHOW) {
	  ProcessOpenDisplayID(HITMAPEQ_0,20);
	}

	// Hits per event ALL
	else if (parm1==HITSPEREVENT_ALLPLUS) {
	  UpdateNrLabelLater(HITSPEREVENTALL,21);
	  ProcessPlusHistogramming(HITSPEREVENTALL,21);
	}
	else if (parm1==HITSPEREVENT_ALLMINUS) {
	  UpdateNrLabelLater(HITSPEREVENTALL,21);
	  ProcessMinusHistogramming(HITSPEREVENTALL,21);
	}
	else if (parm1==HITSPEREVENT_ALLSHOW) {
	  ThreadProcessOpenDisplayID(HITSPEREVENTALL,21);
	}

	// Events with hits ALL
	else if (parm1==EVENTSWITHHITS_ALLPLUS) {
	  UpdateNrLabelLater(EVENTSWITHHITSALL,21);
	  ProcessPlusHistogramming(EVENTSWITHHITSALL,21);
	}
	else if (parm1==EVENTSWITHHITS_ALLMINUS) {
	  UpdateNrLabelLater(EVENTSWITHHITSALL,21);
	  ProcessMinusHistogramming(EVENTSWITHHITSALL,21);
	}
	else if (parm1==EVENTSWITHHITS_ALLSHOW) {
	  ThreadProcessOpenDisplayID(EVENTSWITHHITSALL,21);
	}

	// Global Clu ALL
	else if (parm1==GLOBAL_CLU_ALLPLUS) {
	  UpdateNrLabelLater(GLOBAL_CLUSTER_XY,18);
	  ProcessPlusHistogramming(GLOBAL_CLUSTER_XY,18);
	}
	else if (parm1==GLOBAL_CLU_ALLMINUS) {
	  UpdateNrLabelLater(GLOBAL_CLUSTER_XY,18);
	  ProcessMinusHistogramming(GLOBAL_CLUSTER_XY,18);
	}
	else if (parm1==GLOBAL_CLU_ALLSHOW) {
	  ThreadProcessOpenDisplayID(GLOBAL_CLUSTER_XY,18);
	}

	// Global Pix ALL
	else if (parm1==GLOBAL_PIX_ALLPLUS) {
	  UpdateNrLabelLater(GLOBAL_PIXEL_XY,10);
	  ProcessPlusHistogramming(GLOBAL_PIXEL_XY,10);
	}
	else if (parm1==GLOBAL_PIX_ALLMINUS) {
	  UpdateNrLabelLater(GLOBAL_PIXEL_XY,10);
	  ProcessMinusHistogramming(GLOBAL_PIXEL_XY,10);
	}
	else if (parm1==GLOBAL_PIX_ALLSHOW) {
	  ThreadProcessOpenDisplayID(GLOBAL_PIXEL_XY,10);
	}

	// Global Other ALL
	else if (parm1==GLOBAL_OTHER_ALLPLUS) {
	  UpdateNrLabelLater(BC_MOD_4,1);
	  ProcessPlusHistogramming(BC_MOD_4,1);
	}
	else if (parm1==GLOBAL_OTHER_ALLMINUS) {
	  UpdateNrLabelLater(BC_MOD_4,1);
	  ProcessMinusHistogramming(BC_MOD_4,1);
	}
	else if (parm1==GLOBAL_OTHER_ALLSHOW) {
	  ThreadProcessOpenDisplayID(BC_MOD_4,1);
	}

	// Fastor Eq ALL
	else if (parm1==FASTOR_EQ_ALLPLUS) {
	  UpdateNrLabelLater(FASTOR_ALL,21);
	  ProcessPlusHistogramming(FASTOR_ALL,21);
	}
	else if (parm1==FASTOR_EQ_ALLMINUS) {
	  UpdateNrLabelLater(FASTOR_ALL,21);
	  ProcessMinusHistogramming(FASTOR_ALL,21);
	}
	else if (parm1==FASTOR_EQ_ALLSHOW) {
	  ThreadProcessOpenDisplayID(FASTOR_ALL,21);
	}

	// Fastor Missing ALL
	else if (parm1==FASTOR_MISSING_EQ_ALLPLUS) {
	  UpdateNrLabelLater(FASTOR_MISSING_ALL,21);
	  ProcessPlusHistogramming(FASTOR_MISSING_ALL,21);
	}
	else if (parm1==FASTOR_MISSING_EQ_ALLMINUS) {
	  UpdateNrLabelLater(FASTOR_MISSING_ALL,21);
	  ProcessMinusHistogramming(FASTOR_MISSING_ALL,21);
	}
	else if (parm1==FASTOR_MISSING_EQ_ALLSHOW) {
	  ThreadProcessOpenDisplayID(FASTOR_MISSING_ALL,21);
	}

	// Fastor Problem ALL
	else if (parm1==FASTOR_PROBLEM_EQ_ALLPLUS) {
	  UpdateNrLabelLater(FASTOR_PROBLEM_ALL,21);
	  ProcessPlusHistogramming(FASTOR_PROBLEM_ALL,21);
	}
	else if (parm1==FASTOR_PROBLEM_EQ_ALLMINUS) {
	  UpdateNrLabelLater(FASTOR_PROBLEM_ALL,21);
	  ProcessMinusHistogramming(FASTOR_PROBLEM_ALL,21);
	}
	else if (parm1==FASTOR_PROBLEM_EQ_ALLSHOW) {
	  ThreadProcessOpenDisplayID(FASTOR_PROBLEM_ALL,21);
	}

	// Fastor Additional ALL
	else if (parm1==FASTOR_ADDITIONAL_ALLPLUS) {
	  UpdateNrLabelLater(FASTOR_INNER_VS_OUTER,1);
	  ProcessPlusHistogramming(FASTOR_INNER_VS_OUTER,1);
	}
	else if (parm1==FASTOR_ADDITIONAL_ALLMINUS) {
	  UpdateNrLabelLater(FASTOR_INNER_VS_OUTER,1);
	  ProcessMinusHistogramming(FASTOR_INNER_VS_OUTER,1);
	}
	else if (parm1==FASTOR_ADDITIONAL_ALLSHOW) {
	  ThreadProcessOpenDisplayID(FASTOR_INNER_VS_OUTER,1);
	}

	// CError One ALL
	else if (parm1==CERROR_ONE_ALLPLUS) {
	  UpdateNrLabelLater(CERROR_VS_EQ,7);
	  ProcessPlusHistogramming(CERROR_VS_EQ,7);
	}
	else if (parm1==CERROR_ONE_ALLMINUS) {
	  UpdateNrLabelLater(CERROR_VS_EQ,7);
	  ProcessMinusHistogramming(CERROR_VS_EQ,7);
	}
	else if (parm1==CERROR_ONE_ALLSHOW) {
	  ThreadProcessOpenDisplayID(CERROR_VS_EQ,7);
	}
	// CError Two ALL
	else if (parm1==CERROR_TWO_ALLPLUS) {
	  UpdateNrLabelLater(CERROR_VS_EC_EQ_0,20);
	  ProcessPlusHistogramming(CERROR_VS_EC_EQ_0,20);
	}
	else if (parm1==CERROR_TWO_ALLMINUS) {
	  UpdateNrLabelLater(CERROR_VS_EC_EQ_0,20);
	  ProcessMinusHistogramming(CERROR_VS_EC_EQ_0,20);
	}
	else if (parm1==CERROR_TWO_ALLSHOW) {
	  ThreadProcessOpenDisplayID(CERROR_VS_EC_EQ_0,20);
	}
	// CError Three ALL
	else if (parm1==CERROR_THREE_ALLPLUS) {
	  UpdateNrLabelLater(CERROR_FRACTION_VS_EC_EQ_0,20);
	  ProcessPlusHistogramming(CERROR_FRACTION_VS_EC_EQ_0,20);
	}
	else if (parm1==CERROR_THREE_ALLMINUS) {
	  UpdateNrLabelLater(CERROR_FRACTION_VS_EC_EQ_0,20);
	  ProcessMinusHistogramming(CERROR_FRACTION_VS_EC_EQ_0,20);
	}
	else if (parm1==CERROR_THREE_ALLSHOW) {
	  ThreadProcessOpenDisplayID(CERROR_FRACTION_VS_EC_EQ_0,20);
	}

	// Dump Main ALL
	else if (parm1==DUMP_MAIN_ALLPLUS) {
	  UpdateNrLabelLater(DUMP_PAYLOAD,4);
	  ProcessPlusHistogramming(DUMP_PAYLOAD,4);
	}
	else if (parm1==DUMP_MAIN_ALLMINUS) {
	  UpdateNrLabelLater(DUMP_PAYLOAD,4);
	  ProcessMinusHistogramming(DUMP_PAYLOAD,4);
	}
	else if (parm1==DUMP_MAIN_ALLSHOW) {
	  ThreadProcessOpenDisplayID(DUMP_PAYLOAD,4);
	}

	// Dump Error Only
	else if (parm1==DUMP_ERROR_ONLY_ALLPLUS) {
	  UpdateNrLabelLater(DUMP_ERROR_ONLY_PAYLOAD,3);
	  ProcessPlusHistogramming(DUMP_ERROR_ONLY_PAYLOAD,3);
	}
	else if (parm1==DUMP_ERROR_ONLY_ALLMINUS) {
	  UpdateNrLabelLater(DUMP_ERROR_ONLY_PAYLOAD,3);
	  ProcessMinusHistogramming(DUMP_ERROR_ONLY_PAYLOAD,3);
	}
	else if (parm1==DUMP_ERROR_ONLY_ALLSHOW) {
	  ThreadProcessOpenDisplayID(DUMP_ERROR_ONLY_PAYLOAD,3);
	}


	// ADD CODE HERE maybe...


      }
      break;


    case kCM_CHECKBUTTON :
      break;


    case kCM_MENU:
      {
	TMControlSPD::Instance()->HoldRunning();

	// Setup: load, save, etc.
	if (parm1 == FILE_SAVE_DEFAULT_SETUP) {
	  SaveDefaultSetup();
	}

	else if (parm1 == FILE_LOAD_DEFAULT_SETUP) {
	  LoadDefaultSetup();
	}

	if (parm1 == FILE_SAVE_SETUP) {
	  SaveSetupChoice();
	}

	else if (parm1 == FILE_LOAD_SETUP) {
	  LoadSetupChoice();
	}

	else if (parm1 == FILE_CLEAR_SETUP) {
	  ClearSetup();
	}

	TMControlSPD::Instance()->ContinueRunning();
      }
      break;


    }
  }

//  if (!ihavelock) {
//    TMControlMutex::Instance()->IReleaseLock();
//    pthread_mutex_unlock(&gMutex);
//  }
  fPleaseWait = kFALSE;
  return kTRUE;
}
