Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
// Author: Gero Flucke <mailto:flucke@mail.desy.de>
//____________________________________
// GFHistManager
//   Author:      Gero Flucke
//   Date:        Feb. 10th, 2002
//   last update: $Date: 2012/03/29 07:57:38 $  
//   by:          $Author: flucke $
//

#include <string.h>

// #include <iostream>
// #include <vector>
// RooT header:
#include <TROOT.h>
#include <TError.h>
#include <TH1.h>
#include <TH2.h>
#include <TF1.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TString.h>
#include <TObjArray.h>
#include <TList.h>
#include <TLegend.h>
#include <TLegendEntry.h>
#include <TPaveStats.h>
#include <TStyle.h>

// my header:
#include "GFHistManager.h"
#include "GFUtils/GFHistArray.h"

ClassImp(GFHistManager);

const Int_t GFHistManager::kDefaultPadsPerCanX = 1;
const Int_t GFHistManager::kDefaultPadsPerCanY = 1;
const Int_t GFHistManager::kDefaultDepth = 0;
TString GFHistManager::fgLegendEntryOption = "l";


GFHistManager::GFHistManager()
{
  this->Initialise();
  fBatch = kFALSE;
  fDrawDiffStyle = kTRUE;
  fSameWithStats = kFALSE;
  fLegendY1 = 0.75;
  fLegendX1 = 0.7;
  fLegendY2 = 0.99;
  fLegendX2 = 0.99;
  fStatsX1 = 0.72, fStatsX2 = 0.995, fStatsY1 = .8, fStatsY2 = .995;
  fCanvasName = "canvas";
  fCanvasWidth = 600;
  fCanvasHeight = 600;
}

GFHistManager::GFHistManager(TH1* hist)
{
  // constructing with hist as first histogram 
  this->Initialise();
  
  this->AddHist(hist);
  fBatch = kFALSE;
  fDrawDiffStyle = kTRUE;
  fSameWithStats = kFALSE;
  fLegendY1 = 0.75;
  fLegendX1 = 0.7;
  fLegendY2 = 0.99;
  fLegendX2 = 0.99;
  fStatsX1 = 0.72, fStatsX2 = 0.995, fStatsY1 = .8, fStatsY2 = .995;
  fCanvasName = "canvas";
  fCanvasWidth = 600;
  fCanvasHeight = 600;
}

GFHistManager::GFHistManager(TCollection* hists)
{
  // constructing with histos in 'hists' as first histogram 
  this->Initialise();

  this->AddHists(hists);
  fBatch = kFALSE;
  fDrawDiffStyle = kTRUE;
  fSameWithStats = kFALSE;
  fLegendY1 = 0.75;
  fLegendX1 = 0.7;
  fLegendY2 = 0.99;
  fLegendX2 = 0.99;
  fStatsX1 = 0.72, fStatsX2 = 0.995, fStatsY1 = .8, fStatsY2 = .995;
  fCanvasName = "canvas";
  fCanvasWidth = 600;
  fCanvasHeight = 600;
}

//________________________________________________________
void GFHistManager::Initialise()
{
  fDepth = kDefaultDepth;
  fNoX.Set(fDepth);
  fNoY.Set(fDepth);
#if ROOT_VERSION_CODE >= ROOT_VERSION(3,10,2) && ROOT_VERSION_CODE <= ROOT_VERSION(4,0,3)
  // TArrayI::Reset(Int_t) buggy in 3.10_02:
  for(Int_t i = 0; i < fNoX.GetSize(); ++i) fNoX[i] = kDefaultPadsPerCanX;
  for(Int_t i = 0; i < fNoY.GetSize(); ++i) fNoY[i] = kDefaultPadsPerCanY;
#else
  fNoX.Reset(kDefaultPadsPerCanX);
  fNoY.Reset(kDefaultPadsPerCanY);
#endif  
  fLogY.Set(fDepth);
  fLogY.Reset(); // set to 0

//   fCanvasWidth = 700;
//   fCanvasHeight = 500;
//   fCanvasWidth = 600;
//   fCanvasHeight = 600;

  fHistArrays = new TObjArray;
  for(Int_t i = 0; i < fDepth; ++i){
    fHistArrays->Add(new TObjArray);
  }
  fCanArrays = new TObjArray;
  fLegendArrays = NULL;
  fObjLists = NULL;
}

//________________________________________________________
GFHistManager::~GFHistManager()
{
  // destructor: delete stored arrays, legends and canvases, but not the hists and objects!
  if(fHistArrays) {
    TIter iter(fHistArrays);
    while(TObjArray* array = static_cast<TObjArray*>(iter.Next())){
      array->Delete();
    }
    fHistArrays->Delete();
    delete fHistArrays;
  }
  if(fLegendArrays) {
    TIter legIter(fLegendArrays);
    while(TObjArray* array = static_cast<TObjArray*>(legIter.Next())){
      array->Delete();
    }
    fLegendArrays->Delete();
    delete fLegendArrays;
  }

  if(fObjLists) { 
    TIter listArrayIter(fObjLists);
    while(TObjArray* listArray = static_cast<TObjArray*>(listArrayIter.Next())){
      listArray->Delete();; // delete lists, but not its objects
    }
    fObjLists->Delete(); // delete arrays of lists
    delete fObjLists;// delete array of arrays of lists
  }

  if(fCanArrays) {
    TIter canIter(fCanArrays);
    while(TObjArray* array = static_cast<TObjArray*>(canIter.Next())){
      array->Delete();
    }
    fCanArrays->Delete();
    delete fCanArrays;
  }
}

//________________________________________________________
void GFHistManager::Clear(Bool_t deleteHists)
{
  // delete all canvases and clear the lists of hists stored in the manager 
  // (hists and objects are deleted if deleteHists is true [by default it is false])

  TIter iterCanArrays(fCanArrays);
  while(TObjArray* arr = static_cast<TObjArray*>(iterCanArrays.Next())){
    // A simple 'arr->Delete();' causes a crash if the user has closed a canvas
    // via the GUI - so delete only those that are known to gROOT:
    TIter cans(arr);
    while (TObject *c = cans.Next()) delete gROOT->GetListOfCanvases()->FindObject(c);
  }
  fCanArrays->Delete(); // delete arrays of canvases
  delete fCanArrays;

  if(fLegendArrays){ // by default there are no legends...
    TIter iterLegArrays(fLegendArrays);
    while(TObjArray* arr = static_cast<TObjArray*>(iterLegArrays.Next())){
      arr->Delete(); // delete legends
    }
    fLegendArrays->Delete(); // delete arrays of legends
    delete fLegendArrays;
  }

  if(fObjLists) { 
    TIter listArrayIter(fObjLists);
    while(TObjArray* listArray = static_cast<TObjArray*>(listArrayIter.Next())){
      if(deleteHists) {
	TIter listIter(listArray);
	while(TList* list = static_cast<TList*>(listIter.Next())){
	  list->Delete(); // delete objects if requested
	}
      }
      listArray->Delete(); // delete lists
    }
    fObjLists->Delete(); // delete arrays of lists
    delete fObjLists; // delete array of arrays of lists
  }

  TIter iterHistArrays(fHistArrays);
  while(TObjArray* arr = static_cast<TObjArray*>(iterHistArrays.Next())){
    TIter iterHistArrays2(arr);
    while(TObjArray* arr2 = static_cast<TObjArray*>(iterHistArrays2.Next())){
      if(deleteHists) arr2->Delete(); // delete histograms
      else            arr2->Clear();
    }
    arr->Delete();
  }
  fHistArrays->Delete(); // delete arrays of arrays of histograms
  delete fHistArrays;

  this->Initialise(); // here the arrays are rebuild and fDepth etc. adjusted
}

