#include "PHUrReader.h"
#include "PReaction.h"
#include "PPlutoBulkDecay.h"
#include "PChannel.h"
#include "PParticle.h"
#include "PData.h"


#include "TString.h"
#include "TClonesArray.h"


#include "hphysicsconstants.h"

#include <iostream>

#include <iostream>
using namespace std;

Int_t UserAnalysis(PParticle** pA,Int_t size){
    

    TClonesArray* addon = PHUrReader::getAddon();
    TClonesArray* event = PHUrReader::getEvent();
    if(!addon) return 1;

    Int_t sizeAdd = addon->GetEntries();
    cout<<"UserAnalysis : nparticle ="<<size<<" ninput ="<<sizeAdd<<endl;
    for(Int_t i = 0; i < size; i ++){
	PParticle* p = pA[i];
	if(0&&i<sizeAdd) {
	    PParticle* p1 = (PParticle*)event->At(i);
	    cout<<i<<" "<<p->ID()<<" "<<p1->ID()<<endl;
	}
	if(i >= sizeAdd) {
	    PParticle* p1=0;
	    Int_t parentIndex = p->GetParentIndex() ;
	    cout<<"daughter "<<i<<" id "<<HPhysicsConstants::pid(p->ID())<<flush;
	    while(parentIndex!=-1){
		p1=0;
		if(parentIndex < sizeAdd ){
		    p1 = (PParticle*) event->At(parentIndex);
		} else {
		    p1 = pA[parentIndex];
		}
		if(p1) {
		    cout<<" -> "<<parentIndex<<" id "<<HPhysicsConstants::pid(p1->ID())<<flush;
		    parentIndex = p1->GetParentIndex();
		} else cout<<"p1=0, should not happen"<<endl;
	    }
            cout<<endl;
	}
    }
    return 1;
}

