Exemplo n.º 1
0
int Dense3d_MPI::setup()
{
  //begin
  pA(_srcPos!=NULL && _srcNor!=NULL && _trgPos!=NULL);
  //--------------------------------------------------------------------------
  /* Create a Scatter context - copies all source position values to each processor
	* See http://www-unix.mcs.anl.gov/petsc/petsc-as/snapshots/petsc-current/docs/manualpages/Vec/VecScatterCreateToAll.html */
  {
	 VecScatter ctx;
	 pC( VecScatterCreateToAll(_srcPos, &ctx, &_srcAllPos) );
	 // VecScatterBegin(VecScatter inctx,Vec x,Vec y,InsertMode addv,ScatterMode mode)
	 pC( VecScatterBegin(ctx, _srcPos, _srcAllPos, INSERT_VALUES, SCATTER_FORWARD) );
	 pC( VecScatterEnd(ctx,  _srcPos, _srcAllPos, INSERT_VALUES, SCATTER_FORWARD ) );
	 pC( VecScatterDestroy(ctx) );
  }
  /* Create a Scatter context - copies all source normal values to each processor */
  {
	 VecScatter ctx;
	 pC( VecScatterCreateToAll(_srcNor, &ctx, &_srcAllNor) );
	 pC( VecScatterBegin(ctx, _srcNor, _srcAllNor, INSERT_VALUES, SCATTER_FORWARD) );
	 pC( VecScatterEnd(ctx,  _srcNor, _srcAllNor, INSERT_VALUES, SCATTER_FORWARD) );
	 pC( VecScatterDestroy(ctx) );
  }
  return(0);
}
Exemplo n.º 2
0
tCmd * pP()
{
	tCmd * cmd, *pcmd;
	int iteration = 0;

	cmd = pC();

	if (cmd == NULL)
		return NULL;

	while (cur_l->type == LEX_PIPE)
	{
		tCmd * tmp;
		int type = TREL_PIPE;
		if (glhard())
		{
			delTCmd(&cmd);
			return NULL;
		}

		tmp = pC();

		if (tmp == NULL)
		{
			delTCmd(&cmd);
			return NULL;
		}

		if (!iteration)
		{
			pcmd = tmp;
			tmp = genTCmd(NULL);
			tmp->cmdType = TCMD_PIPE;
			genRelation(cmd, type, pcmd);
			tmp->child = cmd;
			cmd = tmp;
			iteration = 1;
		}
		else
		{
			genRelation(pcmd, type, tmp);
			pcmd = tmp;
		}
	}

	if (iteration)
		genRelation(pcmd, TREL_END, cmd);

	return cmd;
}
	bool EXPORT_API ProjectPointTriangleConstraintsJB(btVector3* p, btVector3* p0, btVector3* p1, btVector3* p2, float radius, float K1, float K2, btVector3* c, btVector3* c0, btVector3* c1, btVector3* c2, btVector3 *debugOut)
	{

		//btScalar massInv = 1.0f / mass;


		Eigen::Vector3f pp(p->x(), p->y(), p->z());
		Eigen::Vector3f pA(p0->x(), p0->y(), p0->z());
		Eigen::Vector3f pB(p1->x(), p1->y(), p1->z());
		Eigen::Vector3f pC(p2->x(), p2->y(), p2->z());

		Eigen::Vector3f corrA;
		Eigen::Vector3f corrB;
		Eigen::Vector3f corrC;
		Eigen::Vector3f corr;
		Eigen::Vector3f debug;

		const bool res = PBD::PositionBasedDynamics::solveTrianglePointDistConstraint(pp, 1, pA, 1, pB, 1, pC, 1, radius, K1, K2, corr, corrA, corrB, corrC);


			if (res)
			{
				c->setValue(corr.x(), corr.y(), corr.z());
				c0->setValue(corrA.x(), corrA.y(), corrA.z());
				c1->setValue(corrB.x(), corrB.y(), corrB.z());
				c2->setValue(corrC.x(), corrC.y(), corrC.z());
				debugOut->setValue(debug.x(), debug.y(), debug.z());
		
			}

		
			return res;

	}