//________________________________________________________
void GFHistManager::Draw(Option_t *)
{
  // draw all layers of histograms (ignore if in batch mode)
  if(fBatch) return; // for speed up...
  for(Int_t i = 0; i < fDepth; ++i){
    this->Draw(i);
  }
}

//________________________________________________________
void GFHistManager::Draw(Int_t layer)
{
  if(fBatch) return;
  this->DrawReally(layer);
}


//________________________________________________________
void GFHistManager::DrawReally(Int_t layer)
{
  if(layer < 0 || layer > fDepth-1) {
    this->Warning("DrawReally","Layer %d does not exist, possible are 0 to %d.", 
		  layer, fDepth-1);
    return;
  }

  this->MakeCanvases(layer);

  TIter canIter(static_cast<TObjArray*>(fCanArrays->At(layer)));
  TIter histIter(static_cast<TObjArray*>(fHistArrays->At(layer)));

  Int_t histNo = 0; // becomes number of histograms in layer
  while(TCanvas* can = static_cast<TCanvas*>(canIter.Next())){
    Int_t nPads = this->NumberOfSubPadsOf(can);
    if (fNoX[layer] * fNoY[layer] != nPads && 
	!(nPads == 0 && fNoX[layer] * fNoY[layer] == 1)) {
      this->Warning("DrawReally", "inconsistent number of pads %d, expect %d*%d",
		    nPads, fNoX[layer], fNoY[layer]);
    }
    for(Int_t i = 0; i <= nPads; ++i){
      if (i == 0 && nPads != 0) i = 1;
      can->cd(i);
      if(GFHistArray* histsOfPad = static_cast<GFHistArray*>(histIter.Next())){
	TIter hists(histsOfPad);
	TH1* firstHist = static_cast<TH1*>(hists.Next());
	firstHist->Draw();
	this->DrawFuncs(firstHist);
	while(TH1* h = static_cast<TH1*>(hists.Next())){
	  h->Draw(Form("SAME%s%s", (fSameWithStats ? "S" : ""), h->GetOption()));
	  this->DrawFuncs(h);
	}
	if(histsOfPad->GetEntriesFast() > 1){
	  const Double_t max = this->MaxOfHists(histsOfPad);
	  if(//firstHist->GetMaximumStored() != -1111. &&  ????
	     //max > firstHist->GetMaximumStored()){
	     max > firstHist->GetMaximum()){
	    firstHist->SetMaximum((fLogY[layer] ? 1.1 : 1.05) * max);
	  }
	  const Double_t min = this->MinOfHists(histsOfPad);
	  if (min < 0.) {
	    firstHist->SetMinimum(min * 1.05);
	  } else if (gStyle->GetHistMinimumZero()) {
	    // nothing to do
	  } else if (min != 0. || !fLogY[layer]) {
	    // Do not set to zero: log scale issue!
	    firstHist->SetMinimum(min * 0.95);
	  }
	}
	if(fLogY[layer] 
	   && (firstHist->GetMinimum() > 0. 
	      || (firstHist->GetMinimum() == 0. 
		  && firstHist->GetMinimumStored() == -1111.)))gPad->SetLogy();
	// draw other objects:
	this->DrawObjects(layer, histNo);
	// make hist style differ
	if(fDrawDiffStyle) GFHistManager::MakeDifferentStyle(histsOfPad);
	// draw legends on top of all
	if(fLegendArrays && layer <= fLegendArrays->GetLast() && fLegendArrays->At(layer)){
	  this->DrawLegend(layer, histNo);
	}
	gPad->Modified();
	this->ColourFuncs(histsOfPad);
	if (fSameWithStats) {
	  gPad->Update(); // not nice to need this here, makes use over network impossible...
	  this->ColourStatsBoxes(histsOfPad);
	}
	histNo++;
      }
    } // loop over pads
    const TString name = can->GetTitle();
    can->SaveAs(name+".pdf");
  } // loop over canvases
}

//________________________________________________________
void GFHistManager::DrawLegend(Int_t layer, Int_t histNo)
{
  // histNo starting at '0'
  // We must already be in the correct pad, layer and histNo must exist

  if(fLegendArrays && layer <= fLegendArrays->GetLast() && fLegendArrays->At(layer)){
    TObjArray* legends = static_cast<TObjArray*>(fLegendArrays->At(layer));
    TObject* legend = (histNo <= legends->GetLast() ? legends->At(histNo) : NULL);
    if(legend) legend->Draw();
  }
}

//________________________________________________________
void GFHistManager::DrawObjects(Int_t layer, Int_t histNo)
{
  // histNo starting at '0'
  // We must already be in the correct pad, layer and histNo must exist
  if(fObjLists && layer <= fObjLists->GetLast() && fObjLists->At(layer)){
    TObjArray* layerLists = static_cast<TObjArray*>(fObjLists->At(layer));
    if(histNo <= layerLists->GetLast() && layerLists->At(histNo)){
      TObjLink *lnk = static_cast<TList*>(layerLists->At(histNo))->FirstLink();
      while (lnk) {
	lnk->GetObject()->Draw(lnk->GetOption());
	lnk = lnk->Next();
      }
    }
  }
}

//________________________________________________________
void GFHistManager::Print(const char* filename, Bool_t add)
{
  // print all layers of histograms to ps-file 'filename'
  // if 'add == true' puts '(' or ')' only if 'filename' ends with it, 
  // e.g. if i is loop variable 
  //   GFHistManager *man = ...;
  //   TString name("XXX.ps");
  //   if(i == 0) man->Print(name + '(');
  //   else if(i == last) man->Print(name + ')');
  //   else man->Print(name, kTRUE);


  const Bool_t rootIsBatch = gROOT->IsBatch();
  if(fBatch){
    gROOT->SetBatch();
    for(Int_t i = 0; i < fDepth; ++i){
      this->DrawReally(i);
    }
  }
  gROOT->SetBatch(rootIsBatch);

  TObjArray cans;
  TIter canArrayIter(fCanArrays);
  while(TObjArray* canArray = static_cast<TObjArray*>(canArrayIter.Next())){
    cans.AddAll(canArray);
  }

  const Int_t nCans = cans.GetEntriesFast();
  if(nCans == 1) {
    cans.At(0)->Print(filename);
    return;
  }

  TString plainName(filename);
  const Bool_t starting = plainName.EndsWith("(");
  if(starting) {
    const Ssiz_t ind = plainName.Last('(');
    plainName.Remove(ind);
    //    plainName.ReplaceAll("(", "");
  }
  const Bool_t ending = plainName.EndsWith(")");
  if(ending) {
    const Ssiz_t ind = plainName.Last(')');
    plainName.Remove(ind);
//     plainName.ReplaceAll(")", "");
  }

  for(Int_t i = 0; i < nCans; ++i){
    if(i == 0 && !ending && (!add || starting)) {
      cans.At(i)->Print(plainName + "(");
    } else if(i == nCans - 1 && !starting && (!add || ending)) {
      cans.At(i)->Print(plainName + ")");
    } else {
      cans.At(i)->Print(plainName);
    }
  }
}

