// @(#)root/proofplayer:$Id$
// Author: Maarten Ballintijn   19/04/2002

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TProofLimitsFinder                                                   //
//                                                                      //
// Class to find nice axis limits and synchronize them between workers  //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TProofLimitsFinder.h"
#include "TProofServ.h"
#include "TSocket.h"
#include "TH1.h"
#include "TMessage.h"
#include "TProofDebug.h"
#include "TError.h"

ClassImp(TProofLimitsFinder)

//______________________________________________________________________________
void TProofLimitsFinder::AutoBinFunc(TString& key,
                                     Double_t& xmin, Double_t& xmax,
                                     Double_t& ymin, Double_t& ymax,
                                     Double_t& zmin, Double_t& zmax)
{
   // Get bining information. Send current min and max and receive common
   // min max in return.

   if (!gProofServ) return;

   TSocket *s = gProofServ->GetSocket();
   TMessage mess(kPROOF_AUTOBIN);

   PDB(kGlobal, 2) {
      ::Info("TProofLimitsFinder::AutoBinFunc",
             "Sending %f, %f, %f, %f, %f, %f", xmin, xmax, ymin, ymax, zmin, zmax);
   }
   mess << key << xmin << xmax << ymin << ymax << zmin << zmax;

   s->Send(mess);

   Bool_t notdone = kTRUE;
   while (notdone) {
      TMessage *answ;
      if (s->Recv(answ) <= 0 || !answ)
         return;

      Int_t what = answ->What();
      if (what == kPROOF_AUTOBIN) {
         (*answ) >> key >> xmin >> xmax >> ymin >> ymax >> zmin >> zmax;
         notdone = kFALSE;
      } else {
         Int_t xrc = gProofServ->HandleSocketInput(answ, kFALSE);
         if (xrc == -1) {
            ::Error("TProofLimitsFinder::AutoBinFunc", "command %d cannot be executed while processing", what);
         } else if (xrc == -2) {
            ::Error("TProofLimitsFinder::AutoBinFunc", "unknown command %d ! Protocol error?", what);
         }
      }
      delete answ;
   }
}

//______________________________________________________________________________
Int_t TProofLimitsFinder::FindGoodLimits(TH1 *h, Axis_t xmin, Axis_t xmax)
{
   // Find good limits

   Double_t dummy = 0;

   TString key = h->GetName();
   AutoBinFunc(key, xmin, xmax, dummy, dummy, dummy, dummy);

   return THLimitsFinder::FindGoodLimits( h, xmin, xmax);
}


//______________________________________________________________________________
Int_t TProofLimitsFinder::FindGoodLimits(TH1 *h, Axis_t xmin, Axis_t xmax, Axis_t ymin, Axis_t ymax)
{
   // Find good limits

   Double_t dummy = 0;

   TString key = h->GetName();
   AutoBinFunc(key, xmin, xmax, ymin, ymax, dummy, dummy);

   return THLimitsFinder::FindGoodLimits( h, xmin, xmax, ymin, ymax);
}


