void fillPFCandVsCount ( Jet & jet, TH2F* hist ){
  
  const int n_ids = 8;
  int pfcand_ids[n_ids + 1];
  memset ( pfcand_ids, 0, sizeof (pfcand_ids));
  
  int nPFCands = jet.getNPFCandidates();
  for (int iPFCand = 0; iPFCand < nPFCands; ++iPFCand){
    PFCand cand = jet.getPFCandidate( iPFCand );
    pfcand_ids[cand.Id()]++;
    pfcand_ids[n_ids]++;
  }
  for (int i = 0; i <= n_ids; ++i) hist -> Fill(i, pfcand_ids[i]);

}
void fillPlots ( CollectionPtr & jets, 
		 CollectionPtr & core_jets, 
		 CollectionPtr & partons,
		 CollectionPtr & hgceeClusters,
		 CollectionPtr & hgchefClusters,
		 CollectionPtr & hgchebClusters,
		 likelihoodGetter & l, 
		 int index,
		 std::vector<TH1F*> quark_th1, std::vector<TH1F*> gluon_th1,
		 std::vector<TH2F*> quark_th2, std::vector<TH2F*> gluon_th2 ){

  int n_jets = jets -> GetSize();

  std::vector<double> likelihood_variables;

  for (int i = 0; i < n_jets; ++i){

    Jet jet = jets -> GetConstituent<Jet>(i);
    CollectionPtr all_core_jets     = core_jets;
    CollectionPtr matched_core_jets = core_jets -> SkimByRequireDRMatch<PFCoreJet, PFPrunedJet>( jet, 0.3 ) ;
    
    rechit_type my_type = NO_TYPE;
    int my_index = -1;
    jet.getLeadClusterTypeAndIndex(my_type, my_index);    
    double clusterLength = -1.;
    double clusterVolume = -1.;
    if ( my_type == NO_TYPE ) continue;

    if ( my_type == HGCEE ){
      HGCEECluster c(*hgceeClusters, my_index);
      clusterLength = c.getLength();
      clusterVolume = c.getVolume();
    }

    else if ( my_type == HGCHEF ){
      HGCHEFCluster c(*hgchefClusters, my_index);
      clusterLength = c.getLength();
      clusterVolume = c.getVolume();
    }

    else {
      HGCHEBCluster c(*hgchebClusters, my_index);
      clusterLength = c.getLength();
      clusterVolume = c.getVolume();
    }

    double core_over_total_pt = -1.;
    double lead_core_jet_dr   = -1.;
    double n_core_jets        = matched_core_jets -> GetSize();
    if ( n_core_jets > 0 ){
      PFCoreJet lead_core_jet = matched_core_jets -> GetLeadPtObject<PFCoreJet> ();
      double core_pt = lead_core_jet.Pt();
      core_over_total_pt = core_pt / jet.Pt();
      lead_core_jet_dr   = jet.DeltaR( &lead_core_jet );
    }
    
    int flavor = getFlavor ( jet , partons );
    if ( flavor == -1 ) continue;
    bool isQuarkJet ( flavor == 1 );
    bool isGluonJet ( flavor == 0 );


    likelihood_variables.clear();

    double weighted_depth = jet.getWeightedDepth();
    double weighted_depth_noEE = jet.getWeightedDepthNoEE();

    double profile[n_radii];
    jet.getProfile(n_radii, radii, profile);

    double profile50 = jet.getProfileRadius(n_radii, radii, profile, 0.5);
    double profile90 = jet.getProfileRadius(n_radii, radii, profile, 0.9);
    double profile95 = jet.getProfileRadius(n_radii, radii, profile, 0.95);
    double profile99 = jet.getProfileRadius(n_radii, radii, profile, 0.99);

    likelihood_variables.push_back ( jet.TrimmedNSubjets(index) );
    likelihood_variables.push_back ( jet.getWidth()   );
    likelihood_variables.push_back ( jet.getNPFCandidates() );
    likelihood_variables.push_back ( jet.getPTD() );
    likelihood_variables.push_back ( jet.NSubJettiness() );
    likelihood_variables.push_back ( jet.TrimmedMass(index) * 1000. );
    likelihood_variables.push_back ( profile50 );
    likelihood_variables.push_back ( core_over_total_pt );
    likelihood_variables.push_back ( lead_core_jet_dr );

    double ee_energy     = jet.getEEEnergy();
    double ee015_energy  = jet.getEEEnergy(0, 15);
    double ee1631_energy = jet.getEEEnergy(16, 31);
    double hef_energy    = jet.getHEFEnergy();
    double heb_energy    = jet.getHEBEnergy();
    double all_energy    = ee_energy + hef_energy + heb_energy;

    double hef11_energy  = jet.getHEFEnergy(1, 1);
    double hef22_energy  = jet.getHEFEnergy(2, 2);
    double hef34_energy  = jet.getHEFEnergy(3, 4);
    double hef56_energy  = jet.getHEFEnergy(5, 6);
    double hef712_energy = jet.getHEFEnergy(7,12);

    double likelihood  = l.getLikelihood ( likelihood_variables );

    if ( isQuarkJet ){

      quark_th1[0] -> Fill ( jet.NSubJettiness() );
      quark_th1[1] -> Fill ( jet.TrimmedMass(index) * 1000. );
      quark_th1[2] -> Fill ( jet.TrimmedNSubjets(index) );
      quark_th1[3] -> Fill ( jet.getNPFCandidates() );
      quark_th1[4] -> Fill ( jet.getWidth() );
      quark_th1[5] -> Fill ( jet.getChargedWidth() );
      quark_th1[6] -> Fill ( jet.getNeutralWidth() );
      quark_th1[7] -> Fill ( weighted_depth );
      quark_th1[8] -> Fill ( weighted_depth_noEE );
      quark_th1[9] -> Fill ( jet.getPTD() );
      quark_th1[10] -> Fill ( likelihood );
      quark_th1[11] -> Fill ( ee_energy / all_energy );
      quark_th1[12] -> Fill ( ee015_energy / all_energy );
      quark_th1[13] -> Fill ( ee1631_energy / all_energy );
      quark_th1[14]-> Fill ( heb_energy / all_energy );
      quark_th1[15]-> Fill ( hef_energy / all_energy );
      quark_th1[16]-> Fill ( hef11_energy / all_energy );
      quark_th1[17]-> Fill ( hef22_energy / all_energy );
      quark_th1[18]-> Fill ( hef34_energy / all_energy );
      quark_th1[19]-> Fill ( hef56_energy / all_energy );
      quark_th1[20]-> Fill ( hef712_energy / all_energy );
      for (int i_radius = 0; i_radius < n_radii; ++i_radius){
	quark_th1[21] -> Fill(radii[i_radius], profile[i_radius]);
      }
      quark_th1[22] -> Fill ( profile50 );
      quark_th1[23] -> Fill ( profile90 );
      quark_th1[24] -> Fill ( profile95 );
      quark_th1[25] -> Fill ( profile99 );
      quark_th1[26] -> Fill ( n_core_jets        );
      quark_th1[27] -> Fill ( lead_core_jet_dr   );
      quark_th1[28] -> Fill ( core_over_total_pt );
      quark_th1[29] -> Fill ( clusterVolume );
      quark_th1[30] -> Fill ( clusterLength );
      
      quark_th2[0] -> Fill ( jet.Pt(), weighted_depth );
      quark_th2[1] -> Fill ( jet.Pt(), jet.getMaxRHDepth());
      quark_th2[2] -> Fill ( jet.getNPFCandidates(), jet.TrimmedNSubjets(index) );
    }

    if ( isGluonJet ){
      
      gluon_th1[0] -> Fill ( jet.NSubJettiness() );
      gluon_th1[1] -> Fill ( jet.TrimmedMass(index) * 1000. );
      gluon_th1[2] -> Fill ( jet.TrimmedNSubjets(index) );
      gluon_th1[3] -> Fill ( jet.getNPFCandidates() );
      gluon_th1[4] -> Fill ( jet.getWidth() );
      gluon_th1[5] -> Fill ( jet.getChargedWidth() );
      gluon_th1[6] -> Fill ( jet.getNeutralWidth() );
      gluon_th1[7] -> Fill ( weighted_depth );
      gluon_th1[8] -> Fill ( weighted_depth_noEE );
      gluon_th1[9] -> Fill ( jet.getPTD() );
      gluon_th1[10] -> Fill ( likelihood );
      gluon_th1[11] -> Fill ( ee_energy / all_energy );

      gluon_th1[12] -> Fill ( ee015_energy / all_energy );
      gluon_th1[13] -> Fill ( ee1631_energy / all_energy );
      gluon_th1[14]-> Fill ( heb_energy / all_energy );
      gluon_th1[15]-> Fill ( hef_energy / all_energy );
      gluon_th1[16]-> Fill ( hef11_energy / all_energy );
      gluon_th1[17]-> Fill ( hef22_energy / all_energy );
      gluon_th1[18]-> Fill ( hef34_energy / all_energy );
      gluon_th1[19]-> Fill ( hef56_energy / all_energy );
      gluon_th1[20]-> Fill ( hef712_energy / all_energy );
      for (int i_radius = 0; i_radius < n_radii; ++i_radius){
	gluon_th1[21] -> Fill(radii[i_radius], profile[i_radius]);
      }
      gluon_th1[22] -> Fill ( profile50 );
      gluon_th1[23] -> Fill ( profile90 );
      gluon_th1[24] -> Fill ( profile95 );
      gluon_th1[25] -> Fill ( profile99 );
      gluon_th1[26] -> Fill ( n_core_jets        );
      gluon_th1[27] -> Fill ( lead_core_jet_dr   );
      gluon_th1[28] -> Fill ( core_over_total_pt );
      gluon_th1[29] -> Fill ( clusterVolume );
      gluon_th1[30] -> Fill ( clusterLength );

      gluon_th2[0] -> Fill ( jet.Pt(), weighted_depth );
      gluon_th2[1] -> Fill ( jet.Pt(), jet.getMaxRHDepth());
      gluon_th2[2] -> Fill ( jet.getNPFCandidates(), jet.TrimmedNSubjets(index) );

      
    }
  }
}