// //________________________________________________________
// void GFHistManager::Print(const char* filename, Int_t layer)
// {
//   // print all canvases of layer into .ps-files with name of their first histogram
//   Bool_t rootIsBatch = gROOT->IsBatch();
//   gROOT->SetBatch();
//   this->DrawReally(layer);

//   TIter canIter(static_cast<TObjArray*>(fCanArrays->At(layer)));
//   Int_t nCan = 0;
//   while(TCanvas* can = static_cast<TCanvas*>(canIter.Next())){
//     TObjArray* histLayer = static_cast<TObjArray*>(fHistArrays->At(layer));
//     TObjArray* firstHistInCan =
//       static_cast<TObjArray*>(histLayer->At(nCan*fNoX[layer]*fNoY[layer]));
//     ++nCan;
//     if(firstHistInCan){
//       TString psFile("/tmp/");
//       psFile+=firstHistInCan->First()->GetName();
//       can->Print(psFile+=".ps");
//     }
//   } // loop over canvases
//   gROOT->SetBatch(rootIsBatch);
// }


//________________________________________________________
void GFHistManager::Update()
{
  // call Update() to all canvases
  for(Int_t i = 0; i < fDepth; ++i){
    this->Update(i);
  }
}

//________________________________________________________
void GFHistManager::Update(Int_t layer)
{
  if(!this->CheckDepth("Update", layer, kFALSE)) {
    return;
  }

  // First loop on canvases:
  // If meanwhile the setting of fNoX/fNoY has changed, we are better with
  // drawing from scratch:
  Bool_t drawFromScratch = kFALSE;
  TIter canIter(static_cast<TObjArray*>(fCanArrays->At(layer)));
  while(TCanvas* can = static_cast<TCanvas*>(canIter.Next())){
    const Int_t nPads = this->NumberOfSubPadsOf(can);
    if (fNoX[layer] * fNoY[layer] != nPads && // does not fit...
	!(nPads == 0 && fNoX[layer] * fNoY[layer] == 1)) {// ...nor single hist canvas
      drawFromScratch = kTRUE;
      break;
    }
  }
  if (drawFromScratch) {
    this->Draw(layer);
    return; // nothing else to be done...
  }

  // Now second loop doing the real Update work:
  canIter = static_cast<TObjArray*>(fCanArrays->At(layer));
  Int_t numPreviousCansHists = 0;
  const Int_t numHistsLayer = this->GetNumHistsOf(layer);
  while(TCanvas* can = static_cast<TCanvas*>(canIter.Next())){
    const Int_t nPads = this->NumberOfSubPadsOf(can); // get numbers of first loop?
    for(Int_t i = 0; i <= nPads; ++i){
      if (i == 0 && nPads != 0) i = 1;// i==0: single hist canvas, else step into pad
      can->cd(i);

      const Int_t histNo = TMath::Max(0, numPreviousCansHists + i - 1);// for nPad == 0
      if (histNo >= numHistsLayer) continue;
      // draw other objects
      this->DrawObjects(layer, histNo);
      // draw legends on top of all
      if(fLegendArrays && fLegendArrays->GetSize() > layer && fLegendArrays->At(layer)){
	this->DrawLegend(layer, histNo);
      }

      if(fLogY[layer]) {
	GFHistArray *histsOfPad = this->GetHistsOf(layer, histNo);
	TH1 *h1 = histsOfPad->First();
	if (h1->GetMinimumStored() == 0. && histsOfPad->GetEntriesFast() > 1
	    && this->MinOfHists(histsOfPad) == 0.) {
	  // trouble with log scale, but assume that 0. set in DrawReally(..)!
	  h1->SetMinimum(-1111.);
	}
	if ((h1->GetMinimum() > 0. 
	     || (h1->GetMinimum() == 0. && h1->GetMinimumStored() == -1111.))) {
	  gPad->SetLogy();
	} else {
	  gPad->SetLogy(kFALSE);
	}
      } else {
	gPad->SetLogy(kFALSE);
      }
      gPad->Modified();
//       gPad->Update();
//       gPad->Modified();
//       gPad->Update();
    }
//     can->Update();
    can->Modified();
    can->Update();
//     can->Modified();
    numPreviousCansHists += nPads;
  } // loop over canvases
}


//_____________________________________________________
TLegendEntry* GFHistManager::AddHist(TH1* hist, Int_t layer, const char* legendTitle,
				     const char* legOpt)
{
  // add hist to 'layer'th list  of histos (expands, if layer does not already exist!)
  if(!hist){
    this->Warning("AddHist", "adding NULL pointer will be ignored!");
    return NULL;
  }

  if(!this->CheckDepth("AddHist", layer)) return NULL;
  GFHistArray* newHist = new GFHistArray;
  newHist->Add(hist);
  TObjArray* layerHistArrays = static_cast<TObjArray*>(fHistArrays->At(layer));
  layerHistArrays->Add(newHist);
  if(legendTitle){
    TObjArray* legends = this->MakeLegends(layer);
    TLegend* legend = new TLegend(fLegendX1, fLegendY1, fLegendX2, fLegendY2);
#if ROOT_VERSION_CODE < ROOT_VERSION(5,6,0)
    if (TString(gStyle->GetName()) == "Plain") legend->SetBorderSize(1);
#endif
    legend->SetTextFont(42);
    legend->SetFillColor(kWhite);
    legends->AddAtAndExpand(legend, layerHistArrays->IndexOf(newHist));
    return legend->AddEntry(hist, legendTitle, legOpt ? legOpt : fgLegendEntryOption.Data());
  }
  return NULL;
}

//_____________________________________________________
void GFHistManager::AddHists(TCollection* hists, Int_t layer, const char* legendTitle,
			     const char* legOpt)
{
  // add contents of 'hists' to 'layer'th list of histos (should be histograms!!!)
  TIter iter(hists);
  while(TObject* hist = iter.Next()){
    if(!hist->InheritsFrom(TH1::Class())){
      this->Warning("AddHists", "Trying to add a non-histogram object, ignore!");
    } else this->AddHist(static_cast<TH1*>(hist), layer, legendTitle, legOpt);
  }
}