//______________________________________________________________________________
Int_t TProofLimitsFinder::FindGoodLimits(TH1 *h, Axis_t xmin, Axis_t xmax, Axis_t ymin, Axis_t ymax, Axis_t zmin, Axis_t zmax)
{
   // Find good limits

   TString key = h->GetName();
   AutoBinFunc(key, xmin, xmax, ymin, ymax, zmin, zmax);

   return THLimitsFinder::FindGoodLimits( h, xmin, xmax, ymin, ymax, zmin, zmax);
}
 TProofLimitsFinder.cxx:1
 TProofLimitsFinder.cxx:2
 TProofLimitsFinder.cxx:3
 TProofLimitsFinder.cxx:4
 TProofLimitsFinder.cxx:5
 TProofLimitsFinder.cxx:6
 TProofLimitsFinder.cxx:7
 TProofLimitsFinder.cxx:8
 TProofLimitsFinder.cxx:9
 TProofLimitsFinder.cxx:10
 TProofLimitsFinder.cxx:11
 TProofLimitsFinder.cxx:12
 TProofLimitsFinder.cxx:13
 TProofLimitsFinder.cxx:14
 TProofLimitsFinder.cxx:15
 TProofLimitsFinder.cxx:16
 TProofLimitsFinder.cxx:17
 TProofLimitsFinder.cxx:18
 TProofLimitsFinder.cxx:19
 TProofLimitsFinder.cxx:20
 TProofLimitsFinder.cxx:21
 TProofLimitsFinder.cxx:22
 TProofLimitsFinder.cxx:23
 TProofLimitsFinder.cxx:24
 TProofLimitsFinder.cxx:25
 TProofLimitsFinder.cxx:26
 TProofLimitsFinder.cxx:27
 TProofLimitsFinder.cxx:28
 TProofLimitsFinder.cxx:29
 TProofLimitsFinder.cxx:30
 TProofLimitsFinder.cxx:31
 TProofLimitsFinder.cxx:32
 TProofLimitsFinder.cxx:33
 TProofLimitsFinder.cxx:34
 TProofLimitsFinder.cxx:35
 TProofLimitsFinder.cxx:36
 TProofLimitsFinder.cxx:37
 TProofLimitsFinder.cxx:38
 TProofLimitsFinder.cxx:39
 TProofLimitsFinder.cxx:40
 TProofLimitsFinder.cxx:41
 TProofLimitsFinder.cxx:42
 TProofLimitsFinder.cxx:43
 TProofLimitsFinder.cxx:44
 TProofLimitsFinder.cxx:45
 TProofLimitsFinder.cxx:46
 TProofLimitsFinder.cxx:47
 TProofLimitsFinder.cxx:48
 TProofLimitsFinder.cxx:49
 TProofLimitsFinder.cxx:50
 TProofLimitsFinder.cxx:51
 TProofLimitsFinder.cxx:52
 TProofLimitsFinder.cxx:53
 TProofLimitsFinder.cxx:54
 TProofLimitsFinder.cxx:55
 TProofLimitsFinder.cxx:56
 TProofLimitsFinder.cxx:57
 TProofLimitsFinder.cxx:58
 TProofLimitsFinder.cxx:59
 TProofLimitsFinder.cxx:60
 TProofLimitsFinder.cxx:61
 TProofLimitsFinder.cxx:62
 TProofLimitsFinder.cxx:63
 TProofLimitsFinder.cxx:64
 TProofLimitsFinder.cxx:65
 TProofLimitsFinder.cxx:66
 TProofLimitsFinder.cxx:67
 TProofLimitsFinder.cxx:68
 TProofLimitsFinder.cxx:69
 TProofLimitsFinder.cxx:70
 TProofLimitsFinder.cxx:71
 TProofLimitsFinder.cxx:72
 TProofLimitsFinder.cxx:73
 TProofLimitsFinder.cxx:74
 TProofLimitsFinder.cxx:75
 TProofLimitsFinder.cxx:76
 TProofLimitsFinder.cxx:77
 TProofLimitsFinder.cxx:78
 TProofLimitsFinder.cxx:79
 TProofLimitsFinder.cxx:80
 TProofLimitsFinder.cxx:81
 TProofLimitsFinder.cxx:82
 TProofLimitsFinder.cxx:83
 TProofLimitsFinder.cxx:84
 TProofLimitsFinder.cxx:85
 TProofLimitsFinder.cxx:86
 TProofLimitsFinder.cxx:87
 TProofLimitsFinder.cxx:88
 TProofLimitsFinder.cxx:89
 TProofLimitsFinder.cxx:90
 TProofLimitsFinder.cxx:91
 TProofLimitsFinder.cxx:92
 TProofLimitsFinder.cxx:93
 TProofLimitsFinder.cxx:94
 TProofLimitsFinder.cxx:95
 TProofLimitsFinder.cxx:96
 TProofLimitsFinder.cxx:97
 TProofLimitsFinder.cxx:98
 TProofLimitsFinder.cxx:99
 TProofLimitsFinder.cxx:100
 TProofLimitsFinder.cxx:101
 TProofLimitsFinder.cxx:102
 TProofLimitsFinder.cxx:103
 TProofLimitsFinder.cxx:104
 TProofLimitsFinder.cxx:105
 TProofLimitsFinder.cxx:106
 TProofLimitsFinder.cxx:107
 TProofLimitsFinder.cxx:108
 TProofLimitsFinder.cxx:109
 TProofLimitsFinder.cxx:110
 TProofLimitsFinder.cxx:111