<commit_before>#include <QSettings>
#include "models/favorite.h"
#include "models/profile.h"
#include "tags/tag.h"
#include "tags/tag-stylist.h"
#include "catch.h"

using Catch::Matchers::Matches;

// Ignore those tests on MacOS because of font difference
// TODO(Bionus): re-enable them and use matching or something to be more resistent
#if !defined(Q_OS_MACOS)

void assertSort(QSettings *settings, const QString &sort, const QStringList &expectedOrder)
{
	settings->setValue("Coloring/Fonts/artists", ",8.25,-1,5,50,0,0,0,0,0");
	settings->setValue("Coloring/Colors/artists", "#aa0000");
	settings->setValue("Coloring/Fonts/generals", ",8.25,-1,5,50,0,0,0,0,0");
	settings->setValue("Coloring/Colors/generals", "#aa0000");
	settings->setValue("Coloring/Fonts/copyrights", ",8.25,-1,5,50,0,0,0,0,0");
	settings->setValue("Coloring/Colors/copyrights", "#aa0000");

	auto tags = QList<Tag>
	{
		Tag("tag3", "general", 3, QStringList()),
		Tag("tag2", "copyright", 2, QStringList()),
		Tag("tag1", "artist", 1, QStringList()),
	};

	TagStylist stylist(new Profile(settings, QList<Favorite>()));
	QStringList actual = stylist.stylished(tags, false, false, sort);

	REQUIRE(actual.count() == expectedOrder.count());

	QString format = "%1";
	QStringList expected;
	for (int i = 0; i < expectedOrder.count(); ++i) {
		const QString &tag = expectedOrder[i];
		REQUIRE_THAT(actual[i].toStdString(), Matches(format.arg(tag).toStdString()));
	}

}