//_____________________________________________________
TLegendEntry* GFHistManager::AddHistSame(TH1* hist, Int_t layer, Int_t histNum,
					 const char* legendTitle, const char* legOpt)
{
  // adds hist to layer to draw it in the same pad as histNum's histo of that layer 
  if(!hist){
    this->Warning("AddHistSame", "adding NULL pointer will be ignored!");
    return NULL;
  }
  if (histNum > 0 && this->CheckDepth("AddHistSame", layer, kTRUE) //maybe added layer?
      && !this->GetHistsOf(layer, histNum-1)) {
    this->Error("AddHistSame", "usage as AddHist only for next free histNum, not %d", histNum);
    return NULL;
  }
  GFHistArray *histsArray = this->GetHistsOf(layer, histNum, kTRUE);// expand!
  TLegendEntry* result = NULL;
  if(histsArray) {
    histsArray->Add(hist); 
    if(legendTitle && strlen(legendTitle)){
      TObjArray* legends = this->MakeLegends(layer);
      TLegend* legend = NULL;
      if(legends->GetLast() >= histNum
	 && legends->At(histNum)){
	legend = static_cast<TLegend*>(legends->At(histNum));
      } else {
	legend = new TLegend(fLegendX1, fLegendY1, fLegendX2, fLegendY2);
#if ROOT_VERSION_CODE < ROOT_VERSION(5,6,0)
	if (TString(gStyle->GetName()) == "Plain") legend->SetBorderSize(1);
#endif
	legends->AddAtAndExpand(legend, histNum);
	legend->SetFillColor(kWhite);
      }
      result = legend->AddEntry(hist,legendTitle, legOpt ? legOpt : fgLegendEntryOption.Data());
    }
  }
  return result;
}

//_____________________________________________________
void GFHistManager::AddHistsSame(GFHistArray* hists, Int_t layer,
				 const char* legendTitle, const char* legOpt)
{
  // adds hists to layer
  // each hist is AddHistSame(hist, layer, pad = 0...n)
  if(!hists){
    this->Warning("AddHistsSame", "adding NULL pointer will be ignored!");
    return;
  }
  for(Int_t i = 0; i < hists->GetEntriesFast(); ++i){
    this->AddHistSame(hists->At(i), layer, i, legendTitle, legOpt);
  }
}

//_____________________________________________________
void GFHistManager::AddHistsSame(GFHistArray* hists, Int_t layer, Int_t histNum)
{
  // adds hists to layer to draw it in the same pad as histNum's histo of that layer
  if(!hists){
    this->Warning("AddHistsSame", "adding NULL pointer will be ignored!");
    return;
  }
  GFHistArray* histsArray = this->GetHistsOf(layer, histNum, kTRUE);
  if(histsArray) histsArray->AddAll(hists);
}

//_____________________________________________________
void GFHistManager::AddLayers(GFHistManager* other)
{
  // append the layers from other to this, hists are not cloned, but legends?
  if(!other) return;
  const Int_t oldDepth = fDepth;
  for(Int_t iLayer = 0; iLayer < other->GetNumLayers(); ++iLayer){
    for(Int_t iPad = 0; iPad < other->GetNumHistsOf(iLayer); ++iPad){
      GFHistArray* hists = other->GetHistsOf(iLayer, iPad);
      this->AddHistsSame(hists, oldDepth + iLayer, iPad);
      TLegend* leg = other->GetLegendOf(iLayer, iPad);
      if(leg) this->AddLegend(static_cast<TLegend*>(leg->Clone()), iLayer, iPad);
    }
  }
}

//_____________________________________________________
void GFHistManager::AddLayer(GFHistManager* other, Int_t layer)
{
  // append the layer 'layer' from other to this, hists are not cloned, but legends?
  if(!other || layer >= other->GetNumLayers()) return;

  const Int_t newLayer = fDepth;
  for(Int_t iPad = 0; iPad < other->GetNumHistsOf(layer); ++iPad){
    GFHistArray* hists = other->GetHistsOf(layer, iPad);
    this->AddHist(hists->At(0), newLayer);
    for(Int_t iHist = 1; iHist < hists->GetEntriesFast(); ++iHist){
      this->AddHistSame(hists->At(iHist), newLayer, iPad);
    }
    TLegend* leg = other->GetLegendOf(layer, iPad);
    if(leg) this->AddLegend(static_cast<TLegend*>(leg->Clone()), newLayer, iPad);
  }
}

//_____________________________________________________
void GFHistManager::Overlay(GFHistManager* other, Int_t otherLayer, Int_t myLayer,
			    const char* legendTitle)
{
  if (!other || otherLayer >= other->GetNumLayers()
      || myLayer >= other->GetNumLayers()
      || other->GetNumHistsOf(otherLayer) != this->GetNumHistsOf(myLayer)) return;

  const Int_t histNo = 0;
  for (Int_t iPad = 0; iPad < other->GetNumHistsOf(otherLayer); ++iPad) {
    GFHistArray* hists = other->GetHistsOf(otherLayer, iPad);
    this->AddHistSame(hists->At(histNo), myLayer, iPad, legendTitle);//, legOpt)
  }
}

//_____________________________________________________
TLegend* GFHistManager::AddLegend(Int_t layer, Int_t histoNum, 
				  const char* header, Bool_t referAll)
{
  // adds a legend referencing all hists in same pad 'histoNum' of layer
  // 

  // FIXME: use help of other AddLegend method?
  if(!this->CheckHistNum("AddLegend", layer, histoNum)) return NULL;

  TObjArray* legendsOfLayer = this->MakeLegends(layer);
  TLegend* legend = (legendsOfLayer->GetSize() <= histoNum ? 
		     NULL : static_cast<TLegend*>(legendsOfLayer->At(histoNum)));
  if(!legend) {
    legend = new TLegend(fLegendX1, fLegendY1, fLegendX2, fLegendY2);
#if ROOT_VERSION_CODE < ROOT_VERSION(5,6,0)
    if (TString(gStyle->GetName()) == "Plain") legend->SetBorderSize(1);
#endif
    legend->SetFillColor(kWhite);
    legendsOfLayer->AddAtAndExpand(legend, histoNum);
  }

  if(header) legend->SetHeader(header);
  GFHistArray* hists = this->GetHistsOf(layer, histoNum);
  TList* legendEntries = legend->GetListOfPrimitives();

  if(referAll){
    TIter histsIter(hists);
    while(TObject* hist = histsIter.Next()){
      Bool_t addNew = kTRUE;
      TIter legEntrIter(legendEntries);
      while(TLegendEntry* entry = static_cast<TLegendEntry*>(legEntrIter())){
	if(hist == entry->GetObject()) {addNew = kFALSE; break;}
      }  
      if(addNew) legend->AddEntry(hist, hist->GetName(), fgLegendEntryOption);
    }
  }

  if(layer < fCanArrays->GetEntriesFast()) {
    this->Update(layer); // if canvas already drawn
  }
  return legend;
}

//_____________________________________________________
Bool_t GFHistManager::RemoveLegend(Int_t layer, Int_t nPad)
{
  // true if there was a legend
  if(!this->CheckHistNum("RemoveLegend", layer, nPad)) return kFALSE;

  TLegend* leg = this->GetLegendOf(layer, nPad);
  if(!leg) return kFALSE;

  TObjArray* legendsOfLayer = this->MakeLegends(layer);
  if(!legendsOfLayer->Remove(leg)) {
    this->Error("RemoveLegend", "inconsistent state for layer %d, nPad %d", layer, nPad);
    return kFALSE;
  }
  delete leg;

  if(layer < fCanArrays->GetEntriesFast()) {
    this->Update(layer); // if canvas already drawn
  }

  return kTRUE;
}

