photos_pythia_example.cxx

00001 /**
00002  * Example of use of photos C++ interface. Pythia events are
00003  * generated first and photos used for FSR.
00004  *
00005  * @author Nadia Davidson
00006  * @date 6 July 2009
00007  */
00008 
00009 //pythia header files
00010 #include "Pythia.h"
00011 #include "HepMCInterface.h"
00012 
00013 //MC-TESTER header files
00014 #include "Generate.h"
00015 #include "HepMCEvent.H"
00016 #include "Setup.H"
00017 
00018 //PHOTOS header files
00019 #include "Photos/Photos.h"
00020 #include "Photos/PhotosHepMCEvent.h"
00021 #include "Photos/Log.h"
00022 
00023 using namespace std;
00024 using namespace Pythia8;
00025 using namespace Photospp;
00026 
00027 bool ShowersOn=true;
00028 unsigned long NumberOfEvents = 10000;
00029 unsigned int EventsToCheck=20;
00030 
00031 // elementary test of HepMC typically executed before
00032 // detector simulation based on http://home.fnal.gov/~mrenna/HCPSS/HCPSShepmc.html
00033 // similar test was performed in Fortran
00034 // we perform it before and after Photos (for the first several events)
00035 void checkMomentumConservationInEvent(HepMC::GenEvent *evt)
00036 {
00037         //cout<<"List of stable particles: "<<endl;
00038 
00039         double px=0.0,py=0.0,pz=0.0,e=0.0;
00040         
00041         for ( HepMC::GenEvent::particle_const_iterator p = evt->particles_begin();
00042               p != evt->particles_end(); ++p )
00043         {
00044                 if( (*p)->status() == 1 )
00045                 {
00046                         HepMC::FourVector m = (*p)->momentum();
00047                         px+=m.px();
00048                         py+=m.py();
00049                         pz+=m.pz();
00050                         e +=m.e();
00051                         //(*p)->print();
00052                 }
00053         }
00054   cout.precision(6);
00055   cout.setf(ios_base::floatfield);
00056         cout<<endl<<"Vector Sum: "<<px<<" "<<py<<" "<<pz<<" "<<e<<endl;
00057 }
00058 
00059 /* Switch Status of History Entries
00060 
00061    If Photos::createHistoryEntries(true,3) was called, this function changes the 
00062    status code of photons added by Photos and particles modified by Photos
00063    to 3, switching the status of history entries to 1.
00064    
00065    This results leaves all modifications performed by Photos as history entries,
00066    while the regular entries represent original, unmodified event.
00067    
00068    This is an example of how such operation can be performed in user analysis.
00069    By default, this function is not used. The example of its use is commented
00070    out in main event loop.
00071    
00072    NOTE: The algorithm works only on stable particles and assumes that
00073          there were no modifications to the order of the particles in
00074          which they were written to HepMC by Photos. */
00075 void switch_history_entries_status(HepMC::GenEvent *evt)
00076 {
00077   for ( HepMC::GenEvent::particle_const_iterator p = evt->particles_begin();
00078               p != evt->particles_end(); ++p )
00079         {
00080     if((*p)->status()==3)
00081     {
00082       if((*p)->pdg_id()==22) continue;
00083 
00084       int barcode = (*p)->barcode();
00085 
00086       HepMC::GenVertex *v = (*p)->production_vertex();
00087       
00088       // History entries are added after photons, so we check what is the
00089       // position of current particle relative to photons.
00090       int position = 0;
00091       int last_photon_position = -1;
00092       
00093       for(HepMC::GenVertex::particles_out_const_iterator p2 = v->particles_out_const_begin();
00094           p2 != v->particles_out_const_end(); ++p2)
00095       {
00096         position++;
00097 
00098         if((*p2)->barcode()==barcode) break;
00099 
00100         if((*p2)->pdg_id()==22) { last_photon_position=position; }
00101       }
00102       
00103       // If particle is found prior to photons - it was already processed, so skip it
00104       if(last_photon_position<0) continue;
00105       
00106       position -= last_photon_position;
00107       HepMC::GenParticle *part = NULL;
00108       
00109       // Now, find the particle that corresponds to this history entry
00110       for(HepMC::GenVertex::particles_out_const_iterator p2 = v->particles_out_const_begin();
00111           p2 != v->particles_out_const_end(); ++p2)
00112       {
00113         --position;
00114         
00115         if     (position >  0) continue;
00116         else if(position == 0) part = *p2;
00117         else
00118         {
00119           // Give all remaining photons status 3
00120           if((*p2)->pdg_id()==22 ) (*p2)->set_status(3);
00121 
00122           // Finish if there are no more photons
00123           else break;
00124         }
00125       }
00126 
00127       // Check if this is the particle we are looking for
00128       if( part->pdg_id() != (*p)->pdg_id())
00129       {
00130         cout<<"switch_history_entries_status: mismatch in pdg_id of history entry"<<endl;
00131         cout<<"and its corresponding particle. The algorithm does not work correctly."<<endl;
00132         exit(-1);
00133       }
00134 
00135       // Skip this particle if its status is not 1
00136       if(part->status()!=1) continue;
00137       
00138       // Switch status codes of these particles
00139       part->set_status(3);
00140       (*p)->set_status(1);
00141     }
00142   }
00143 }
00144 
00145 int main(int argc,char **argv)
00146 {
00147         // Initialization of pythia
00148         HepMC::I_Pythia8 ToHepMC;
00149         Pythia pythia;
00150         Event& event = pythia.event;
00151         //pythia.settings.listAll();
00152 
00153         pythia.readString("PartonLevel:ISR = on");
00154         pythia.readString("PartonLevel:FSR = off");
00155 
00156         pythia.readString("WeakSingleBoson:ffbar2gmZ = on");
00157         pythia.readString("23:onMode = off");
00158         pythia.readString("23:onIfAny = 13");
00159         pythia.init( 11, -11, 91.187);                           //e+ e- collisions
00160 
00161         MC_Initialize();
00162 
00163         Photos::initialize();
00164         //Photos::setDoubleBrem(false);
00165         //Photos::setExponentiation(false);
00166 
00167         Photos::setInfraredCutOff(0.01/91.187); // 10MeV for scale to M_Z=91.187
00168         Photos::maxWtInterference(3.0);
00169   //Photos::createHistoryEntries(true,3);
00170 
00171         Photos::iniInfo();
00172         Log::SummaryAtExit();
00173         cout.setf(ios::fixed);
00174 
00175         // Begin event loop
00176         for(unsigned long iEvent = 0; iEvent < NumberOfEvents; ++iEvent)
00177         {
00178                 if(iEvent%1000==0) Log::Info()<<"Event: "<<iEvent<<"\t("<<iEvent*(100./NumberOfEvents)<<"%)"<<endl;
00179                 if (!pythia.next()) continue;
00180 
00181                 // Convert event record to HepMC
00182                 HepMC::GenEvent * HepMCEvt = new HepMC::GenEvent();
00183                 ToHepMC.fill_next_event(event, HepMCEvt);
00184                 //HepMCEvt->print();
00185 
00186                 if(iEvent<EventsToCheck)
00187                 {
00188                         cout<<"                                          "<<endl;
00189                         cout<<"Momentum conservation chceck BEFORE/AFTER Photos"<<endl;
00190                         checkMomentumConservationInEvent(HepMCEvt);
00191                 }
00192 
00193                 //Log::LogPhlupa(1,3);
00194 
00195                 // Run PHOTOS on the event
00196                 PhotosHepMCEvent evt(HepMCEvt);
00197                 evt.process();
00198 
00199     // Uncomment to turn on switching of the status code of history entries
00200     // with the regular entries for stable particles
00201     //switch_history_entries_status(HepMCEvt);
00202 
00203                 if(iEvent<EventsToCheck)
00204                 {
00205                         checkMomentumConservationInEvent(HepMCEvt);
00206                 }
00207 
00208                 //HepMCEvt->print();
00209 
00210                 // Run MC-TESTER on the event
00211                 HepMCEvent temp_event(*HepMCEvt,false);
00212                 MC_Analyze(&temp_event);
00213 
00214                 // Print out last 5 events
00215                 if(iEvent>=NumberOfEvents-5) HepMCEvt->print();
00216 
00217                 // Clean up
00218                 delete HepMCEvt;
00219         }
00220         pythia.statistics();
00221         MC_Finalize();
00222 }
Generated on Sun Oct 20 20:23:56 2013 for C++InterfacetoPHOTOS by  doxygen 1.6.3