TEST_CASE("TagStylist")
{
	QSettings settings("tests/resources/settings.ini", QSettings::IniFormat);

	SECTION("Basic")
	{
		settings.setValue("Coloring/Fonts/artists", ",8.25,-1,5,50,0,0,0,0,0");
		settings.setValue("Coloring/Colors/artists", "#aa0000");

		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		QList<Favorite> favorites;
		favorites.append(Favorite("tag_other", 50, QDateTime::currentDateTime()));

		TagStylist stylist(new Profile(&settings, favorites));
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Ignored")
	{
		settings.setValue("Coloring/Fonts/ignoreds", ",8.25,-1,5,50,0,0,0,0,0");
		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		Profile pro(&settings, QList<Favorite>());
		pro.addIgnored("tag_text");

		TagStylist stylist(&pro);
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Blacklisted")
	{
		settings.setValue("Coloring/Fonts/blacklisteds", ",8.25,-1,5,50,0,0,0,0,0");
		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		Profile pro(&settings, QList<Favorite>());
		pro.addBlacklistedTag("tag_text");

		TagStylist stylist(&pro);
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Favorite")
	{
		settings.setValue("Coloring/Fonts/favorites", ",8.25,-1,5,50,0,0,0,0,0");
		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		QList<Favorite> favorites;
		favorites.append(Favorite("tag_text", 50, QDateTime::currentDateTime()));

		TagStylist stylist(new Profile(&settings, favorites));
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("KeptForLater")
	{
		settings.setValue("Coloring/Fonts/keptForLater", ",8.25,-1,5,50,0,0,0,0,0");
		settings.setValue("Coloring/Colors/keptForLater", "#aa0000");

		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		QStringList keptForLater = QStringList() << "tag_text";

		TagStylist stylist(new Profile(&settings, QList<Favorite>(), keptForLater));
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("WithCount")
	{
		settings.setValue("Coloring/Fonts/artists", ",8.25,-1,5,50,0,0,0,0,0");
		settings.setValue("Coloring/Colors/artists", "#aa0000");

		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		TagStylist stylist(new Profile(&settings, QList<Favorite>()));
		QString actual = stylist.stylished(QList<Tag>() << tag, true).join("");
		std::string expected = "tag_text \\(123\\)";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Sort")
	{
		SECTION("Name")
		{
			assertSort(&settings, "name", QStringList() << "tag1" << "tag2" << "tag3");
		}

		SECTION("Type")
		{
			assertSort(&settings, "type", QStringList() << "tag2" << "tag1" << "tag3");
		}

		SECTION("Count")
		{
			assertSort(&settings, "count", QStringList() << "tag3" << "tag2" << "tag1");
		}
	}
}

#endif
<commit_msg>Re-enable TagStylist tests on macOS<commit_after>#include <QSettings>
#include "models/favorite.h"
#include "models/profile.h"
#include "tags/tag.h"
#include "tags/tag-stylist.h"
#include "catch.h"

using Catch::Matchers::Matches;

void assertSort(QSettings *settings, const QString &sort, const QStringList &expectedOrder)
{
	settings->setValue("Coloring/Fonts/artists", ",8.25,-1,5,50,0,0,0,0,0");
	settings->setValue("Coloring/Colors/artists", "#aa0000");
	settings->setValue("Coloring/Fonts/generals", ",8.25,-1,5,50,0,0,0,0,0");
	settings->setValue("Coloring/Colors/generals", "#aa0000");
	settings->setValue("Coloring/Fonts/copyrights", ",8.25,-1,5,50,0,0,0,0,0");
	settings->setValue("Coloring/Colors/copyrights", "#aa0000");

	auto tags = QList<Tag>
	{
		Tag("tag3", "general", 3, QStringList()),
		Tag("tag2", "copyright", 2, QStringList()),
		Tag("tag1", "artist", 1, QStringList()),
	};

	TagStylist stylist(new Profile(settings, QList<Favorite>()));
	QStringList actual = stylist.stylished(tags, false, false, sort);

	REQUIRE(actual.count() == expectedOrder.count());

	QString format = "%1";
	QStringList expected;
	for (int i = 0; i < expectedOrder.count(); ++i) {
		const QString &tag = expectedOrder[i];
		REQUIRE_THAT(actual[i].toStdString(), Matches(format.arg(tag).toStdString()));
	}

}

TEST_CASE("TagStylist")
{
	QSettings settings("tests/resources/settings.ini", QSettings::IniFormat);

	SECTION("Basic")
	{
		settings.setValue("Coloring/Fonts/artists", ",8.25,-1,5,50,0,0,0,0,0");
		settings.setValue("Coloring/Colors/artists", "#aa0000");

		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		QList<Favorite> favorites;
		favorites.append(Favorite("tag_other", 50, QDateTime::currentDateTime()));

		TagStylist stylist(new Profile(&settings, favorites));
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Ignored")
	{
		settings.setValue("Coloring/Fonts/ignoreds", ",8.25,-1,5,50,0,0,0,0,0");
		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		Profile pro(&settings, QList<Favorite>());
		pro.addIgnored("tag_text");

		TagStylist stylist(&pro);
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Blacklisted")
	{
		settings.setValue("Coloring/Fonts/blacklisteds", ",8.25,-1,5,50,0,0,0,0,0");
		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		Profile pro(&settings, QList<Favorite>());
		pro.addBlacklistedTag("tag_text");

		TagStylist stylist(&pro);
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Favorite")
	{
		settings.setValue("Coloring/Fonts/favorites", ",8.25,-1,5,50,0,0,0,0,0");
		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		QList<Favorite> favorites;
		favorites.append(Favorite("tag_text", 50, QDateTime::currentDateTime()));

		TagStylist stylist(new Profile(&settings, favorites));
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("KeptForLater")
	{
		settings.setValue("Coloring/Fonts/keptForLater", ",8.25,-1,5,50,0,0,0,0,0");
		settings.setValue("Coloring/Colors/keptForLater", "#aa0000");

		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		QStringList keptForLater = QStringList() << "tag_text";

		TagStylist stylist(new Profile(&settings, QList<Favorite>(), keptForLater));
		QString actual = stylist.stylished(QList<Tag>() << tag).join("");
		std::string expected = "tag_text";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("WithCount")
	{
		settings.setValue("Coloring/Fonts/artists", ",8.25,-1,5,50,0,0,0,0,0");
		settings.setValue("Coloring/Colors/artists", "#aa0000");

		Tag tag("tag_text", "artist", 123, QStringList() << "related1" << "related2" << "related3");

		TagStylist stylist(new Profile(&settings, QList<Favorite>()));
		QString actual = stylist.stylished(QList<Tag>() << tag, true).join("");
		std::string expected = "tag_text \\(123\\)";
		REQUIRE_THAT(actual.toStdString(), Matches(expected));
	}

	SECTION("Sort")
	{
		SECTION("Name")
		{
			assertSort(&settings, "name", QStringList() << "tag1" << "tag2" << "tag3");
		}

		SECTION("Type")
		{
			assertSort(&settings, "type", QStringList() << "tag2" << "tag1" << "tag3");
		}

		SECTION("Count")
		{
			assertSort(&settings, "count", QStringList() << "tag3" << "tag2" << "tag1");
		}
	}
}
<|endoftext|>


<commit_before>//
// Wagon contacts: EMCAL Gustavo.Conesa.Balbastre@cern.ch
//                 PHOS  Yuri.Kharlov@cern.ch
//
AliAnalysisTaskParticleCorrelation *AddTaskCalorimeterQA(TString data, Bool_t kPrintSettings = kFALSE,Bool_t kSimulation = kFALSE,TString outputFile = "", Bool_t oldAOD=kFALSE)
{
  // Creates a PartCorr task for calorimeters performance studies, configures it and adds it to the analysis manager.

  // Get the pointer to the existing analysis manager via the static access method.
  //==============================================================================
  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
  if (!mgr) {
    ::Error("AddTaskPartCorr", "No analysis manager to connect to.");
    return NULL;
  }

  // Check the analysis type using the event handlers connected to the analysis manager.
  //==============================================================================
  if (!mgr->GetInputEventHandler()) {
    ::Error("AddTaskPartCorr", "This task requires an input event handler");
    return NULL;
  }
  TString inputDataType = mgr->GetInputEventHandler()->GetDataType(); // can be "ESD" or "AOD"

  Bool_t kUseKinematics = (mgr->GetMCtruthEventHandler())?kTRUE:kFALSE;

  cout<<"********* ACCESS KINE? "<<kUseKinematics<<endl;

  // Configure analysis
  //===========================================================================

  //Reader
  //For this particular analysis few things done by the reader.
  //Nothing else needs to be set.
  AliCaloTrackReader * reader = 0x0;
  if     (data=="AOD") reader = new AliCaloTrackAODReader();
  else if(data=="ESD") reader = new AliCaloTrackESDReader();
  //reader->SetDebug(10);//10 for lots of messages
  reader->SwitchOnEMCALCells();
  reader->SwitchOnPHOSCells();
  reader->SwitchOnEMCAL();
  reader->SwitchOnPHOS();
  reader->SwitchOnCTS();
  reader->SetEMCALPtMin(0.);
  reader->SetPHOSPtMin (0.);
  reader->SetCTSPtMin  (0.);
  reader->SetZvertexCut(10.);

  if(kUseKinematics){
    if(inputDataType == "ESD"){
      reader->SwitchOnStack();
      reader->SwitchOffAODMCParticles();
    }
    else if(inputDataType == "AOD"){
      reader->SwitchOffStack();
      reader->SwitchOnAODMCParticles();
    }
  }
  //if(!kSimulation) reader->SetFiredTriggerClassName("CINT1B-ABCE-NOPF-ALL");
  reader->SetDeltaAODFileName(""); //Do not create deltaAOD file, this analysis do not create branches.
  reader->SwitchOffWriteDeltaAOD()  ;
  if(oldAOD)         reader->SwitchOnOldAODs();
  if(kPrintSettings) reader->Print("");

  // *** Calorimeters Utils	***
  AliCalorimeterUtils *cu = new AliCalorimeterUtils;
  // Remove clusters close to borders, at least max energy cell is 1 cell away
  cu->SetNumberOfCellsFromEMCALBorder(1);
  cu->SetNumberOfCellsFromPHOSBorder(2);
  cu->SetEMCALGeometryName("EMCAL_COMPLETEV1");
  // Remove EMCAL hottest channels for first LHC10 periods
  cu->SwitchOnBadChannelsRemoval();
  // SM0
  //cu->SetEMCALChannelStatus(0,3,13);  cu->SetEMCALChannelStatus(0,44,1); cu->SetEMCALChannelStatus(0,3,13);
  //cu->SetEMCALChannelStatus(0,20,7);  cu->SetEMCALChannelStatus(0,38,2);
  // SM1
  //cu->SetEMCALChannelStatus(1,4,7);   cu->SetEMCALChannelStatus(1,4,13);  cu->SetEMCALChannelStatus(1,9,20);
  //cu->SetEMCALChannelStatus(1,14,15); cu->SetEMCALChannelStatus(1,23,16); cu->SetEMCALChannelStatus(1,32,23);
  //cu->SetEMCALChannelStatus(1,37,5);  cu->SetEMCALChannelStatus(1,40,1);  cu->SetEMCALChannelStatus(1,40,2);
  //cu->SetEMCALChannelStatus(1,40,5);  cu->SetEMCALChannelStatus(1,41,0);  cu->SetEMCALChannelStatus(1,41,1);
  //cu->SetEMCALChannelStatus(1,41,2);  cu->SetEMCALChannelStatus(1,41,4);
  // SM2
  //cu->SetEMCALChannelStatus(2,14,15); cu->SetEMCALChannelStatus(2,18,16); cu->SetEMCALChannelStatus(2,18,17);
  //cu->SetEMCALChannelStatus(2,18,18); cu->SetEMCALChannelStatus(2,18,20); cu->SetEMCALChannelStatus(2,18,21);
  //cu->SetEMCALChannelStatus(2,18,23); cu->SetEMCALChannelStatus(2,19,16); cu->SetEMCALChannelStatus(2,19,17);
  //cu->SetEMCALChannelStatus(2,19,19); cu->SetEMCALChannelStatus(2,19,20); cu->SetEMCALChannelStatus(2,19,21);
  //cu->SetEMCALChannelStatus(2,19,22);
  //SM3
  //cu->SetEMCALChannelStatus(3,4,7);

  //Recalibration
  //cu->SwitchOnRecalibration();
  //TFile * f = new TFile("RecalibrationFactors.root","read");
  //cu->SetEMCALChannelRecalibrationFactors(0,(TH2F*)f->Get("EMCALRecalFactors_SM0"));
  //cu->SetEMCALChannelRecalibrationFactors(1,(TH2F*)f->Get("EMCALRecalFactors_SM1"));
  //cu->SetEMCALChannelRecalibrationFactors(2,(TH2F*)f->Get("EMCALRecalFactors_SM2"));
  //cu->SetEMCALChannelRecalibrationFactors(3,(TH2F*)f->Get("EMCALRecalFactors_SM3"));

  cu->SetDebug(-1);
  if(kPrintSettings) cu->Print("");

  // ##### Analysis algorithm settings ####
  //AliFiducialCut * fidCut = new AliFiducialCut();
  //fidCut->DoCTSFiducialCut(kFALSE) ;
  //fidCut->DoEMCALFiducialCut(kTRUE) ;
  //fidCut->DoPHOSFiducialCut(kTRUE) ;

  AliAnaCalorimeterQA *emcalQA = new AliAnaCalorimeterQA();
  //emcalQA->SetDebug(10); //10 for lots of messages
  emcalQA->SetCalorimeter("EMCAL");
  if(kUseKinematics) emcalQA->SwitchOnDataMC() ;//Access MC stack and fill more histograms, AOD MC not implemented yet.
  else  emcalQA->SwitchOffDataMC() ;
  emcalQA->AddToHistogramsName("EMCAL_"); //Begining of histograms name
  //emcalQA->SetFiducialCut(fidCut);
  emcalQA->SwitchOffFiducialCut();
  emcalQA->SwitchOffPlotsMaking();
  emcalQA->SwitchOnCorrelation();
//  if(!kUseKinematics)emcalQA->SetTimeCut(400,850);//Open for the moment
  //Set Histrograms bins and ranges
  emcalQA->SetHistoPtRangeAndNBins(0, 50, 200) ;
  emcalQA->SetHistoFinePtRangeAndNBins(0, 10, 200) ; // bining for fhAmpId
  emcalQA->SetHistoPhiRangeAndNBins(79*TMath::DegToRad(), 181*TMath::DegToRad(), 200) ;
  emcalQA->SetHistoEtaRangeAndNBins(-0.71, 0.71, 200) ;
  emcalQA->SetNumberOfModules(10);
  emcalQA->SetHistoMassRangeAndNBins(0., 1, 400) ;
  emcalQA->SetHistoAsymmetryRangeAndNBins(0., 1. , 10 );
  emcalQA->SetHistoPOverERangeAndNBins(0,10.,100);
  emcalQA->SetHistodEdxRangeAndNBins(0.,200.,200);
  emcalQA->SetHistodRRangeAndNBins(0.,TMath::Pi(),150);
  emcalQA->SetHistoTimeRangeAndNBins(300.,900,300);
  emcalQA->SetHistoRatioRangeAndNBins(0.,2.,100);
  emcalQA->SetHistoVertexDistRangeAndNBins(0.,500.,500);
  emcalQA->SetHistoNClusterCellRangeAndNBins(0,500,500);
  emcalQA->SetHistoXRangeAndNBins(-230,90,120);
  emcalQA->SetHistoYRangeAndNBins(370,450,40);
  emcalQA->SetHistoZRangeAndNBins(-400,400,200);
  emcalQA->SetHistoRRangeAndNBins(400,450,25);
  emcalQA->SetHistoV0SignalRangeAndNBins(0,5000,500);
  emcalQA->SetHistoV0MultiplicityRangeAndNBins(0,5000,500);
  emcalQA->SetHistoTrackMultiplicityRangeAndNBins(0,5000,500);
  //emcalQA->GetMCAnalysisUtils()->SetDebug(10);

  if(kPrintSettings) emcalQA->Print("");

  AliAnaCalorimeterQA *phosQA = new AliAnaCalorimeterQA();
  //phosQA->SetDebug(2); //10 for lots of messages
  phosQA->SetCalorimeter("PHOS");
  if(kUseKinematics) phosQA->SwitchOnDataMC() ;//Access MC stack and fill more histograms, AOD MC not implemented yet.
  else  phosQA->SwitchOffDataMC() ;
  phosQA->AddToHistogramsName("PHOS_");//Begining of histograms name
  //phosQA->SetFiducialCut(fidCut);
  phosQA->SwitchOffFiducialCut();
  //phosQA->GetMCAnalysisUtils()->SetDebug(10);
  phosQA->SwitchOffPlotsMaking();
  //Set Histrograms bins and ranges
  phosQA->SetHistoPtRangeAndNBins(0, 50, 200) ;
  phosQA->SetHistoFinePtRangeAndNBins(0, 10, 200) ; // bining for fhAmpId
  phosQA->SetHistoPhiRangeAndNBins(259*TMath::DegToRad(), 321*TMath::DegToRad(), 130) ;
  phosQA->SetHistoEtaRangeAndNBins(-0.125, 0.125, 57) ;
  phosQA->SetNumberOfModules(3); //PHOS first year
  phosQA->SetHistoMassRangeAndNBins(0., 1., 400) ;
  phosQA->SetHistoAsymmetryRangeAndNBins(0., 1. , 10) ;
  phosQA->SetHistoPOverERangeAndNBins(0,10.,100);
  phosQA->SetHistodEdxRangeAndNBins(0.,200.,200);
  phosQA->SetHistodRRangeAndNBins(0.,TMath::Pi(),150);
  phosQA->SetHistoTimeRangeAndNBins(0.,300,300);
  phosQA->SetHistoRatioRangeAndNBins(0.,2.,100);
  phosQA->SetHistoVertexDistRangeAndNBins(0.,500.,500);
  phosQA->SetHistoNClusterCellRangeAndNBins(0,500,500);
  phosQA->SetHistoXRangeAndNBins(-100,400,100);
  phosQA->SetHistoYRangeAndNBins(-490,-290,100);
  phosQA->SetHistoZRangeAndNBins(-80,80,100);
  phosQA->SetHistoRRangeAndNBins(440,480,80);
  phosQA->SetHistoV0SignalRangeAndNBins(0,5000,500);
  phosQA->SetHistoV0MultiplicityRangeAndNBins(0,5000,500);
  phosQA->SetHistoTrackMultiplicityRangeAndNBins(0,5000,500);

  // #### Configure Maker ####
  AliAnaPartCorrMaker * maker = new AliAnaPartCorrMaker();
  maker->SetReader(reader);//pointer to reader
  maker->SetCaloUtils(cu); //pointer to calorimeter utils
  maker->AddAnalysis(emcalQA,0);
  maker->AddAnalysis(phosQA,1);
  maker->SetAnaDebug(-1)  ; // 0 to at least print the event number
  maker->SwitchOnHistogramsMaker()  ;
  maker->SwitchOffAODsMaker()  ;
  if(kPrintSettings) maker->Print("");

  printf("======================== \n");
  printf(" End Configuration of Calorimeter QA \n");
  printf("======================== \n");

  // Create task
  //===========================================================================
  AliAnalysisTaskParticleCorrelation * task = new AliAnalysisTaskParticleCorrelation ("CalorimeterPerformance");
  task->SetConfigFileName(""); //Don't configure the analysis via configuration file.
  //task->SetDebugLevel(-1);
  task->SelectCollisionCandidates();
  task->SetAnalysisMaker(maker);
  mgr->AddTask(task);

  //Create containers
  //  AliAnalysisDataContainer *cout_pc = mgr->CreateContainer("Calo.Performance",TList::Class(),
  //							   AliAnalysisManager::kOutputContainer, "Calo.Performance.root");

  if(outputFile.Length()==0)outputFile = AliAnalysisManager::GetCommonFileName();

  AliAnalysisDataContainer *cout_pc   = mgr->CreateContainer("CaloQA", TList::Class(),
                                                             AliAnalysisManager::kOutputContainer,
                                                             Form("%s:CaloQA",outputFile.Data()));

  AliAnalysisDataContainer *cout_cuts = mgr->CreateContainer("CaloQACuts", TList::Class(),
                                                             AliAnalysisManager::kParamContainer,
                                                             Form("%s:CaloQACuts",outputFile.Data()));
	//Form("%s:PartCorrCuts",outputfile.Data()));
  // Create ONLY the output containers for the data produced by the task.
  // Get and connect other common input/output containers via the manager as below
  //==============================================================================
  mgr->ConnectInput  (task, 0, mgr->GetCommonInputContainer());
  mgr->ConnectOutput (task, 1, cout_pc);
  mgr->ConnectOutput (task, 2, cout_cuts);

  return task;
}

<commit_msg>prepare different configuration for 2010 and 2011<commit_after>//
// Wagon contacts: EMCAL Gustavo.Conesa.Balbastre@cern.ch
//                 PHOS  Yuri.Kharlov@cern.ch
//
AliAnalysisTaskParticleCorrelation *AddTaskCalorimeterQA(TString data, Int_t year = 2011, Bool_t kPrintSettings = kFALSE,Bool_t kSimulation = kFALSE,TString outputFile = "", Bool_t oldAOD=kFALSE)
{
  // Creates a PartCorr task for calorimeters performance studies, configures it and adds it to the analysis manager.

  // Get the pointer to the existing analysis manager via the static access method.
  //==============================================================================
  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
  if (!mgr) {
    ::Error("AddTaskPartCorr", "No analysis manager to connect to.");
    return NULL;
  }

  // Check the analysis type using the event handlers connected to the analysis manager.
  //==============================================================================
  if (!mgr->GetInputEventHandler()) {
    ::Error("AddTaskPartCorr", "This task requires an input event handler");
    return NULL;
  }
  TString inputDataType = mgr->GetInputEventHandler()->GetDataType(); // can be "ESD" or "AOD"

  Bool_t kUseKinematics = (mgr->GetMCtruthEventHandler())?kTRUE:kFALSE;

  cout<<"********* ACCESS KINE? "<<kUseKinematics<<endl;

  // Configure analysis
  //===========================================================================

  //Reader
  //For this particular analysis few things done by the reader.
  //Nothing else needs to be set.
  AliCaloTrackReader * reader = 0x0;
  if     (data=="AOD") reader = new AliCaloTrackAODReader();
  else if(data=="ESD") reader = new AliCaloTrackESDReader();
  //reader->SetDebug(10);//10 for lots of messages
  reader->SwitchOnEMCALCells();
  reader->SwitchOnPHOSCells();
  reader->SwitchOnEMCAL();
  reader->SwitchOnPHOS();
  reader->SwitchOnCTS();
  reader->SetEMCALPtMin(0.);
  reader->SetPHOSPtMin (0.);
  reader->SetCTSPtMin  (0.);
  reader->SetZvertexCut(10.);

  if(kUseKinematics){
    if(inputDataType == "ESD"){
      reader->SwitchOnStack();
      reader->SwitchOffAODMCParticles();
    }
    else if(inputDataType == "AOD"){
      reader->SwitchOffStack();
      reader->SwitchOnAODMCParticles();
    }
  }
  //if(!kSimulation) reader->SetFiredTriggerClassName("CINT1B-ABCE-NOPF-ALL");
  reader->SetDeltaAODFileName(""); //Do not create deltaAOD file, this analysis do not create branches.
  reader->SwitchOffWriteDeltaAOD()  ;
  if(oldAOD)         reader->SwitchOnOldAODs();
  if(kPrintSettings) reader->Print("");

  // *** Calorimeters Utils	***
  AliCalorimeterUtils *cu = new AliCalorimeterUtils;
  // Remove clusters close to borders, at least max energy cell is 1 cell away
  cu->SetNumberOfCellsFromEMCALBorder(1);
  cu->SetNumberOfCellsFromPHOSBorder(2);
  if(year==2010){
    cu->SetEMCALGeometryName("EMCAL_FIRSTYEARV1");
    cu->SwitchOnBadChannelsRemoval(); // just a trick
  }
  else {
    cu->SetEMCALGeometryName("EMCAL_COMPLETEV1");
    // Remove EMCAL hottest channels for LHC11 periods
    cu->SwitchOnBadChannelsRemoval();

    // SM0
    cu->SetEMCALChannelStatus(0,12,16);  cu->SetEMCALChannelStatus(0,12,17); cu->SetEMCALChannelStatus(0,12,18);
    cu->SetEMCALChannelStatus(0,12,19);  cu->SetEMCALChannelStatus(0,12,20); cu->SetEMCALChannelStatus(0,12,21);
    cu->SetEMCALChannelStatus(0,12,22);  cu->SetEMCALChannelStatus(0,12,23);

    cu->SetEMCALChannelStatus(0,13,16);  cu->SetEMCALChannelStatus(0,13,17); cu->SetEMCALChannelStatus(0,13,18);
    cu->SetEMCALChannelStatus(0,13,19);  cu->SetEMCALChannelStatus(0,13,20); cu->SetEMCALChannelStatus(0,13,21);
    cu->SetEMCALChannelStatus(0,13,22);  cu->SetEMCALChannelStatus(0,13,23);

    cu->SetEMCALChannelStatus(0,14,16);  cu->SetEMCALChannelStatus(0,14,17); cu->SetEMCALChannelStatus(0,14,18);
    cu->SetEMCALChannelStatus(0,14,19);  cu->SetEMCALChannelStatus(0,14,20); cu->SetEMCALChannelStatus(0,14,21);
    cu->SetEMCALChannelStatus(0,14,22);  cu->SetEMCALChannelStatus(0,14,23);

    cu->SetEMCALChannelStatus(0,15,16);  cu->SetEMCALChannelStatus(0,15,17); cu->SetEMCALChannelStatus(0,15,18);
    cu->SetEMCALChannelStatus(0,15,19);  cu->SetEMCALChannelStatus(0,15,20); cu->SetEMCALChannelStatus(0,15,21);
    cu->SetEMCALChannelStatus(0,15,22);  cu->SetEMCALChannelStatus(0,15,23);

    // SM1
    cu->SetEMCALChannelStatus(1,4,13);   cu->SetEMCALChannelStatus(1,14,15); cu->SetEMCALChannelStatus(1,29,18);
    cu->SetEMCALChannelStatus(1,36,15);  cu->SetEMCALChannelStatus(1,40,2);  cu->SetEMCALChannelStatus(1,47,21);

    // SM5
    cu->SetEMCALChannelStatus(5,12,23);  cu->SetEMCALChannelStatus(5,14,7);   cu->SetEMCALChannelStatus(5,35,8);

    cu->SetEMCALChannelStatus(5,42,16);  cu->SetEMCALChannelStatus(5,42,17);  cu->SetEMCALChannelStatus(5,42,18);
    cu->SetEMCALChannelStatus(5,42,19);  cu->SetEMCALChannelStatus(5,42,20);  cu->SetEMCALChannelStatus(5,42,21);
    cu->SetEMCALChannelStatus(5,42,22);  cu->SetEMCALChannelStatus(5,42,23);

    cu->SetEMCALChannelStatus(5,43,16);  cu->SetEMCALChannelStatus(5,43,17);  cu->SetEMCALChannelStatus(5,43,18);
    cu->SetEMCALChannelStatus(5,43,19);  cu->SetEMCALChannelStatus(5,43,20);  cu->SetEMCALChannelStatus(5,43,21);
    cu->SetEMCALChannelStatus(5,43,22);  cu->SetEMCALChannelStatus(5,43,23);

    //SM6
    cu->SetEMCALChannelStatus(6,24,1);   cu->SetEMCALChannelStatus(6,32,14);

    //SM7
    cu->SetEMCALChannelStatus(7,12,0);   cu->SetEMCALChannelStatus(7,12,2);
    cu->SetEMCALChannelStatus(7,13,0);   cu->SetEMCALChannelStatus(7,13,2);
    cu->SetEMCALChannelStatus(7,31,12);  cu->SetEMCALChannelStatus(7,31,13);
    cu->SetEMCALChannelStatus(7,31,14);  cu->SetEMCALChannelStatus(7,31,15);

    //SM8
    cu->SetEMCALChannelStatus(8,24,11);

  }

  cu->SetDebug(-1);
  if(kPrintSettings) cu->Print("");

  // ##### Analysis algorithm settings ####

  AliAnaCalorimeterQA *emcalQA = new AliAnaCalorimeterQA();
  //emcalQA->SetDebug(10); //10 for lots of messages
  emcalQA->SetCalorimeter("EMCAL");
  if(kUseKinematics) emcalQA->SwitchOnDataMC() ;//Access MC stack and fill more histograms, AOD MC not implemented yet.
  else  emcalQA->SwitchOffDataMC() ;
  emcalQA->AddToHistogramsName("EMCAL_"); //Begining of histograms name
  //emcalQA->SetFiducialCut(fidCut);
  emcalQA->SwitchOffFiducialCut();
  emcalQA->SwitchOffPlotsMaking();
  emcalQA->SwitchOnCorrelation();
//  if(!kUseKinematics)emcalQA->SetTimeCut(400,850);//Open for the moment
  //Set Histrograms bins and ranges
  emcalQA->SetHistoPtRangeAndNBins(0, 50, 200) ;
  emcalQA->SetHistoFinePtRangeAndNBins(0, 10, 200) ; // bining for fhAmpId
  emcalQA->SetHistoEtaRangeAndNBins(-0.71, 0.71, 200) ;

  if(year==2010){
    emcalQA->SetNumberOfModules(4);
    emcalQA->SetHistoPhiRangeAndNBins(79*TMath::DegToRad(), 121*TMath::DegToRad(), 200) ;
    emcalQA->SetHistoXRangeAndNBins(-230,90,120);
    emcalQA->SetHistoYRangeAndNBins(370,450,40);
  }
  else{
    emcalQA->SetNumberOfModules(10);
    emcalQA->SetHistoPhiRangeAndNBins(79*TMath::DegToRad(), 181*TMath::DegToRad(), 200) ;
    emcalQA->SetHistoXRangeAndNBins(-600,90,200);
    emcalQA->SetHistoYRangeAndNBins(100,450,100);
  }

  emcalQA->SetHistoMassRangeAndNBins(0., 1, 400) ;
  emcalQA->SetHistoAsymmetryRangeAndNBins(0., 1. , 10 );
  emcalQA->SetHistoPOverERangeAndNBins(0,10.,100);
  emcalQA->SetHistodEdxRangeAndNBins(0.,200.,200);
  emcalQA->SetHistodRRangeAndNBins(0.,TMath::Pi(),150);
  emcalQA->SetHistoTimeRangeAndNBins(300.,900,300);
  emcalQA->SetHistoRatioRangeAndNBins(0.,2.,100);
  emcalQA->SetHistoVertexDistRangeAndNBins(0.,500.,500);
  emcalQA->SetHistoNClusterCellRangeAndNBins(0,500,500);
  emcalQA->SetHistoZRangeAndNBins(-400,400,200);
  emcalQA->SetHistoRRangeAndNBins(400,450,25);
  emcalQA->SetHistoV0SignalRangeAndNBins(0,5000,500);
  emcalQA->SetHistoV0MultiplicityRangeAndNBins(0,5000,500);
  emcalQA->SetHistoTrackMultiplicityRangeAndNBins(0,5000,500);
  //emcalQA->GetMCAnalysisUtils()->SetDebug(10);

  if(kPrintSettings) emcalQA->Print("");

  AliAnaCalorimeterQA *phosQA = new AliAnaCalorimeterQA();
  //phosQA->SetDebug(2); //10 for lots of messages
  phosQA->SetCalorimeter("PHOS");
  if(kUseKinematics) phosQA->SwitchOnDataMC() ;//Access MC stack and fill more histograms, AOD MC not implemented yet.
  else  phosQA->SwitchOffDataMC() ;
  phosQA->AddToHistogramsName("PHOS_");//Begining of histograms name
  //phosQA->SetFiducialCut(fidCut);
  phosQA->SwitchOffFiducialCut();
  //phosQA->GetMCAnalysisUtils()->SetDebug(10);
  phosQA->SwitchOffPlotsMaking();
  //Set Histrograms bins and ranges
  phosQA->SetHistoPtRangeAndNBins(0, 50, 200) ;
  phosQA->SetHistoFinePtRangeAndNBins(0, 10, 200) ; // bining for fhAmpId
  phosQA->SetHistoPhiRangeAndNBins(259*TMath::DegToRad(), 321*TMath::DegToRad(), 130) ;
  phosQA->SetHistoEtaRangeAndNBins(-0.125, 0.125, 57) ;
  phosQA->SetNumberOfModules(3); //PHOS first year
  phosQA->SetHistoMassRangeAndNBins(0., 1., 400) ;
  phosQA->SetHistoAsymmetryRangeAndNBins(0., 1. , 10) ;
  phosQA->SetHistoPOverERangeAndNBins(0,10.,100);
  phosQA->SetHistodEdxRangeAndNBins(0.,200.,200);
  phosQA->SetHistodRRangeAndNBins(0.,TMath::Pi(),150);
  phosQA->SetHistoTimeRangeAndNBins(0.,300,300);
  phosQA->SetHistoRatioRangeAndNBins(0.,2.,100);
  phosQA->SetHistoVertexDistRangeAndNBins(0.,500.,500);
  phosQA->SetHistoNClusterCellRangeAndNBins(0,500,500);
  phosQA->SetHistoXRangeAndNBins(-100,400,100);
  phosQA->SetHistoYRangeAndNBins(-490,-290,100);
  phosQA->SetHistoZRangeAndNBins(-80,80,100);
  phosQA->SetHistoRRangeAndNBins(440,480,80);
  phosQA->SetHistoV0SignalRangeAndNBins(0,5000,500);
  phosQA->SetHistoV0MultiplicityRangeAndNBins(0,5000,500);
  phosQA->SetHistoTrackMultiplicityRangeAndNBins(0,5000,500);

  // #### Configure Maker ####
  AliAnaPartCorrMaker * maker = new AliAnaPartCorrMaker();
  maker->SetReader(reader);//pointer to reader
  maker->SetCaloUtils(cu); //pointer to calorimeter utils
  maker->AddAnalysis(emcalQA,0);
  maker->AddAnalysis(phosQA,1);
  maker->SetAnaDebug(-1)  ; // 0 to at least print the event number
  maker->SwitchOnHistogramsMaker()  ;
  maker->SwitchOffAODsMaker()  ;
  if(kPrintSettings) maker->Print("");

  printf("======================== \n");
  printf(" End Configuration of Calorimeter QA \n");
  printf("======================== \n");

  // Create task
  //===========================================================================
  AliAnalysisTaskParticleCorrelation * task = new AliAnalysisTaskParticleCorrelation ("CalorimeterPerformance");
  task->SetConfigFileName(""); //Don't configure the analysis via configuration file.
  //task->SetDebugLevel(-1);
  task->SelectCollisionCandidates();
  task->SetAnalysisMaker(maker);
  mgr->AddTask(task);

  //Create containers
  //  AliAnalysisDataContainer *cout_pc = mgr->CreateContainer("Calo.Performance",TList::Class(),
  //							   AliAnalysisManager::kOutputContainer, "Calo.Performance.root");

  if(outputFile.Length()==0)outputFile = AliAnalysisManager::GetCommonFileName();

  AliAnalysisDataContainer *cout_pc   = mgr->CreateContainer("CaloQA", TList::Class(),
                                                             AliAnalysisManager::kOutputContainer,
                                                             Form("%s:CaloQA",outputFile.Data()));

  AliAnalysisDataContainer *cout_cuts = mgr->CreateContainer("CaloQACuts", TList::Class(),
                                                             AliAnalysisManager::kParamContainer,
                                                             Form("%s:CaloQACuts",outputFile.Data()));
	//Form("%s:PartCorrCuts",outputfile.Data()));
  // Create ONLY the output containers for the data produced by the task.
  // Get and connect other common input/output containers via the manager as below
  //==============================================================================
  mgr->ConnectInput  (task, 0, mgr->GetCommonInputContainer());
  mgr->ConnectOutput (task, 1, cout_pc);
  mgr->ConnectOutput (task, 2, cout_cuts);

  return task;
}

<|endoftext|>


<commit_before>/*
 * Android File Transfer for Linux: MTP client for android devices
 * Copyright (C) 2015  Vladimir Menshakov

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <stdexcept>

#include <usb/Context.h>
#include <mtp/ptp/Container.h>
#include <mtp/ptp/Device.h>
#include <mtp/ptp/Messages.h>
#include <mtp/ptp/IObjectStream.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

namespace
{

	class ObjectInputStream : public mtp::IObjectInputStream
	{
		int		_fd;
		size_t	_size;

	public:
		ObjectInputStream(const std::string &fname) : _fd(open(fname.c_str(), O_RDONLY))
		{
			if (_fd < 0)
			{
				perror("open");
				throw std::runtime_error("cannot open file: " + fname);
			}
			struct stat st;
			if (stat(fname.c_str(), &st) != 0)
				throw std::runtime_error("stat failed");
			_size = st.st_size;
		}

		~ObjectInputStream()
		{ close(_fd); }

		size_t GetSize() const
		{ return _size; }

		virtual size_t Read(mtp::u8 *data, size_t size)
		{
			ssize_t r = read(_fd, data, size);
			if (r < 0)
				throw std::runtime_error("read failed");
			return r;
		}
	};

	class ObjectOutputStream : public mtp::IObjectOutputStream
	{
		int		_fd;

	public:
		ObjectOutputStream(const std::string &fname) : _fd(open(fname.c_str(), O_WRONLY | O_CREAT | O_TRUNC))
		{
			if (_fd < 0)
			{
				perror("open");
				throw std::runtime_error("cannot open file: " + fname);
			}
		}

		~ObjectOutputStream()
		{ close(_fd); }

		virtual size_t Write(const mtp::u8 *data, size_t size)
		{
			ssize_t r = write(_fd, data, size);
			if (r < 0)
				throw std::runtime_error("write failed");
			return r;
		}
	};

}

int main(int argc, char **argv)
{
	using namespace mtp;

	DevicePtr mtp(Device::Find());
	if (!mtp)
	{
		printf("no mtp device found\n");
		return 1;
	}

	msg::DeviceInfo gdi = mtp->GetDeviceInfo();
	printf("%s\n", gdi.VendorExtensionDesc.c_str());
	printf("%s ", gdi.Manufactorer.c_str());
	printf("%s ", gdi.Model.c_str());
	printf("%s ", gdi.DeviceVersion.c_str());
	printf("%s\n", gdi.SerialNumber.c_str());
	printf("supported op codes: ");
	for(u16 code : gdi.OperationsSupported)
	{
		printf("%04x ", (unsigned)code);
	}
	printf("\n");
	printf("supported properties: ");
	for(u16 code : gdi.DevicePropertiesSupported)
	{
		printf("%04x ", (unsigned)code);
	}
	printf("\n");

	if (argc < 2)
		return 0;

	SessionPtr session = mtp->OpenSession(1);
	std::string command = argv[1];
	if (command == "list")
	{
		mtp::u32 parent = mtp::Session::Root;
		if (argc > 2)
			if (sscanf(argv[2], "%u", &parent) != 1)
				return 1;

		msg::ObjectHandles handles = session->GetObjectHandles(mtp::Session::AllStorages, mtp::Session::AllFormats, parent);

		for(u32 objectId : handles.ObjectHandles)
		{
			try
			{
				msg::ObjectInfo info = session->GetObjectInfo(objectId);
				printf("%-10u %04x %s %u %ux%u, %s\n", objectId, info.ObjectFormat, info.Filename.c_str(), info.ObjectCompressedSize, info.ImagePixWidth, info.ImagePixHeight, info.CaptureDate.c_str());
			}
			catch(const std::exception &ex)
			{
				printf("error: %s\n", ex.what());
			}
		}
		//libusb_release_interface(device->GetHandle(), interface->GetIndex());
	}
	else if (command == "get")
	{
		if (argc < 3)
			return 1;
		mtp::u32 objectId;
		if (sscanf(argv[2], "%u", &objectId) != 1)
			return 1;
		printf("object id = %u\n", objectId);
		msg::ObjectInfo info = session->GetObjectInfo(objectId);
		printf("filename = %s\n", info.Filename.c_str());

		session->GetObject(objectId, std::make_shared<ObjectOutputStream>(info.Filename));
	}
	else if (command == "set")
	{
		if (argc < 4)
			return 1;

		mtp::u32 parentObjectId;
		if (sscanf(argv[2], "%u", &parentObjectId) != 1)
			return 1;

		std::string filename(argv[3]);
		printf("uploading %s to %u\n", filename.c_str(), parentObjectId);

		msg::ObjectInfo oi;
		oi.Filename = filename;
		oi.ObjectFormat = ObjectFormatFromFilename(filename);

		std::shared_ptr<ObjectInputStream> objectInput(new ObjectInputStream(filename));
		oi.ObjectCompressedSize = objectInput->GetSize();

		Session::NewObjectInfo noi = session->SendObjectInfo(oi, 0, parentObjectId);
		printf("new object id = %u\n", noi.ObjectId);
		session->SendObject(objectInput);
		printf("done\n");
	}
	else if (command == "delete")
	{
		if (argc < 3)
			return 1;

		mtp::u32 objectId;
		if (sscanf(argv[2], "%u", &objectId) != 1)
			return 1;

		printf("object id = %u\n", objectId);
		session->DeleteObject(objectId);
	}
	else if (command == "mkdir")
	{
		if (argc < 3)
			return 1;

		mtp::u32 parentObjectId = mtp::Session::Root;
		if (argc > 3 && sscanf(argv[3], "%u", &parentObjectId) != 1)
			return 1;

		std::string filename = argv[2];
		msg::ObjectInfo oi;
		oi.Filename = filename;
		oi.ObjectFormat = ObjectFormat::Association;
		session->SendObjectInfo(oi, 0, parentObjectId);
	}
	else if (command == "properties")
	{
		if (argc < 3)
			return 1;

		mtp::u32 objectId;
		if (sscanf(argv[2], "%u", &objectId) != 1)
			return 1;
		msg::ObjectPropsSupported ops = session->GetObjectPropsSupported(objectId);
		printf("properties supported: ");
		for(u16 prop: ops.ObjectPropCodes)
		{
			printf("%02x ", prop);
		}
		printf("\n");
	}

	return 0;
}
<commit_msg>added missing mode<commit_after>/*
 * Android File Transfer for Linux: MTP client for android devices
 * Copyright (C) 2015  Vladimir Menshakov

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdio.h>
#include <stdexcept>

#include <usb/Context.h>
#include <mtp/ptp/Container.h>
#include <mtp/ptp/Device.h>
#include <mtp/ptp/Messages.h>
#include <mtp/ptp/IObjectStream.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

namespace
{

	class ObjectInputStream : public mtp::IObjectInputStream
	{
		int		_fd;
		size_t	_size;

	public:
		ObjectInputStream(const std::string &fname) : _fd(open(fname.c_str(), O_RDONLY))
		{
			if (_fd < 0)
			{
				perror("open");
				throw std::runtime_error("cannot open file: " + fname);
			}
			struct stat st;
			if (stat(fname.c_str(), &st) != 0)
				throw std::runtime_error("stat failed");
			_size = st.st_size;
		}

		~ObjectInputStream()
		{ close(_fd); }

		size_t GetSize() const
		{ return _size; }

		virtual size_t Read(mtp::u8 *data, size_t size)
		{
			ssize_t r = read(_fd, data, size);
			if (r < 0)
				throw std::runtime_error("read failed");
			return r;
		}
	};

	class ObjectOutputStream : public mtp::IObjectOutputStream
	{
		int		_fd;

	public:
		ObjectOutputStream(const std::string &fname) : _fd(open(fname.c_str(), O_WRONLY | O_CREAT | O_TRUNC), 0644)
		{
			if (_fd < 0)
			{
				perror("open");
				throw std::runtime_error("cannot open file: " + fname);
			}
		}

		~ObjectOutputStream()
		{ close(_fd); }

		virtual size_t Write(const mtp::u8 *data, size_t size)
		{
			ssize_t r = write(_fd, data, size);
			if (r < 0)
				throw std::runtime_error("write failed");
			return r;
		}
	};

}

int main(int argc, char **argv)
{
	using namespace mtp;

	DevicePtr mtp(Device::Find());
	if (!mtp)
	{
		printf("no mtp device found\n");
		return 1;
	}

	msg::DeviceInfo gdi = mtp->GetDeviceInfo();
	printf("%s\n", gdi.VendorExtensionDesc.c_str());
	printf("%s ", gdi.Manufactorer.c_str());
	printf("%s ", gdi.Model.c_str());
	printf("%s ", gdi.DeviceVersion.c_str());
	printf("%s\n", gdi.SerialNumber.c_str());
	printf("supported op codes: ");
	for(u16 code : gdi.OperationsSupported)
	{
		printf("%04x ", (unsigned)code);
	}
	printf("\n");
	printf("supported properties: ");
	for(u16 code : gdi.DevicePropertiesSupported)
	{
		printf("%04x ", (unsigned)code);
	}
	printf("\n");

	if (argc < 2)
		return 0;

	SessionPtr session = mtp->OpenSession(1);
	std::string command = argv[1];
	if (command == "list")
	{
		mtp::u32 parent = mtp::Session::Root;
		if (argc > 2)
			if (sscanf(argv[2], "%u", &parent) != 1)
				return 1;

		msg::ObjectHandles handles = session->GetObjectHandles(mtp::Session::AllStorages, mtp::Session::AllFormats, parent);

		for(u32 objectId : handles.ObjectHandles)
		{
			try
			{
				msg::ObjectInfo info = session->GetObjectInfo(objectId);
				printf("%-10u %04x %s %u %ux%u, %s\n", objectId, info.ObjectFormat, info.Filename.c_str(), info.ObjectCompressedSize, info.ImagePixWidth, info.ImagePixHeight, info.CaptureDate.c_str());
			}
			catch(const std::exception &ex)
			{
				printf("error: %s\n", ex.what());
			}
		}
		//libusb_release_interface(device->GetHandle(), interface->GetIndex());
	}
	else if (command == "get")
	{
		if (argc < 3)
			return 1;
		mtp::u32 objectId;
		if (sscanf(argv[2], "%u", &objectId) != 1)
			return 1;
		printf("object id = %u\n", objectId);
		msg::ObjectInfo info = session->GetObjectInfo(objectId);
		printf("filename = %s\n", info.Filename.c_str());

		session->GetObject(objectId, std::make_shared<ObjectOutputStream>(info.Filename));
	}
	else if (command == "set")
	{
		if (argc < 4)
			return 1;

		mtp::u32 parentObjectId;
		if (sscanf(argv[2], "%u", &parentObjectId) != 1)
			return 1;

		std::string filename(argv[3]);
		printf("uploading %s to %u\n", filename.c_str(), parentObjectId);

		msg::ObjectInfo oi;
		oi.Filename = filename;
		oi.ObjectFormat = ObjectFormatFromFilename(filename);

		std::shared_ptr<ObjectInputStream> objectInput(new ObjectInputStream(filename));
		oi.ObjectCompressedSize = objectInput->GetSize();

		Session::NewObjectInfo noi = session->SendObjectInfo(oi, 0, parentObjectId);
		printf("new object id = %u\n", noi.ObjectId);
		session->SendObject(objectInput);
		printf("done\n");
	}
	else if (command == "delete")
	{
		if (argc < 3)
			return 1;

		mtp::u32 objectId;
		if (sscanf(argv[2], "%u", &objectId) != 1)
			return 1;

		printf("object id = %u\n", objectId);
		session->DeleteObject(objectId);
	}
	else if (command == "mkdir")
	{
		if (argc < 3)
			return 1;

		mtp::u32 parentObjectId = mtp::Session::Root;
		if (argc > 3 && sscanf(argv[3], "%u", &parentObjectId) != 1)
			return 1;

		std::string filename = argv[2];
		msg::ObjectInfo oi;
		oi.Filename = filename;
		oi.ObjectFormat = ObjectFormat::Association;
		session->SendObjectInfo(oi, 0, parentObjectId);
	}
	else if (command == "properties")
	{
		if (argc < 3)
			return 1;

		mtp::u32 objectId;
		if (sscanf(argv[2], "%u", &objectId) != 1)
			return 1;
		msg::ObjectPropsSupported ops = session->GetObjectPropsSupported(objectId);
		printf("properties supported: ");
		for(u16 prop: ops.ObjectPropCodes)
		{
			printf("%02x ", prop);
		}
		printf("\n");
	}

	return 0;
}
<|endoftext|>


<commit_before>/*VRP*********************************************************************
 *
 * vehicle routing problems
 *      A collection of C++ classes for developing VRP solutions
 *      and specific solutions developed using these classes.
 *
 * Copyright 2014 Stephen Woodbridge <woodbri@imaptools.com>
 * Copyright 2014 Vicky Vergara <vicky_vergara@hotmail.com>
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the MIT License. Please file LICENSE for details.
 *
 ********************************************************************VRP*/
#include <limits>
#include <stdexcept>
#include <algorithm>
#include <math.h>

#ifdef DOVRPLOG
#include "logger.h"
#endif

#include "prob_trash.h"

// Class functions
#if 0
bool Prob_trash::checkIntegrity() const
{
  bool flag = true;
  int nodesCant = datanodes.size();

  if ( datanodes.empty() ) {
#ifdef DOVRPLOG
    DLOG( INFO ) << "Nodes is empty";
#endif
    flag = false;
  }

#ifdef DOVRPLOG
  else DLOG( INFO ) << "# of Nodes:" << nodesCant;

#endif

  for ( int i = 1; i < nodesCant - 1; i++ ) {
    flag = flag and datanodes[i].isValid();
  }
}
#endif

// DUMPS ********************************************
#ifdef DOVRPLOG
void Prob_trash::nodesdump()
{
  DLOG( INFO ) << "---- Nodes  --------------";

  for ( UINT i = 0; i < datanodes.size(); i++ )
    datanodes[i].dump();
}

#if 0
void Prob_trash::nodesdumpeval()
{
  DLOG( INFO ) << "---- Nodes  Evaluation--------------";

  for ( UINT i = 0; i < datanodes.size(); i++ )
    datanodes[i].dumpeval();
}

void Prob_trash::dump()
{
  DLOG( INFO ) << "---- Problem -------------";
  nodesdump();
  DLOG( INFO ) << "INITIAL EVALUATION";
  nodesdumpeval();

}
#endif

void Prob_trash::dumpdataNodes() const
{
  DLOG( INFO ) << "--------- Nodes ------------";

  for ( UINT i = 0; i < datanodes.size(); i++ )
    datanodes[i].dump();
}

void Prob_trash::dumpDepots() const
{
  DLOG( INFO ) << "--------- Depots ------------";
  depots.dump( "Depots" );

  for ( UINT i = 0; i < depots.size(); i++ )
    depots[i].dump();
}

void Prob_trash::dumpDumps() const
{
  DLOG( INFO ) << "--------- Dumps ------------";
  dumps.dump( "Dumps" );

  for ( UINT i = 0; i < dumps.size(); i++ )
    dumps[i].dump();
}

void Prob_trash::dumpPickups() const
{
  DLOG( INFO ) << "--------- Pickups ------------";
  pickups.dump( "pickups" );

  for ( UINT i = 0; i < pickups.size(); i++ )
    pickups[i].dump();
}
#endif

// PLOTS  ********************************************
#ifdef DOPLOTS
void Prob_trash::plot( Plot<Trashnode> &graph )
{
  for ( int i = 0; i < datanodes.size(); i++ ) {
    if ( datanodes[i].isPickup() )  {
      graph.drawPoint( datanodes[i], 0x0000ff, 9, true );
    } else if ( datanodes[i].isDump() ) {
      graph.drawPoint( datanodes[i], 0x00ff00, 5, true );
    } else  {
      graph.drawPoint( datanodes[i], 0xff0000, 7, true );
    }
  }
};
#endif

Prob_trash::Prob_trash( const char *infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "---- char * Constructor --------------";
#endif
  std::string file = infile;
  loadProblem( file );
}

Prob_trash::Prob_trash( const std::string &infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash---- string Constructor --------------";
#endif
  loadProblem( infile );
}

void Prob_trash::loadProblem( const std::string &infile )
{
  datafile = infile;
  Bucket nodes;
  Bucket intersection;
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash LoadProblem --------------" << datafile <<
               "--------";
#endif

  // read the nodes
  //int cnt = 0;
  //int nid = 0;
  int id = 0;

  twc->emptiedTruck = false;

  load_pickups( datafile + ".containers.txt" );
  load_otherlocs( datafile + ".otherlocs.txt" );

  intersection = otherlocs * pickups;
  invalid = invalid + intersection;
  pickups = pickups - intersection;
  nodes = nodes - intersection;

#ifdef VRPMINTRACE
  invalid.dump( "invalid" );
#endif

  nodes = pickups + otherlocs;
  nodes.push_back( C );

  for ( UINT i = 0; i < nodes.size(); i++ ) {
    nodes[i].set_nid( i );
    id = nodes[i].id();

    if ( pickups.hasId( id ) )
      pickups[ pickups.posFromId( id ) ].set_nid( i );
    else if ( otherlocs.hasId( id ) )
      otherlocs[ otherlocs.posFromId( id ) ].set_nid( i );
  };

  C = nodes.back();
  assert( pickups.size() );
  assert( otherlocs.size() );

  datanodes = nodes;

  twc->loadAndProcess_distance( datafile + ".dmatrix-time.txt", datanodes,
                                invalid );
  load_trucks( datafile + ".vehicles.txt" );

#ifdef OSRMCLIENT
  DLOG(INFO) << "Setting hints";
  twc->setHints( dumps );
  twc->setHints( nodes );
  twc->setHints( depots );
  twc->setHints( pickups );
  twc->setHints( endings );
  twc->settCC( C, pickups );
#endif  // OSRMCLIENT

  assert( trucks.size() and depots.size() and dumps.size() and endings.size() );

  for ( UINT i = 0; i < trucks.size(); i++ ) {
    trucks[i].setInitialValues( C, pickups );
  }

#ifdef VRPMAXTRACE
  C.dump();
  nodes.dump( "nodes" );
  dumps.dump( "dumps" );
  depots.dump( "depots" );
  pickups.dump( "pickups" );
  endings.dump( "endings" );
  datanodes.dump( "datanodes" );
  invalid.dump( "invalid" );
  DLOG( INFO ) << "TRUCKS";

  for ( int i = 0; i < trucks.size(); i++ ) trucks[i].tau();

  DLOG( INFO ) << "INVALID TRUCKS";
  if (invalidTrucks.size()==0) DLOG( INFO ) << " NONE\n";
  for ( int i = 0; i < invalidTrucks.size(); i++ ) invalidTrucks[i].tau();

  //twc->dump();
#endif
}

void Prob_trash::load_trucks( std::string infile )
{
  assert ( otherlocs.size() );
  std::ifstream in( infile.c_str() );
  std::string line;
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash:LoadTrucks" << infile;
#endif

  trucks.clear();

  while ( getline( in, line ) ) {

    if ( line[0] == '#' ) continue;

    Vehicle truck( line, otherlocs );

    if ( truck.isvalid() ) {
      trucks.push_back( truck );
      depots.push_back( truck.getStartingSite() );
      dumps.push_back( truck.getDumpSite() );
      endings.push_back( truck.getEndingSite() );
    } else {
      invalidTrucks.push_back( truck );
    }
  }

  in.close();

}

#if 0
void Prob_trash::load_depots( std::string infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash:Load_depots" << infile;
#endif
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;

  depots.clear();

  while ( getline( in, line ) ) {
    cnt++;

    if ( line[0] == '#' ) continue;

    Trashnode node( line );

    if ( not node.isValid() or not node.isDepot() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      depots.push_back( node );
    }
  }

  in.close();
}
#endif

void Prob_trash::load_otherlocs( std::string infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash:Load_otherlocs" << infile;
#endif
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;

  otherlocs.clear();

  while ( getline( in, line ) ) {
    cnt++;

    if ( line[0] == '#' ) continue;

    Trashnode node( line );

    if ( not node.isValid() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      otherlocs.push_back( node );
    }
  }

  in.close();
}

#if 0
void Prob_trash::load_dumps( std::string infile )   //1 dump problem
{
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;
  dumps.clear();

  while ( getline( in, line ) ) {
    cnt++;

    // skip comment lines
    if ( line[0] == '#' ) continue;

    Trashnode node( line );

    if ( not node.isValid() or not node.isDump() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      dumps.push_back( node );
    }
  }

  in.close();
}
#endif

void Prob_trash::load_pickups( std::string infile )
{
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;
  pickups.clear();
  double st, op, cl, dm, x, y;
  st = op = cl = dm = x = y = 0;

  while ( getline( in, line ) ) {
    cnt++;

    if ( line[0] == '#' ) continue;

    Trashnode node( line );
    node.set_type( Twnode::kPickup );

    if ( not node.isValid() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      pickups.push_back( node );
      st += node.serviceTime();
      op += node.opens();
      cl += node.closes();
      dm += node.demand();
      x += node.x();
      y += node.y();
    }
  }

  in.close();
  st = st / pickups.size();
  op = op / pickups.size();
  cl = cl / pickups.size();
  dm = dm / pickups.size();
  x = x / pickups.size();
  y = y / pickups.size();
  C.set( -1, -1, x, y, dm, op, cl, st );
}

<commit_msg>Add some logging<commit_after>/*VRP*********************************************************************
 *
 * vehicle routing problems
 *      A collection of C++ classes for developing VRP solutions
 *      and specific solutions developed using these classes.
 *
 * Copyright 2014 Stephen Woodbridge <woodbri@imaptools.com>
 * Copyright 2014 Vicky Vergara <vicky_vergara@hotmail.com>
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the MIT License. Please file LICENSE for details.
 *
 ********************************************************************VRP*/
#include <limits>
#include <stdexcept>
#include <algorithm>
#include <math.h>

#ifdef DOVRPLOG
#include "logger.h"
#endif

#include "prob_trash.h"

// Class functions
#if 0
bool Prob_trash::checkIntegrity() const
{
  bool flag = true;
  int nodesCant = datanodes.size();

  if ( datanodes.empty() ) {
#ifdef DOVRPLOG
    DLOG( INFO ) << "Nodes is empty";
#endif
    flag = false;
  }

#ifdef DOVRPLOG
  else DLOG( INFO ) << "# of Nodes:" << nodesCant;

#endif

  for ( int i = 1; i < nodesCant - 1; i++ ) {
    flag = flag and datanodes[i].isValid();
  }
}
#endif

// DUMPS ********************************************
#ifdef DOVRPLOG
void Prob_trash::nodesdump()
{
  DLOG( INFO ) << "---- Nodes  --------------";

  for ( UINT i = 0; i < datanodes.size(); i++ )
    datanodes[i].dump();
}

#if 0
void Prob_trash::nodesdumpeval()
{
  DLOG( INFO ) << "---- Nodes  Evaluation--------------";

  for ( UINT i = 0; i < datanodes.size(); i++ )
    datanodes[i].dumpeval();
}

void Prob_trash::dump()
{
  DLOG( INFO ) << "---- Problem -------------";
  nodesdump();
  DLOG( INFO ) << "INITIAL EVALUATION";
  nodesdumpeval();

}
#endif

void Prob_trash::dumpdataNodes() const
{
  DLOG( INFO ) << "--------- Nodes ------------";

  for ( UINT i = 0; i < datanodes.size(); i++ )
    datanodes[i].dump();
}

void Prob_trash::dumpDepots() const
{
  DLOG( INFO ) << "--------- Depots ------------";
  depots.dump( "Depots" );

  for ( UINT i = 0; i < depots.size(); i++ )
    depots[i].dump();
}

void Prob_trash::dumpDumps() const
{
  DLOG( INFO ) << "--------- Dumps ------------";
  dumps.dump( "Dumps" );

  for ( UINT i = 0; i < dumps.size(); i++ )
    dumps[i].dump();
}

void Prob_trash::dumpPickups() const
{
  DLOG( INFO ) << "--------- Pickups ------------";
  pickups.dump( "pickups" );

  for ( UINT i = 0; i < pickups.size(); i++ )
    pickups[i].dump();
}
#endif

// PLOTS  ********************************************
#ifdef DOPLOTS
void Prob_trash::plot( Plot<Trashnode> &graph )
{
  for ( int i = 0; i < datanodes.size(); i++ ) {
    if ( datanodes[i].isPickup() )  {
      graph.drawPoint( datanodes[i], 0x0000ff, 9, true );
    } else if ( datanodes[i].isDump() ) {
      graph.drawPoint( datanodes[i], 0x00ff00, 5, true );
    } else  {
      graph.drawPoint( datanodes[i], 0xff0000, 7, true );
    }
  }
};
#endif

Prob_trash::Prob_trash( const char *infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "---- char * Constructor --------------";
#endif
  std::string file = infile;
  loadProblem( file );
}

Prob_trash::Prob_trash( const std::string &infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash---- string Constructor --------------";
#endif
  loadProblem( infile );
}

void Prob_trash::loadProblem( const std::string &infile )
{
  datafile = infile;
  Bucket nodes;
  Bucket intersection;
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash LoadProblem --------------" << datafile <<
               "--------";
#endif

  // read the nodes
  //int cnt = 0;
  //int nid = 0;
  int id = 0;

  twc->emptiedTruck = false;

  load_pickups( datafile + ".containers.txt" );
  load_otherlocs( datafile + ".otherlocs.txt" );

  intersection = otherlocs * pickups;
  invalid = invalid + intersection;
  pickups = pickups - intersection;
  nodes = nodes - intersection;

#ifdef VRPMINTRACE
  invalid.dump( "invalid" );
#endif

  nodes = pickups + otherlocs;
  nodes.push_back( C );

  for ( UINT i = 0; i < nodes.size(); i++ ) {
    nodes[i].set_nid( i );
    id = nodes[i].id();

    if ( pickups.hasId( id ) )
      pickups[ pickups.posFromId( id ) ].set_nid( i );
    else if ( otherlocs.hasId( id ) )
      otherlocs[ otherlocs.posFromId( id ) ].set_nid( i );
  };

  C = nodes.back();
  assert( pickups.size() );
  assert( otherlocs.size() );

  datanodes = nodes;

  twc->loadAndProcess_distance( datafile + ".dmatrix-time.txt", datanodes,
                                invalid );
  load_trucks( datafile + ".vehicles.txt" );

#ifdef OSRMCLIENT
#ifdef VRPMINTRACE
  DLOG(INFO) << "Setting hints";
#endif
  twc->setHints( dumps );
#ifdef VRPMINTRACE
  DLOG(INFO) << "    hints for dumps done";
#endif
  twc->setHints( nodes );
#ifdef VRPMINTRACE
  DLOG(INFO) << "    hints for nodes done";
#endif
  twc->setHints( depots );
#ifdef VRPMINTRACE
  DLOG(INFO) << "    hints for depots done";
#endif
  twc->setHints( pickups );
#ifdef VRPMINTRACE
  DLOG(INFO) << "    hints for pickups done";
#endif
  twc->setHints( endings );
#ifdef VRPMINTRACE
  DLOG(INFO) << "    hints for endings done";
#endif
  twc->settCC( C, pickups );
#ifdef VRPMINTRACE
  DLOG(INFO) << "    settCC for pickups done";
#endif
#endif  // OSRMCLIENT

  assert( trucks.size() and depots.size() and dumps.size() and endings.size() );

  for ( UINT i = 0; i < trucks.size(); i++ ) {
    trucks[i].setInitialValues( C, pickups );
  }
#ifdef VRPMINTRACE
  DLOG(INFO) << "trucks[i].setInitialValues( C, pickups ) done";
#endif

#ifdef VRPMAXTRACE
  C.dump();
  nodes.dump( "nodes" );
  dumps.dump( "dumps" );
  depots.dump( "depots" );
  pickups.dump( "pickups" );
  endings.dump( "endings" );
  datanodes.dump( "datanodes" );
  invalid.dump( "invalid" );
  DLOG( INFO ) << "TRUCKS";

  for ( int i = 0; i < trucks.size(); i++ ) trucks[i].tau();

#ifdef VRPMINTRACE
  DLOG( INFO ) << "INVALID TRUCKS";
#endif
  if (invalidTrucks.size()==0) DLOG( INFO ) << " NONE\n";
  for ( int i = 0; i < invalidTrucks.size(); i++ ) invalidTrucks[i].tau();

  //twc->dump();
#endif
#ifdef VRPMINTRACE
  DLOG( INFO ) << "-------- Leaving Prob_trash::LoadProblem --------------";
#endif
}

void Prob_trash::load_trucks( std::string infile )
{
  assert ( otherlocs.size() );
  std::ifstream in( infile.c_str() );
  std::string line;
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash:LoadTrucks" << infile;
#endif

  trucks.clear();

  while ( getline( in, line ) ) {

    if ( line[0] == '#' ) continue;

    Vehicle truck( line, otherlocs );

    if ( truck.isvalid() ) {
      trucks.push_back( truck );
      depots.push_back( truck.getStartingSite() );
      dumps.push_back( truck.getDumpSite() );
      endings.push_back( truck.getEndingSite() );
    } else {
      invalidTrucks.push_back( truck );
    }
  }

  in.close();

}

#if 0
void Prob_trash::load_depots( std::string infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash:Load_depots" << infile;
#endif
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;

  depots.clear();

  while ( getline( in, line ) ) {
    cnt++;

    if ( line[0] == '#' ) continue;

    Trashnode node( line );

    if ( not node.isValid() or not node.isDepot() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      depots.push_back( node );
    }
  }

  in.close();
}
#endif

void Prob_trash::load_otherlocs( std::string infile )
{
#ifdef VRPMINTRACE
  DLOG( INFO ) << "Prob_trash:Load_otherlocs" << infile;
#endif
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;

  otherlocs.clear();

  while ( getline( in, line ) ) {
    cnt++;

    if ( line[0] == '#' ) continue;

    Trashnode node( line );

    if ( not node.isValid() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      otherlocs.push_back( node );
    }
  }

  in.close();
}

#if 0
void Prob_trash::load_dumps( std::string infile )   //1 dump problem
{
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;
  dumps.clear();

  while ( getline( in, line ) ) {
    cnt++;

    // skip comment lines
    if ( line[0] == '#' ) continue;

    Trashnode node( line );

    if ( not node.isValid() or not node.isDump() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      dumps.push_back( node );
    }
  }

  in.close();
}
#endif

void Prob_trash::load_pickups( std::string infile )
{
  std::ifstream in( infile.c_str() );
  std::string line;
  int cnt = 0;
  pickups.clear();
  double st, op, cl, dm, x, y;
  st = op = cl = dm = x = y = 0;

  while ( getline( in, line ) ) {
    cnt++;

    if ( line[0] == '#' ) continue;

    Trashnode node( line );
    node.set_type( Twnode::kPickup );

    if ( not node.isValid() ) {
#ifdef DOVRPLOG
      DLOG( INFO ) << "ERROR: line: " << cnt << ": " << line;
#endif
      invalid.push_back( node );
    } else {
      pickups.push_back( node );
      st += node.serviceTime();
      op += node.opens();
      cl += node.closes();
      dm += node.demand();
      x += node.x();
      y += node.y();
    }
  }

  in.close();
  st = st / pickups.size();
  op = op / pickups.size();
  cl = cl / pickups.size();
  dm = dm / pickups.size();
  x = x / pickups.size();
  y = y / pickups.size();
  C.set( -1, -1, x, y, dm, op, cl, st );
}

<|endoftext|>


<commit_before>#include "matrix_all.h"

using namespace std;

namespace matrix_hao_lib
{

 /*************************************/
 /*Check Hermitian of the matrix*******/
 /*************************************/
 void check_Hermitian(const Matrix<complex<double>,2>& A)
 {
     if(A.L1!=A.L2) {cout<<"Input for Hermitian is not square matrix! \n"; exit(1);}
     double error=0; double norm=0;
     for(size_t i=0; i<A.L1; i++)
     {
         for(size_t j=i; j<A.L2; j++)
         {
             error+=abs(A(i,j)-conj(A(j,i)));
             norm+=abs(A(i,j));
         }
     }
     norm/=(A.L_f()*1.0);
     if(error/norm>1e-12) cout<<"Warning!!!!!Matrix is not Hermition!";
 }

 /************************/
 /*Determinant of  Matrix*/
 /************************/
 complex<double> determinant(const LUDecomp<complex<double>>& x)
 {
     if(x.info>0) return 0;

     complex<double> det={1,0};
     BL_INT L=x.ipiv.L1;
     for(BL_INT i=0;i<L;i++)
     {
         if(x.ipiv(i)!=(i+1)) det*=(-x.A(i,i));
         else det*=x.A(i,i);
     }
     return det;
 }

 /*****************************************/
 /*Get Log(|det|) and det/|det| of  Matrix*/
 /*****************************************/
 void lognorm_phase_determinant(const LUDecomp<complex<double>>& x, complex<double>& lognorm, complex<double>& phase)
 {
     if(x.info>0)
     {
         cout<<"WARNING!!!! lognorm_phase_determinant function has zero determinant!\n";
         lognorm=complex<double>(-1e300,0.0);
         phase=complex<double>(1.0,0.0);
         return;
     }

     lognorm=complex<double>(0.0,0.0); phase=complex<double>(1.0,0.0);
     BL_INT L=x.ipiv.L1;
     for(BL_INT i=0;i<L;i++)
     {
         lognorm+=log(abs(x.A(i,i)));
         if(x.ipiv(i)!=(i+1)) phase*=(-x.A(i,i)/abs(x.A(i,i)));
         else phase*=(x.A(i,i)/abs(x.A(i,i)));
     }
     return;
 }

 /****************************/
 /*Log Determinant of  Matrix*/
 /****************************/
 complex<double> log_determinant(const LUDecomp<complex<double>>& x)
 {
     complex<double> log_det,phase;
     lognorm_phase_determinant(x,log_det,phase);
     log_det+=log(phase);
     return log_det;
 }

 /*******************************/
 /*Diagonal array multipy matrix*/
 /*******************************/
 Matrix<complex<double>,2> D_Multi_Matrix(const Matrix<complex<double>,1>& D,const Matrix<complex<double>,2>& ph)
 {
     if(D.L1!=ph.L1) {cout<<"D_Multi_Matrix input error: D.L1!=ph.L1! \n"; exit(1);}
     Matrix<complex<double>,2> ph_new(ph.L1,ph.L2);
     for(size_t i=0; i<ph.L1; i++)
     {
         for(size_t j=0; j<ph.L2; j++) ph_new(i,j)=D(i)*ph(i,j);
     }
     return ph_new;
 }

 /******************************************/
 /*Check skew symmetric of the matrix*******/
 /******************************************/
 void check_skew_symmetric(const Matrix<complex<double>,2>& A)
 {
     if(A.L1!=A.L2) {cout<<"Input for skew_symmetric is not square matrix! \n"; exit(1);}
     double error=0; double norm=0;
     for(size_t i=0; i<A.L1; i++)
     {
         for(size_t j=i; j<A.L2; j++)
         {
             error+=abs( A(i,j)+A(j,i) );
             norm+=abs(A(i,j));
         }
     }
     norm/=(A.L_f()*1.0);
     if(error/norm>1e-12) cout<<"Warning!!!!!Matrix is not skew symmetric!";
 }

 /**********************/
 /*Pfaffian of a matrix*/
 /**********************/
 complex<double> Pfaffian(Matrix<complex<double>,2>& A)
 {
    if(A.L1!=A.L2) {cout<<"pfaffian input error: A.L1!=A.L2! \n"; exit(1);}
    return pfaffian_aitken(A.base_array, A.L1, A.L2);
 }

} //end namespace matrix_hao_lib
<commit_msg>add \n in checking hermition and skew-symmetry.<commit_after>#include "matrix_all.h"

using namespace std;

namespace matrix_hao_lib
{

 /*************************************/
 /*Check Hermitian of the matrix*******/
 /*************************************/
 void check_Hermitian(const Matrix<complex<double>,2>& A)
 {
     if(A.L1!=A.L2) {cout<<"Input for Hermitian is not square matrix! \n"; exit(1);}
     double error=0; double norm=0;
     for(size_t i=0; i<A.L1; i++)
     {
         for(size_t j=i; j<A.L2; j++)
         {
             error+=abs(A(i,j)-conj(A(j,i)));
             norm+=abs(A(i,j));
         }
     }
     norm/=(A.L_f()*1.0);
     if(error/norm>1e-12) cout<<"Warning!!!!!Matrix is not Hermition!"<<endl;
 }

 /************************/
 /*Determinant of  Matrix*/
 /************************/
 complex<double> determinant(const LUDecomp<complex<double>>& x)
 {
     if(x.info>0) return 0;

     complex<double> det={1,0};
     BL_INT L=x.ipiv.L1;
     for(BL_INT i=0;i<L;i++)
     {
         if(x.ipiv(i)!=(i+1)) det*=(-x.A(i,i));
         else det*=x.A(i,i);
     }
     return det;
 }

 /*****************************************/
 /*Get Log(|det|) and det/|det| of  Matrix*/
 /*****************************************/
 void lognorm_phase_determinant(const LUDecomp<complex<double>>& x, complex<double>& lognorm, complex<double>& phase)
 {
     if(x.info>0)
     {
         cout<<"WARNING!!!! lognorm_phase_determinant function has zero determinant!\n";
         lognorm=complex<double>(-1e300,0.0);
         phase=complex<double>(1.0,0.0);
         return;
     }

     lognorm=complex<double>(0.0,0.0); phase=complex<double>(1.0,0.0);
     BL_INT L=x.ipiv.L1;
     for(BL_INT i=0;i<L;i++)
     {
         lognorm+=log(abs(x.A(i,i)));
         if(x.ipiv(i)!=(i+1)) phase*=(-x.A(i,i)/abs(x.A(i,i)));
         else phase*=(x.A(i,i)/abs(x.A(i,i)));
     }
     return;
 }

 /****************************/
 /*Log Determinant of  Matrix*/
 /****************************/
 complex<double> log_determinant(const LUDecomp<complex<double>>& x)
 {
     complex<double> log_det,phase;
     lognorm_phase_determinant(x,log_det,phase);
     log_det+=log(phase);
     return log_det;
 }

 /*******************************/
 /*Diagonal array multipy matrix*/
 /*******************************/
 Matrix<complex<double>,2> D_Multi_Matrix(const Matrix<complex<double>,1>& D,const Matrix<complex<double>,2>& ph)
 {
     if(D.L1!=ph.L1) {cout<<"D_Multi_Matrix input error: D.L1!=ph.L1! \n"; exit(1);}
     Matrix<complex<double>,2> ph_new(ph.L1,ph.L2);
     for(size_t i=0; i<ph.L1; i++)
     {
         for(size_t j=0; j<ph.L2; j++) ph_new(i,j)=D(i)*ph(i,j);
     }
     return ph_new;
 }

 /******************************************/
 /*Check skew symmetric of the matrix*******/
 /******************************************/
 void check_skew_symmetric(const Matrix<complex<double>,2>& A)
 {
     if(A.L1!=A.L2) {cout<<"Input for skew_symmetric is not square matrix! \n"; exit(1);}
     double error=0; double norm=0;
     for(size_t i=0; i<A.L1; i++)
     {
         for(size_t j=i; j<A.L2; j++)
         {
             error+=abs( A(i,j)+A(j,i) );
             norm+=abs(A(i,j));
         }
     }
     norm/=(A.L_f()*1.0);
     if(error/norm>1e-12) cout<<"Warning!!!!!Matrix is not skew symmetric!"<<endl;
 }

 /**********************/
 /*Pfaffian of a matrix*/
 /**********************/
 complex<double> Pfaffian(Matrix<complex<double>,2>& A)
 {
    if(A.L1!=A.L2) {cout<<"pfaffian input error: A.L1!=A.L2! \n"; exit(1);}
    return pfaffian_aitken(A.base_array, A.L1, A.L2);
 }

} //end namespace matrix_hao_lib
<|endoftext|>


<commit_before>/*
 * CameraParamsVirtualViewOverride.cpp
 *
 * Copyright (C) 2006 - 2009 by Universitaet Stuttgart (VIS).
 * Alle Rechte vorbehalten.
 * Copyright (C) 2009 by Christoph Mueller. Alle Rechte vorbehalten.
 */

#include "vislib/graphics/CameraParamsVirtualViewOverride.h"

/*
 * CameraParamsVirtualViewOverride::CameraParamsVirtualViewOverride
 */
vislib::graphics::CameraParamsVirtualViewOverride
::CameraParamsVirtualViewOverride(void) : Super() {
}

/*
 *CameraParamsVirtualViewOverride::CameraParamsVirtualViewOverride
 */
vislib::graphics::CameraParamsVirtualViewOverride
::CameraParamsVirtualViewOverride(const SmartPtr<CameraParameters>& params)
        : Super(params), overrideValue(params->VirtualViewSize()) {
    this->indicateValueChange();
}

/*
 * CameraParamsVirtualViewOverride::~CameraParamsVirtualViewOverride
 */
vislib::graphics::CameraParamsVirtualViewOverride
::~CameraParamsVirtualViewOverride(void) {
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::SetVirtualViewSize
 */
void vislib::graphics::CameraParamsVirtualViewOverride::SetVirtualViewSize(
        const ImageSpaceDimension& viewSize) {
    ASSERT(!this->paramsBase().IsNull());

    if (math::IsEqual(this->TileRect().GetLeft(), 0.0f)
            && math::IsEqual(this->TileRect().GetBottom(), 0.0f)
            && math::IsEqual(this->TileRect().GetRight(),
            this->overrideValue.Width())
            && math::IsEqual(this->TileRect().GetTop(),
            this->overrideValue.Height())) {
        math::Point<ImageSpaceType, 2> origin;
        ImageSpaceRectangle rect(origin, viewSize);
        this->SetTileRect(rect);
    }

    this->overrideValue = viewSize;
    this->indicateValueChange();
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::VirtualViewSize
 */
const vislib::graphics::ImageSpaceDimension&
vislib::graphics::CameraParamsVirtualViewOverride::VirtualViewSize(void) const {
    ASSERT(!this->paramsBase().IsNull());
    return this->overrideValue;
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::operator =
 */
vislib::graphics::CameraParamsVirtualViewOverride&
vislib::graphics::CameraParamsVirtualViewOverride::operator =(
        const CameraParamsVirtualViewOverride& rhs) {
    Super::operator =(rhs);
    this->SetVirtualViewSize(rhs.overrideValue);
    return *this;
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::operator ==
 */
bool vislib::graphics::CameraParamsVirtualViewOverride::operator ==(
        const CameraParamsVirtualViewOverride& rhs) const {
    return (Super::operator ==(rhs)
        && (this->overrideValue == rhs.overrideValue));
}

/*
 *  vislib::graphics::CameraParamsEyeOverride::preBaseSet
 */
void vislib::graphics::CameraParamsVirtualViewOverride::preBaseSet(
        const SmartPtr<CameraParameters>& params) {
    // TODO: Should probably do Sebastian's black magic I don't know
}

/*
 *  vislib::graphics::CameraParamsVirtualViewOverride::resetOverride
 */
void vislib::graphics::CameraParamsVirtualViewOverride::resetOverride(void) {
    ASSERT(!this->paramsBase().IsNull());
    this->SetVirtualViewSize(this->paramsBase()->VirtualViewSize());
}
<commit_msg>assign_and_sync in CameraParamsVirtualViewOverride<commit_after>/*
 * CameraParamsVirtualViewOverride.cpp
 *
 * Copyright (C) 2006 - 2009 by Universitaet Stuttgart (VIS).
 * Alle Rechte vorbehalten.
 * Copyright (C) 2009 by Christoph Mueller. Alle Rechte vorbehalten.
 */

#include "vislib/graphics/CameraParamsVirtualViewOverride.h"

/*
 * CameraParamsVirtualViewOverride::CameraParamsVirtualViewOverride
 */
vislib::graphics::CameraParamsVirtualViewOverride
::CameraParamsVirtualViewOverride(void) : Super() {
}

/*
 *CameraParamsVirtualViewOverride::CameraParamsVirtualViewOverride
 */
vislib::graphics::CameraParamsVirtualViewOverride
::CameraParamsVirtualViewOverride(const SmartPtr<CameraParameters>& params)
        : Super(params), overrideValue(params->VirtualViewSize()) {
    this->indicateValueChange();
}

/*
 * CameraParamsVirtualViewOverride::~CameraParamsVirtualViewOverride
 */
vislib::graphics::CameraParamsVirtualViewOverride
::~CameraParamsVirtualViewOverride(void) {
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::SetVirtualViewSize
 */
void vislib::graphics::CameraParamsVirtualViewOverride::SetVirtualViewSize(
        const ImageSpaceDimension& viewSize) {
    ASSERT(!this->paramsBase().IsNull());

    if (math::IsEqual(this->TileRect().GetLeft(), 0.0f)
            && math::IsEqual(this->TileRect().GetBottom(), 0.0f)
            && math::IsEqual(this->TileRect().GetRight(),
            this->overrideValue.Width())
            && math::IsEqual(this->TileRect().GetTop(),
            this->overrideValue.Height())) {
        math::Point<ImageSpaceType, 2> origin;
        ImageSpaceRectangle rect(origin, viewSize);
        if (this->TileRect() != rect) {
            this->SetTileRect(rect);
            this->indicateValueChange();
        }
    }

    assign_and_sync(this->overrideValue, viewSize);
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::VirtualViewSize
 */
const vislib::graphics::ImageSpaceDimension&
vislib::graphics::CameraParamsVirtualViewOverride::VirtualViewSize(void) const {
    ASSERT(!this->paramsBase().IsNull());
    return this->overrideValue;
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::operator =
 */
vislib::graphics::CameraParamsVirtualViewOverride&
vislib::graphics::CameraParamsVirtualViewOverride::operator =(
        const CameraParamsVirtualViewOverride& rhs) {
    Super::operator =(rhs);
    this->SetVirtualViewSize(rhs.overrideValue);
    return *this;
}

/*
 * vislib::graphics::CameraParamsVirtualViewOverride::operator ==
 */
bool vislib::graphics::CameraParamsVirtualViewOverride::operator ==(
        const CameraParamsVirtualViewOverride& rhs) const {
    return (Super::operator ==(rhs)
        && (this->overrideValue == rhs.overrideValue));
}

/*
 *  vislib::graphics::CameraParamsEyeOverride::preBaseSet
 */
void vislib::graphics::CameraParamsVirtualViewOverride::preBaseSet(
        const SmartPtr<CameraParameters>& params) {
    // TODO: Should probably do Sebastian's black magic I don't know
}

/*
 *  vislib::graphics::CameraParamsVirtualViewOverride::resetOverride
 */
void vislib::graphics::CameraParamsVirtualViewOverride::resetOverride(void) {
    ASSERT(!this->paramsBase().IsNull());
    this->SetVirtualViewSize(this->paramsBase()->VirtualViewSize());
}
<|endoftext|>


<commit_before>//

#include "new_pe_builder.h"
#include "import_packer.h"
#include "common_utils.h"
#include <iostream>

uint32_t rebuildMZHeader(PeFilePtr& peFile, std::vector<uint8_t>& outFileBuffer, const std::vector<uint8_t>& sourceFileBuff)
{
  std::vector<PeLib::byte> mzHeadBuffer;
  auto mzHead = peFile->mzHeader();
  mzHead.rebuild(mzHeadBuffer);

  outFileBuffer = std::move(mzHeadBuffer);

  // append mzHeader with undocumented data located between MZHeader & PEHeader
  auto peOffset = mzHead.getAddressOfPeHeader();
  auto cbMZHead = mzHead.size();
  auto cbJunkData = std::max(peOffset - cbMZHead, static_cast<uint32_t>(0));
  if (cbJunkData)
  {
    std::copy(&sourceFileBuff.at(cbMZHead), &sourceFileBuff.at(cbMZHead + cbJunkData), std::back_inserter(outFileBuffer));
  }

  return cbMZHead + cbJunkData;
}

//==============================================================================

class RebuildPeHeaderVisitor : public PeLib::PeFileVisitor
{
public:
  RebuildPeHeaderVisitor(
    std::vector<uint8_t>& outFileBuffer
    , const SectionsArr& newSections
    , const ImportsArr& newImports
    , IStubPacker* stubPacker
    , uint32_t& offset) :
    outFileBuffer_(outFileBuffer)
    , newSections_(newSections)
    , newImports_(newImports)
    , stubPacker_(stubPacker)
    , offset_(offset)
  {}
  virtual void callback(PeLib::PeFile32& file) { rebuildPEHeader<32>(file); }
  virtual void callback(PeLib::PeFile64& file) { rebuildPEHeader<64>(file); }
private:
  template <int bits>
  void UpdateSizeOfImage(PeLib::PeHeaderT<bits> newPeHeader)
  {
    auto lastSectionHead = newSections_.sections.back().newHeader;
    newPeHeader.setSizeOfImage(lastSectionHead.VirtualAddress + lastSectionHead.VirtualSize);
  }

  template<int bits>
  void rebuildPEHeader(PeLib::PeFile& peFile)
  {
    const PeLib::PeHeaderT<bits>& peh = static_cast<PeLib::PeFileT<bits>&>(peFile).peHeader();

    PeLib::PeHeaderT<bits> nPeh(peh); // rebuilding new Pe-header

    //----------------------------------------------

    nPeh.killSections(); // clean section headers
    nPeh.setNumberOfSections(newSections_.sections.size());

    PeLib::dword sectionsSize = 0;
    for (const auto& section : newSections_.sections)
    {
      nPeh.addSection(section.newHeader);
      sectionsSize += section.newHeader.SizeOfRawData;
    }

    // update PE-file with new import data
    auto importBlock = std::find_if(
      newSections_.additionalDataBlocks.cbegin()
      , newSections_.additionalDataBlocks.cend()
      ,[](const auto& block)->auto
    {
      return block.ownerType == PackerType::kImportPacker;
    });

    if (importBlock == newSections_.additionalDataBlocks.cend())
    {
      throw std::runtime_error("Cant find new import block");
    }
    nPeh.setIddImportRva(importBlock->virtualOffset);
    nPeh.setIddImportSize(importBlock->size);

    //--------------------------------------------------------------------------------
    // Updating SizeOfImage

    UpdateSizeOfImage(nPeh);

    //----------------------------------------------
    // saving new PE-HEad in output buffer

    std::vector<PeLib::byte> peHeaderBuff;
    nPeh.rebuild(peHeaderBuff);

    auto cbAfterHeader = nPeh.size();
    offset_ += peHeaderBuff.size();

    std::copy(std::begin(peHeaderBuff), std::end(peHeaderBuff), std::back_inserter(outFileBuffer_));

    // append with aligned zero-bytes
    auto headersSize = nPeh.getSizeOfHeaders() - offset_;
    if (headersSize)
    {
      //todo(azerg): strip unused data by fixing size of headers ?
      outFileBuffer_.insert(outFileBuffer_.end(), headersSize, 0);
    }
    offset_ += headersSize;
  }

  std::vector<uint8_t>& outFileBuffer_;
  const SectionsArr& newSections_;
  const ImportsArr& newImports_;
  IStubPacker* stubPacker_;
  uint32_t& offset_;
};

//==================================================================================

NewPEBuilder::NewPEBuilder(
  PeFilePtr& srcPeFile
  , const std::vector<uint8_t>& sourceFileBuff
  , const std::vector<RequiredDataBlock>& additionalSizeRequest
  , IImportPacker* pImportPacker
  , IStubPacker* pStubPacker
  , ISectionsPacker* pSectionsPacker):
  srcPeFile_(srcPeFile)
  , sourceFileBuff_(sourceFileBuff)
  , additionalSizeRequest_(additionalSizeRequest)
  , importPacker_(pImportPacker)
  , stubPacker_(pStubPacker)
  , sectionsPacker_(pSectionsPacker)
{
  newSections_ = pSectionsPacker->ProcessExecutable(sourceFileBuff, additionalSizeRequest_);
  // new import RVA passed here
  newImports_ = importPacker_->ProcessExecutable(newSections_.additionalDataBlocks);
}

Expected<std::vector<uint8_t>> NewPEBuilder::GenerateOutputPEFile()
{
  std::vector<uint8_t> outFileBuffer;
  auto offset = rebuildMZHeader(srcPeFile_, outFileBuffer, sourceFileBuff_);

  RebuildPeHeaderVisitor peVisitor(outFileBuffer, newSections_, newImports_, stubPacker_, offset);
  srcPeFile_->visit(peVisitor);

  //--------------------------------------------------------------------------------
  // configure sections data
  for (const auto& section : newSections_.sections)
  {
    std::move(section.data.cbegin(), section.data.cend(), std::back_inserter(outFileBuffer));
  }

  //--------------------------------------------------------------------------------
  // insert imports data

  AdditionalDataBlocksType importsBlocks;
  std::copy_if(
    newSections_.additionalDataBlocks.cbegin()
    , newSections_.additionalDataBlocks.cend()
    , std::back_inserter(importsBlocks)
    , [](const auto& block)->auto
  {
    return block.ownerType == PackerType::kImportPacker;
  });

  for (auto& importBlock: importsBlocks)
  {
    if (importBlock.packerParam == (int32_t)ImportBlockTypes::kNewImportData)
    {
      utils::ReplaceContainerData(outFileBuffer, importBlock.rawOffset, newImports_.new_imports);
    }
    else
    if (importBlock.packerParam == (int32_t)ImportBlockTypes::kOldImportData)
    {
      utils::ReplaceContainerData(outFileBuffer, importBlock.rawOffset, newImports_.old_imports);
    }
    else
    {
      return Expected<std::vector<uint8_t>>::fromException(std::runtime_error("eek unexpected buffer type"));
    }
  }

  //--------------------------------------------------------------------------------
  // Insert stub data
  stubPacker_->ProcessExecutable(outFileBuffer, newSections_.additionalDataBlocks);

  return outFileBuffer;
}
<commit_msg>updating iat in packed file<commit_after>//

#include "new_pe_builder.h"
#include "import_packer.h"
#include "common_utils.h"
#include <iostream>

uint32_t rebuildMZHeader(PeFilePtr& peFile, std::vector<uint8_t>& outFileBuffer, const std::vector<uint8_t>& sourceFileBuff)
{
  std::vector<PeLib::byte> mzHeadBuffer;
  auto mzHead = peFile->mzHeader();
  mzHead.rebuild(mzHeadBuffer);

  outFileBuffer = std::move(mzHeadBuffer);

  // append mzHeader with undocumented data located between MZHeader & PEHeader
  auto peOffset = mzHead.getAddressOfPeHeader();
  auto cbMZHead = mzHead.size();
  auto cbJunkData = std::max(peOffset - cbMZHead, static_cast<uint32_t>(0));
  if (cbJunkData)
  {
    std::copy(&sourceFileBuff.at(cbMZHead), &sourceFileBuff.at(cbMZHead + cbJunkData), std::back_inserter(outFileBuffer));
  }

  return cbMZHead + cbJunkData;
}

//==============================================================================
template <PackerType ownerTypeId, class newSetionsType>
auto GetAdditionalDataBlocks(const newSetionsType& newSections)
{
  auto targetBlocks = std::find_if(
    newSections.additionalDataBlocks.cbegin()
    , newSections.additionalDataBlocks.cend()
    , [](const auto& block)->auto
  {
    return block.ownerType == ownerTypeId;
  });

  if (targetBlocks == newSections.additionalDataBlocks.cend())
  {
    throw std::runtime_error("Cant find new required data block");
  }

  return targetBlocks;
}

class RebuildPeHeaderVisitor : public PeLib::PeFileVisitor
{
public:
  RebuildPeHeaderVisitor(
    std::vector<uint8_t>& outFileBuffer
    , const SectionsArr& newSections
    , const ImportsArr& newImports
    , uint32_t& offset) :
    outFileBuffer_(outFileBuffer)
    , newSections_(newSections)
    , newImports_(newImports)
    , offset_(offset)
  {}
  virtual void callback(PeLib::PeFile32& file) { rebuildPEHeader<32>(file); }
  virtual void callback(PeLib::PeFile64& file) { rebuildPEHeader<64>(file); }
private:
  template <int bits>
  void UpdateSizeOfImage(PeLib::PeHeaderT<bits> newPeHeader)
  {
    auto lastSectionHead = newSections_.sections.back().newHeader;
    newPeHeader.setSizeOfImage(lastSectionHead.VirtualAddress + lastSectionHead.VirtualSize);
  }

  template<int bits>
  void rebuildPEHeader(PeLib::PeFile& peFile)
  {
    const PeLib::PeHeaderT<bits>& peh = static_cast<PeLib::PeFileT<bits>&>(peFile).peHeader();

    PeLib::PeHeaderT<bits> nPeh(peh); // rebuilding new Pe-header

    //----------------------------------------------

    nPeh.killSections(); // clean section headers
    nPeh.setNumberOfSections(newSections_.sections.size());

    PeLib::dword sectionsSize = 0;
    for (const auto& section : newSections_.sections)
    {
      nPeh.addSection(section.newHeader);
      sectionsSize += section.newHeader.SizeOfRawData;
    }

    // update PE-file with new import data
    auto importBlock = GetAdditionalDataBlocks<PackerType::kImportPacker>(newSections_);
    nPeh.setIddImportRva(importBlock->virtualOffset);
    nPeh.setIddImportSize(importBlock->size);

    // update PE-file with new IAT address, pointer to our Stub structure, allocated in new file.
    auto stubBlock = GetAdditionalDataBlocks<PackerType::kStubPacker>(newSections_);
    //nPeh.setIddIatRva(stubBlock->virtualOffset);
    //nPeh.setIddIatSize(16); // todo(aserg) REPLACE THIS WITH VALID IAT SIZE, DETECTED IN COMPILE-TIME !!!!!!

    //--------------------------------------------------------------------------------
    // Updating SizeOfImage

    UpdateSizeOfImage(nPeh);

    //----------------------------------------------
    // saving new PE-HEad in output buffer

    std::vector<PeLib::byte> peHeaderBuff;
    nPeh.rebuild(peHeaderBuff);

    auto cbAfterHeader = nPeh.size();
    offset_ += peHeaderBuff.size();

    std::copy(std::begin(peHeaderBuff), std::end(peHeaderBuff), std::back_inserter(outFileBuffer_));

    // append with aligned zero-bytes
    auto headersSize = nPeh.getSizeOfHeaders() - offset_;
    if (headersSize)
    {
      //todo(azerg): strip unused data by fixing size of headers ?
      outFileBuffer_.insert(outFileBuffer_.end(), headersSize, 0);
    }
    offset_ += headersSize;
  }

  std::vector<uint8_t>& outFileBuffer_;
  const SectionsArr& newSections_;
  const ImportsArr& newImports_;
  uint32_t& offset_;
};

//==================================================================================

NewPEBuilder::NewPEBuilder(
  PeFilePtr& srcPeFile
  , const std::vector<uint8_t>& sourceFileBuff
  , const std::vector<RequiredDataBlock>& additionalSizeRequest
  , IImportPacker* pImportPacker
  , IStubPacker* pStubPacker
  , ISectionsPacker* pSectionsPacker):
  srcPeFile_(srcPeFile)
  , sourceFileBuff_(sourceFileBuff)
  , additionalSizeRequest_(additionalSizeRequest)
  , importPacker_(pImportPacker)
  , stubPacker_(pStubPacker)
  , sectionsPacker_(pSectionsPacker)
{
  newSections_ = pSectionsPacker->ProcessExecutable(sourceFileBuff, additionalSizeRequest_);
  // new import RVA passed here
  newImports_ = importPacker_->ProcessExecutable(newSections_.additionalDataBlocks);
}

Expected<std::vector<uint8_t>> NewPEBuilder::GenerateOutputPEFile()
{
  std::vector<uint8_t> outFileBuffer;
  auto offset = rebuildMZHeader(srcPeFile_, outFileBuffer, sourceFileBuff_);

  RebuildPeHeaderVisitor peVisitor(outFileBuffer, newSections_, newImports_, offset);
  srcPeFile_->visit(peVisitor);

  //--------------------------------------------------------------------------------
  // configure sections data
  for (const auto& section : newSections_.sections)
  {
    std::move(section.data.cbegin(), section.data.cend(), std::back_inserter(outFileBuffer));
  }

  //--------------------------------------------------------------------------------
  // insert imports data

  AdditionalDataBlocksType importsBlocks;
  std::copy_if(
    newSections_.additionalDataBlocks.cbegin()
    , newSections_.additionalDataBlocks.cend()
    , std::back_inserter(importsBlocks)
    , [](const auto& block)->auto
  {
    return block.ownerType == PackerType::kImportPacker;
  });

  for (auto& importBlock: importsBlocks)
  {
    if (importBlock.packerParam == (int32_t)ImportBlockTypes::kNewImportData)
    {
      utils::ReplaceContainerData(outFileBuffer, importBlock.rawOffset, newImports_.new_imports);
    }
    else
    if (importBlock.packerParam == (int32_t)ImportBlockTypes::kOldImportData)
    {
      utils::ReplaceContainerData(outFileBuffer, importBlock.rawOffset, newImports_.old_imports);
    }
    else
    {
      return Expected<std::vector<uint8_t>>::fromException(std::runtime_error("eek unexpected buffer type"));
    }
  }

  //--------------------------------------------------------------------------------
  // Insert stub data
  stubPacker_->ProcessExecutable(outFileBuffer, newSections_.additionalDataBlocks);

  return outFileBuffer;
}
<|endoftext|>


<commit_before>/*ckwg +5
 * Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include "load_pipe.h"

#include "pipe_declaration_types.h"

#include <vistk/pipeline/config.h>
#include <vistk/pipeline/process.h>

#include <boost/algorithm/string/join.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/foreach.hpp>
#include <boost/variant.hpp>

#include <utility>
#include <vector>

/**
 * \file pipe_bakery.cxx
 *
 * \brief Implementation of baking a pipeline.
 */

namespace vistk
{

namespace
{

static config_flag_t const flag_read_only = config_flag_t("ro");

}

class VISTK_PIPELINE_UTIL_NO_EXPORT pipe_bakery
  : public boost::static_visitor<>
{
  public:
    pipe_bakery();
    ~pipe_bakery();

    void operator () (config_pipe_block const& config_block);
    void operator () (process_pipe_block const& process_block);
    void operator () (connect_pipe_block const& connect_block);
    void operator () (group_pipe_block const& group_block);

    /**
     * \note We do *not* want std::map for the groupings. With a map, we may
     * hide errors in the blocks (setting ro values, duplicate process names,
     * etc.)
     */

    typedef std::pair<config_provider_t, config::value_t> provider_request_t;
    typedef boost::variant<config::value_t, provider_request_t> config_reference_t;
    typedef boost::tuple<config_reference_t, bool> config_info_t;
    typedef std::pair<config::key_t, config_info_t> config_decl_t;
    typedef std::vector<config_decl_t> config_decls_t;

    typedef std::pair<process::name_t, process_registry::type_t> process_decl_t;
    typedef std::vector<process_decl_t> process_decls_t;

    typedef std::pair<process::port_addr_t, process::port_addr_t> connection_t;
    typedef std::vector<connection_t> connections_t;

    typedef boost::tuple<process::port_t, process::port_flags_t, process::port_addr_t> mapping_t;
    typedef std::vector<mapping_t> mappings_t;
    typedef std::pair<mappings_t, mappings_t> group_info_t;
    typedef std::pair<process::name_t, group_info_t> group_decl_t;
    typedef std::vector<group_decl_t> group_decls_t;

    static config::key_t flatten_keys(config::keys_t const& keys);
    void register_config_value(config::key_t const& root_key, config_value_t const& value);

    config_decls_t m_configs;
    process_decls_t m_processes;
    connections_t m_connections;
    group_decls_t m_groups;
};

pipeline_t
bake_pipe_blocks(pipe_blocks const& /*blocks*/)
{
  pipeline_t pipeline;

  /// \todo Bake pipe blocks into a pipeline.

  return pipeline;
}

pipe_bakery
::pipe_bakery()
{
}

pipe_bakery
::~pipe_bakery()
{
}

void
pipe_bakery
::operator () (config_pipe_block const& config_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::operator () (process_pipe_block const& process_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::operator () (connect_pipe_block const& connect_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::operator () (group_pipe_block const& group_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::register_config_value(config::key_t const& root_key, config_value_t const& value)
{
  config_key_t const key = value.key;

  config::key_t const subkey = flatten_keys(key.key_path);

  config_reference_t c_value;

  if (key.options.provider)
  {
    c_value = provider_request_t(*key.options.provider, value.value);
  }
  else
  {
    c_value = value.value;
  }

  bool is_readonly = false;

  if (key.options.flags)
  {
    BOOST_FOREACH (config_flag_t const& flag, *key.options.flags)
    {
      if (flag == flag_read_only)
      {
        is_readonly = true;
      }
      else
      {
        /// \todo Log warning about unrecognized flag.
      }
    }
  }

  config::key_t const full_key = root_key + config::block_sep + subkey;

  config_info_t const info = config_info_t(c_value, is_readonly);

  config_decl_t const decl = config_decl_t(full_key, info);

  m_configs.push_back(decl);
}

config::key_t
pipe_bakery
::flatten_keys(config::keys_t const& keys)
{
  return boost::join(keys, config::block_sep);
}

}
<commit_msg>Implement handling of a config block<commit_after>/*ckwg +5
 * Copyright 2011 by Kitware, Inc. All Rights Reserved. Please refer to
 * KITWARE_LICENSE.TXT for licensing information, or contact General Counsel,
 * Kitware, Inc., 28 Corporate Drive, Clifton Park, NY 12065.
 */

#include "load_pipe.h"

#include "pipe_declaration_types.h"

#include <vistk/pipeline/config.h>
#include <vistk/pipeline/process.h>

#include <boost/algorithm/string/join.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/foreach.hpp>
#include <boost/variant.hpp>

#include <utility>
#include <vector>

/**
 * \file pipe_bakery.cxx
 *
 * \brief Implementation of baking a pipeline.
 */

namespace vistk
{

namespace
{

static config_flag_t const flag_read_only = config_flag_t("ro");

}

class VISTK_PIPELINE_UTIL_NO_EXPORT pipe_bakery
  : public boost::static_visitor<>
{
  public:
    pipe_bakery();
    ~pipe_bakery();

    void operator () (config_pipe_block const& config_block);
    void operator () (process_pipe_block const& process_block);
    void operator () (connect_pipe_block const& connect_block);
    void operator () (group_pipe_block const& group_block);

    /**
     * \note We do *not* want std::map for the groupings. With a map, we may
     * hide errors in the blocks (setting ro values, duplicate process names,
     * etc.)
     */

    typedef std::pair<config_provider_t, config::value_t> provider_request_t;
    typedef boost::variant<config::value_t, provider_request_t> config_reference_t;
    typedef boost::tuple<config_reference_t, bool> config_info_t;
    typedef std::pair<config::key_t, config_info_t> config_decl_t;
    typedef std::vector<config_decl_t> config_decls_t;

    typedef std::pair<process::name_t, process_registry::type_t> process_decl_t;
    typedef std::vector<process_decl_t> process_decls_t;

    typedef std::pair<process::port_addr_t, process::port_addr_t> connection_t;
    typedef std::vector<connection_t> connections_t;

    typedef boost::tuple<process::port_t, process::port_flags_t, process::port_addr_t> mapping_t;
    typedef std::vector<mapping_t> mappings_t;
    typedef std::pair<mappings_t, mappings_t> group_info_t;
    typedef std::pair<process::name_t, group_info_t> group_decl_t;
    typedef std::vector<group_decl_t> group_decls_t;

    static config::key_t flatten_keys(config::keys_t const& keys);
    void register_config_value(config::key_t const& root_key, config_value_t const& value);

    config_decls_t m_configs;
    process_decls_t m_processes;
    connections_t m_connections;
    group_decls_t m_groups;
};

pipeline_t
bake_pipe_blocks(pipe_blocks const& /*blocks*/)
{
  pipeline_t pipeline;

  /// \todo Bake pipe blocks into a pipeline.

  return pipeline;
}

pipe_bakery
::pipe_bakery()
{
}

pipe_bakery
::~pipe_bakery()
{
}

void
pipe_bakery
::operator () (config_pipe_block const& config_block)
{
  config::key_t const root_key = flatten_keys(config_block.key);

  config_values_t const& values = config_block.values;

  BOOST_FOREACH (config_value_t const& value, values)
  {
    register_config_value(root_key, value);
  }
}

void
pipe_bakery
::operator () (process_pipe_block const& process_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::operator () (connect_pipe_block const& connect_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::operator () (group_pipe_block const& group_block)
{
  /// \todo Implement.
}

void
pipe_bakery
::register_config_value(config::key_t const& root_key, config_value_t const& value)
{
  config_key_t const key = value.key;

  config::key_t const subkey = flatten_keys(key.key_path);

  config_reference_t c_value;

  if (key.options.provider)
  {
    c_value = provider_request_t(*key.options.provider, value.value);
  }
  else
  {
    c_value = value.value;
  }

  bool is_readonly = false;

  if (key.options.flags)
  {
    BOOST_FOREACH (config_flag_t const& flag, *key.options.flags)
    {
      if (flag == flag_read_only)
      {
        is_readonly = true;
      }
      else
      {
        /// \todo Log warning about unrecognized flag.
      }
    }
  }

  config::key_t const full_key = root_key + config::block_sep + subkey;

  config_info_t const info = config_info_t(c_value, is_readonly);

  config_decl_t const decl = config_decl_t(full_key, info);

  m_configs.push_back(decl);
}

config::key_t
pipe_bakery
::flatten_keys(config::keys_t const& keys)
{
  return boost::join(keys, config::block_sep);
}

}
<|endoftext|>