//_____________________________________________________
void GFHistManager::AddLegend(TLegend* leg, Int_t layer, Int_t histoNum)
{
  // hist and layer must already exist
  if(!this->CheckHistNum("AddLegend", layer, histoNum)) return;

  TObjArray* legendsOfLayer = this->MakeLegends(layer);
  TLegend* legend = (legendsOfLayer->GetSize() < histoNum ? 
		     NULL : static_cast<TLegend*>(legendsOfLayer->At(histoNum)));
  if(legend) {
    this->Error("AddLegend", "legend exists, replacing it");
    delete legend;
  }
  legend = leg;
  legendsOfLayer->AddAtAndExpand(legend, histoNum);

  if(layer < fCanArrays->GetEntriesFast()) {
    this->Update(layer); // if canvas already drawn
  }
}


//_____________________________________________________
void GFHistManager::AddObject(TObject* obj, Int_t layer, Int_t histoNum, Option_t* opt)
{
  // Hist and layer must already exist.
  // If the given pad is already drawn, it will get updated to display the object.
  // If you add many objects, this can become pretty slow, so it is recommended
  // to first work in batch mode (SetBatch()), add all hists and objects and then
  // go back and draw: SetBatch(false); Draw();// or Draw(layer)
  if(!this->CheckHistNum("AddObject", layer, histoNum)) return;

  TList* objList = this->MakeObjList(layer, histoNum);
  objList->Add(obj, opt);

  if(layer < fCanArrays->GetEntriesFast()) {
    // Would be nice to update only for histoNum to speed up...
    this->Update(layer); // if canvas already drawn
  }
}


//_____________________________________________________
void GFHistManager::WriteCanvases(TFile* file)
{
  // write canvases with their content to file  (overwrite)
  if(!file) {
    this->Warning("WriteCanvases", "no file given, ignore!");
    return;
  }

  TDirectory* saveDir = gDirectory;
  file->cd();
  for(Int_t i = 0; i < fDepth; ++i){
    TIter canvases(static_cast<TObjArray*>(fCanArrays->At(i)));
    while(TCanvas* can = static_cast<TCanvas*>(canvases.Next())){
      can->Write(0, kOverwrite);
    }
  }
  saveDir->cd();
}

//_____________________________________________________
void GFHistManager::WriteHistos(TFile* file)
{
  // write histos to file (overwrite)

  if(!file) {
    this->Warning("WriteHistos", "no file given, ignore!");
    return;
  }

  TDirectory* saveDir = gDirectory;
  file->cd();
  for(Int_t i = 0; i < fDepth; ++i){
    TIter iterHistsArr(static_cast<TObjArray*>(fHistArrays->At(i)));
    while(TObjArray* arr2 = static_cast<TObjArray*>(iterHistsArr.Next())){
      TIter iterHistArr2(arr2);
      while(TObject* hist = iterHistArr2.Next()){
	hist->Write(0, kOverwrite);
      }
    }
  }
  saveDir->cd();
}

//_____________________________________________________
void GFHistManager::MakeCanvases(Int_t layer)
{
  // no check done whether layer is consistent with depth...

  Int_t nHists = static_cast<TObjArray*>(fHistArrays->At(layer))->GetEntriesFast();
  Int_t nCanvases = nHists / (fNoX[layer] * fNoY[layer]);
  if(nHists > nCanvases * fNoX[layer] * fNoY[layer]){
    ++nCanvases;
  }

  Bool_t oneCanvas = kFALSE;
  while(nHists < fNoX[layer] * fNoY[layer]){
    oneCanvas = kTRUE;
//     fNoX[layer] > 1 ? --(fNoX[layer]) : --(fNoY[layer]);
    (fNoX[layer] > 1 && fNoX[layer] >= fNoY[layer]) ? --(fNoX[layer]) : --(fNoY[layer]);
//  if(nHists < fNoX[layer] * fNoY[layer]) fNoY[layer] > 1 ? --(fNoY[layer]) : --(fNoX[layer]);
    if(nHists < fNoX[layer] * fNoY[layer])
      (fNoY[layer] > 1 && fNoY[layer] >= fNoX[layer]) ? --(fNoY[layer]) : --(fNoX[layer]);
  }
//   if(oneCanvas && nHists > fNoX[layer] * fNoY[layer]) ++(fNoX[layer]);
//   if(oneCanvas && nHists > fNoX[layer] * fNoY[layer]) 
//     (fNoX[layer] > fNoY[layer]) ? ++(fNoY[layer]) : ++(fNoX[layer]);
  while(oneCanvas && nHists > fNoX[layer] * fNoY[layer]){
    (fNoX[layer] > fNoY[layer]) ? ++(fNoY[layer]) : ++(fNoX[layer]);
  }

  if(fCanArrays->GetSize() > layer && fCanArrays->At(layer)){
    static_cast<TObjArray*>(fCanArrays->At(layer))->Delete();
  } else {
    fCanArrays->AddAtAndExpand(new TObjArray, layer);
  }

  TString canName(fCanvasName);
  (canName += layer) += "_";

  for(Long_t i = 0; i < nCanvases; i++){
    Int_t width = fCanvasWidth, height = fCanvasHeight;
//  on screen this is nice, but Print for different canvas sizes in one .ps fails...
//     if(fNoX[layer] < fNoY[layer]){
//       width = (width * 11) / 14;
//       height *= 73; height /= 50;
//     } else if(fNoX[layer] > fNoY[layer]){ // new!
//       width = (width * 73) / 50;
//       //      height *= 11; height /= 14;
//     }

    while(gROOT->FindObject(canName+i)){
      canName += 'n';
    }

    TCanvas* can = new TCanvas(canName+i, canName+i, 10, 10, width, height);
    if (fNoX[layer] != 1 || fNoY[layer] != 1) can->Divide(fNoX[layer], fNoY[layer]);
    static_cast<TObjArray*>(fCanArrays->At(layer))->Add(can);
  }
}


//________________________________________________________
Int_t GFHistManager::NumberOfSubPadsOf(TCanvas* can)
{
  Int_t n = 0;

  TIter next(can ? can->GetListOfPrimitives() : NULL);
  while (TObject* obj = next()) {
    if (obj->InheritsFrom(TVirtualPad::Class())){
      ++n;
    }
  }

  return n;
}

//________________________________________________________
void GFHistManager::SetLegendX1Y1X2Y2(Double_t x1, Double_t y1, Double_t x2,Double_t y2)
{
  fLegendX1 = x1;
  fLegendY1 = y1;
  fLegendX2 = x2;
  fLegendY2 = y2;
}

//________________________________________________________
void GFHistManager::SetLegendX1(Double_t x1) {fLegendX1 = x1;}
//________________________________________________________
void GFHistManager::SetLegendY1(Double_t y1) {fLegendY1 = y1;}
//________________________________________________________
void GFHistManager::SetLegendX2(Double_t x2) {fLegendX2 = x2;}
//________________________________________________________
void GFHistManager::SetLegendY2(Double_t y2) {fLegendY2 = y2;}

