Index: external/fastjet/plugins/GridJet/GridJetPlugin.cc
===================================================================
--- external/fastjet/plugins/GridJet/GridJetPlugin.cc	(revision d69dfe4aebfe1009f10ecbbabd92ce813ba93f3e)
+++ external/fastjet/plugins/GridJet/GridJetPlugin.cc	(revision 35cdc461e095e63247ab9dded070d76bc2c888e2)
@@ -1,6 +1,6 @@
-//STARTHEADER
-// $Id$
-//
-// Copyright (c) 2011, Matteo Cacciari, Gavin Salam and Gregory Soyez
+//FJSTARTHEADER
+// $Id: GridJetPlugin.cc 2268 2011-06-20 15:12:26Z salam $
+//
+// Copyright (c) 2005-2014, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
 //
 //----------------------------------------------------------------------
@@ -13,7 +13,9 @@
 //
 //  The algorithms that underlie FastJet have required considerable
-//  development and are described in hep-ph/0512210. If you use
+//  development. They are described in the original FastJet paper,
+//  hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
 //  FastJet as part of work towards a scientific publication, please
-//  include a citation to the FastJet paper.
+//  quote the version you use and include a citation to the manual and
+//  optionally also to hep-ph/0512210.
 //
 //  FastJet is distributed in the hope that it will be useful,
@@ -25,5 +27,5 @@
 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
 //----------------------------------------------------------------------
-//ENDHEADER
+//FJENDHEADER
 
 // fastjet stuff
@@ -43,4 +45,8 @@
                               double requested_grid_spacing, 
                               const JetDefinition & post_jet_def) :
+#ifdef FASTJET_GRIDJET_USEFJGRID
+  RectangularGrid(ymax, requested_grid_spacing), _post_jet_def(post_jet_def) {
+}
+#else
   _ymin(-ymax), _ymax(ymax), 
   _requested_grid_spacing(requested_grid_spacing) ,
@@ -49,5 +55,16 @@
   setup_grid();
 }
-
+#endif
+
+#ifdef FASTJET_GRIDJET_USEFJGRID
+GridJetPlugin::GridJetPlugin (const RectangularGrid & grid,
+                              const JetDefinition & post_jet_def) : 
+  RectangularGrid(grid), _post_jet_def(post_jet_def) {
+  if (!RectangularGrid::is_initialised()) 
+    throw Error("attempt to construct GridJetPlugin with uninitialised RectangularGrid");
+}
+#endif // FASTJET_GRIDJET_USEFJGRID
+
+#ifndef FASTJET_GRIDJET_USEFJGRID
 void GridJetPlugin::setup_grid() {
   // since we've exchanged the arguments of the constructor,
@@ -69,22 +86,6 @@
 }
 
