ROOT logo
// This macro reads a starlight output file (default name slight.out) and creates a root file 
// with  TLorentzVectors for the parent and a TClonesArray of TLorentzVectors for the daughter 
// particles.  The output is stored in a root file (default name starlight.root) with one branch 
// labeled "parent" and the other labeled "daughters". Any number of daughter tracks can be 
// accomodated.  Daughter species currently accomodated are:  electrons, muons, charged or neutral 
// pions, charged or neutral kaons, and protons.  
//
// To use this macro, open root and then 
// type .x convertStarlightAsciiToTree.C("inputfilename", "outputfilename")
// 
// The root file produced can be examined in a root TBrowser.
//
// A macro to read this root file and make some standard plots is also provided.  This macro is 
// called AnalyzeTree.cxx; it can be compiled and run with the anaTree.C macro by opening root 
// and typing .x anaTree.C()

#include <iostream>
#include <fstream>
#include <sstream>

#include "TLorentzVector.h"
#include "TClonesArray.h"
#include "TTree.h"
#include "TFile.h"


using namespace std;


void convertStarlightAsciiToTree(const char* inFileName  = "slight.out",
                        const char* outFileName = "starlight.root")
{

	// create output tree
	TFile* outFile = new TFile(outFileName, "RECREATE");
	if (!outFile) {
		cerr << "    error: could not create output file '" << outFileName << "'" << endl;
		return;
	}
	TTree*          outTree           = new TTree("starlightTree", "starlightTree");
	TLorentzVector* parentParticle    = new TLorentzVector();
  	TClonesArray*   daughterParticles = new TClonesArray("TLorentzVector");
	outTree->Branch("parent",    "TLorentzVector", &parentParticle,    32000, -1);
	outTree->Branch("daughters", "TClonesArray",   &daughterParticles, 32000, -1);

	ifstream inFile;
	inFile.open(inFileName);
	unsigned int countLines = 0;
	while (inFile.good()) {
		string       line;
		stringstream lineStream;
		
		// read EVENT
		string label;
		int    eventNmb, nmbTracks;
		if (!getline(inFile, line))
			break;
		++countLines;
		lineStream.str(line);
		assert(lineStream >> label >> eventNmb >> nmbTracks);
		if (!(label == "EVENT:"))
			continue;
		
		// read vertex
		if (!getline(inFile, line))
			break;
		++countLines;
		lineStream.str(line);
		assert(lineStream >> label);
		assert(label == "VERTEX:");
			
		*parentParticle = TLorentzVector(0, 0, 0, 0);
		for (int i = 0; i < nmbTracks; ++i) {
			// read tracks
			int    particleCode;
			double momentum[3];
			if (!getline(inFile, line))
				break;
			++countLines;
			lineStream.str(line);
			assert(lineStream >> label >> particleCode >> momentum[0] >> momentum[1] >> momentum[2]);
			assert(label == "TRACK:");
			Double_t daughterMass = IDtoMass(particleCode);
			if (daughterMass < 0) {break;}
			const double E = sqrt(  momentum[0] * momentum[0] + momentum[1] * momentum[1]
			                      + momentum[2] * momentum[2] + daughterMass * daughterMass);
			new ( (*daughterParticles)[i] ) TLorentzVector(momentum[0], momentum[1], momentum[2], E);
			*parentParticle += *(static_cast<TLorentzVector*>(daughterParticles->At(i)));
		}
		daughterParticles->Compress();
		outTree->Fill();
	}

	outTree->Write();
	if (outFile) {
		outFile->Close();
		delete outFile;
	}
}

	double IDtoMass(int particleCode){
		double mass;
		if (particleCode == 2 || particleCode==3) {mass = 0.0051099907;} // electron
		else if (particleCode == 5 || particleCode==6) {mass = 0.105658389;} // muon
		else if (particleCode == 8 || particleCode==9)  {mass = 0.13956995;} // charged pion
		else if (particleCode == 7) {mass = 0.1345766;} // neutral pion
		else if (particleCode == 11|| particleCode==12) {mass = 0.493677;} // charged kaon
		else if (particleCode == 10 || particleCode == 16)  {mass = 0.497614;} // neutral kaon
		else if (particleCode == 14)	{mass = 0.93827231;} // proton
		else {
			cout << "unknown daughter particle (ID = " << particleCode << "), please modify code to accomodate" << endl;
			mass = -1.0;
//			exit(0); 
		     } 

		return mass;
	}
 convertStarlightAsciiToTree.C:1
 convertStarlightAsciiToTree.C:2
 convertStarlightAsciiToTree.C:3
 convertStarlightAsciiToTree.C:4
 convertStarlightAsciiToTree.C:5
 convertStarlightAsciiToTree.C:6
 convertStarlightAsciiToTree.C:7
 convertStarlightAsciiToTree.C:8
 convertStarlightAsciiToTree.C:9
 convertStarlightAsciiToTree.C:10
 convertStarlightAsciiToTree.C:11
 convertStarlightAsciiToTree.C:12
 convertStarlightAsciiToTree.C:13
 convertStarlightAsciiToTree.C:14
 convertStarlightAsciiToTree.C:15
 convertStarlightAsciiToTree.C:16
 convertStarlightAsciiToTree.C:17
 convertStarlightAsciiToTree.C:18
 convertStarlightAsciiToTree.C:19
 convertStarlightAsciiToTree.C:20
 convertStarlightAsciiToTree.C:21
 convertStarlightAsciiToTree.C:22
 convertStarlightAsciiToTree.C:23
 convertStarlightAsciiToTree.C:24
 convertStarlightAsciiToTree.C:25
 convertStarlightAsciiToTree.C:26
 convertStarlightAsciiToTree.C:27
 convertStarlightAsciiToTree.C:28
 convertStarlightAsciiToTree.C:29
 convertStarlightAsciiToTree.C:30
 convertStarlightAsciiToTree.C:31
 convertStarlightAsciiToTree.C:32
 convertStarlightAsciiToTree.C:33
 convertStarlightAsciiToTree.C:34
 convertStarlightAsciiToTree.C:35
 convertStarlightAsciiToTree.C:36
 convertStarlightAsciiToTree.C:37
 convertStarlightAsciiToTree.C:38
 convertStarlightAsciiToTree.C:39
 convertStarlightAsciiToTree.C:40
 convertStarlightAsciiToTree.C:41
 convertStarlightAsciiToTree.C:42
 convertStarlightAsciiToTree.C:43
 convertStarlightAsciiToTree.C:44
 convertStarlightAsciiToTree.C:45
 convertStarlightAsciiToTree.C:46
 convertStarlightAsciiToTree.C:47
 convertStarlightAsciiToTree.C:48
 convertStarlightAsciiToTree.C:49
 convertStarlightAsciiToTree.C:50
 convertStarlightAsciiToTree.C:51
 convertStarlightAsciiToTree.C:52
 convertStarlightAsciiToTree.C:53
 convertStarlightAsciiToTree.C:54
 convertStarlightAsciiToTree.C:55
 convertStarlightAsciiToTree.C:56
 convertStarlightAsciiToTree.C:57
 convertStarlightAsciiToTree.C:58
 convertStarlightAsciiToTree.C:59
 convertStarlightAsciiToTree.C:60
 convertStarlightAsciiToTree.C:61
 convertStarlightAsciiToTree.C:62
 convertStarlightAsciiToTree.C:63
 convertStarlightAsciiToTree.C:64
 convertStarlightAsciiToTree.C:65
 convertStarlightAsciiToTree.C:66
 convertStarlightAsciiToTree.C:67
 convertStarlightAsciiToTree.C:68
 convertStarlightAsciiToTree.C:69
 convertStarlightAsciiToTree.C:70
 convertStarlightAsciiToTree.C:71
 convertStarlightAsciiToTree.C:72
 convertStarlightAsciiToTree.C:73
 convertStarlightAsciiToTree.C:74
 convertStarlightAsciiToTree.C:75
 convertStarlightAsciiToTree.C:76
 convertStarlightAsciiToTree.C:77
 convertStarlightAsciiToTree.C:78
 convertStarlightAsciiToTree.C:79
 convertStarlightAsciiToTree.C:80
 convertStarlightAsciiToTree.C:81
 convertStarlightAsciiToTree.C:82
 convertStarlightAsciiToTree.C:83
 convertStarlightAsciiToTree.C:84
 convertStarlightAsciiToTree.C:85
 convertStarlightAsciiToTree.C:86
 convertStarlightAsciiToTree.C:87
 convertStarlightAsciiToTree.C:88
 convertStarlightAsciiToTree.C:89
 convertStarlightAsciiToTree.C:90
 convertStarlightAsciiToTree.C:91
 convertStarlightAsciiToTree.C:92
 convertStarlightAsciiToTree.C:93
 convertStarlightAsciiToTree.C:94
 convertStarlightAsciiToTree.C:95
 convertStarlightAsciiToTree.C:96
 convertStarlightAsciiToTree.C:97
 convertStarlightAsciiToTree.C:98
 convertStarlightAsciiToTree.C:99
 convertStarlightAsciiToTree.C:100
 convertStarlightAsciiToTree.C:101
 convertStarlightAsciiToTree.C:102
 convertStarlightAsciiToTree.C:103
 convertStarlightAsciiToTree.C:104
 convertStarlightAsciiToTree.C:105
 convertStarlightAsciiToTree.C:106
 convertStarlightAsciiToTree.C:107
 convertStarlightAsciiToTree.C:108
 convertStarlightAsciiToTree.C:109
 convertStarlightAsciiToTree.C:110
 convertStarlightAsciiToTree.C:111
 convertStarlightAsciiToTree.C:112
 convertStarlightAsciiToTree.C:113
 convertStarlightAsciiToTree.C:114
 convertStarlightAsciiToTree.C:115
 convertStarlightAsciiToTree.C:116
 convertStarlightAsciiToTree.C:117
 convertStarlightAsciiToTree.C:118