//________________________________________________________
void GFHistManager::SetStatsX1Y1X2Y2(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
{
  fStatsX1 = x1;
  fStatsX2 = x2;
  fStatsY1 = y1;
  fStatsY2 = y2;
}

//________________________________________________________
void GFHistManager::SetNumHistsX(UInt_t numX)
{
  for(Int_t i = 0; i < fDepth; ++i){
    fNoX[i] = numX;
  }
}

//________________________________________________________
void GFHistManager::SetNumHistsX(UInt_t numX, Int_t layer)
{
  if(this->CheckDepth("SetNumHistsX", layer, kFALSE)) {
    fNoX[layer] = numX;
  }
}

//________________________________________________________
void GFHistManager::SetNumHistsY(UInt_t numY)
{
  for(Int_t i = 0; i < fDepth; ++i){
    fNoY[i] = numY;
  }
}

//________________________________________________________
void GFHistManager::SetNumHistsY(UInt_t numY, Int_t layer)
{
  if(this->CheckDepth("SetNumHistsY", layer, kFALSE)) {
    fNoY[layer] = numY;
  }
}

//________________________________________________________
void GFHistManager::SetNumHistsXY(UInt_t numX, UInt_t numY)
{
  this->SetNumHistsX(numX);
  this->SetNumHistsY(numY);
}

//________________________________________________________
void GFHistManager::SetNumHistsXY(UInt_t numX, UInt_t numY, Int_t layer)
{
  this->SetNumHistsX(numX, layer);
  this->SetNumHistsY(numY, layer);
}

//________________________________________________________
void GFHistManager::SetLogY(Bool_t yesNo)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetLogY(i, yesNo);
  }
}

//________________________________________________________
void GFHistManager::SetLogY(Int_t layer, Bool_t yesNo)
{
  if(this->CheckDepth("SetLogY", layer, kFALSE)) {
    fLogY[layer] = yesNo ? 1 : 0;
    if(layer < fCanArrays->GetEntriesFast()) {
      this->Update(layer); // if canvas already drawn
    }
  }
}

//________________________________________________________
void GFHistManager::SetHistsOption(Option_t* option)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsOption(option, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsOption(Option_t* option, Int_t layer)
{
  if(!this->CheckDepth("SetHistsOption", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      TString opt(option); opt.ToLower();
      if(!hist->InheritsFrom(TH2::Class()) && opt.Contains("box")){
	opt.ReplaceAll("box",0);
      }
      hist->SetOption(opt);
    }
  }
}

//________________________________________________________
void GFHistManager::SetHistsMinMax(Double_t minMax, Bool_t min)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsMinMax(minMax, min, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsMinMax(Double_t minMax, Bool_t min, Int_t layer)
{
  if(!this->CheckDepth("SetHistsMinMax", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      if(min) hist->SetMinimum(minMax);
      else hist->SetMaximum(minMax);
    }
  }
  if(layer < fCanArrays->GetEntriesFast()) {
    this->Update(layer); // if canvas already drawn
  }
}


//________________________________________________________
void GFHistManager::AddHistsOption(Option_t* option)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->AddHistsOption(option, i);
  }
}

//________________________________________________________
void GFHistManager::AddHistsOption(Option_t* option, Int_t layer)
{
  // if 'layer' exists, add 'option' to all hists so far existing in this layer
  // (ignore option 'box' for 1-D histograms)
  if(!this->CheckDepth("AddHistsOption", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      TString opt(option); opt.ToLower();
      if(!hist->InheritsFrom(TH2::Class()) && opt.Contains("box",TString::kIgnoreCase)){
	opt.ReplaceAll("box",0);
      }
      hist->SetOption(opt += hist->GetOption());
    }
  }
}