-
-//----------------------------------------------------------------------
-string GridJetPlugin::description () const {
-  ostringstream desc;
-  desc << "GridJetPlugin plugin with ymax = " << _ymax << ", dy = " << _dy << ", dphi = " << _dphi << " (requested grid spacing was " << _requested_grid_spacing << ")";
-  if (_post_jet_def.jet_algorithm() != undefined_jet_algorithm) {
-    desc << ", followed by " << _post_jet_def.description();
-  }
-  return desc.str();
-}
-
-
-//----------------------------------------------------------------------
-double GridJetPlugin::R() const {return sqrt(_dy*_dphi/pi);}
-
-
-//----------------------------------------------------------------------
-int GridJetPlugin::igrid(const PseudoJet & p) const {
+//----------------------------------------------------------------------
+int GridJetPlugin::tile_index(const PseudoJet & p) const {
   // directly taking int does not work for values between -1 and 0
   // so use floor instead
@@ -108,4 +109,25 @@
   return igrid_res;
 }
+#endif // not FASTJET_GRIDJET_USEFJGRID
+
+
+//----------------------------------------------------------------------
+string GridJetPlugin::description () const {
+  ostringstream desc;
+  desc << "GridJetPlugin plugin with ";
+#ifndef FASTJET_GRIDJET_USEFJGRID
+  desc << "ymax = " << _ymax << ", dy = " << _dy << ", dphi = " << _dphi << " (requested grid spacing was " << _requested_grid_spacing << ")";
+#else
+  desc << RectangularGrid::description();
+#endif
+  if (_post_jet_def.jet_algorithm() != undefined_jet_algorithm) {
+    desc << ", followed by " << _post_jet_def.description();
+  }
+  return desc.str();
+}
+
+
+//----------------------------------------------------------------------
+double GridJetPlugin::R() const {return sqrt(drap()*dphi()/pi);}
 
 
@@ -117,5 +139,5 @@
   //  * a number >= 0 will mean that particle indicated by the index
   //    is currently the jet on the grid
-  vector<int> grid(_ntotal, -1);
+  vector<int> grid(n_tiles(), -1);
  
   int nparticles = cs.jets().size();
@@ -126,9 +148,9 @@
   // combine particles with whatever is in the grid
   for (int i = 0; i < nparticles; i++) {
-    int igrd = igrid(cs.jets()[i]);
+    int igrd = tile_index(cs.jets()[i]);
     //cout << i << " " << cs.jets()[i].rap() << " " << cs.jets()[i].phi() 
     // 	 << " " << igrd << " " << grid.size() << " " << _ntotal << endl;
     if (igrd < 0) continue;
-    assert(igrd <= _ntotal);
+    assert(igrd <= n_tiles());
     if (grid[igrd] == -1) {
       grid[igrd] = i; // jet index of initial particle i is i
@@ -145,6 +167,6 @@
     // make the final jets via iB recombinations
     for (unsigned igrd = 0; igrd < grid.size(); igrd++) {
-      if (grid[igrd] != -1) cs.plugin_record_iB_recombination(grid[igrd], 
-							      dij_or_diB);
+      if (grid[igrd] != -1 && tile_is_good(igrd)) 
+                   cs.plugin_record_iB_recombination(grid[igrd], dij_or_diB);
     }
   } else {
Index: external/fastjet/plugins/GridJet/fastjet/GridJetPlugin.hh
===================================================================
--- external/fastjet/plugins/GridJet/fastjet/GridJetPlugin.hh	(revision d69dfe4aebfe1009f10ecbbabd92ce813ba93f3e)
+++ external/fastjet/plugins/GridJet/fastjet/GridJetPlugin.hh	(revision 35cdc461e095e63247ab9dded070d76bc2c888e2)
@@ -1,9 +1,9 @@
-#ifndef __GRIDJETPLUGIN_HH__
-#define __GRIDJETPLUGIN_HH__
+#ifndef __FASTJET_GRIDJETPLUGIN_HH__
+#define __FASTJET_GRIDJETPLUGIN_HH__
 
-//STARTHEADER
+//FJSTARTHEADER
 // $Id: GridJetPlugin.hh 2267 2011-06-20 15:10:23Z salam $
 //
-// Copyright (c) 2011, Matteo Cacciari, Gavin Salam and Gregory Soyez
+// Copyright (c) 2005-2014, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
 //
 //----------------------------------------------------------------------
@@ -16,7 +16,9 @@
 //
 //  The algorithms that underlie FastJet have required considerable
-//  development and are described in hep-ph/0512210. If you use
+//  development. They are described in the original FastJet paper,
+//  hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
 //  FastJet as part of work towards a scientific publication, please
-//  include a citation to the FastJet paper.
+//  quote the version you use and include a citation to the manual and
+//  optionally also to hep-ph/0512210.
 //
 //  FastJet is distributed in the hope that it will be useful,
@@ -28,8 +30,16 @@
 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
 //----------------------------------------------------------------------
-//ENDHEADER
+//FJENDHEADER
 
 
 #include "fastjet/JetDefinition.hh"
+
+// makes it easy to switch back and forth between use of
+// RectangularGrid or not; this got enabled in FJ3.1
+#define FASTJET_GRIDJET_USEFJGRID
+
+#ifdef FASTJET_GRIDJET_USEFJGRID
+#include "fastjet/RectangularGrid.hh"
+#endif
 
 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
@@ -49,7 +59,11 @@
 /// but is rather provided for comparison purposes with the 
 /// GridMedianBackgroundEstimator (which is even faster).
-class GridJetPlugin : public JetDefinition::Plugin {
+class GridJetPlugin : public JetDefinition::Plugin 
+#ifdef FASTJET_GRIDJET_USEFJGRID
+                                      , RectangularGrid
+#endif 
+{
 public:
-  /// Main constructor for the GridJetPlugin Plugin class.
+  /// Basic constructor for the GridJetPlugin Plugin class.
   ///
   /// \param ymax           The maximal rapidity extent of the grid
@@ -62,8 +76,23 @@
 		 const JetDefinition & post_jet_def = JetDefinition());
 
-  /// copy constructor
-  GridJetPlugin (const GridJetPlugin & plugin) {
-    *this = plugin;
-  }
+#ifdef FASTJET_GRIDJET_USEFJGRID
+  /// Constructor for the GridJetPlugin Plugin class that allows
+  /// full control over the underlying grid. New in FastJet 3.1.
+  ///
+  /// \param grid           The maximal rapidity extent of the grid
+  /// \param post_jet_def   if present, and not == JetDefinition()
+  ///                       (which has undefined_jet_algorithm), then
+  ///                       run the post_jet_def on the result of the grid
+  ///                       clustering.
+  GridJetPlugin (const RectangularGrid & grid,
+		 const JetDefinition & post_jet_def = JetDefinition());
+#endif // FASTJET_GRIDJET_USEFJGRID
+
+  
+
+  // /// copy constructor
+  // GridJetPlugin (const GridJetPlugin & plugin) {
+  //   *this = plugin;
+  // }
 
   // the things that are required by base class
@@ -75,4 +104,7 @@
   virtual double R() const;
 
+  // As of FastJet 3.1 the following functions become available through
+  // the underlying RectangularGrid class.
+#ifndef FASTJET_GRIDJET_USEFJGRID
   /// returns the actual rapidity spacing of the grid
   double drap()   const {return _dy;}
@@ -83,13 +115,20 @@
   /// returns the maximum rapidity of the grid
   double rapmax() const {return _ymax;}
+#endif
 
 private:
 
+#ifndef FASTJET_GRIDJET_USEFJGRID
   void setup_grid();
 
-  int igrid(const PseudoJet & p) const;
+  int n_tiles() const {return _ntotal;}
+  int n_good_tiles() const {return _ntotal;}
+
+  int tile_index(const PseudoJet & p) const;
+  bool tile_is_good(int /* itile */) const {return true;}
 
   double _ymin, _ymax, _dy, _dphi, _requested_grid_spacing;
   int _ny, _nphi, _ntotal;
+#endif
 
   JetDefinition _post_jet_def;
@@ -99,4 +138,4 @@
 FASTJET_END_NAMESPACE        // defined in fastjet/internal/base.hh
 
-#endif // __GRIDJETPLUGIN_HH__
+#endif // __FASTJET_GRIDJETPLUGIN_HH__
 