void urqmd_f15_input(TString infile = "/hera/hades/dstsim/apr12/urqmd/bmax9/f15/Au_Au_1230MeV_1000evts_1.f15",
		     TString outfile ="out-dilep_pluto",
                     Int_t nEvents=10
		    )
{
    // Read in urqmd f15 file (collision history) and put the particles
    // on the PLUTO bulk stack. Either dileptons (D0,D+,w,rho0,phi,eta,eta' and pi0)
    // can be decayed via shining (translated for original UrQMD fortran routines,
    // authors: Katharina Schmidt, Sascha Vogel, Christian Sturm 2007-2009) into
    // e+/e- or by PLUTO bulk decay. Optional stable non dileptons can be
    // written out. PHUrReader adds a TClonesArray "Addon" to the PLUTO Tree
    // that contains original UrQMD infos about creation and absorbtion times , densities etc.
    //

    Int_t maxnum                      = 19999;
    Int_t writeNonLeptons             = 2;   // default : 0=no nonleptons, 1=stable non leptons, 2=all nonlepton
    Bool_t runUrmdShining             = kFALSE;   // default : kTRUE  = run urqmd dilep shining code
    Bool_t writeFreezeOutLeptonsOnly  = kFALSE;   // default : kFALSE = store intermediate dileptons to output
    Bool_t doPlutoDecay               = kFALSE;   // default : kFALSE = do not decay dileptons with PLUTO
    TString outputUrmdDilepton        = "test_dilep.dat";

    if(doPlutoDecay && runUrmdShining){
	cout<<"WRONG CONFIG: RUNNING UrQMD SHINING and PLUTO DECAY AT THE SAME TIME MAKES NO SENSE! PLUTO SELECTED! "<<endl;
        runUrmdShining = kFALSE;
    }

    makeStaticData()->AddDecay(-1,"eta' -> dilepton, gamma","eta'","dilepton,gamma",0.0009); // missing channel: BR from PDG upper limit
    *(makeStaticData()->GetBatchValue("_system_particle_stacksize")) = maxnum;   // we have a lot of particles from urqmd

    //------------------------------------------------------
    // setup Urqmd Reader
    PHUrReader *input = new PHUrReader();
    input->Input                      (infile);
    input->Output                     (outputUrmdDilepton);
    input->SetOutputNonDileptons      (writeNonLeptons);
    input->SetOutputLeptons           (runUrmdShining );
    input->SetOutputFreezeoutDileptons(writeFreezeOutLeptonsOnly);
    input->SetMaxNumParticles(maxnum);
    //------------------------------------------------------
    // setup PLUTO

    //######################################################
    // Database work for selected channels of bulk decay (needed for PLUTO decay only)
    //------------------------------------------------------
    // switch all channels off
    makeStaticData()->DisableAllChannelBR("D0");
    makeStaticData()->DisableAllChannelBR("D+");
    makeStaticData()->DisableAllChannelBR("w");
    makeStaticData()->DisableAllChannelBR("rho0");
    makeStaticData()->DisableAllChannelBR("phi");
    makeStaticData()->DisableAllChannelBR("eta");
    makeStaticData()->DisableAllChannelBR("eta'");
    makeStaticData()->DisableAllChannelBR("pi0");


    //------------------------------------------------------
    // switch all wanted channels on
    makeStaticData()->SetEnhanceChannelBR("D0"  , "dilepton, n"    ,1.);
    makeStaticData()->SetEnhanceChannelBR("D+"  , "dilepton, p"    ,1.);
    makeStaticData()->SetEnhanceChannelBR("w"   , "dilepton, pi0"  ,0.000595/(0.000595+0.000071));   // both dal +dir  : Branching ratio=0.000595
    makeStaticData()->SetEnhanceChannelBR("eta" , "dilepton, gamma",1.);
    makeStaticData()->SetEnhanceChannelBR("eta'", "dilepton, gamma",1.); // missing channel!!
    makeStaticData()->SetEnhanceChannelBR("pi0" , "dilepton, gamma",1.);

    makeStaticData()->SetEnhanceChannelBR("phi" , "e+, e-"         ,1.);
    makeStaticData()->SetEnhanceChannelBR("w"   , "e+, e-"         ,0.000071/(0.000595+0.000071));   // both dal +dir   : Branching ratio=0.000071
    makeStaticData()->SetEnhanceChannelBR("rho0", "e+, e-"         ,1.);
    //######################################################

    PPlutoBulkDecay *pl = new PPlutoBulkDecay(); // needed to decay Dileptons with PLUTO
    pl->SetRecursiveMode(1);                     // Let also the products decay
    pl->SetTauMax(0.001);                        // maxTau in ns


    PReaction my_reaction((Char_t*)outfile.Data());
    //my_reaction.SetUserAnalysis(UserAnalysis);
    my_reaction.AddBulk(input);
    if(doPlutoDecay){
	my_reaction.AddBulk(pl);
    }

    my_reaction.Print();

    cout << my_reaction.Loop(nEvents) << " events converted" << endl;


}
 plugin_urqmd_f15_input.C:1
 plugin_urqmd_f15_input.C:2
 plugin_urqmd_f15_input.C:3
 plugin_urqmd_f15_input.C:4
 plugin_urqmd_f15_input.C:5
 plugin_urqmd_f15_input.C:6
 plugin_urqmd_f15_input.C:7
 plugin_urqmd_f15_input.C:8
 plugin_urqmd_f15_input.C:9
 plugin_urqmd_f15_input.C:10
 plugin_urqmd_f15_input.C:11
 plugin_urqmd_f15_input.C:12
 plugin_urqmd_f15_input.C:13
 plugin_urqmd_f15_input.C:14
 plugin_urqmd_f15_input.C:15
 plugin_urqmd_f15_input.C:16
 plugin_urqmd_f15_input.C:17
 plugin_urqmd_f15_input.C:18
 plugin_urqmd_f15_input.C:19
 plugin_urqmd_f15_input.C:20
 plugin_urqmd_f15_input.C:21
 plugin_urqmd_f15_input.C:22
 plugin_urqmd_f15_input.C:23
 plugin_urqmd_f15_input.C:24
 plugin_urqmd_f15_input.C:25
 plugin_urqmd_f15_input.C:26
 plugin_urqmd_f15_input.C:27
 plugin_urqmd_f15_input.C:28
 plugin_urqmd_f15_input.C:29
 plugin_urqmd_f15_input.C:30
 plugin_urqmd_f15_input.C:31
 plugin_urqmd_f15_input.C:32
 plugin_urqmd_f15_input.C:33
 plugin_urqmd_f15_input.C:34
 plugin_urqmd_f15_input.C:35
 plugin_urqmd_f15_input.C:36
 plugin_urqmd_f15_input.C:37
 plugin_urqmd_f15_input.C:38
 plugin_urqmd_f15_input.C:39
 plugin_urqmd_f15_input.C:40
 plugin_urqmd_f15_input.C:41
 plugin_urqmd_f15_input.C:42
 plugin_urqmd_f15_input.C:43
 plugin_urqmd_f15_input.C:44
 plugin_urqmd_f15_input.C:45
 plugin_urqmd_f15_input.C:46
 plugin_urqmd_f15_input.C:47
 plugin_urqmd_f15_input.C:48
 plugin_urqmd_f15_input.C:49
 plugin_urqmd_f15_input.C:50
 plugin_urqmd_f15_input.C:51
 plugin_urqmd_f15_input.C:52
 plugin_urqmd_f15_input.C:53
 plugin_urqmd_f15_input.C:54
 plugin_urqmd_f15_input.C:55
 plugin_urqmd_f15_input.C:56
 plugin_urqmd_f15_input.C:57
 plugin_urqmd_f15_input.C:58
 plugin_urqmd_f15_input.C:59
 plugin_urqmd_f15_input.C:60
 plugin_urqmd_f15_input.C:61
 plugin_urqmd_f15_input.C:62
 plugin_urqmd_f15_input.C:63
 plugin_urqmd_f15_input.C:64
 plugin_urqmd_f15_input.C:65
 plugin_urqmd_f15_input.C:66
 plugin_urqmd_f15_input.C:67
 plugin_urqmd_f15_input.C:68
 plugin_urqmd_f15_input.C:69
 plugin_urqmd_f15_input.C:70
 plugin_urqmd_f15_input.C:71
 plugin_urqmd_f15_input.C:72
 plugin_urqmd_f15_input.C:73
 plugin_urqmd_f15_input.C:74
 plugin_urqmd_f15_input.C:75
 plugin_urqmd_f15_input.C:76
 plugin_urqmd_f15_input.C:77
 plugin_urqmd_f15_input.C:78
 plugin_urqmd_f15_input.C:79
 plugin_urqmd_f15_input.C:80
 plugin_urqmd_f15_input.C:81
 plugin_urqmd_f15_input.C:82
 plugin_urqmd_f15_input.C:83
 plugin_urqmd_f15_input.C:84
 plugin_urqmd_f15_input.C:85
 plugin_urqmd_f15_input.C:86
 plugin_urqmd_f15_input.C:87
 plugin_urqmd_f15_input.C:88
 plugin_urqmd_f15_input.C:89
 plugin_urqmd_f15_input.C:90
 plugin_urqmd_f15_input.C:91
 plugin_urqmd_f15_input.C:92
 plugin_urqmd_f15_input.C:93
 plugin_urqmd_f15_input.C:94
 plugin_urqmd_f15_input.C:95
 plugin_urqmd_f15_input.C:96
 plugin_urqmd_f15_input.C:97
 plugin_urqmd_f15_input.C:98
 plugin_urqmd_f15_input.C:99
 plugin_urqmd_f15_input.C:100
 plugin_urqmd_f15_input.C:101
 plugin_urqmd_f15_input.C:102
 plugin_urqmd_f15_input.C:103
 plugin_urqmd_f15_input.C:104
 plugin_urqmd_f15_input.C:105
 plugin_urqmd_f15_input.C:106
 plugin_urqmd_f15_input.C:107
 plugin_urqmd_f15_input.C:108
 plugin_urqmd_f15_input.C:109
 plugin_urqmd_f15_input.C:110
 plugin_urqmd_f15_input.C:111
 plugin_urqmd_f15_input.C:112
 plugin_urqmd_f15_input.C:113
 plugin_urqmd_f15_input.C:114
 plugin_urqmd_f15_input.C:115
 plugin_urqmd_f15_input.C:116
 plugin_urqmd_f15_input.C:117
 plugin_urqmd_f15_input.C:118
 plugin_urqmd_f15_input.C:119
 plugin_urqmd_f15_input.C:120
 plugin_urqmd_f15_input.C:121
 plugin_urqmd_f15_input.C:122
 plugin_urqmd_f15_input.C:123
 plugin_urqmd_f15_input.C:124
 plugin_urqmd_f15_input.C:125
 plugin_urqmd_f15_input.C:126
 plugin_urqmd_f15_input.C:127
 plugin_urqmd_f15_input.C:128
 plugin_urqmd_f15_input.C:129
 plugin_urqmd_f15_input.C:130
 plugin_urqmd_f15_input.C:131
 plugin_urqmd_f15_input.C:132
 plugin_urqmd_f15_input.C:133
 plugin_urqmd_f15_input.C:134
 plugin_urqmd_f15_input.C:135
 plugin_urqmd_f15_input.C:136
 plugin_urqmd_f15_input.C:137
 plugin_urqmd_f15_input.C:138
 plugin_urqmd_f15_input.C:139
 plugin_urqmd_f15_input.C:140
 plugin_urqmd_f15_input.C:141
 plugin_urqmd_f15_input.C:142
 plugin_urqmd_f15_input.C:143
 plugin_urqmd_f15_input.C:144