Exemplo n.º 4
0
Task * pT()
{
	Task * task = NULL;

	if (cur_l->type == LEX_EOL)
	{
		waitLex();
		return NULL;
	}
	else if (cur_l->type == LEX_EOF)
		return NULL;

	task = pC();

	if (cur_l->type != LEX_EOL &&
		cur_l->type != LEX_EOF)
	{
		setParserError(PE_UNEXPECTED_END_OF_COMMAND);
		delTask(&task);
		return NULL;
	}

	waitLex();

	return task;
}
Exemplo n.º 5
0
void pC()
{
	switch (ch) {
	case 'c':
		pc(); pC();
		break;
	case 'd':
		pd();
		break;
	default:
		error();
	}
}
Exemplo n.º 6
0
void pB()
{
	switch (ch) {
	case 'b':
		pb(); pB();
		break;
	case 'c':
		pc(); pC();
		break;
	default :
		error();
	}
}
void GenePool::pushBackIndiv(Individual& indiv)
{
    Individual * p2Indiv = &indiv;

    if(LangermannPoint * pIndivL = dynamic_cast<LangermannPoint*>(p2Indiv))
    {
        IndivPtr pL(new LangermannPoint(*pIndivL));
        individualList.push_back(pL);
    }
    else if(CurveParams * pIndivC = dynamic_cast<CurveParams*>(p2Indiv))
    {
        IndivPtr pC(new CurveParams(*pIndivC));
        individualList.push_back(pC);
    }
}
bool test(bool is_kernel_exact = true)
{
	// types
  typedef typename K::FT FT;
  typedef typename K::Line_3 Line;
  typedef typename K::Point_3 Point;
  typedef typename K::Segment_3 Segment;
  typedef typename K::Ray_3 Ray;
  typedef typename K::Line_3 Line;
  typedef typename K::Triangle_3 Triangle;

  /* -------------------------------------
  // Test data is something like that (in t supporting plane)
  // Triangle is (p1,p2,p3)
  //
  //       +E          +1
  //                 /   \
  //        +C     6+  +8  +4      +B
  //              /   9++7  \
  //            3+-------+5--+2
  //     
  //         +F        +A      
  ------------------------------------- */
  
  Point p1(FT(1.), FT(0.), FT(0.));
  Point p2(FT(0.), FT(1.), FT(0.));
  Point p3(FT(0.), FT(0.), FT(1.));
  
  Triangle t(p1,p2,p3);
  
  // Edges of t 
  Segment s12(p1,p2);
  Segment s21(p2,p1);
  Segment s13(p1,p3);
  Segment s23(p2,p3);
  Segment s32(p3,p2);
  Segment s31(p3,p1);
  
  bool b = test_aux(is_kernel_exact,t,s12,"t-s12",s12);
  b &= test_aux(is_kernel_exact,t,s21,"t-s21",s21);
  b &= test_aux(is_kernel_exact,t,s13,"t-s13",s13);
  b &= test_aux(is_kernel_exact,t,s23,"t-s23",s23);

  // Inside points
  Point p4(FT(0.5), FT(0.5), FT(0.));
  Point p5(FT(0.), FT(0.75), FT(0.25));
  Point p6(FT(0.5), FT(0.), FT(0.5));
  Point p7(FT(0.25), FT(0.625), FT(0.125));
  Point p8(FT(0.5), FT(0.25), FT(0.25));
  
  Segment s14(p1,p4);
  Segment s41(p4,p1);
  Segment s24(p2,p4);
  Segment s42(p4,p2);
  Segment s15(p1,p5);
  Segment s25(p2,p5);
  Segment s34(p3,p4);
  Segment s35(p3,p5);
  Segment s36(p3,p6);
  Segment s45(p4,p5);
  Segment s16(p1,p6);
  Segment s26(p2,p6);
  Segment s62(p6,p2);
  Segment s46(p4,p6);
  Segment s48(p4,p8);
  Segment s56(p5,p6);
  Segment s65(p6,p5);
  Segment s64(p6,p4);
  Segment s17(p1,p7);
  Segment s67(p6,p7);
  Segment s68(p6,p8);
  Segment s86(p8,p6);
  Segment s78(p7,p8);
  Segment s87(p8,p7);
  
  b &= test_aux(is_kernel_exact,t,s14,"t-s14",s14);
  b &= test_aux(is_kernel_exact,t,s41,"t-s41",s41);
  b &= test_aux(is_kernel_exact,t,s24,"t-s24",s24);
  b &= test_aux(is_kernel_exact,t,s42,"t-s42",s42);
  b &= test_aux(is_kernel_exact,t,s15,"t-s15",s15);
  b &= test_aux(is_kernel_exact,t,s25,"t-s25",s25);
  b &= test_aux(is_kernel_exact,t,s34,"t-s34",s34);
  b &= test_aux(is_kernel_exact,t,s35,"t-s35",s35);
  b &= test_aux(is_kernel_exact,t,s36,"t-s36",s36);
  b &= test_aux(is_kernel_exact,t,s45,"t-s45",s45);
  b &= test_aux(is_kernel_exact,t,s16,"t-s16",s16);
  b &= test_aux(is_kernel_exact,t,s26,"t-s26",s26);
  b &= test_aux(is_kernel_exact,t,s62,"t-s62",s62);
  b &= test_aux(is_kernel_exact,t,s46,"t-s46",s46);
  b &= test_aux(is_kernel_exact,t,s65,"t-s65",s65);
  b &= test_aux(is_kernel_exact,t,s64,"t-s64",s64);
  b &= test_aux(is_kernel_exact,t,s48,"t-s48",s48);
  b &= test_aux(is_kernel_exact,t,s56,"t-s56",s56);
  b &= test_aux(is_kernel_exact,t,s17,"t-t17",s17);
  b &= test_aux(is_kernel_exact,t,s67,"t-t67",s67);
  b &= test_aux(is_kernel_exact,t,s68,"t-s68",s68);
  b &= test_aux(is_kernel_exact,t,s86,"t-s86",s86);
  b &= test_aux(is_kernel_exact,t,s78,"t-t78",s78);
  b &= test_aux(is_kernel_exact,t,s87,"t-t87",s87);
  
  // Outside points (in triangle plane)
  Point pA(FT(-0.5), FT(1.), FT(0.5));
  Point pB(FT(0.5), FT(1.), FT(-0.5));
  Point pC(FT(0.5), FT(-0.5), FT(1.));
  Point pE(FT(1.), FT(-1.), FT(1.));
  Point pF(FT(-1.), FT(0.), FT(2.));
  
  Segment sAB(pA,pB);
  Segment sBC(pB,pC);
  Segment s2E(p2,pE);
  Segment sE2(pE,p2);
  Segment s2A(p2,pA);
  Segment s6E(p6,pE);
  Segment sB8(pB,p8);
  Segment sC8(pC,p8);
  Segment s8C(p8,pC);
  Segment s1F(p1,pF);
  Segment sF6(pF,p6);
  
  b &= test_aux(is_kernel_exact,t,sAB,"t-sAB",p2);
  b &= test_aux(is_kernel_exact,t,sBC,"t-sBC",s46);
  b &= test_aux(is_kernel_exact,t,s2E,"t-s2E",s26);
  b &= test_aux(is_kernel_exact,t,sE2,"t-sE2",s62);
  b &= test_aux(is_kernel_exact,t,s2A,"t-s2A",p2);
  b &= test_aux(is_kernel_exact,t,s6E,"t-s6E",p6);
  b &= test_aux(is_kernel_exact,t,sB8,"t-sB8",s48);
  b &= test_aux(is_kernel_exact,t,sC8,"t-sC8",s68);
  b &= test_aux(is_kernel_exact,t,s8C,"t-s8C",s86);
  b &= test_aux(is_kernel_exact,t,s1F,"t-s1F",s13);
  b &= test_aux(is_kernel_exact,t,sF6,"t-sF6",s36);
  
  // Outside triangle plane
  Point pa(FT(0.), FT(0.), FT(0.));
  Point pb(FT(2.), FT(0.), FT(0.));
  Point pc(FT(1.), FT(0.), FT(1.));
  Point pe(FT(1.), FT(0.5), FT(0.5));
  
  Segment sab(pa,pb);
  Segment sac(pa,pc);
  Segment sae(pa,pe);
  Segment sa8(pa,p8);
  Segment sb2(pb,p2);
  
  b &= test_aux(is_kernel_exact,t,sab,"t-sab",p1);
  b &= test_aux(is_kernel_exact,t,sac,"t-sac",p6);
  b &= test_aux(is_kernel_exact,t,sae,"t-sae",p8);
  b &= test_aux(is_kernel_exact,t,sa8,"t-sa8",p8);
  b &= test_aux(is_kernel_exact,t,sb2,"t-sb2",p2);
  
  // -----------------------------------
  // ray queries
  // -----------------------------------
  // Edges of t 
  Ray r12(p1,p2);
  Ray r21(p2,p1);
  Ray r13(p1,p3);
  Ray r23(p2,p3);
  
  b &= test_aux(is_kernel_exact,t,r12,"t-r12",s12);
  b &= test_aux(is_kernel_exact,t,r21,"t-r21",s21);
  b &= test_aux(is_kernel_exact,t,r13,"t-r13",s13);
  b &= test_aux(is_kernel_exact,t,r23,"t-r23",s23);
  
  // In triangle
  Point p9_(FT(0.), FT(0.5), FT(0.5));
  Point p9(FT(0.25), FT(0.375), FT(0.375));
  
  Ray r14(p1,p4);
  Ray r41(p4,p1);
  Ray r24(p2,p4);
  Ray r42(p4,p2);
  Ray r15(p1,p5);
  Ray r25(p2,p5);
  Ray r34(p3,p4);
  Ray r35(p3,p5);
  Ray r36(p3,p6);
  Ray r45(p4,p5);
  Ray r16(p1,p6);
  Ray r26(p2,p6);
  Ray r62(p6,p2);
  Ray r46(p4,p6);
  Ray r48(p4,p8);
  Ray r56(p5,p6);
  Ray r47(p4,p7);
  Ray r89(p8,p9);
  Ray r86(p8,p6);
  Ray r68(p6,p8);
  Segment r89_res(p8,p9_);
  
  b &= test_aux(is_kernel_exact,t,r14,"t-r14",s12);
  b &= test_aux(is_kernel_exact,t,r41,"t-r41",s41);
  b &= test_aux(is_kernel_exact,t,r24,"t-r24",s21);
  b &= test_aux(is_kernel_exact,t,r42,"t-r42",s42);
  b &= test_aux(is_kernel_exact,t,r15,"t-r15",s15);
  b &= test_aux(is_kernel_exact,t,r25,"t-r25",s23);
  b &= test_aux(is_kernel_exact,t,r34,"t-r34",s34);
  b &= test_aux(is_kernel_exact,t,r35,"t-r35",s32);
  b &= test_aux(is_kernel_exact,t,r36,"t-r36",s31);
  b &= test_aux(is_kernel_exact,t,r45,"t-r45",s45);
  b &= test_aux(is_kernel_exact,t,r16,"t-r16",s13);
  b &= test_aux(is_kernel_exact,t,r26,"t-r26",s26);
  b &= test_aux(is_kernel_exact,t,r62,"t-r62",s62);
  b &= test_aux(is_kernel_exact,t,r46,"t-r46",s46);
  b &= test_aux(is_kernel_exact,t,r48,"t-r48",s46);
  b &= test_aux(is_kernel_exact,t,r56,"t-r56",s56);
  b &= test_aux(is_kernel_exact,t,r47,"t-r47",s45);
  b &= test_aux(is_kernel_exact,t,r89,"t-t89",r89_res);
  b &= test_aux(is_kernel_exact,t,r68,"t-r68",s64);
  b &= test_aux(is_kernel_exact,t,r86,"t-r86",s86);
  
  
  // Outside points (in triangre prane)
  Ray rAB(pA,pB);
  Ray rBC(pB,pC);
  Ray r2E(p2,pE);
  Ray rE2(pE,p2);
  Ray r2A(p2,pA);
  Ray r6E(p6,pE);
  Ray rB8(pB,p8);
  Ray rC8(pC,p8);
  Ray r8C(p8,pC);
  Ray r1F(p1,pF);
  Ray rF6(pF,p6);
  
  b &= test_aux(is_kernel_exact,t,rAB,"t-rAB",p2);
  b &= test_aux(is_kernel_exact,t,rBC,"t-rBC",s46);
  b &= test_aux(is_kernel_exact,t,r2E,"t-r2E",s26);
  b &= test_aux(is_kernel_exact,t,rE2,"t-rE2",s62);
  b &= test_aux(is_kernel_exact,t,r2A,"t-r2A",p2);
  b &= test_aux(is_kernel_exact,t,r6E,"t-r6E",p6);
  b &= test_aux(is_kernel_exact,t,rB8,"t-rB8",s46);
  b &= test_aux(is_kernel_exact,t,rC8,"t-rC8",s64);
  b &= test_aux(is_kernel_exact,t,r8C,"t-r8C",s86);
  b &= test_aux(is_kernel_exact,t,r1F,"t-r1F",s13);
  b &= test_aux(is_kernel_exact,t,rF6,"t-rF6",s31);
  
  // Outside triangle plane
  Ray rab(pa,pb);
  Ray rac(pa,pc);
  Ray rae(pa,pe);
  Ray ra8(pa,p8);
  Ray rb2(pb,p2);
  
  b &= test_aux(is_kernel_exact,t,rab,"t-rab",p1);
  b &= test_aux(is_kernel_exact,t,rac,"t-rac",p6);
  b &= test_aux(is_kernel_exact,t,rae,"t-rae",p8);
  b &= test_aux(is_kernel_exact,t,ra8,"t-ra8",p8);
  b &= test_aux(is_kernel_exact,t,rb2,"t-rb2",p2);
  
  // -----------------------------------
  // Line queries
  // -----------------------------------
  // Edges of t 
  Line l12(p1,p2);
  Line l21(p2,p1);
  Line l13(p1,p3);
  Line l23(p2,p3);
  
  b &= test_aux(is_kernel_exact,t,l12,"t-l12",s12);
  b &= test_aux(is_kernel_exact,t,l21,"t-l21",s21);
  b &= test_aux(is_kernel_exact,t,l13,"t-l13",s13);
  b &= test_aux(is_kernel_exact,t,l23,"t-l23",s23);
  
  // In triangle
  Line l14(p1,p4);
  Line l41(p4,p1);
  Line l24(p2,p4);
  Line l42(p4,p2);
  Line l15(p1,p5);
  Line l25(p2,p5);
  Line l34(p3,p4);
  Line l35(p3,p5);
  Line l36(p3,p6);
  Line l45(p4,p5);
  Line l16(p1,p6);
  Line l26(p2,p6);
  Line l62(p6,p2);
  Line l46(p4,p6);
  Line l48(p4,p8);
  Line l56(p5,p6);
  Line l47(p4,p7);
  Line l89(p8,p9);
  Line l86(p8,p6);
  Line l68(p6,p8);
  Segment l89_res(p1,p9_);

  
  b &= test_aux(is_kernel_exact,t,l14,"t-l14",s12);
  b &= test_aux(is_kernel_exact,t,l41,"t-l41",s21);
  b &= test_aux(is_kernel_exact,t,l24,"t-l24",s21);
  b &= test_aux(is_kernel_exact,t,l42,"t-l42",s12);
  b &= test_aux(is_kernel_exact,t,l15,"t-l15",s15);
  b &= test_aux(is_kernel_exact,t,l25,"t-l25",s23);
  b &= test_aux(is_kernel_exact,t,l34,"t-l34",s34);
  b &= test_aux(is_kernel_exact,t,l35,"t-l35",s32);
  b &= test_aux(is_kernel_exact,t,l36,"t-l36",s31);
  b &= test_aux(is_kernel_exact,t,l45,"t-l45",s45);
  b &= test_aux(is_kernel_exact,t,l16,"t-l16",s13);
  b &= test_aux(is_kernel_exact,t,l26,"t-l26",s26);
  b &= test_aux(is_kernel_exact,t,l62,"t-l62",s62);
  b &= test_aux(is_kernel_exact,t,l46,"t-l46",s46);
  b &= test_aux(is_kernel_exact,t,l48,"t-l48",s46);
  b &= test_aux(is_kernel_exact,t,l56,"t-l56",s56);
  b &= test_aux(is_kernel_exact,t,l47,"t-l47",s45);
  b &= test_aux(is_kernel_exact,t,l89,"t-t89",l89_res);
  b &= test_aux(is_kernel_exact,t,l68,"t-l68",s64);
  b &= test_aux(is_kernel_exact,t,l86,"t-l86",s46);

  
  // Outside points (in triangle plane)
  Line lAB(pA,pB);
  Line lBC(pB,pC);
  Line l2E(p2,pE);
  Line lE2(pE,p2);
  Line l2A(p2,pA);
  Line l6E(p6,pE);
  Line lB8(pB,p8);
  Line lC8(pC,p8);
  Line l8C(p8,pC);
  Line l1F(p1,pF);
  Line lF6(pF,p6);
  
  b &= test_aux(is_kernel_exact,t,lAB,"t-lAB",p2);
  b &= test_aux(is_kernel_exact,t,lBC,"t-lBC",s46);
  b &= test_aux(is_kernel_exact,t,l2E,"t-l2E",s26);
  b &= test_aux(is_kernel_exact,t,lE2,"t-lE2",s62);
  b &= test_aux(is_kernel_exact,t,l2A,"t-l2A",p2);
  b &= test_aux(is_kernel_exact,t,l6E,"t-l6E",s26);
  b &= test_aux(is_kernel_exact,t,lB8,"t-lB8",s46);
  b &= test_aux(is_kernel_exact,t,lC8,"t-lC8",s64);
  b &= test_aux(is_kernel_exact,t,l8C,"t-l8C",s46);
  b &= test_aux(is_kernel_exact,t,l1F,"t-l1F",s13);
  b &= test_aux(is_kernel_exact,t,lF6,"t-lF6",s31);
  
  // Outside triangle plane
  Line lab(pa,pb);
  Line lac(pa,pc);
  Line lae(pa,pe);
  Line la8(pa,p8);
  Line lb2(pb,p2);
  
  b &= test_aux(is_kernel_exact,t,lab,"t-lab",p1);
  b &= test_aux(is_kernel_exact,t,lac,"t-lac",p6);
  b &= test_aux(is_kernel_exact,t,lae,"t-lae",p8);
  b &= test_aux(is_kernel_exact,t,la8,"t-la8",p8);
  b &= test_aux(is_kernel_exact,t,lb2,"t-lb2",p2);
  
  
	return b;
}
Exemplo n.º 9
0
//! main program
int main (int argc, char** argv)
{

//---- output File ---- where saving histograms ----
    bool flagOutput = false;
    std::string outputRootName = "outputHistos.root";
    std::string testName = "-o";

    if (argc>2) {
        if (argv[1] ==  testName) {
            outputRootName = argv[2];
            flagOutput = true;
            std::cerr << " Name Output file = " << outputRootName << std::endl;
        }
    }
    if (argc>4) {
        if (argv[3] ==  testName) {
            outputRootName = argv[4];
            flagOutput = true;
            std::cerr << " Name Output file = " << outputRootName << std::endl;
        }
    }


//---- input File ---- where finding input parameters ----

//---- Input Variables ----
    double R = 0.2; //---- Radius to match MC and Cluster
    double dEta_C_Cut = 0.05; //---- dEta to match MC and Cluster
    double dPhi_C_Cut = 0.1; //---- dPhi to match MC and Cluster


    double S4oS9C_Cut = 0.85; //---- S4oS9C_-Cluster cut ----
    double ptC_Cut = 1.;      //---- pt-Cluster cut ----
    double ptSum_Cut = 2.;    //---- pt-coppia cut ----

    double RMax_Cut = 0.2;            //---- RMax_Cut cut ---- isolation ----
    double DeltaEtaMax_Cut = 0.05;    //---- DeltaEtaMax_Cut ---- isolation ----
    double et_Cut = 1.;               //---- et_Cut ---- isolation ----
    double iso_Cut = 0.4;             //---- iso/pt-coppia cut ---- isolation ----

    double angle_Cut = 0.105; //---- ~6° ---- Angle between two SC ----

    double R_eta = 0.1; //---- summed cluster match with MC
    double dEta_eta = 0.05;
    double dPhi_eta = 0.1;
    double ptPh_Cut = 1.;      //---- pt-Photon cut ----

    int N_events = 10; //----numeber of events to analize -> -1 = all ----


    std::string inputName;
    bool flagInput = false;
    std::string testNameInput = "-i";
    if (argc>2) {
        if (argv[1] ==  testNameInput) {
            inputName = argv[2];
            flagInput = true;
            std::cerr << " Name input file variables = " << inputName << std::endl;
        }
    }
    if (argc>4) {
        if (argv[3] ==  testNameInput) {
            inputName = argv[4];
            flagInput = true;
            std::cerr << " Name input file variables = " << inputName << std::endl;
        }
    }
    if (flagInput) {
        char buffer[1000];
        ifstream file(inputName.c_str());

        std::string variableNameR = "R";
        std::string variableNameS4oS9C_Cut = "S4oS9C_Cut";
        std::string variableNamePtC_Cut = "ptC_Cut";
        std::string variableNamePtSum_Cut = "ptSum_Cut";
        std::string variableNameMax_Cut = "RMax_Cut";
        std::string variableNameDeltaEtaMax_Cut = "DeltaEtaMax_Cut";
        std::string variableNameEt_Cut = "et_Cut";
        std::string variableNameIso_Cut = "iso_Cut";
        std::string variableNameAngle_Cut = "angle_Cut";
        std::string variableNameR_eta = "R_eta";
        std::string variableNameN_events = "N_events";
        std::string variableNameDEta_C_Cut = "dEta_C_Cut";
        std::string variableNameDPhi_C_Cut = "dPhi_C_Cut";
        std::string variableNameDEta_eta = "dEta_eta";
        std::string variableNameDPhi_eta = "dPhi_eta";
        std::string variableNamePtPh_Cut = "ptPh_Cut";

        std::cerr << " Reading  " << inputName << " ... " << std::endl;
        while (!file.eof()) {
            file.getline (&buffer[0],1000);
            std::istrstream line(buffer);
            std::string variableName;
            line >> variableName;
            double variableValue;
            line >> variableValue;
            std::cerr << "  File ->  " << variableName << "  =  " << variableValue << std::endl;
            if (variableName == variableNameR) R = variableValue;
            if (variableName == variableNameS4oS9C_Cut) S4oS9C_Cut = variableValue;
            if (variableName == variableNamePtC_Cut) ptC_Cut = variableValue;
            if (variableName == variableNamePtSum_Cut) ptSum_Cut = variableValue;
            if (variableName == variableNameMax_Cut) RMax_Cut = variableValue;
            if (variableName == variableNameDeltaEtaMax_Cut) DeltaEtaMax_Cut = variableValue;
            if (variableName == variableNameEt_Cut) et_Cut = variableValue;
            if (variableName == variableNameIso_Cut) iso_Cut = variableValue;
            if (variableName == variableNameAngle_Cut) angle_Cut = variableValue;
            if (variableName == variableNameR_eta) R_eta = variableValue;
            if (variableName == variableNameN_events) N_events = variableValue;
            if (variableName == variableNameDEta_C_Cut) dEta_C_Cut = variableValue;
            if (variableName == variableNameDPhi_C_Cut) dPhi_C_Cut = variableValue;
            if (variableName == variableNameDEta_eta) dEta_eta = variableValue;
            if (variableName == variableNameDPhi_eta) dPhi_eta = variableValue;
            if (variableName == variableNamePtPh_Cut) ptPh_Cut = variableValue;
        }
    }

    std::string testHelp = "--help";
    if (argc==2) {
        if (argv[1] == testHelp) {
            std::cout << "Help" << std::endl ;
            std::cout << " --help : display help" << std::endl ;
            std::cout << " -o : output root file name (eg histograms.root)" << std::endl ;
            std::cout << " -i : input file name with cuts values (eg Cuts.txt)" << std::endl ;
            std::cout << " name of input file : list name of input files ntuples" << std::endl ;
            exit(1);
        }
    }

    if (argc < 2)
    {
        std::cerr << "ERROR : ntuple name missing" << std::endl ;
        exit (1) ;
    }



    TChain * chain = new TChain ("simplePhotonAnalyzer/tTreeUtilities") ;

    int numC_;
    int numCB_;
    int numCE_;
    std::vector<double> * etaC_;
    std::vector<double> * thetaC_;
    std::vector<double> * phiC_;
    std::vector<double> * S4oS9C_;
    std::vector<double> * S4C_;
    std::vector<double> * S9C_;
    std::vector<double> * S16C_;
    std::vector<double> * S25C_;
    std::vector<double> * pxC_;
    std::vector<double> * pyC_;
    std::vector<double> * pzC_;
    std::vector<double> * etC_;
    std::vector<int> * HitsC_;
    std::vector<double> * HitsEnergyC_;

    int numEta_;
    std::vector<int> * numPh_; //---- number of photons for each Eta ----
    std::vector<double> * thetaPh_;
    std::vector<double> * etaPh_;
    std::vector<double> * phiPh_;
    std::vector<double> * pxPh_;
    std::vector<double> * pyPh_;
    std::vector<double> * pzPh_;



    etaC_ = new std::vector<double>;
    thetaC_ = new std::vector<double>;
    phiC_ = new std::vector<double>;
    S4oS9C_ = new std::vector<double>;
    S4C_ = new std::vector<double>;
    S9C_ = new std::vector<double>;
    S16C_ = new std::vector<double>;
    S25C_ = new std::vector<double>;
    pxC_ = new std::vector<double>;
    pyC_ = new std::vector<double>;
    pzC_ = new std::vector<double>;
    etC_ = new std::vector<double>;
    HitsC_ = new std::vector<int>;
    HitsEnergyC_ = new std::vector<double>;

    numPh_ = new std::vector<int>;
    thetaPh_ = new std::vector<double>;
    etaPh_ = new std::vector<double>;
    phiPh_ = new std::vector<double>;
    pxPh_ = new std::vector<double>;
    pyPh_ = new std::vector<double>;
    pzPh_ = new std::vector<double>;


    std::cerr << "---- Tree initialization ---- " << std::endl;

//---- ---- Clusters ---- C = cluster ----
    chain->SetBranchAddress("numC_",&numC_);
    chain->SetBranchAddress("numCB_",&numCB_);
    chain->SetBranchAddress("numCE_",&numCE_);
    chain->SetBranchAddress("etaC_",&etaC_);
    chain->SetBranchAddress("thetaC_",&thetaC_);
    chain->SetBranchAddress("phiC_",&phiC_);
    chain->SetBranchAddress("S4oS9C_",&S4oS9C_);
    chain->SetBranchAddress("S4C_",&S4C_);
    chain->SetBranchAddress("S9C_",&S9C_);
    chain->SetBranchAddress("S16C_",&S16C_);
    chain->SetBranchAddress("S25C_",&S25C_);
    chain->SetBranchAddress("pxC_",&pxC_);
    chain->SetBranchAddress("pyC_",&pyC_);
    chain->SetBranchAddress("pzC_",&pzC_);
    chain->SetBranchAddress("etC_",&etC_);
    chain->SetBranchAddress("HitsC_",&HitsC_);
    chain->SetBranchAddress("HitsEnergyC_",&HitsEnergyC_); //---- non è presente per ora ----

//---- ---- Photons from Eta ----
    chain->SetBranchAddress("numEta_",&numEta_);
    chain->SetBranchAddress("numPh_",&numPh_);
    chain->SetBranchAddress("thetaPh_",&thetaPh_);
    chain->SetBranchAddress("etaPh_",&etaPh_);
    chain->SetBranchAddress("phiPh_",&phiPh_);
    chain->SetBranchAddress("pxPh_",&pxPh_);
    chain->SetBranchAddress("pyPh_",&pyPh_);
    chain->SetBranchAddress("pzPh_",&pzPh_);



    std::cerr << "---- End Tree initialization ---- " << std::endl;



    if (flagOutput && flagInput) {
        chain->Add (argv[5]) ;
    }
    if ((flagOutput && !flagInput) || ((!flagOutput && flagInput))) {
        chain->Add (argv[3]) ;
    }
    if (!flagOutput && !flagInput) {
        chain->Add (argv[1]) ;
    }


    std::cerr << "---- End Chain initialization ---- " << std::endl;



//---- Histograms ----



//  TH1F hInvMassEtaC("hInvMassEtaC","Invariant mass Eta from Clusters",1000, 0., 1.);
//  TH2F hInvMassEtaC_numC("hInvMassEtaC_numC","Invariant mass Eta from Clusters versus number of cluster selected",1000, 0., 1.,100, 0., 100.);
//  TH1F hPhotonPt("hPhotonPt","Photon pt",10000, 0., 10.);
//  TH1F hClusterPt("hClusterPt","Single Cluster pt",30000, 0., 30.);

//  TH1F hPtSum("hPtSum","Summed cluster pt",40000, 0., 40.);
//  TH1F hInvMassEta2CNoCuts("hInvMassEta2CNoCuts","Invariant mass Eta two cluster selection. No Cuts",1000, 0., 1.);
//


//  TH2F hInvMassEta2CAndPtC1("hInvMassEta2CAndPtC1","Invariant mass Eta two cluster selection versus PtC1. No Cuts.",1000, 0., 1.,1000, 0., 10.);

//  TH2F hInvMassEta2CAndS4oS9C2("hInvMassEta2CAndS4oS9C2","Invariant mass Eta two cluster selection versus S4oS9. Cuts PtC1 e S4oS9 e PtC2.",1000, 0., 1.,1000, 0., 10.);

//  TH1F hIsolation("hIsolation","Isolation after all other cuts",1000, 0., 10.);
//  TH1F hAngle("hAngle","Angle between two BC, any pair of 2 BC",3600, 0., 2.*PI);
//
//  //---- MC matching ----

//  TH2F hInvMassEta2CAndPtC1MC("hInvMassEta2CAndPtC1MC","MC match. Invariant mass Eta two cluster selection versus PtC1. No Cuts.",1000, 0., 1.,1000, 0., 10.);
//  TH2F hInvMassEta2CAndS4oS9C1MC("hInvMassEta2CAndS4oS9C1MC","MC match. Invariant mass Eta two cluster selection versus S4oS9. Cuts PtC1.",1000, 0., 1.,1000, 0., 10.);
//  TH2F hInvMassEta2CAndPtC2MC("hInvMassEta2CAndPtC2MC","MC match. Invariant mass Eta two cluster selection versus PtC2. Cuts PtC1 e S4oS9.",1000, 0., 1.,1000, 0., 10.);
//  TH2F hInvMassEta2CAndS4oS9C2MC("hInvMassEta2CAndS4oS9C2MC","MC match. Invariant mass Eta two cluster selection versus S4oS9. Cuts PtC1 e S4oS9 e PtC2.",1000, 0., 1.,1000, 0., 10.);





//---- MC Analysis and MC matching ----
    TH1F hAngleMC("hAngleMC","MC Data. Angle between the two photons from eta",3600, 0., 2.*PI);
    TH1F hAngleEtaMC("hAngleEtaMC","MC Data. Angle Eta between the two photons from eta",1000,0.,10.);
    TH1F hAnglePhiMC("hAnglePhiMC","MC Data. Angle Phi between the two photons from eta",3600, 0., 2.*PI);
    TH1F hR_eta_C("hR_eta_C","Angle between reconstructed eta from BC without cuts and MC photons",1000, 0., 10.);
    TH1F hInvMassEtaPh("hInvMassEtaPh","Invariant mass Eta from MC Truth",10000, 0.547449999999, 0.547450000001);
    TH1F hInvMassEtaCMCTruth("hInvMassEtaCMCTruth","Invariant mass Eta from Clusters. Match with MC Truth",1000, 0., 1.);

    TH1F hAngleC_MCMatch("hAngleC_MCMatch","Angle between the two photons from BC and MC matching",3600, 0., 2.*PI);
    TH1F hNPhoton("hNPhoton","number of photons per eta from MC Truth",100, 0., 100.);
    TH1F hPhotonPt("hPhotonPt","Photon pt",10000, 0., 10.);

//---- Data Analysis ---- before cuts -----
    TH1F hAngle("hAngle","Angle between two BC, any pair of 2 BC",3600, 0., 2.*PI);
    TH1F hInvMassEta2CNoCuts("hInvMassEta2CNoCuts","Invariant mass Eta two cluster selection. NO CUTS",1000, 0., 1.);
    TH2F hInvMassEta2CETNoCuts("hInvMassEta2CETNoCuts","Invariant mass Eta two cluster selection Vs Et. No Cuts.",1000, 0., 1.,1000, 0., 10.);
    TH2F hInvMassEta2CETNoCutsMC("hInvMassEta2CETNoCutsMC","MC match. Invariant mass Eta two cluster selection vs Et. No Cuts.",1000, 0., 1.,1000, 0., 10.);
    TH2F h2CPtC1AndPtC2("h2CPtC1AndPtC2","PtC1 versus PtC2. No Cuts",1000, 0., 10.,1000, 0., 10.);

    //---- Data Analysis ---- after cuts -----
    TH2F hInvMassEta2CAndS4oS9C("hInvMassEta2CAndS4oS9C","Invariant mass Eta two cluster selection versus S4oS9. Cuts PtC.",1000, 0., 1.,1000, 0., 10.);
    TH2F hInvMassEta2CAndS4oS9CMC("hInvMassEta2CAndS4oS9CMC","MC match. Invariant mass Eta two cluster selection versus S4oS9. Cuts PtC.",1000, 0., 1.,1000, 0., 10.);

    TH3F hInvMassEta2CAndS4oS9CAndPtC("hInvMassEta2CAndS4oS9CAndPtC","Invariant mass Eta two cluster selection versus S4oS9 and PtC. Cuts PtC",100, 0., 1.,100, 0., 10.,100, 0., 10.);
    TH2F hEta2CS4oS9CAndPtC("hEta2CS4oS9CAndPtC","S4oS9 C versus PtC. Cuts PtC",1000, 0., 10.,1000, 0., 10.);

    TH2F hInvMassEta2CAndPtCMC("hInvMassEta2CAndPtCMC","MC match. Invariant mass Eta two cluster selection versus PtC. Cuts PtC and S4oS9.",1000, 0., 1.,1000, 0., 10.);
    TH2F hInvMassEta2CAndPtC("hInvMassEta2CAndPtC","Invariant mass Eta two cluster selection versus PtC. Cuts PtC and S4oS9.",1000, 0., 1.,1000, 0., 10.);

    TH2F hInvMassEta2CAndIsolationMC("hInvMassEta2CAndIsolationMC","MC match. Invariant mass Eta two cluster selection versus Iso/PtSum. Cut ON",1000, 0., 1.,1000, 0., 10.);
    TH2F hInvMassEta2CAndIsolation("hInvMassEta2CAndIsolation","Invariant mass Eta two cluster selection versus Iso/PtSum. Cut ON",1000, 0., 1.,1000, 0., 10.);

    TH1F hIsolation("hIsolation","Isolation after all other cuts",1000, 0., 10.);
    TH1F hIsolationMC("hIsolationMC","MC match. Isolation after all other cuts",1000, 0., 10.);

    TH1F hInvMassEta2C("hInvMassEta2C","Invariant mass Eta two cluster selection. After ALL CUTS",1000, 0., 1.);
    TH1F hInvMassEta2CMC("hInvMassEta2CMC","MC match. Invariant mass Eta two cluster selection. After ALL CUTS",1000, 0., 1.);

    TH1F hAngleC("hAngleC","Angle between two BC. After ALL CUTS",3600, 0., 2.*PI);

    TH2F hInvMassEta2PtSumMC("hInvMassEta2PtSumMC","MC Match. Invariant mass Eta two cluster selection versus PtC_Sum. All Cuts",1000, 0., 1.,1000, 0., 100.);
    TH2F hInvMassEta2PtSum("hInvMassEta2PtSum","Invariant mass Eta two cluster selection versus PtC_Sum. All Cuts",1000, 0., 1.,1000, 0., 100.);




//---- Input Variables Loaded ----
    std::cerr << "Input Variables Loaded: " << std::endl;
    std::cerr << " R = " << R << std::endl;
    std::cerr << " S4oS9C_Cut = " << S4oS9C_Cut << std::endl;
    std::cerr << " ptC_Cut = " << ptC_Cut << std::endl;
    std::cerr << " ptSum_Cut = " << ptSum_Cut << std::endl;
    std::cerr << " RMax_Cut = " << RMax_Cut << std::endl;
    std::cerr << " DeltaEtaMax_Cut = " << DeltaEtaMax_Cut << std::endl;
    std::cerr << " et_Cut = " << et_Cut << std::endl;
    std::cerr << " iso_Cut = " << iso_Cut << std::endl;
    std::cerr << " angle_Cut = " << angle_Cut << std::endl;
    std::cerr << " R_eta = " << R_eta << std::endl;
    std::cerr << " N_events = " << N_events << std::endl;
    std::cerr << " dEta_C_Cut = " << dEta_C_Cut << std::endl;
    std::cerr << " dPhi_C_Cut = " << dPhi_C_Cut << std::endl;
    std::cerr << " dEta_eta = " << dEta_eta << std::endl;
    std::cerr << " dPhi_eta = " << dPhi_eta << std::endl;
    std::cerr << " ptPh_Cut = " << ptPh_Cut << std::endl;



    double RQ = R*R;
    double R_etaQ = R_eta*R_eta;

//---- Starting analysis ----
    int nEntries = chain->GetEntries () ;

//---- MC Truth analysis ----
    int FirstCluster = -1;
    int SecondCluster = -1;
    int numEntry = -1;
    TTree ClusterPairCMC("ClusterPairCMC","ClusterPairCMC");
    ClusterPairCMC.Branch("FirstCluster",&FirstCluster,"FirstCluster/I");
    ClusterPairCMC.Branch("SecondCluster",&SecondCluster,"SecondCluster/I");
    ClusterPairCMC.Branch("numEntry",&numEntry,"numEntry/I");
    bool flag2Photons;


//---------------------
//---- MC Analysis ----
//---------------------

    for (int entry = 0 ; entry < nEntries ; ++entry) //---- TEST VELOCE ----
    {
        chain->GetEntry (entry) ;
        if (entry%10000 == 0) std::cout << "------> reading entry " << entry << " <------\n" ;

        numEntry = entry;

        //---- loop over etas ----
        int counterPhotons = 0;
        for (int ii=0; ii<numEta_; ii++) {
            flag2Photons = false;
            FirstCluster = -1;
            SecondCluster = -1;

            math::XYZTLorentzVector pPh(0,0,0,0);
            math::XYZTLorentzVector pC(0,0,0,0);
            hNPhoton.Fill(numPh_->at(ii));

            //---- calculate angle between two photons from Eta MC ----
            double angle_MC = -1; //---- default value ----
            if (numPh_->at(ii) == 2) {
                int counterPhotons_Angle = counterPhotons;

                //---- first photon ----
                double pxPh_Angle_1 = pxPh_->at(counterPhotons_Angle);
                double pyPh_Angle_1 = pyPh_->at(counterPhotons_Angle);
                double pzPh_Angle_1 = pzPh_->at(counterPhotons_Angle);
                double EnergyPh_Angle_1 = sqrt(pxPh_Angle_1*pxPh_Angle_1 + pyPh_Angle_1*pyPh_Angle_1 + pzPh_Angle_1*pzPh_Angle_1);
                math::XYZTLorentzVector pPh_temp_Angle_1(pxPh_Angle_1,pyPh_Angle_1,pzPh_Angle_1,EnergyPh_Angle_1);
                double etaPh_Angle_1 = etaPh_->at(counterPhotons_Angle);
                double phiPh_Angle_1 = phiPh_->at(counterPhotons_Angle);

                //---- second photon ----
                counterPhotons_Angle++;
                double pxPh_Angle_2 = pxPh_->at(counterPhotons_Angle);
                double pyPh_Angle_2 = pyPh_->at(counterPhotons_Angle);
                double pzPh_Angle_2 = pzPh_->at(counterPhotons_Angle);
                double EnergyPh_Angle_2 = sqrt(pxPh_Angle_2*pxPh_Angle_2 + pyPh_Angle_2*pyPh_Angle_2 + pzPh_Angle_2*pzPh_Angle_2);
                math::XYZTLorentzVector pPh_temp_Angle_2(pxPh_Angle_2,pyPh_Angle_2,pzPh_Angle_2,EnergyPh_Angle_2);
                double etaPh_Angle_2 = etaPh_->at(counterPhotons_Angle);
                double phiPh_Angle_2 = phiPh_->at(counterPhotons_Angle);

                double cosAngle = pxPh_Angle_1 * pxPh_Angle_2 + pyPh_Angle_1 * pyPh_Angle_2 + pzPh_Angle_1 * pzPh_Angle_2;
                if (EnergyPh_Angle_1!=0 && EnergyPh_Angle_2!=0) cosAngle = cosAngle / (EnergyPh_Angle_1 * EnergyPh_Angle_2);
                angle_MC = acos(cosAngle);
                hAngleMC.Fill(angle_MC);

                hAngleEtaMC.Fill(fabs(etaPh_Angle_1-etaPh_Angle_2));
                hAnglePhiMC.Fill(fabs(phiPh_Angle_1-phiPh_Angle_2));

            }
            //---- end calculate angle between two photons from Eta MC ----

            int counterPh1 = -1;
            int counterPh2 = -1;

            bool flagFirstPhoton = false;
            bool flagSecondPhoton = false;
            //---- loop over photons generating an eta ----
            for (int jj=0; jj<numPh_->at(ii); jj++) {
                double pxPh = pxPh_->at(counterPhotons);
                double pyPh = pyPh_->at(counterPhotons);
                double pzPh = pzPh_->at(counterPhotons);
                double ptPh = sqrt(pxPh * pxPh + pyPh * pyPh);
                double EnergyPh = sqrt(pxPh*pxPh + pyPh*pyPh + pzPh*pzPh);
                math::XYZTLorentzVector pPh_temp(pxPh,pyPh,pzPh,EnergyPh);
                double etaPh = etaPh_->at(counterPhotons);
                double phiPh = phiPh_->at(counterPhotons);
                pPh = pPh + pPh_temp;

                double bestTestValueRCQ = 100000;
                //---- loop over clusters ----
                for (int kk=0; kk<numC_; kk++) {
                    double etaC1 = etaC_->at(kk);
                    double phiC1 = phiC_->at(kk);
                    double RC1Q = (etaPh - etaC1) * (etaPh - etaC1) + (phiPh - phiC1) * (phiPh - phiC1); //---- Q at the end stands for ^2 ----
                    if (RC1Q < RQ) { //---- found a cluster near a photon ----
                        if (jj == 0) { //---- first photon ----
                            if ((bestTestValueRCQ > RC1Q) && (fabs(etaPh - etaC1) < dEta_C_Cut) && (fabs(phiPh - phiC1) < dPhi_C_Cut) && (ptPh > ptPh_Cut)) {
                                FirstCluster = kk;
                                counterPh1 = counterPhotons;
                                bestTestValueRCQ = RC1Q;
                                flagFirstPhoton = true;
                            }
                        }
                        else { //---- second photon ----
                            if ((bestTestValueRCQ > RC1Q) && (fabs(etaPh - etaC1) < dEta_C_Cut) && (deltaPhi(phiPh,phiC1) < dPhi_C_Cut) && (ptPh > ptPh_Cut)) {
                                SecondCluster = kk; //---- if I find a third photon ? --> Never found :D --> if it happens throw away eta event -> flag2Photons = false
                                counterPh2= counterPhotons;
                                bestTestValueRCQ = RC1Q;
                                flagSecondPhoton = true;
                                if (flagFirstPhoton) flag2Photons = true; //---- found 2 photons ----
                            }
                        }
                        double pxC = pxC_->at(kk);
                        double pyC = pyC_->at(kk);
                        double pzC = pzC_->at(kk);
                        double EnergyC = sqrt(pxC*pxC + pyC*pyC + pzC*pzC);
                        math::XYZTLorentzVector pC_temp(pxC,pyC,pzC,EnergyC);
                        pC = pC + pC_temp;
                    }//---- end found a cluster near a photon ----
                }//---- end loop clusters ----
                counterPhotons++;
            }//---- end loop photons generating an eta ----

            if (numPh_->at(ii) != 2) flag2Photons = false; //---- I do NOT want != 2 photons events ----

            bool flagEtaDirection = true;
            if (flag2Photons) {
                //---- match composite cluster <-> eta ----
                //---- first photon ----
                double etaPh1 = etaPh_->at(counterPh1);
                double phiPh1 = phiPh_->at(counterPh1);
                double pxPh1 = pxPh_->at(counterPh1);
                double pyPh1 = pyPh_->at(counterPh1);
                double pzPh1 = pzPh_->at(counterPh1);
                double EnergyPh1 = sqrt(pxPh1*pxPh1 + pyPh1*pyPh1 + pzPh1*pzPh1);
                math::XYZTLorentzVector pPh_temp1(pxPh1,pyPh1,pzPh1,EnergyPh1);
                //---- second photon ----
                double etaPh2 = etaPh_->at(counterPh2);
                double phiPh2 = phiPh_->at(counterPh2);
                double pxPh2 = pxPh_->at(counterPh2);
                double pyPh2 = pyPh_->at(counterPh2);
                double pzPh2 = pzPh_->at(counterPh2);
                double EnergyPh2 = sqrt(pxPh2*pxPh2 + pyPh2*pyPh2 + pzPh2*pzPh2);
                math::XYZTLorentzVector pPh_temp2(pxPh2,pyPh2,pzPh2,EnergyPh2);
                math::XYZTLorentzVector pPh_Eta = pPh_temp1 + pPh_temp2;
                double etaEta = pPh_Eta.eta();
                double phiEta = pPh_Eta.phi();
                //---- sum cluster ----
                double etaSumCluster = pC.eta();
                double phiSumCluster = pC.phi();
                double R_etaQ_C = (etaEta - etaSumCluster) * (etaEta - etaSumCluster) + deltaPhi(phiEta,phiSumCluster) * deltaPhi(phiEta,phiSumCluster);
                if ((R_etaQ_C < R_etaQ) && (fabs(etaSumCluster - etaEta) < dEta_eta) && (deltaPhi(phiSumCluster,phiEta) < dPhi_eta)) { //---- found a cluster near a photon ----
                    flagEtaDirection = true;
                }
                hR_eta_C.Fill(sqrt(R_etaQ_C));
            }
            //---- end match composite cluster <-> eta ----

            if (flag2Photons && flagEtaDirection) ClusterPairCMC.Fill(); //---- tree filling ----

            //---- result in histograms ---- Only if I found two photons and if they are in the right position ----
            if (flag2Photons && flagEtaDirection) {
                double ptPh = sqrt(pPh.x() * pPh.x() + pPh.y() * pPh.y());
                hPhotonPt.Fill(ptPh);
                double InvMassPh = pPh.mag();
                hInvMassEtaPh.Fill(InvMassPh);
                double InvMassC = pC.mag();
                if (InvMassC != 0) hInvMassEtaCMCTruth.Fill(InvMassC); //---- if zero -> no clusters found ----
            }
        }
        //---- end loop over etas ----
    }







//---- NO MC Truth analysis ---- Pair Analysis ----
//---- and MC Comparison ----

    int FirstClusterAfter = -1;
    int SecondClusterAfter = -1;
    int numEntryAfter = -1;

    ClusterPairCMC.SetBranchAddress("FirstCluster",&FirstClusterAfter);
    ClusterPairCMC.SetBranchAddress("SecondCluster",&SecondClusterAfter);
    ClusterPairCMC.SetBranchAddress("numEntry",&numEntryAfter);

    int nEntriesMC = ClusterPairCMC.GetEntries () ;

    if (N_events == -1) N_events = nEntries;
    for (int entry = 0 ; entry < N_events ; ++entry) //---- TEST VELOCE ----
    {
        chain->GetEntry (entry) ;
        if (entry%10000 == 0) std::cout << "------> No MC Truth ---- reading entry " << entry << " <------\n" ;
        std::cout << "------> No MC Truth ---- reading entry " << entry << " <------\n" ;

        std::vector<int> numC_Selected;
        math::XYZTLorentzVector pC(0,0,0,0);

        int numberC1;
        int numberC2;
        double EnergyPair;
        bool flagMCMatch;
        double angle_C;

        //---- Tree with Cluster data ----
        TTree PairCluster("PairCluster","PairCluster");
        PairCluster.Branch("numberC1",&numberC1,"numberC1/I");
        PairCluster.Branch("numberC2",&numberC2,"numberC2/I");
        PairCluster.Branch("EnergyPair",&EnergyPair,"EnergyPair/D");
        PairCluster.Branch("flagMCMatch",&flagMCMatch,"flagMCMatch/O");
        PairCluster.Branch("angle_C",&angle_C,"angle_C/D");

        //---- loop over clusters ----
        for (int kk=0; kk<numC_; kk++) {

            double etaC1 = etaC_->at(kk);
            double phiC1 = phiC_->at(kk);
            double pxC1 = pxC_->at(kk);
            double pyC1 = pyC_->at(kk);
            double pzC1 = pzC_->at(kk);
            double ptC1 = sqrt(pxC1*pxC1 + pyC1*pyC1);
            double EnergyC1 = sqrt(pxC1*pxC1 + pyC1*pyC1 + pzC1*pzC1);
            double EnergyTC1 = sqrt(pxC1*pxC1 + pyC1*pyC1);

            numberC1 = kk;
            math::XYZTLorentzVector pC2C_1(pxC1,pyC1,pzC1,EnergyC1);

            //---- save all pair-photons ----
            for (int ll=kk+1; ll<numC_; ll++) { //---- inizio da kk così non rischio di avere doppioni ----
                if (ll!=kk) {
                    double pxCIn = pxC_->at(ll);
                    double pyCIn = pyC_->at(ll);
                    double pzCIn = pzC_->at(ll);
                    double EnergyCIn = sqrt(pxCIn*pxCIn + pyCIn*pyCIn + pzCIn*pzCIn);
                    math::XYZTLorentzVector pC2C_2(pxCIn,pyCIn,pzCIn,EnergyCIn);
                    math::XYZTLorentzVector pC2C_sum = pC2C_1 + pC2C_2;
                    numberC2 = ll;
                    EnergyPair = pC2C_sum.mag();

                    //---- check if cluster pair correspond to a MC photon pair from eta ----
                    for (int entryMC = 0 ; entryMC < nEntriesMC ; ++entryMC) {
                        ClusterPairCMC.GetEntry(entryMC);
                        if (numEntryAfter == entry) {
                            if (((FirstClusterAfter == ll) && (SecondClusterAfter == kk)) || ((FirstClusterAfter == kk) && (SecondClusterAfter == ll))) flagMCMatch  = true;
                            else flagMCMatch = false;
                        }
                    }
                    //---- angle ----
                    double cosAngle = pxCIn * pxC1 + pyCIn * pyC1 + pzCIn * pzC1;
                    if (EnergyCIn!=0 && EnergyC1!=0) cosAngle = cosAngle / (EnergyCIn * EnergyC1);
                    else cosAngle = 0; //---- -> 90° -> rejected ----
                    angle_C = acos(cosAngle);
                    hAngle.Fill(angle_C);
                    if (flagMCMatch) hAngleC_MCMatch.Fill(angle_C);
                    //---- Add to pair cluster only if Angle between Basic Clusters is less than "angle_cut" ----
                    if (angle_C < angle_Cut) PairCluster.Fill();
                }
            }//---- end save all pair-photons ----
        }//---- end loop clusters ----




        //---- pair-photon tree analysis ----
        //---- here add cuts ----

        int numberC1After;
        int numberC2After;
        double EnergyPairAfter;
        bool flagMCMatchAfter;
        double angle_C_After;

        int nEntriesTreePairCluster = PairCluster.GetEntries () ;
        PairCluster.SetBranchAddress("numberC1",&numberC1After);
        PairCluster.SetBranchAddress("numberC2",&numberC2After);
        PairCluster.SetBranchAddress("EnergyPair",&EnergyPairAfter);
        PairCluster.SetBranchAddress("flagMCMatch",&flagMCMatchAfter);
        PairCluster.SetBranchAddress("angle_C",&angle_C_After);

        //---- loop over cluster-pair ----
        for (int pairNumber = 0 ; pairNumber < nEntriesTreePairCluster ; ++pairNumber) {
            PairCluster.GetEntry(pairNumber);

            double etaC1_After = etaC_->at(numberC1After);
            double phiC1_After = phiC_->at(numberC1After);
            double pxC1_After = pxC_->at(numberC1After);
            double pyC1_After = pyC_->at(numberC1After);
            double pzC1_After = pzC_->at(numberC1After);
            double ptC1_After = sqrt(pxC1_After * pxC1_After + pyC1_After * pyC1_After);
            double EnergyC1_After = sqrt(pxC1_After * pxC1_After + pyC1_After * pyC1_After + pzC1_After * pzC1_After);

            math::XYZTLorentzVector pC1_After(pxC1_After,pyC1_After,pzC1_After,EnergyC1_After);


            double etaC2_After = etaC_->at(numberC2After);
            double phiC2_After = phiC_->at(numberC2After);
            double pxC2_After = pxC_->at(numberC2After);
            double pyC2_After = pyC_->at(numberC2After);
            double pzC2_After = pzC_->at(numberC2After);
            double ptC2_After = sqrt(pxC2_After * pxC2_After + pyC2_After * pyC2_After);
            double EnergyC2_After = sqrt(pxC2_After * pxC2_After + pyC2_After * pyC2_After + pzC2_After * pzC2_After);

            math::XYZTLorentzVector pC2_After(pxC2_After,pyC2_After,pzC2_After,EnergyC2_After);


            math::XYZTLorentzVector pC_Sum_After = pC1_After + pC2_After;
            double etaSum_After = pC_Sum_After.eta();
            double phiSum_After = pC_Sum_After.phi();
            double pxSum_After = pC_Sum_After.x();
            double pySum_After = pC_Sum_After.y();
            double pzSum_After = pC_Sum_After.z();
            double ptSum_After = sqrt(pxSum_After * pxSum_After + pySum_After * pySum_After);
            double EnergySum_After = sqrt(pxSum_After * pxSum_After + pySum_After * pySum_After + pzSum_After * pzSum_After);



            //---- some graphs before cuts ----
            hInvMassEta2CNoCuts.Fill(EnergyPairAfter);
            hInvMassEta2CETNoCuts.Fill(EnergyPairAfter,ptC1_After); //---- save only first cluster -> second cluster selected after ----
            if (flagMCMatchAfter) { //---- save only first cluster -> second cluster selected after ----
                hInvMassEta2CETNoCutsMC.Fill(EnergyPairAfter,ptC1_After);
            }
            h2CPtC1AndPtC2.Fill(ptC1_After,ptC2_After);
            //---- end some graphs before cuts ----

            //---- add cuts ----
            //---- cut in pair -> both Clusters ----



            //---- pt-Cluster cut Cluster 1 and Cluster 2 ----
            if ((ptC1_After > ptC_Cut) && (ptC2_After > ptC_Cut)) {

                if (flagMCMatchAfter) hInvMassEta2CAndS4oS9CMC.Fill(EnergyPairAfter,S4oS9C_->at(numberC1After));
                hInvMassEta2CAndS4oS9C.Fill(EnergyPairAfter,S4oS9C_->at(numberC1After));

                hInvMassEta2CAndS4oS9CAndPtC.Fill(EnergyPairAfter,S4oS9C_->at(numberC1After),ptC1_After);
                hEta2CS4oS9CAndPtC.Fill(S4oS9C_->at(numberC1After),ptC1_After);


                //---- S4oS9C_-Cluster cut Cluster 1 and Cluster 2 ----
                if ((S4oS9C_->at(numberC1After) > S4oS9C_Cut) && (S4oS9C_->at(numberC2After) > S4oS9C_Cut)) {

                    if (flagMCMatchAfter) hInvMassEta2CAndPtCMC.Fill(EnergyPairAfter,ptC1_After);
                    hInvMassEta2CAndPtC.Fill(EnergyPairAfter,ptC1_After);

                    //---- Isolatio cut ----
                    double iso = 0;
                    for (int ll=0; ll<numC_; ll++) {
                        if ((ll!=numberC1After) && (ll!=numberC2After)) {
                            double etaCIso = etaC_->at(ll);
                            double phiCIso = phiC_->at(ll);
                            double R = sqrt((etaCIso-etaSum_After)*(etaCIso-etaSum_After) + deltaPhi(phiCIso,phiSum_After) * deltaPhi(phiCIso,phiSum_After));
                            double deta = fabs(etaCIso - etaSum_After);
                            double et = etC_->at(ll);
                            if ( (R < RMax_Cut)  && (deta < DeltaEtaMax_Cut) && (et > et_Cut)  ) iso = iso + et ;
                        }
                    }

                    if (flagMCMatchAfter) hInvMassEta2CAndIsolationMC.Fill(EnergyPairAfter,iso/ptSum_After);
                    hInvMassEta2CAndIsolation.Fill(EnergyPairAfter,iso/ptSum_After);
                    if (flagMCMatchAfter) hIsolationMC.Fill(iso/ptSum_After);
                    hIsolation.Fill(iso/ptSum_After);

                    if ((iso/ptSum_After) < iso_Cut) {
                        if (flagMCMatchAfter) hInvMassEta2CMC.Fill(EnergyPairAfter);
                        if (flagMCMatchAfter) hInvMassEta2PtSumMC.Fill(EnergyPairAfter,ptSum_Cut);
                        hInvMassEta2PtSum.Fill(ptSum_Cut,EnergyPairAfter);
                        if (ptSum_After < ptSum_Cut) hInvMassEta2C.Fill(EnergyPairAfter);
                        hAngleC.Fill(angle_C_After); //---- cut on angle has been performed in pair filling ----
                    }//---- end Isolatio cut ----
                }//---- end S4oS9C_-Cluster cut Cluster 1 and Cluster 2 ----
            }//---- end pt-Cluster cut Cluster 1 and Cluster 2
        }//---- end loop over cluster-pair ----
    } //---- end loop entries ----


//---- save in file ----
    TFile saving (outputRootName.c_str (),"recreate") ;
    saving.cd () ;


    hInvMassEtaPh.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEtaPh.Write();

    hInvMassEtaCMCTruth.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEtaCMCTruth.Write();

    hInvMassEta2C.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2C.Write();

    hInvMassEta2CNoCuts.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CNoCuts.Write();

    hInvMassEta2CETNoCuts.GetYaxis()->SetTitle("Et (GeV)");
    hInvMassEta2CETNoCuts.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CETNoCuts.Write();

    hInvMassEta2CAndIsolation.GetYaxis()->SetTitle("iso/PtSum");
    hInvMassEta2CAndIsolation.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndIsolation.Write();

    hInvMassEta2CAndPtC.GetYaxis()->SetTitle("PtC (GeV)");
    hInvMassEta2CAndPtC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndPtC.Write();

    hInvMassEta2CAndS4oS9C.GetYaxis()->SetTitle("S4oS9");
    hInvMassEta2CAndS4oS9C.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndS4oS9C.Write();

    hInvMassEta2CAndS4oS9CAndPtC.GetYaxis()->SetTitle("S4oS9");
    hInvMassEta2CAndS4oS9CAndPtC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndS4oS9CAndPtC.GetZaxis()->SetTitle("Pt (GeV)");
    hInvMassEta2CAndS4oS9CAndPtC.Write();

    hEta2CS4oS9CAndPtC.GetYaxis()->SetTitle("Pt (GeV)");
    hEta2CS4oS9CAndPtC.GetXaxis()->SetTitle("S4oS9");
    hEta2CS4oS9CAndPtC.Write();

    h2CPtC1AndPtC2.GetYaxis()->SetTitle("Pt C2 (GeV)");
    h2CPtC1AndPtC2.GetXaxis()->SetTitle("Pt C1 (GeV)");
    h2CPtC1AndPtC2.Write();

    hIsolation.GetXaxis()->SetTitle("iso/PtSum");
    hIsolation.Write();

    hIsolationMC.GetXaxis()->SetTitle("iso/PtSum");
    hIsolationMC.Write();

    hAngleC.GetXaxis()->SetTitle("Angle between BC (rad)");
    hAngleC.Write();

    hAngle.GetXaxis()->SetTitle("Angle between BC (rad)");
    hAngle.Write();

    hInvMassEta2PtSum.GetYaxis()->SetTitle("Pt_Sum (GeV)");
    hInvMassEta2PtSum.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2PtSum.Write();


//---- MC Match ----
    hInvMassEta2PtSumMC.GetYaxis()->SetTitle("Pt_Sum (GeV)");
    hInvMassEta2PtSumMC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2PtSumMC.Write();

    hInvMassEta2CETNoCutsMC.GetYaxis()->SetTitle("Et (GeV)");
    hInvMassEta2CETNoCutsMC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CETNoCutsMC.Write();

    hInvMassEta2CAndPtCMC.GetYaxis()->SetTitle("PtC (GeV/c)");
    hInvMassEta2CAndPtCMC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndPtCMC.Write();

    hInvMassEta2CAndS4oS9CMC.GetYaxis()->SetTitle("S4oS9");
    hInvMassEta2CAndS4oS9CMC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndS4oS9CMC.Write();

    hInvMassEta2CAndIsolationMC.GetYaxis()->SetTitle("Iso/PtSum");
    hInvMassEta2CAndIsolationMC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CAndIsolationMC.Write();

    hInvMassEta2CMC.GetXaxis()->SetTitle("Invariant Mass (GeV)");
    hInvMassEta2CMC.Write();

    hAngleMC.GetXaxis()->SetTitle("Angle between Photons(rad)");
    hAngleMC.Write();

    hAngleC_MCMatch.GetXaxis()->SetTitle("Angle between BC (rad)");
    hAngleC_MCMatch.Write();

    hAngleEtaMC.GetXaxis()->SetTitle("Angle Eta photons (rad)");
    hAngleEtaMC.Write();

    hAnglePhiMC.GetXaxis()->SetTitle("Angle Phi between photons (rad)");
    hAnglePhiMC.Write();

    hR_eta_C.GetXaxis()->SetTitle("#eta and #phi plane radius");
    hR_eta_C.Write();

    hNPhoton.GetXaxis()->SetTitle("number of photons per Eta");
    hNPhoton.Write();

    hPhotonPt.GetXaxis()->SetTitle("Pt of summed photons from Eta (GeV/c)");
    hPhotonPt.Write();


//---- Save Cuts in File ----

    TTree Cuts("Cuts","Cuts");
    Cuts.Branch("R",&R,"R/D");
    Cuts.Branch("S4oS9C_Cut",&S4oS9C_Cut,"S4oS9C_Cut/D");
    Cuts.Branch("ptC_Cut",&ptC_Cut,"ptC_Cut/D");
    Cuts.Branch("ptSum_Cut",&ptSum_Cut,"ptSum_Cut/D");
    Cuts.Branch("RMax_Cut",&RMax_Cut,"RMax_Cut/D");
    Cuts.Branch("DeltaEtaMax_Cut",&DeltaEtaMax_Cut,"DeltaEtaMax_Cut/D");
    Cuts.Branch("et_Cut",&et_Cut,"et_Cut/D");
    Cuts.Branch("iso_Cut",&iso_Cut,"iso_Cut/D");
    Cuts.Branch("angle_Cut",&angle_Cut,"angle_Cut/D");
    Cuts.Branch("R_eta",&R_eta,"R_eta/D");
    Cuts.Branch("N_events",&N_events,"N_events/I");
    Cuts.Branch("dEta_C_Cut",&dEta_C_Cut,"dEta_C_Cut/D");
    Cuts.Branch("dPhi_C_Cut",&dPhi_C_Cut,"dPhi_C_Cut/D");
    Cuts.Branch("dEta_eta",&dEta_eta,"dEta_eta/D");
    Cuts.Branch("dPhi_eta",&dPhi_eta,"dPhi_eta/D");
    Cuts.Branch("ptPh_Cut",&ptPh_Cut,"ptPh_Cut/D");


    Cuts.Fill();
    Cuts.Write();

    saving.Close ();
    delete chain;
    return 0 ;

}
Exemplo n.º 10
0
int Dense3d_MPI::evaluate(Vec srcDen, Vec trgVal) 
{
  //begin
  // CHECK
  pA(_srcPos!=NULL && _srcNor!=NULL && _trgPos!=NULL && srcDen!=NULL && trgVal!=NULL);
  //-----------------------------------
  int dim  = this->dim();
  int srcDOF = this->srcDOF();
  int trgDOF = this->trgDOF();
  /* Get global number of source positions */
  PetscInt srcGlbNum = procGlbNum(_srcPos);
  /* Get local number of target positions */
  PetscInt trgLclNum = procLclNum(_trgPos);
  
  Vec srcAllPos = _srcAllPos;
  Vec srcAllNor = _srcAllNor;
  Vec srcAllDen;
  /* Create scatter context to scatter source densities to all processors */
  {
	 VecScatter ctx;
	 pC( VecScatterCreateToAll(srcDen, &ctx, &srcAllDen) );
	 pC( VecScatterBegin(ctx, srcDen, srcAllDen, INSERT_VALUES, SCATTER_FORWARD) );
	 pC( VecScatterEnd(ctx,  srcDen, srcAllDen, INSERT_VALUES, SCATTER_FORWARD) );
	 pC( VecScatterDestroy(ctx) );
  }
  
  Vec trgLclPos = _trgPos;
  Vec trgLclVal =  trgVal;

  /* Create matrices for source positions, normals, densities.  See common/nummat.hpp for
	* more information on matrices */
  double* srcAllPosArr; pC( VecGetArray(srcAllPos, &srcAllPosArr) );
  DblNumMat srcAllPosMat(dim, srcGlbNum, false, srcAllPosArr);
  double* srcAllNorArr; pC( VecGetArray(srcAllNor, &srcAllNorArr) );
  DblNumMat srcAllNorMat(dim, srcGlbNum, false, srcAllNorArr);
  double* srcAllDenArr; pC( VecGetArray(srcAllDen, &srcAllDenArr) );
  DblNumVec srcAllDenVec(srcDOF*srcGlbNum, false, srcAllDenArr);

  /* Create matrices for target positions and values */
  double* trgLclPosArr; pC( VecGetArray(trgLclPos, &trgLclPosArr) );
  DblNumMat trgLclPosMat(dim, trgLclNum, false, trgLclPosArr);
  double* trgLclValArr; pC( VecGetArray(trgLclVal, &trgLclValArr) );
  DblNumVec trgLclValVec(trgDOF*trgLclNum, false, trgLclValArr);

  /* Create an evaluation context and evaluate based on kernel type */
  DblNumMat inter(trgDOF, srcGlbNum*srcDOF);
  /* Do multiplication one line of the matrices at a time */
  for(int i=0; i<trgLclNum; i++) {
	 DblNumMat onePosMat(dim, 1, false, trgLclPosMat.clmdata(i));
	 DblNumVec oneValVec(trgDOF, false, trgLclValVec.data()+trgDOF*i);
	 /* Create kernel multiplier context based on kernel type */
	 pC( _knl.buildKnlIntCtx(srcAllPosMat, srcAllNorMat, onePosMat, inter) );
	 /* Computes 1.0*inter*srcAllDenVec + 0.0*oneValVec = oneValVec */
	 pC( dgemv(1.0, inter, srcAllDenVec, 0.0, oneValVec) );
  }
  
  pC( VecRestoreArray(srcAllPos, &srcAllPosArr) );
  pC( VecRestoreArray(srcAllNor, &srcAllNorArr) );
  pC( VecRestoreArray(srcAllDen, &srcAllDenArr) );
  
  pC( VecRestoreArray(trgLclPos, &trgLclPosArr) );
  pC( VecRestoreArray(trgLclVal, &trgLclValArr) );
  
  pC( VecDestroy(srcAllDen) );
  
  return(0);
}
Exemplo n.º 11
0
void _FSSEnvironment::runStage(const Operation& subject, 
		DynamicArray<StyleTreeNode*>& styleTreePositions,
		HashMap<StyleTreeNode*, bool, HashCollection::pointerHash<StyleTreeNode>,
			HashCollection::pointerMatch<StyleTreeNode>>& styleTreePositionSet,
		Allocator& stackAllocator, FDUint depth) {

#ifdef FD_FSS_DIAGNOSTIC_PRINT
	const char* depthString = "                                         ";
	FDUint depthStringLen = strlen(depthString);
	printf("%s%c/%s\n", depthString + depthStringLen - depth, 
			subject.getName().ownerType, subject.getName().name);
#endif

	int positionsListingStart = styleTreePositions.getSize();
	StyleTree& tree = styleSheet.getTree();
	DynamicArray<StyleTreeNode*> styleTreePositionsToAdd(stackAllocator);
	HashMap<PropertyDef*, void*, HashCollection::pointerHash<PropertyDef>,
			HashCollection::pointerMatch<PropertyDef>> defToProperty(
			stackAllocator);

	// Check the style sheet tree for the properties of the subject.
	{
		Profiler p("FSS check for properties", true);

		Profiler pApplicable("Def Applicable Rules", true);
		DynamicArray<StyleRule*> applicableRules(stackAllocator);
		pApplicable.close();

		// Follow up on rules suggested by the subject.
		// Note that we do not keep such rules in the listing of styleTreePositions
		// - instead we only store multi-atom rules that have a first-atom match.
		{
			Profiler p("FSS check rules", true);

			Array<StyleRule* const> relatedRules = subject.getRelatedRules();
			for (int i = 0; i < relatedRules.length; i++) {
				applicableRules.append(relatedRules[i]);
				Selector& selector = relatedRules[i]->getSelector();
				tree.advanceTopRule(*relatedRules[i], subject, 
						styleTreePositionsToAdd, styleTreePositionSet);
			}
		}

		// Extract properties from the list of rules.
		{
			Profiler p("FSS extract properties", true);

			FDUint ruleCount = applicableRules.getSize();
			for (FDUint i = 0; i < ruleCount; i++) {
				Array<Style> styles = applicableRules[i]->getStyles();

				for (int j = 0; j < styles.length; j++) {
					Style& style = styles[j];
					StyleDef& styleDef = style.getDef();
					PropertyDef& propertyDef = styleDef.getPropertyDef();

					// Create the property if it doesn't already exist.
					void* prop;
					if (!defToProperty.get(&propertyDef, prop)) {
						prop = propertyDef.createProperty(stackAllocator);
						defToProperty[&propertyDef] = prop;
					}

					style.invoke(prop);
				}
			}
		}

		{
			Profiler p("FSS do inline styles.", true);

			Array<const Style> inlineStyles = subject.getInlineStyles();
			FDUint inlineStyleCount = inlineStyles.length;

			for (FDUint i = 0; i < inlineStyleCount; i++) {
				const Style& style = inlineStyles[i];

				StyleDef& styleDef = style.getDef();
				PropertyDef& propertyDef = styleDef.getPropertyDef();

				// Create the property if it doesn't already exist.
				void* prop;
				if (!defToProperty.get(&propertyDef, prop)) {
					Profiler pNF("Not found shit", true);
					prop = propertyDef.createProperty(stackAllocator);
					defToProperty[&propertyDef] = prop;
				}

				Profiler p("Invoke Style", true);
				style.invoke(prop);
			}
		}

		Profiler pC("FSS prop check cleanup.", true);
	}

	
	void* hideVS;
	FDbool hasHideProp = defToProperty.get(environment.hidePDef, hideVS);
	if (!hasHideProp || 
			!((SinglePropertyDef<FDbool>::ValueSpecificity*)hideVS)->value) {

		DoOperation doOp(subject, defToProperty);

		user->onDoOperation(doOp);

		// Recurse on children.
		void* childrenVoid;
		bool hasChildren = defToProperty.get(
				styleSheet.getDefaultDefs().childrenDef, childrenVoid);
		if (hasChildren) {
			Profiler p("FSS Children operations", true);

			DefaultDefCollection::OpSet* children = 
					(DefaultDefCollection::OpSet*)childrenVoid;

			FDUint count = children->getSize();
			OpConstraint* ops = FD_NEW_ARRAY(OpConstraint, count, *allocator, 
					allocator);

			DefaultDefCollection::OpSetIterator childrenIterator(*children);
			Operation* key;
			bool value;
			FDUint i = 0;
			while (childrenIterator.getNext(key, value)) {
				ops[i++].op = key;
			}

			// Set up constraints here
			void* orderVoid;
			bool hasConstraints = defToProperty.get(
					styleSheet.getDefaultDefs().orderDef, orderVoid);
			if (hasConstraints) {
				DefaultDefCollection::ConstraintSet* constraints =
						(DefaultDefCollection::ConstraintSet*)orderVoid;

				// Iterate through constraints.
				DefaultDefCollection::ConstraintSetIterator 
						constraintIterator(*constraints);
				Pair<Selector, Selector>* key;
				bool value;
				while (constraintIterator.getNext(key, value)) {

					// TODO: Make this better. I know this can be better,
					// this is a w f u l.

					// We first obtain a list of the things that are supposed to
					// happen before.
					OpConstraint* priors[100];
					FDUint priorCount = 0;
					for (i = 0; i < count; i++) {
						if (key->first.isAtomMatch(*ops[i].op)) {
							priors[priorCount++] = &ops[i];
						}
					}

					// We then match that list to the things that are supposed to
					// happen after.
					for (i = 0; i < count; i++) {
						if (!key->second.isAtomMatch(*ops[i].op)) {
							continue;
						}

						for (FDUint j = 0; j < priorCount; j++) {

							// We check that i isn't already dependent on j.
							if (ops[i].constraints.contains(priors[j])) {
								continue;
							}

							ops[i].constraints.append(priors[j]);
						}
					}

				}
			}

			p.close();

			// Go through the children, respecting the constraints.
			FDUint renderCount = 0;
			for (i = 0; i < count; i++) {

				FDUint current = i;
				if (!ops[i].alreadyRendered) {
					constraintRecurse(ops[i], styleTreePositions, 
							styleTreePositionSet, stackAllocator, depth);
				}
			}

			FD_FREE(ops, *allocator);
		}

		Profiler p("FSS Undo", true);

		user->onUndoOperation(doOp);
	}

	// Clean up positions listing
	int positionsToAddCount = styleTreePositionsToAdd.getSize();
	for (int i = 0; i < positionsToAddCount; i++) {
		styleTreePositionSet.remove(styleTreePositionsToAdd[i]);
	}

	// Clean up defToProperty listing
	{
		HashMap<PropertyDef*, void*, HashCollection::pointerHash<PropertyDef>,
				HashCollection::pointerMatch<PropertyDef>>::Iterator 
				propertyIterator(defToProperty);
		PropertyDef* key;
		void* value;
		while (propertyIterator.getNext(key, value)) {
			key->deleteProperty(value, stackAllocator);
		}
	}
}