//________________________________________________________
void GFHistManager::SetHistsXTitle(const char* title)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsXTitle(title, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsXTitle(const char* title, Int_t layer)
{
  if(!this->CheckDepth("SetHistsXTitle", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      hist->SetXTitle(title);
    }
  }
}

//________________________________________________________
void GFHistManager::SetHistsYTitle(const char* title)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsYTitle(title, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsYTitle(const char* title, Int_t layer)
{
  if(!this->CheckDepth("SetHistsYTitle", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr);// arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      hist->SetYTitle(title);
    }
  }
}


//________________________________________________________
void GFHistManager::SetHistsFillColor(Color_t color)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsFillColor(color, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsFillColor(Color_t color, Int_t layer)
{
  if(!this->CheckDepth("SetHistsFillColor", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      hist->SetFillColor(color);
    }
  }
}

//________________________________________________________
void GFHistManager::SetHistsLineWidth(Width_t width)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsLineWidth(width, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsLineWidth(Width_t width, Int_t layer)
{
  if(!this->CheckDepth("SetHistsLineWidth", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      hist->SetLineWidth(width);
    }
  }
}

//________________________________________________________
void GFHistManager::SetHistsLineStyle(Int_t s)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsLineStyle(s, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsLineStyle(Int_t s, Int_t layer, Int_t numHistInPad)
{
  // sets style 's' toall hists in 'layer'.
  // if numHistInPad >= 0: only the given histNum in each pad is changed in style
  // (default is numHistInPad = -1)
  if(!this->CheckDepth("SetHistsLineStyle", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(const GFHistArray* arr = static_cast<GFHistArray*>(iter2.Next())){
    if(numHistInPad < 0 || numHistInPad >= arr->GetEntriesFast()){ // all hist in pad
      TIter iter(arr); // arr is GFHistArray* !
      while(TH1* hist = static_cast<TH1*>(iter.Next())){
	hist->SetLineStyle(s);
      }
    } else {
      (*arr)[numHistInPad]->SetLineStyle(s);
    }
  }
}


//________________________________________________________
void GFHistManager::SetHistsLineColor(Color_t color)
{
  for(Int_t i = 0; i < fDepth; ++i){
    this->SetHistsLineColor(color, i);
  }
}

//________________________________________________________
void GFHistManager::SetHistsLineColor(Color_t color, Int_t layer)
{
  if(!this->CheckDepth("SetHistsLineColor", layer, kFALSE)) return;

  TIter iter2(static_cast<TObjArray*>(fHistArrays->At(layer)));
  while(TObjArray* arr = static_cast<TObjArray*>(iter2.Next())){
    TIter iter(arr); // arr is GFHistArray* !
    while(TH1* hist = static_cast<TH1*>(iter.Next())){
      hist->SetLineColor(color);
    }
  }
}

//________________________________________________________
Bool_t GFHistManager::CheckDepth(const char* method, Int_t layer, 
				 Bool_t mayExpand)
{
  // true, if layer is possible, if (mayExpand) expands to layer >= 0
  if(layer < 0){
    this->Warning("CheckDepth", "Layer below 0 (%d) called in '%s'!",
		  layer, method);
    return kFALSE;
  }

  if(layer > fDepth-1){
    if(mayExpand) {
      this->ExpandTo(layer);
      return kTRUE;
    } else {
      this->Warning("CheckDepth", "Layer %d called in '%s', max. is %d",
		  layer, method, fDepth-1);
      return kFALSE;
    }
  }
  return kTRUE;
}

//________________________________________________________
void GFHistManager::ExpandTo(Int_t layer)
{
  if(layer+1 <= fDepth){
    this->Error("ExpandTo",
		"Shrinking forbidden, fDepth = %d, should expand to = %d",
		fDepth, layer+1);
    return;
  }

  fNoX.Set(layer+1);
  fNoY.Set(layer+1);
  fLogY.Set(layer+1);

  for(Int_t i = fDepth; i <= layer; ++i){
    fNoX[i] = kDefaultPadsPerCanX;
    fNoY[i] = kDefaultPadsPerCanY;
    fLogY[i]= 0;
    fHistArrays->AddAtAndExpand(new TObjArray, i);
    fCanArrays->AddAtAndExpand(new TObjArray, i);
  }

  fDepth = layer+1;
}

//________________________________________________________
Bool_t GFHistManager::CheckHistNum(const char* method, Int_t layer, 
				 Int_t histNum, Bool_t mayExpand)
{
  // true if hist 'histNum' exists in 'layer' 
  // if(mayExpand == kTRUE) expands to this size if necessary! (default: kFALSE)
  if(!this->CheckDepth(method, layer, mayExpand)) return kFALSE; 
  

  TObjArray * layerArr = static_cast<TObjArray*>(fHistArrays->At(layer));
  if(histNum < 0) {
    this->Warning("CheckHistNum", "histogram number %d requested!", histNum);
    return kFALSE;
  }
  while(histNum >= layerArr->GetEntriesFast()){
    if(mayExpand){
      layerArr->AddAtAndExpand(new GFHistArray, layerArr->GetEntriesFast());
    } else {
      this->Warning("CheckHistNum", "layer %d has only %d histograms, number %d requested!",
		    layer, layerArr->GetEntriesFast(), histNum);
      return kFALSE;
    }
  }
  return kTRUE;
}

//________________________________________________________
TObjArray* GFHistManager::MakeLegends(Int_t layer)
{
  //  returns array of legends of 'layer' (to be called if 'layer' really exist!)
  // creates if necessary
  if(!fLegendArrays) fLegendArrays = new TObjArray(fDepth);
  if(layer > fLegendArrays->GetLast() || !fLegendArrays->At(layer)) {
    fLegendArrays->AddAtAndExpand(new TObjArray, layer);
  }

  return static_cast<TObjArray*>(fLegendArrays->At(layer));
}

//________________________________________________________
TList* GFHistManager::MakeObjList(Int_t layer, Int_t histoNum)
{
  // return list of objects to be drawn upon hists in pad histoNum of 'layer' 
  // (to be called if 'layer' really exist!)
  if(!fObjLists) fObjLists = new TObjArray(fDepth);
  if(layer > fObjLists->GetLast() || !fObjLists->At(layer)){
    fObjLists->AddAtAndExpand(new TObjArray(this->GetNumHistsOf(layer)),layer);
  }
  TObjArray* layerLists = static_cast<TObjArray*>(fObjLists->At(layer));
  if(histoNum > layerLists->GetLast() || !layerLists->At(histoNum)){
    layerLists->AddAtAndExpand(new TList, histoNum); 
  }

  return static_cast<TList*>(layerLists->At(histoNum));
}

//________________________________________________________
GFHistArray* GFHistManager::GetHistsOf(Int_t layer, Int_t histNum, Bool_t mayExpand)
{
  //  returns array of histograms for pad 'histNum' of 'layer'
  // if(mayExpand) creates if necessary!
  if(!this->CheckHistNum("GetHistsOf", layer, histNum, mayExpand)) return NULL;
  TObjArray* layerHists = static_cast<TObjArray*>(fHistArrays->At(layer));
  return static_cast<GFHistArray*>(layerHists->At(histNum));
}

//________________________________________________________
TList* GFHistManager::GetObjectsOf(Int_t layer, Int_t histNo)
{
  if(!this->CheckHistNum("GetObjectsOf", layer, histNo, kFALSE)) return NULL;

  if(fObjLists && layer <= fObjLists->GetLast() && fObjLists->At(layer)){
    TObjArray* layerLists = static_cast<TObjArray*>(fObjLists->At(layer));
    if(histNo <= layerLists->GetLast() && layerLists->At(histNo)){
      return static_cast<TList*>(layerLists->At(histNo));
    }
  }

  return NULL;
}

//________________________________________________________
Int_t GFHistManager::GetNumHistsOf(Int_t layer)
{
  if(!this->CheckDepth("GetNumHistsOf", layer, kFALSE)) return 0;
  TObjArray* layerHists = static_cast<TObjArray*>(fHistArrays->At(layer));
  if(layerHists) return layerHists->GetEntriesFast();
  return 0;
}

//________________________________________________________
TLegend* GFHistManager::GetLegendOf(Int_t layer, Int_t histoNum)
{
  // if it already exists!
  if(!this->CheckHistNum("AddLegend", layer, histoNum)) return NULL;

  TObjArray* legendsOfLayer = this->MakeLegends(layer);
  TLegend* legend = (legendsOfLayer->GetSize() < histoNum ? 
		     NULL : static_cast<TLegend*>(legendsOfLayer->At(histoNum)));
  return legend;
}


//________________________________________________________
void GFHistManager::MakeDifferentStyle(GFHistArray* hists)
{
  // easy version: adjust the histogram lines to colors
  // kBlack, kRed, kGreen, kBlue, kYellow, kMagenta, kCyan (c.f. $ROOTSYS/include/Gtypes.h)
  // 1       2     3       4      5        6         7 (skip 3 and 5: kGreen hardly visible...)
  //                                                           (... kYellow dito)
  // also set line style, set marker color to line color

  if (!hists || hists->GetEntriesFast() < 2) return; // nothing to do

  Color_t color = 0;
  for(Int_t i = 0; i < hists->GetEntriesFast(); ++i){
//     Color_t color = i+1;
    ++color;
    Style_t style = i+1;
    while(style > 4) style -= 4;
    if(color == 3) ++color; //omit kGreen
    if(color == 5) ++color; // and kYellow 
    if(color > 7 ) {
      ::Error("GFHistManager::MakeDifferentStyle", "Out of colors");
      color = 0;//      break;
    }
    hists->At(i)->SetLineColor(color);
    hists->At(i)->SetMarkerColor(color);
    hists->At(i)->SetMarkerSize(0.02);
    hists->At(i)->SetLineStyle(style);
  }
}

//________________________________________________________
Double_t GFHistManager::MaxOfHist(const TH1* h) const
{
  // Take bin with max content, adds error and returns (no *1.05 applied!),
  // but cares that - together with an error bar - another bin might be higher...
  // If the hists option contains nothing about errors: error is NOT added!

  TString option = h->GetOption();
  option.ToLower();
  option.ReplaceAll("same", 0);
  
  Int_t maxBin = h->GetMaximumBin();
  Double_t result = h->GetBinContent(maxBin);
  if(option.Contains('e') || (h->GetSumw2N() && !option.Contains("hist"))){
    for(Int_t bin = 1; bin <= h->GetNbinsX(); ++bin){ //FIXME: for 2/3D: loop over y/z!
      result = TMath::Max(result, (h->GetBinContent(bin) + h->GetBinError(bin))); 
    }
  }
  return result;
}

//________________________________________________________
Double_t GFHistManager::MaxOfHists(const TObjArray* hists) const
{
  Double_t result = 0.;
  TIter nextHist(hists);
  while(TObject* hist = nextHist()){
    if(hist->InheritsFrom(TH1::Class())){
      result = TMath::Max(result, this->MaxOfHist(static_cast<TH1*>(hist)));
    } else {
      this->Warning("MaxOfHists", "Entry in input array is not a histogram!");
    }
  }
  return result;
}

//________________________________________________________
Double_t GFHistManager::MinOfHist(const TH1* h) const
{
  // Take bin with min content, subtract error and returns (no *1.05 applied!),
  // but cares that - together with an error bar - another bin might be lower...
  // If the hists option contains nothing about errors: error is NOT subtracted!

  TString option = h->GetOption();
  option.ToLower();
  option.ReplaceAll("same", 0);
  
  const Int_t minBin = h->GetMinimumBin();
  Double_t result = h->GetBinContent(minBin);
  if(option.Contains('e') || (h->GetSumw2N() && !option.Contains("hist"))){
    for(Int_t bin = 1; bin <= h->GetNbinsX(); ++bin){ //FIXME: for 2/3D: loop over y/z!
      result = TMath::Min(result, (h->GetBinContent(bin) - h->GetBinError(bin))); 
    }
  }
  return result;
}

//________________________________________________________
Double_t GFHistManager::MinOfHists(const TObjArray* hists) const
{
  Double_t result = DBL_MAX;
  TIter nextHist(hists);
  while(TObject* hist = nextHist()){
    if(hist->InheritsFrom(TH1::Class())){
      result = TMath::Min(result, this->MinOfHist(static_cast<TH1*>(hist)));
    } else {
      this->Warning("MinOfHists", "Entry in input array is not a histogram!");
    }
  }

  return result;
}
//________________________________________________________
TCanvas*  GFHistManager::GetCanvas(Int_t layer, Int_t number)
{
  // after draw!!
  if(!fCanArrays || fCanArrays->GetEntriesFast() <= layer) return NULL;
  TObjArray* cans = static_cast<TObjArray*>(fCanArrays->At(layer));
  if(cans && cans->GetEntriesFast() > number){
    return static_cast<TCanvas*>(cans->At(number));
  }
  return NULL;
}

//________________________________________________________
TVirtualPad* GFHistManager::GetPad(Int_t layer, Int_t histNum)
{
  // pointer to pad where hists from layer/histNum are painted in
  // callable after draw!
    
  Int_t totHistsYet = 0;
  Int_t numHists = 0;
  TCanvas *can = NULL;

  for (Int_t numCan = 0; ; ++numCan) {
    can = this->GetCanvas(layer, numCan);
    if (!can) break;
    numHists = TMath::Max(1, this->NumberOfSubPadsOf(can));
    totHistsYet += numHists;
    if (totHistsYet > histNum) {
      totHistsYet -= numHists;
      break;
    }
  }

  TVirtualPad *result = NULL;
  if (can) {
    TVirtualPad *oldPad = gPad;
    if (numHists <= 1) can->cd(0); // one hist per canvas: no pads!
    else can->cd(histNum - totHistsYet + 1);
    result = gPad;
    oldPad->cd();
  }

  return result;
}

//________________________________________________________
Int_t GFHistManager::GetNumHistsX(Int_t layer) const
{
  if(layer >= 0 && layer < fDepth) return fNoX[layer];
  else return 0;
}

//________________________________________________________
Int_t GFHistManager::GetNumHistsY(Int_t layer) const
{
  if(layer >= 0 && layer < fDepth) return fNoY[layer];
  else return 0;
}

//________________________________________________________
void GFHistManager::GetLegendX1Y1X2Y2(Double_t& x1, Double_t& y1, 
				      Double_t& x2, Double_t& y2) const
{
   x1 = fLegendX1;
   y1 = fLegendY1;
   x2 = fLegendX2;
   y2 = fLegendY2;
}

//________________________________________________________
void GFHistManager::DrawFuncs(const TH1* hist) const
{
  // calls Draw("SAME") for all TF1 in hist's GetListOfFunctions if necessary
  if(!hist || !TString(hist->GetOption()).Contains("HIST", TString::kIgnoreCase)) return;
  TIter nextprim(hist->GetListOfFunctions());
  while(TObject* next = nextprim()){
    if(next->InheritsFrom(TF1::Class())){
      next->Draw("SAME");
    }
  }
}

//________________________________________________________
void GFHistManager::ColourStatsBoxes(GFHistArray *hists) const
{
  // colours stats boxes like hists' line colors and moves the next to each other
  if (!hists) return;
  Double_t x1 = fStatsX1, x2 = fStatsX2, y1 = fStatsY1, y2 = fStatsY2;
  for (Int_t iH = 0; iH < hists->GetEntriesFast(); ++iH) {
    TH1 *h = hists->At(iH);
    if (!h) continue;
    TObject *statObj = h->GetListOfFunctions()->FindObject("stats");
    if (statObj && statObj->InheritsFrom(TPaveStats::Class())) {
      TPaveStats *stats = static_cast<TPaveStats*>(statObj);
      stats->SetLineColor(hists->At(iH)->GetLineColor());
      stats->SetTextColor(hists->At(iH)->GetLineColor());
      stats->SetX1NDC(x1);
      stats->SetX2NDC(x2);
      stats->SetY1NDC(y1);
      stats->SetY2NDC(y2);
      y2 = y1 - 0.005; // shift down 2
      y1 = y2 - (fStatsY2 - fStatsY1); // shift down 1
      if (y1 < 0.) {
	y1 = fStatsY1; y2 = fStatsY2; // restart y-positions
	x2 = x1 - 0.005; // shift left 2
	x1 = x2 - (fStatsX2 - fStatsX1); // shift left 1
	if (x1 < 0.) { // give up, start again:
	  x1 = fStatsX1, x2 = fStatsX2, y1 = fStatsY1, y2 = fStatsY2;
	}
      }
    } else if (gStyle->GetOptStat() != 0) { // failure in case changed in list via TExec....
      this->Warning("ColourStatsBoxes", "No stats found for %s", hists->At(iH)->GetName());
    }
  }
}

//________________________________________________________
void GFHistManager::ColourFuncs(GFHistArray *hists) const
{
  // adjust colour of funcs to match hist, but only if exactly one function per hist
  if (!hists) return;
  for (Int_t iH = 0; iH < hists->GetEntriesFast(); ++iH) {
    TH1 *h = hists->At(iH);
    if (!h) continue;

    // look for _the_ TF1 (not > 1!)
    TF1 *func = NULL;
    TIter nextprim(h->GetListOfFunctions());
    while (TObject* next = nextprim()) {
      if (next->InheritsFrom(TF1::Class())) {
	if (func) { // there is already a TF1, so...
	  func = NULL; // remove it again...
	  break;       // ...and stop searching for  more!
	} else {
	  func = static_cast<TF1*>(next);
	}
      }
    }
// if exactly 1 found, adjust line style/colour
    if (func) {
      func->SetLineColor(h->GetLineColor());
      func->SetLineStyle(h->GetLineStyle());
    }
  } 
}

//________________________________________________________
void GFHistManager::SetCanvasName(const TString& name) {
  fCanvasName = name;
}