Exemple #1
0
bool Cdata::insert(Cparticle &_p) {
    
   Ctree_node *_ptree_node = get_root_node();
   //cout << "inserting particle with r="<<_p.r<<" and tag="<<_p.tag<<endl;
   //cout << "starting at root node with key="<<_ptree_node->key<<endl;
   while (1) {
      _ptree_node->updateNodeWithParticle(_p);

      Cparticle *_ppexist = _ptree_node->p;
      if (_ppexist != NULL) {
         Cparticle &_pexist = *_ppexist;
         //cout << "this node has a particle. removing particle with r="<<_pexist.r<<"and tag="<<_pexist.tag<<endl;
         _ptree_node->p = NULL;
         Ctree_node *_pdexist = daughter(_ptree_node,_pexist);
         _pdexist->updateNodeWithParticle(_pexist);
         Ctree_node *_pdp = daughter(_ptree_node,_p);
         _pdp->updateNodeWithParticle(_p);
	 while (_pdexist==_pdp) { 
            //cout << "both daughters are the same. going deeper...(key="<<_pdp->key<<")"<<endl;
            _pdexist->leaf = false;
            _pdexist = daughter(_pdexist,_pexist);
            _pdexist->updateNodeWithParticle(_pexist);       
            _pdp = daughter(_pdp,_p);
            _pdp->updateNodeWithParticle(_p);
         }
         //cout << "found two separate daughters. orig key="<<_pdp->key<<"orig centre="<<_pdp->centre<<". exist key="<<_pdexist->key<<" exist centre="<<_pdexist->centre<<endl;
         _pdexist->add_particle(_pexist);
         _pdp->add_particle(_p);
         return true;
      } else if (_ptree_node->leaf) {
         //cout << "this node is a leaf. adding particle..."<<endl;
         _ptree_node->add_particle(_p);
         return true;
      }
      _ptree_node = daughter(_ptree_node,_p);
      //cout << "moving down tree... new key = "<<_ptree_node->key<<" new centre="<<_ptree_node->centre<<endl;
   }
}
Exemple #2
0
void GenTopEvent::Fill(const std::vector<reco::GenParticle>& prunedGenParticles, int ttXid_){

  ttXid=ttXid_;
  for(auto p=prunedGenParticles.begin(); p!=prunedGenParticles.end(); p++){
    if (abs(p->pdgId())==6){
      bool lastTop=true;
      for(uint i=0;i<p->numberOfDaughters();i++){
	if (abs(p->daughter(i)->pdgId())==6)
	  lastTop=false;
      }
      if(lastTop){
	if(p->pdgId()==6) top=*p;
	if(p->pdgId()==-6) topbar=*p;
	bool setTDecay=false;
	bool setTBarDecay=false;
	for(uint i=0;i<p->numberOfDaughters();i++){
	  if(p->pdgId()==6 && abs(p->daughter(i)->pdgId())<6){
	    if(setTDecay) std::cerr << "GenTopEvent: error 1"<<std::endl;
	    top_decay_quark=*(reco::GenParticle*)p->daughter(i);
	    setTDecay=true;
	  }
	  if(p->pdgId()==-6 && abs(p->daughter(i)->pdgId())<6){
	    if(setTBarDecay) std::cerr << "GenTopEvent: error 1"<<std::endl;
	    topbar_decay_quark=*(reco::GenParticle*)p->daughter(i);
	    setTBarDecay=true;
	  }
	}
      }
    }
  
    if (abs(p->pdgId())==24){
      bool lastW=true;
      for(uint i=0;i<p->numberOfDaughters();i++){
	if (abs(p->daughter(i)->pdgId())==24)
	  lastW=false;
      }
      bool fromT=false;
      const reco::Candidate* mother=&(*p);
      while(mother!=0 && abs(mother->pdgId())==24){
	if (abs(mother->mother()->pdgId())==6){
	  fromT=true;
	  break;
	}
	else mother=mother->mother();
	
      }
      if(lastW&&fromT){
	if(p->pdgId()==24) wplus=*p;
	if(p->pdgId()==-24) wminus=*p;
	for(uint i=0;i<p->numberOfDaughters();i++){
	  if(p->pdgId()==24 && abs(p->daughter(i)->pdgId())<=16){
	    wplus_decay_products.push_back(*(reco::GenParticle*)p->daughter(i));
	  }
	  if(p->pdgId()==-24 && abs(p->daughter(i)->pdgId())<=16){
	    wminus_decay_products.push_back(*(reco::GenParticle*)p->daughter(i));
	  }
	}
      }
    }

    if (abs(p->pdgId())==25){
      bool lastH=true;
      for(uint i=0;i<p->numberOfDaughters();i++){
	if (abs(p->daughter(i)->pdgId())==25)
	  lastH=false;
      }
      if(lastH){
	higgs=*p;
	for(uint i=0;i<p->numberOfDaughters();i++){
	  if(p->pdgId()==25 && abs(p->daughter(i)->pdgId())!=25){
	    higgs_decay_products.push_back(*(reco::GenParticle*)p->daughter(i));
	  }
	}
      }
    }

  }
  if(wminus_decay_products.size()!=2 || wplus_decay_products.size()!=2) std::cerr << "GenTopEvent: error 2"<<std::endl;
  if(top.energy()<1||topbar.energy()<1||wplus.energy()<1||wminus.energy()<1||top_decay_quark.energy()<1||topbar_decay_quark.energy()<1) std::cerr << "GenTopEvent: error 4"<<std::endl;

  int nquarks_from_wplus=0;
  for(auto p=wplus_decay_products.begin(); p!=wplus_decay_products.end();p++){
    if(abs(p->pdgId())<6) nquarks_from_wplus++;
  }
  int nquarks_from_wminus=0;
  for(auto p=wminus_decay_products.begin(); p!=wminus_decay_products.end();p++){
    if(abs(p->pdgId())<6) nquarks_from_wminus++;
  }
  topIsHadronic=nquarks_from_wplus==2;
  topbarIsHadronic=nquarks_from_wminus==2;
  isFilled=true;
}
Exemple #3
0
        Problem_Representation_Type const 
        operator()( Chromosome_Args ... ch_args )  
        {
            //initialize first generation randomly
            for ( std::size_t i = 0; i != population_per_generation; ++i )
                current_population.push_back( chromosome_type( ch_args... ) );

            //std::cerr << "\ninitialized " << population_per_generation << " chromosomes.\n";


            std::size_t debug_counter = 0;
            best_fitness = std::numeric_limits<Fitness_Type>::max();
            srand ( unsigned ( time (NULL) ) ); //for random_shuffle
            Fitness_Type total_fit;

            Problem_Representation_Type pr_0;
            Problem_Representation_Type pr_1;
            Problem_Representation_Type pr_2;
            Problem_Representation_Type pr_3;

            std::ofstream elite_out( "elite.dat" );
            std::ofstream elite_fit( "elite_fit.dat");
            
            for (;;)
            {
            //evaluate
                //feng::for_each( current_population.begin(), current_population.end(), [](chromosome_type& chrom) { Evaluation_Method()(Chromosome_Problem_Translation_Method()(chrom)); });
                //std::cerr << "\nevaluating........";

#if 0
                for ( auto& ch : current_population )
                    if ( ch.modification_after_last_evaluation_flag )
                    {
                        Chromosome_Problem_Translation_Method()( *(ch.chrom), pr );
                        ch.fit = Evaluation_Method()(pr);
                        //Evaluation_Method()(Chromosome_Problem_Translation_Method()(ch));
                        ch.modification_after_last_evaluation_flag = false;
                    }
#endif
#if 1
                std::thread t0( parallel_evaluator(), current_population.begin(), current_population.begin()+(current_population.size()/4), &pr_0 );
                std::thread t1( parallel_evaluator(), current_population.begin()+(current_population.size()/4), current_population.begin()+(current_population.size()/2), &pr_1 );
                std::thread t2( parallel_evaluator(), current_population.begin()+(current_population.size()/2), current_population.begin()+(current_population.size()*3/4), &pr_2 );
                std::thread t3( parallel_evaluator(), current_population.begin()+(current_population.size()*3/4), current_population.end(), &pr_3 );
                //parallel_evaluator()( current_population.begin()+(current_population.size()*3/4), current_population.end(), &pr_3 );
                t0.join();
                t1.join();
                t2.join();
                t3.join();
#endif

                //using nth_element?
                //mutate duplicated chromosome?
                std::sort( current_population.begin(), current_population.end() );

                auto const elite_one = *(current_population.begin());
                Chromosome_Problem_Translation_Method()( *(elite_one.chrom), pr );

                //keep the best individual of the current generation
                if ( elite_one.fit < best_fitness )
                {
                    debug_counter++;

                    best_fitness = elite_one.fit;
                    best_one = pr;
                    
                    elite_out << best_one;
                    elite_fit << best_fitness;


                    if ( debug_counter & 0xf )
                    {
                        elite_out << "\n";
                        elite_fit << "\n";
                    }
                    else
                    {
                        elite_out << std::endl;
                        elite_fit << std::endl;
                    }
                }

                current_population.resize( std::distance( current_population.begin(), std::unique( current_population.begin(), current_population.end() )) );
                if ( current_population.size() < population_per_generation )
                {
                    //generate someone randomly and evaluate
                    for ( std::size_t i = current_population.size(); i != population_per_generation; ++i )
                    {
                        auto ch = chromosome_type( ch_args...);
                        Chromosome_Problem_Translation_Method()( *(ch.chrom), pr );
                        ch.fit = Evaluation_Method()(pr);
                        ch.modification_after_last_evaluation_flag = false;
                        current_population.push_back( ch );
                    }
                }
                else
                {
                    //shuffle the resize
                    std::random_shuffle( current_population.begin(), current_population.end() );
                    current_population.resize( population_per_generation );
                }
    
                std::sort( current_population.begin(), current_population.end() );

                //total_fit = 0;
                //for( auto& ch : current_population )
                //    total_fit += ch.fit;
                //std::cout.precision(20);
                //std::cout << total_fit << "\n";

                //if timeout, return output elite
                auto& t_manager = feng::singleton<time_manager>::instance();
                if ( t_manager.is_timeout() )
                {
                    std::cerr << "\nthe residual for the best individual is " << best_fitness;

                    elite_out.close();
                    elite_fit.close();

                    return best_one;
                    //return pr;
                }

                //std::cerr << "\nElite of " << debug_counter++ << " is \n" << pr;
                //std::cerr << "\nElite of " << debug_counter << " is \n" << *(elite_one.chrom);


            //select 
                auto& xpm = feng::singleton<xover_probability_manager<>>::instance();
                std::size_t const selection_number = xpm(population_per_generation);
                //selected_population_for_xover_father.resize(selection_number);
                //selected_population_for_xover_mother.resize(selection_number);
                selected_population_for_xover_father.clear();
                selected_population_for_xover_mother.clear();

                //std::cerr << "\nselecting............." << selection_number;

                auto& xs_manager = feng::singleton<xover_selection_manager>::instance();
                for ( std::size_t i = 0; i != selection_number; ++i )
                {
                    //selected_population_for_xover_father[i] = current_population[xs_manager()];
                    //selected_population_for_xover_mother[i] = current_population[xs_manager()];
                    
                    const std::size_t select1 = xs_manager();
                    const std::size_t select2 = xs_manager();

                    //selected_population_for_xover_father.push_back(current_population[xs_manager()]);
                    //selected_population_for_xover_mother.push_back(current_population[xs_manager()]);
                    selected_population_for_xover_father.push_back(current_population[select1]);
                    selected_population_for_xover_mother.push_back(current_population[select2]);
                }

                //std::cerr << "\nselectedted";
            //xover
#if 0
                //not working as selected population for xover are pure reference of current populaiton
                for ( std::size_t i = 0; i != selection_number; ++i )
                    feng::for_each( selected_population_for_xover_father[i].begin(), selected_population_for_xover_father[i].end(), 
                                    selected_population_for_xover_mother[i].begin(), Chromosome_Xover_Method() );

                current_population.reserve( population_per_generation + selection_number + selection_number );
                //append newly generated genes into current population
                current_population.insert( current_population.begin()+population_per_generation, selected_population_for_xover_father.begin(), selected_population_for_xover_father.end() );
                current_population.insert( current_population.begin()+population_per_generation+selection_number, selected_population_for_xover_mother.begin(), selected_population_for_xover_mother.end() );
#endif
                //std::cerr << "\nxover.............";
                
                for ( std::size_t i = 0; i != selection_number; ++i )
                {
                    chromosome_type son( ch_args... ); 
                    chromosome_type daughter( ch_args... ); 

                    feng::for_each( selected_population_for_xover_father[i].begin(), selected_population_for_xover_father[i].end(), selected_population_for_xover_mother[i].begin(), son.begin(), daughter.begin(), Chromosome_Xover_Method() );

                    current_population.push_back( son ); 
                    current_population.push_back( daughter ); 
                }
                //mark new generation not evaluated
                feng::for_each( current_population.begin()+population_per_generation, current_population.end(), [](chromosome_type& ch) { ch.modification_after_last_evaluation_flag = true; } );

                //std::cerr << "\nxovered";

            //mutate
                //std::cerr << "\nmutating";

                auto& mpm = feng::singleton<mutation_probability_manager<>>::instance();
                mpm.reset();
                for ( auto& chromosome : current_population )
                    if ( mpm.should_mutate_current_gene() )
                    {
                        for ( auto& gene : chromosome )
                            Chromosome_Mutation_Method()(gene);
                        //makr muated ones as not evaluated
                        chromosome.modification_after_last_evaluation_flag = true;
                    }

                //std::cerr << "\nmutated";

            //goto evaluate



            }

            assert( !"Should never reach here!!" );

            return Problem_Representation_Type();
                


        }